Skip to content

Commit

Permalink
fix: sync too-new headers
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad authored and doitian committed Jul 19, 2020
1 parent e10ca96 commit ae3c791
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
17 changes: 12 additions & 5 deletions sync/src/synchronizer/headers_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ where
// HeaderVerifier return HeaderError or UnknownParentError
if let Some(header_error) = error.downcast_ref::<HeaderError>() {
if header_error.is_too_new() {
state.temporary_invalid(Some(ValidationError::Verify(error)));
false
} else {
state.dos(Some(ValidationError::Verify(error)), 100);
Expand Down Expand Up @@ -390,13 +391,14 @@ where

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ValidationState {
VALID,
INVALID,
Valid,
TemporaryInvalid,
Invalid,
}

impl Default for ValidationState {
fn default() -> Self {
ValidationState::VALID
ValidationState::Valid
}
}

Expand All @@ -422,10 +424,15 @@ impl ValidationResult {
pub fn dos(&mut self, error: Option<ValidationError>, misbehavior: u32) {
self.error = error;
self.misbehavior += misbehavior;
self.state = ValidationState::INVALID;
self.state = ValidationState::Invalid;
}

pub fn temporary_invalid(&mut self, error: Option<ValidationError>) {
self.error = error;
self.state = ValidationState::TemporaryInvalid;
}

pub fn is_valid(&self) -> bool {
self.state == ValidationState::VALID
self.state == ValidationState::Valid
}
}
22 changes: 13 additions & 9 deletions test/src/specs/sync/block_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,28 +513,32 @@ impl Spec for SyncTooNewBlock {
net.exit_ibd_mode();

let future = Duration::from_secs(6_000).as_millis() as u64;
let too_new_block = node0
.new_block_builder(None, None, None)
.timestamp((now_ms() + future).pack())
.build();

// node1 sync node1 with too new block
let _too_new_hash = node0.process_block_without_verify(&too_new_block, false);
for _ in 0..3 {
let too_new_block = node0
.new_block_builder(None, None, None)
.timestamp((now_ms() + future).pack())
.build();

// node1 sync node1 with too new block
let _too_new_hash = node0.process_block_without_verify(&too_new_block, false);
}

let (rpc_client0, rpc_client1) = (node0.rpc_client(), node1.rpc_client());
node1.connect(node0);

// sync node0 node2
node2.generate_blocks(2);
node2.generate_blocks(6);
node2.connect(node0);
node2.waiting_for_sync(node0, node2.get_tip_block_number());
node2.disconnect(node0);

sleep(15); // GET_HEADERS_TIMEOUT 15s
node0.generate_block();
let (rpc_client0, rpc_client1) = (node0.rpc_client(), node1.rpc_client());
let ret = wait_until(20, || {
let header0 = rpc_client0.get_tip_header();
let header1 = rpc_client1.get_tip_header();
header0 == header1 && header1.inner.number.value() == 4
header0 == header1 && header1.inner.number.value() == 8
});
assert!(ret, "Node1 should not ban Node0",);
}
Expand Down

0 comments on commit ae3c791

Please # to comment.