From ae3c791068f2f76c67cd5483501f09de3fd8cc0b Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Sat, 18 Jul 2020 21:14:04 +0800 Subject: [PATCH] fix: sync too-new headers --- sync/src/synchronizer/headers_process.rs | 17 ++++++++++++----- test/src/specs/sync/block_sync.rs | 22 +++++++++++++--------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/sync/src/synchronizer/headers_process.rs b/sync/src/synchronizer/headers_process.rs index 2fdd179bde..a972d4feca 100644 --- a/sync/src/synchronizer/headers_process.rs +++ b/sync/src/synchronizer/headers_process.rs @@ -310,6 +310,7 @@ where // HeaderVerifier return HeaderError or UnknownParentError if let Some(header_error) = error.downcast_ref::() { if header_error.is_too_new() { + state.temporary_invalid(Some(ValidationError::Verify(error))); false } else { state.dos(Some(ValidationError::Verify(error)), 100); @@ -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 } } @@ -422,10 +424,15 @@ impl ValidationResult { pub fn dos(&mut self, error: Option, misbehavior: u32) { self.error = error; self.misbehavior += misbehavior; - self.state = ValidationState::INVALID; + self.state = ValidationState::Invalid; + } + + pub fn temporary_invalid(&mut self, error: Option) { + self.error = error; + self.state = ValidationState::TemporaryInvalid; } pub fn is_valid(&self) -> bool { - self.state == ValidationState::VALID + self.state == ValidationState::Valid } } diff --git a/test/src/specs/sync/block_sync.rs b/test/src/specs/sync/block_sync.rs index fb3d0a442a..ffaddff9ab 100644 --- a/test/src/specs/sync/block_sync.rs +++ b/test/src/specs/sync/block_sync.rs @@ -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",); }