Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit 805864f

Browse files
committed
Be more graceful on Aura difficulty validation (#9164)
* Be more graceful on Aura difficulty validation * test: rejects_step_backwards * test: proposer_switching * test: rejects_future_block * test: reports_skipped * test: verify_empty_seal_steps
1 parent 085035f commit 805864f

File tree

1 file changed

+32
-14
lines changed
  • ethcore/src/engines/authority_round

1 file changed

+32
-14
lines changed

ethcore/src/engines/authority_round/mod.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -1149,9 +1149,10 @@ impl Engine<EthereumMachine> for AuthorityRound {
11491149

11501150
// If empty step messages are enabled we will validate the messages in the seal, missing messages are not
11511151
// reported as there's no way to tell whether the empty step message was never sent or simply not included.
1152-
if header.number() >= self.empty_steps_transition {
1153-
let validate_empty_steps = || -> Result<(), Error> {
1152+
let empty_steps_len = if header.number() >= self.empty_steps_transition {
1153+
let validate_empty_steps = || -> Result<usize, Error> {
11541154
let empty_steps = header_empty_steps(header)?;
1155+
let empty_steps_len = empty_steps.len();
11551156
for empty_step in empty_steps {
11561157
if empty_step.step <= parent_step || empty_step.step >= step {
11571158
Err(EngineError::InsufficientProof(
@@ -1168,16 +1169,27 @@ impl Engine<EthereumMachine> for AuthorityRound {
11681169
format!("invalid empty step proof: {:?}", empty_step)))?;
11691170
}
11701171
}
1171-
Ok(())
1172+
Ok(empty_steps_len)
11721173
};
11731174

1174-
if let err @ Err(_) = validate_empty_steps() {
1175-
self.validators.report_benign(header.author(), set_number, header.number());
1176-
return err;
1175+
match validate_empty_steps() {
1176+
Ok(len) => len,
1177+
Err(err) => {
1178+
self.validators.report_benign(header.author(), set_number, header.number());
1179+
return Err(err);
1180+
},
11771181
}
1178-
11791182
} else {
11801183
self.report_skipped(header, step, parent_step, &*validators, set_number);
1184+
1185+
0
1186+
};
1187+
1188+
if header.number() >= self.validate_score_transition {
1189+
let expected_difficulty = calculate_score(parent_step.into(), step.into(), empty_steps_len.into());
1190+
if header.difficulty() != &expected_difficulty {
1191+
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty().clone() })));
1192+
}
11811193
}
11821194

11831195
Ok(())
@@ -1408,7 +1420,7 @@ mod tests {
14081420
use engines::{Seal, Engine, EngineError, EthEngine};
14091421
use engines::validator_set::TestSet;
14101422
use error::{Error, ErrorKind};
1411-
use super::{AuthorityRoundParams, AuthorityRound, EmptyStep, SealedEmptyStep};
1423+
use super::{AuthorityRoundParams, AuthorityRound, EmptyStep, SealedEmptyStep, calculate_score};
14121424

14131425
#[test]
14141426
fn has_valid_metadata() {
@@ -1514,12 +1526,15 @@ mod tests {
15141526

15151527
let engine = Spec::new_test_round().engine;
15161528

1517-
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
15181529
// Two validators.
15191530
// Spec starts with step 2.
1531+
header.set_difficulty(calculate_score(U256::from(0), U256::from(2), U256::zero()));
1532+
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
15201533
header.set_seal(vec![encode(&2usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]);
15211534
assert!(engine.verify_block_family(&header, &parent_header).is_ok());
15221535
assert!(engine.verify_block_external(&header).is_err());
1536+
header.set_difficulty(calculate_score(U256::from(0), U256::from(1), U256::zero()));
1537+
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
15231538
header.set_seal(vec![encode(&1usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]);
15241539
assert!(engine.verify_block_family(&header, &parent_header).is_ok());
15251540
assert!(engine.verify_block_external(&header).is_ok());
@@ -1540,9 +1555,10 @@ mod tests {
15401555

15411556
let engine = Spec::new_test_round().engine;
15421557

1543-
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
15441558
// Two validators.
15451559
// Spec starts with step 2.
1560+
header.set_difficulty(calculate_score(U256::from(0), U256::from(1), U256::zero()));
1561+
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
15461562
header.set_seal(vec![encode(&1usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]);
15471563
assert!(engine.verify_block_family(&header, &parent_header).is_ok());
15481564
assert!(engine.verify_block_external(&header).is_ok());
@@ -1569,8 +1585,10 @@ mod tests {
15691585
// Two validators.
15701586
// Spec starts with step 2.
15711587
header.set_seal(vec![encode(&5usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]);
1588+
header.set_difficulty(calculate_score(U256::from(4), U256::from(5), U256::zero()));
15721589
assert!(engine.verify_block_family(&header, &parent_header).is_ok());
15731590
header.set_seal(vec![encode(&3usize).into_vec(), encode(&(&*signature as &[u8])).into_vec()]);
1591+
header.set_difficulty(calculate_score(U256::from(4), U256::from(3), U256::zero()));
15741592
assert!(engine.verify_block_family(&header, &parent_header).is_err());
15751593
}
15761594

@@ -1604,6 +1622,7 @@ mod tests {
16041622
parent_header.set_seal(vec![encode(&1usize).into_vec()]);
16051623
parent_header.set_gas_limit("222222".parse::<U256>().unwrap());
16061624
let mut header: Header = Header::default();
1625+
header.set_difficulty(calculate_score(U256::from(1), U256::from(3), U256::zero()));
16071626
header.set_gas_limit("222222".parse::<U256>().unwrap());
16081627
header.set_seal(vec![encode(&3usize).into_vec()]);
16091628

@@ -1963,16 +1982,15 @@ mod tests {
19631982
let empty_step3 = sealed_empty_step(engine, 3, &parent_header.hash());
19641983

19651984
let empty_steps = vec![empty_step2, empty_step3];
1985+
header.set_difficulty(calculate_score(U256::from(0), U256::from(4), U256::from(2)));
1986+
let signature = tap.sign(addr1, Some("1".into()), header.bare_hash()).unwrap();
19661987
header.set_seal(vec![
19671988
encode(&4usize).into_vec(),
19681989
encode(&(&*signature as &[u8])).into_vec(),
19691990
::rlp::encode_list(&empty_steps).into_vec(),
19701991
]);
19711992

1972-
assert!(match engine.verify_block_family(&header, &parent_header) {
1973-
Ok(_) => true,
1974-
_ => false,
1975-
});
1993+
assert!(engine.verify_block_family(&header, &parent_header).is_ok());
19761994
}
19771995

19781996
#[test]

0 commit comments

Comments
 (0)