From 7263f9cb684ee57a484591344a22c371c04e27e8 Mon Sep 17 00:00:00 2001 From: keorn Date: Tue, 7 Mar 2017 10:09:09 +0000 Subject: [PATCH 1/5] calibrate before rejection --- ethcore/src/engines/authority_round.rs | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/ethcore/src/engines/authority_round.rs b/ethcore/src/engines/authority_round.rs index 04328a32de2..1f5c076c8e1 100644 --- a/ethcore/src/engines/authority_round.rs +++ b/ethcore/src/engines/authority_round.rs @@ -86,6 +86,8 @@ pub struct AuthorityRound { account_provider: Mutex>, password: RwLock>, validators: Box, + /// Is this Engine just for testing (prevents step calibration). + test: bool, } fn header_step(header: &Header) -> Result { @@ -126,6 +128,7 @@ impl AuthorityRound { account_provider: Mutex::new(Arc::new(AccountProvider::transient_provider())), password: RwLock::new(None), validators: new_validator_set(our_params.validators), + test: our_params.start_step.is_some(), }); // Do not initialize timeouts for tests. if should_timeout { @@ -135,6 +138,12 @@ impl AuthorityRound { Ok(engine) } + fn calibrate_step(&self) { + if !self.test { + self.step.store((unix_now().as_secs() / self.step_duration.as_secs()) as usize, AtomicOrdering::SeqCst); + } + } + fn remaining_step_duration(&self) -> Duration { let now = unix_now(); let step_end = self.step_duration * (self.step.load(AtomicOrdering::SeqCst) as u32 + 1); @@ -152,6 +161,16 @@ impl AuthorityRound { fn is_step_proposer(&self, step: usize, address: &Address) -> bool { self.step_proposer(step) == *address } + + fn is_future_step(&self, step: usize) -> bool { + if step > self.step.load(AtomicOrdering::SeqCst) + 1 { + // Make absolutely sure that the step is correct. + self.calibrate_step(); + step > self.step.load(AtomicOrdering::SeqCst) + 1 + } else { + false + } + } } fn unix_now() -> Duration { @@ -286,7 +305,11 @@ impl Engine for AuthorityRound { fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { let header_step = header_step(header)?; // Give one step slack if step is lagging, double vote is still not possible. - if header_step <= self.step.load(AtomicOrdering::SeqCst) + 1 { + if self.is_future_step(header_step) { + trace!(target: "engine", "verify_block_unordered: block from the future"); + self.validators.report_benign(header.author()); + Err(BlockError::InvalidSeal)? + } else { let proposer_signature = header_signature(header)?; let ok_sig = verify_address(&self.step_proposer(header_step), &proposer_signature, &header.bare_hash())?; if ok_sig { @@ -295,9 +318,6 @@ impl Engine for AuthorityRound { trace!(target: "poa", "verify_block_unordered: invalid seal signature"); Err(BlockError::InvalidSeal)? } - } else { - trace!(target: "poa", "verify_block_unordered: block from the future"); - Err(BlockError::InvalidSeal)? } } From b6c30f35067c72845eb70e6d2f886be38e531e9b Mon Sep 17 00:00:00 2001 From: keorn Date: Tue, 7 Mar 2017 16:48:35 +0000 Subject: [PATCH 2/5] change flag name --- ethcore/src/engines/authority_round.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethcore/src/engines/authority_round.rs b/ethcore/src/engines/authority_round.rs index 1f5c076c8e1..0212532deb5 100644 --- a/ethcore/src/engines/authority_round.rs +++ b/ethcore/src/engines/authority_round.rs @@ -87,7 +87,7 @@ pub struct AuthorityRound { password: RwLock>, validators: Box, /// Is this Engine just for testing (prevents step calibration). - test: bool, + calibrate_step: bool, } fn header_step(header: &Header) -> Result { @@ -128,7 +128,7 @@ impl AuthorityRound { account_provider: Mutex::new(Arc::new(AccountProvider::transient_provider())), password: RwLock::new(None), validators: new_validator_set(our_params.validators), - test: our_params.start_step.is_some(), + calibrate_step: our_params.start_step.is_none(), }); // Do not initialize timeouts for tests. if should_timeout { @@ -139,7 +139,7 @@ impl AuthorityRound { } fn calibrate_step(&self) { - if !self.test { + if self.calibrate_step { self.step.store((unix_now().as_secs() / self.step_duration.as_secs()) as usize, AtomicOrdering::SeqCst); } } From c0a56479af9be127873cbf05af9cd00f9a93a266 Mon Sep 17 00:00:00 2001 From: keorn Date: Tue, 7 Mar 2017 18:08:54 +0000 Subject: [PATCH 3/5] add eip155 --- ethcore/src/engines/authority_round.rs | 4 ++++ ethcore/src/engines/instant_seal.rs | 4 ++++ ethcore/src/engines/tendermint/mod.rs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/ethcore/src/engines/authority_round.rs b/ethcore/src/engines/authority_round.rs index 0212532deb5..50118acf041 100644 --- a/ethcore/src/engines/authority_round.rs +++ b/ethcore/src/engines/authority_round.rs @@ -238,6 +238,10 @@ impl Engine for AuthorityRound { Schedule::new_post_eip150(usize::max_value(), true, true, true) } + fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { + Some(self.params.chain_id) + } + fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) { header.set_difficulty(parent.difficulty().clone()); header.set_gas_limit({ diff --git a/ethcore/src/engines/instant_seal.rs b/ethcore/src/engines/instant_seal.rs index 21fb1b54d50..603154b6614 100644 --- a/ethcore/src/engines/instant_seal.rs +++ b/ethcore/src/engines/instant_seal.rs @@ -62,6 +62,10 @@ impl Engine for InstantSeal { Schedule::new_post_eip150(usize::max_value(), true, true, true) } + fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { + Some(self.params.chain_id) + } + fn is_sealer(&self, _author: &Address) -> Option { Some(true) } fn generate_seal(&self, _block: &ExecutedBlock) -> Seal { diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index 19d617c64a5..37d33095670 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -409,6 +409,10 @@ impl Engine for Tendermint { Schedule::new_post_eip150(usize::max_value(), true, true, true) } + fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { + Some(self.params.chain_id) + } + fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) { header.set_difficulty(parent.difficulty().clone()); header.set_gas_limit({ From 350dd0526eb740c0eb9029fa51c844c11c42b4da Mon Sep 17 00:00:00 2001 From: keorn Date: Tue, 7 Mar 2017 18:38:28 +0000 Subject: [PATCH 4/5] fix build --- ethcore/src/engines/authority_round.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ethcore/src/engines/authority_round.rs b/ethcore/src/engines/authority_round.rs index 50118acf041..5d34ade7707 100644 --- a/ethcore/src/engines/authority_round.rs +++ b/ethcore/src/engines/authority_round.rs @@ -311,7 +311,6 @@ impl Engine for AuthorityRound { // Give one step slack if step is lagging, double vote is still not possible. if self.is_future_step(header_step) { trace!(target: "engine", "verify_block_unordered: block from the future"); - self.validators.report_benign(header.author()); Err(BlockError::InvalidSeal)? } else { let proposer_signature = header_signature(header)?; From 89b805212883465509597dbea824652faf11963b Mon Sep 17 00:00:00 2001 From: keorn Date: Wed, 8 Mar 2017 10:56:34 +0000 Subject: [PATCH 5/5] make network_id default --- ethcore/src/engines/authority_round.rs | 4 ---- ethcore/src/engines/instant_seal.rs | 4 ---- ethcore/src/engines/mod.rs | 4 +++- ethcore/src/engines/tendermint/mod.rs | 4 ---- 4 files changed, 3 insertions(+), 13 deletions(-) diff --git a/ethcore/src/engines/authority_round.rs b/ethcore/src/engines/authority_round.rs index 5d34ade7707..49871327514 100644 --- a/ethcore/src/engines/authority_round.rs +++ b/ethcore/src/engines/authority_round.rs @@ -238,10 +238,6 @@ impl Engine for AuthorityRound { Schedule::new_post_eip150(usize::max_value(), true, true, true) } - fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { - Some(self.params.chain_id) - } - fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) { header.set_difficulty(parent.difficulty().clone()); header.set_gas_limit({ diff --git a/ethcore/src/engines/instant_seal.rs b/ethcore/src/engines/instant_seal.rs index 603154b6614..21fb1b54d50 100644 --- a/ethcore/src/engines/instant_seal.rs +++ b/ethcore/src/engines/instant_seal.rs @@ -62,10 +62,6 @@ impl Engine for InstantSeal { Schedule::new_post_eip150(usize::max_value(), true, true, true) } - fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { - Some(self.params.chain_id) - } - fn is_sealer(&self, _author: &Address) -> Option { Some(true) } fn generate_seal(&self, _block: &ExecutedBlock) -> Seal { diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index b82fce1b83f..6454e846264 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -160,7 +160,9 @@ pub trait Engine : Sync + Send { fn verify_transaction(&self, _t: &SignedTransaction, _header: &Header) -> Result<(), Error> { Ok(()) } /// The network ID that transactions should be signed with. - fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { None } + fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { + Some(self.params().chain_id) + } /// Verify the seal of a block. This is an auxilliary method that actually just calls other `verify_` methods /// to get the job done. By default it must pass `verify_basic` and `verify_block_unordered`. If more or fewer diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index 37d33095670..19d617c64a5 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -409,10 +409,6 @@ impl Engine for Tendermint { Schedule::new_post_eip150(usize::max_value(), true, true, true) } - fn signing_network_id(&self, _env_info: &EnvInfo) -> Option { - Some(self.params.chain_id) - } - fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) { header.set_difficulty(parent.difficulty().clone()); header.set_gas_limit({