This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix aura difficulty race #7198
Merged
Merged
Fix aura difficulty race #7198
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,20 +450,29 @@ impl Engine<EthereumMachine> for AuthorityRound { | |
} | ||
|
||
fn seals_internally(&self) -> Option<bool> { | ||
// TODO: accept a `&Call` here so we can query the validator set. | ||
Some(self.signer.read().is_some()) | ||
} | ||
|
||
/// Attempt to seal the block internally. | ||
/// | ||
/// This operation is synchronous and may (quite reasonably) not be available, in which case | ||
/// `Seal::None` will be returned. | ||
fn generate_seal(&self, block: &ExecutedBlock) -> Seal { | ||
fn generate_seal(&self, block: &ExecutedBlock, parent: &Header) -> Seal { | ||
// first check to avoid generating signature most of the time | ||
// (but there's still a race to the `compare_and_swap`) | ||
if !self.can_propose.load(AtomicOrdering::SeqCst) { return Seal::None; } | ||
|
||
let header = block.header(); | ||
let parent_step: U256 = header_step(parent) | ||
.expect("Header has been verified; qed").into(); | ||
|
||
let step = self.step.load(); | ||
let expected_diff = U256::from(U128::max_value()) + parent_step - step.into(); | ||
|
||
if header.difficulty() != &expected_diff { | ||
return Seal::None; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to seal and release a block with a previous step instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case can also only happen when |
||
} | ||
|
||
// fetch correct validator set for current epoch, taking into account | ||
// finality of previous transitions. | ||
|
@@ -505,6 +514,7 @@ impl Engine<EthereumMachine> for AuthorityRound { | |
trace!(target: "engine", "generate_seal: {} not a proposer for step {}.", | ||
header.author(), step); | ||
} | ||
|
||
Seal::None | ||
} | ||
|
||
|
@@ -546,7 +556,7 @@ impl Engine<EthereumMachine> for AuthorityRound { | |
} | ||
|
||
/// Check the number of seal fields. | ||
fn verify_block_basic(&self, header: &Header,) -> Result<(), Error> { | ||
fn verify_block_basic(&self, header: &Header) -> Result<(), Error> { | ||
if header.number() >= self.validate_score_transition && *header.difficulty() >= U256::from(U128::max_value()) { | ||
Err(From::from(BlockError::DifficultyOutOfBounds( | ||
OutOfBounds { min: None, max: Some(U256::from(U128::max_value())), found: *header.difficulty() } | ||
|
@@ -857,17 +867,51 @@ mod tests { | |
let b2 = b2.close_and_lock(); | ||
|
||
engine.set_signer(tap.clone(), addr1, "1".into()); | ||
if let Seal::Regular(seal) = engine.generate_seal(b1.block()) { | ||
if let Seal::Regular(seal) = engine.generate_seal(b1.block(), &genesis_header) { | ||
assert!(b1.clone().try_seal(engine, seal).is_ok()); | ||
// Second proposal is forbidden. | ||
assert!(engine.generate_seal(b1.block()) == Seal::None); | ||
assert!(engine.generate_seal(b1.block(), &genesis_header) == Seal::None); | ||
} | ||
|
||
engine.set_signer(tap, addr2, "2".into()); | ||
if let Seal::Regular(seal) = engine.generate_seal(b2.block()) { | ||
if let Seal::Regular(seal) = engine.generate_seal(b2.block(), &genesis_header) { | ||
assert!(b2.clone().try_seal(engine, seal).is_ok()); | ||
// Second proposal is forbidden. | ||
assert!(engine.generate_seal(b2.block()) == Seal::None); | ||
assert!(engine.generate_seal(b2.block(), &genesis_header) == Seal::None); | ||
} | ||
} | ||
|
||
#[test] | ||
fn checks_difficulty_in_generate_seal() { | ||
let tap = Arc::new(AccountProvider::transient_provider()); | ||
let addr1 = tap.insert_account(keccak("1").into(), "1").unwrap(); | ||
let addr2 = tap.insert_account(keccak("0").into(), "0").unwrap(); | ||
|
||
let spec = Spec::new_test_round(); | ||
let engine = &*spec.engine; | ||
|
||
let genesis_header = spec.genesis_header(); | ||
let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); | ||
let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap(); | ||
let last_hashes = Arc::new(vec![genesis_header.hash()]); | ||
|
||
let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap(); | ||
let b1 = b1.close_and_lock(); | ||
let b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes, addr2, (3141562.into(), 31415620.into()), vec![], false).unwrap(); | ||
let b2 = b2.close_and_lock(); | ||
|
||
engine.set_signer(tap.clone(), addr1, "1".into()); | ||
match engine.generate_seal(b1.block(), &genesis_header) { | ||
Seal::None | Seal::Proposal(_) => panic!("wrong seal"), | ||
Seal::Regular(_) => { | ||
engine.step(); | ||
|
||
engine.set_signer(tap.clone(), addr2, "0".into()); | ||
match engine.generate_seal(b2.block(), &genesis_header) { | ||
Seal::Regular(_) | Seal::Proposal(_) => panic!("sealed despite wrong difficulty"), | ||
Seal::None => {} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the difficulty calcution is already copy pasted in couple of places, maybe we should extract it into a helper function?