From 7a502f8626220ee77b4c787e74d8e2dc7db87c18 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 18 Jul 2018 21:21:22 +0800 Subject: [PATCH 1/6] Be more graceful on Aura difficulty validation --- ethcore/src/engines/authority_round/mod.rs | 26 ++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index f3f4504ec6d..c077303537a 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1149,9 +1149,10 @@ impl Engine for AuthorityRound { // If empty step messages are enabled we will validate the messages in the seal, missing messages are not // reported as there's no way to tell whether the empty step message was never sent or simply not included. - if header.number() >= self.empty_steps_transition { - let validate_empty_steps = || -> Result<(), Error> { + let empty_steps_len = if header.number() >= self.empty_steps_transition { + let validate_empty_steps = || -> Result { let empty_steps = header_empty_steps(header)?; + let empty_steps_len = empty_steps.len(); for empty_step in empty_steps { if empty_step.step <= parent_step || empty_step.step >= step { Err(EngineError::InsufficientProof( @@ -1168,16 +1169,27 @@ impl Engine for AuthorityRound { format!("invalid empty step proof: {:?}", empty_step)))?; } } - Ok(()) + Ok(empty_steps_len) }; - if let err @ Err(_) = validate_empty_steps() { - self.validators.report_benign(header.author(), set_number, header.number()); - return err; + match validate_empty_steps() { + Ok(len) => len, + Err(err) => { + self.validators.report_benign(header.author(), set_number, header.number()); + return Err(err); + }, } - } else { self.report_skipped(header, step, parent_step, &*validators, set_number); + + 0 + }; + + if header.number() >= self.validate_score_transition { + let expected_difficulty = calculate_score(parent_step.into(), step.into(), empty_steps_len.into()); + if header.difficulty() != &expected_difficulty { + return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty().clone() }))); + } } Ok(()) From 928977003afd8628748ccb3ab0db846bddbae9bb Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 19 Jul 2018 08:16:58 +0800 Subject: [PATCH 2/6] test: rejects_step_backwards --- ethcore/src/engines/authority_round/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index c077303537a..7861b71fd76 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1424,7 +1424,7 @@ mod tests { use engines::{Seal, Engine, EngineError, EthEngine}; use engines::validator_set::TestSet; use error::{Error, ErrorKind}; - use super::{AuthorityRoundParams, AuthorityRound, EmptyStep, SealedEmptyStep}; + use super::{AuthorityRoundParams, AuthorityRound, EmptyStep, SealedEmptyStep, calculate_score}; #[test] fn has_valid_metadata() { @@ -1585,8 +1585,10 @@ mod tests { // Two validators. // Spec starts with step 2. header.set_seal(vec![encode(&5usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]); + header.set_difficulty(calculate_score(U256::from(4), U256::from(5), U256::zero())); assert!(engine.verify_block_family(&header, &parent_header).is_ok()); header.set_seal(vec![encode(&3usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]); + header.set_difficulty(calculate_score(U256::from(4), U256::from(3), U256::zero())); assert!(engine.verify_block_family(&header, &parent_header).is_err()); } From 7af6fc3eb87c4c00d7b7a2e39e49910e7bdbb540 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 19 Jul 2018 08:42:26 +0800 Subject: [PATCH 3/6] test: proposer_switching --- ethcore/src/engines/authority_round/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 7861b71fd76..92400b23dd4 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1530,12 +1530,15 @@ mod tests { let engine = Spec::new_test_round().engine; - let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap(); // Two validators. // Spec starts with step 2. + header.set_difficulty(calculate_score(U256::from(0), U256::from(2), U256::zero())); + let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap(); header.set_seal(vec![encode(&2usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]); assert!(engine.verify_block_family(&header, &parent_header).is_ok()); assert!(engine.verify_block_external(&header).is_err()); + header.set_difficulty(calculate_score(U256::from(0), U256::from(1), U256::zero())); + let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap(); header.set_seal(vec![encode(&1usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]); assert!(engine.verify_block_family(&header, &parent_header).is_ok()); assert!(engine.verify_block_external(&header).is_ok()); From c83d817f72b08c5c9e6391a1cf573ff46a2d3a8e Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 19 Jul 2018 08:44:53 +0800 Subject: [PATCH 4/6] test: rejects_future_block --- ethcore/src/engines/authority_round/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 92400b23dd4..cf956064ced 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1559,9 +1559,10 @@ mod tests { let engine = Spec::new_test_round().engine; - let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap(); // Two validators. // Spec starts with step 2. + header.set_difficulty(calculate_score(U256::from(0), U256::from(1), U256::zero())); + let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap(); header.set_seal(vec![encode(&1usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]); assert!(engine.verify_block_family(&header, &parent_header).is_ok()); assert!(engine.verify_block_external(&header).is_ok()); From 45bbde48b58b76a4011ee2dd8ebf3a6f8bdafe69 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 19 Jul 2018 08:47:45 +0800 Subject: [PATCH 5/6] test: reports_skipped --- ethcore/src/engines/authority_round/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index cf956064ced..36fa2b6dc10 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1626,6 +1626,7 @@ mod tests { parent_header.set_seal(vec![encode(&1usize).into_vec()]); parent_header.set_gas_limit("222222".parse::().unwrap()); let mut header: Header = Header::default(); + header.set_difficulty(calculate_score(U256::from(1), U256::from(3), U256::zero())); header.set_gas_limit("222222".parse::().unwrap()); header.set_seal(vec![encode(&3usize).into_vec()]); From b4ebeb2c24c0b47d240df138fe131ae293c91384 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 19 Jul 2018 08:50:38 +0800 Subject: [PATCH 6/6] test: verify_empty_seal_steps --- ethcore/src/engines/authority_round/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 36fa2b6dc10..0c4906be565 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1997,16 +1997,15 @@ mod tests { let empty_step3 = sealed_empty_step(engine, 3, &parent_header.hash()); let empty_steps = vec![empty_step2, empty_step3]; + header.set_difficulty(calculate_score(U256::from(0), U256::from(4), U256::from(2))); + let signature = tap.sign(addr1, Some("1".into()), header.bare_hash()).unwrap(); header.set_seal(vec![ encode(&4usize).into_vec(), encode(&(&*signature as &[u8])).into_vec(), ::rlp::encode_list(&empty_steps).into_vec(), ]); - assert!(match engine.verify_block_family(&header, &parent_header) { - Ok(_) => true, - _ => false, - }); + assert!(engine.verify_block_family(&header, &parent_header).is_ok()); } #[test]