Skip to content

Commit

Permalink
Merge pull request #171 from yangby-cryptape/feature/ban-peers-if-it-…
Browse files Browse the repository at this point in the history
…behind-too-many-blocks

feat: ban a peer if it's still in IBD with a very high probability
  • Loading branch information
quake authored Dec 30, 2023
2 parents 99bd4b8 + d180c49 commit be21607
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/protocols/light_client/components/send_last_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::super::{LastState, LightClientProtocol, Status};
use super::super::{LastState, LightClientProtocol, Status, StatusCode};
use ckb_constant::sync::MAX_TIP_AGE;
use ckb_network::{CKBProtocolContext, PeerIndex};
use ckb_systemtime::unix_time_as_millis;
use ckb_types::{packed, prelude::*, utilities::merkle_mountain_range::VerifiableHeader};
use log::{debug, trace};

Expand Down Expand Up @@ -30,6 +32,7 @@ impl<'a> SendLastStateProcess<'a> {

let last_header: VerifiableHeader = self.message.last_header().to_entity().into();
return_if_failed!(self.protocol.check_verifiable_header(&last_header));
return_if_failed!(check_last_state(&last_header));

let last_state = LastState::new(last_header);

Expand Down Expand Up @@ -94,3 +97,16 @@ impl<'a> SendLastStateProcess<'a> {
Status::ok()
}
}

fn check_last_state(last_header: &VerifiableHeader) -> Result<(), Status> {
let now = unix_time_as_millis();
let timestamp = last_header.header().timestamp();
if now.saturating_sub(timestamp) > MAX_TIP_AGE {
let errmsg = format!(
"still in initial block download with a very high probability \
since {now} - {timestamp} > {MAX_TIP_AGE}",
);
return Err(StatusCode::PeerIsInIBD.with_context(errmsg));
}
Ok(())
}
2 changes: 2 additions & 0 deletions src/protocols/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum StatusCode {
InvalidLastState = 412,
/// The peer state is not correct for transition.
IncorrectLastState = 413,
/// The peer is still in initial block download with a very high probability.
PeerIsInIBD = 414,

/// Receives a response but the peer isn't waiting for a response.
PeerIsNotOnProcess = 421,
Expand Down

0 comments on commit be21607

Please sign in to comment.