Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Apr 24, 2023
1 parent 4311267 commit eff0b47
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl ChainService {

for leader_hash in self.orphan_blocks_broker().clone_leaders() {
if !db_txn.get_block_epoch_index(&leader_hash).is_some() {
debug!("block {}'s block_epoch_index not stored", leader_hash);
trace!("a orphan leader: {} not stored", leader_hash);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion sync/src/relayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
};
use ckb_chain::chain::ChainController;
use ckb_constant::sync::BAD_MESSAGE_BAN_TIME;
use ckb_logger::{debug_target, error_target, info, info_target, trace_target, warn_target};
use ckb_logger::{debug, debug_target, error_target, info, info_target, trace_target, warn_target};
use ckb_network::{
async_trait, bytes::Bytes, tokio, CKBProtocolContext, CKBProtocolHandler, PeerIndex,
SupportProtocols, TargetSession,
Expand Down
21 changes: 18 additions & 3 deletions sync/src/synchronizer/block_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use ckb_shared::{BlockStatus, HeaderView};
use ckb_systemtime::unix_time_as_millis;
use ckb_types::{core, packed};
use std::cmp::min;
use std::time::Duration;

pub struct BlockFetcher<'a> {
synchronizer: &'a Synchronizer,
Expand Down Expand Up @@ -190,10 +189,11 @@ impl<'a> BlockFetcher<'a> {
&& inflight.insert(self.peer, (header.number(), hash).into())
{
debug!(
"request peer-{} for {}-{}",
"request peer-{} for {}-{}, tip: {}",
self.peer,
header.number(),
header.hash()
header.hash(),
self.active_chain.tip_number(),
);
fetch.push(header)
}
Expand Down Expand Up @@ -237,6 +237,21 @@ impl<'a> BlockFetcher<'a> {
inflight.total_inflight_count(),
*inflight
)
} else {
let fetch_head = fetch.first().map_or(0_u64.into(), |v| v.number());
let fetch_last = fetch.last().map_or(0_u64.into(), |v| v.number());
let inflight_peer_count = inflight.peer_inflight_count(self.peer);
let inflight_total_count = inflight.total_inflight_count();
debug!(
"request peer-{} for batch blocks: [{}-{}], batch len:{} , tip_header: {}, [peer / total inflight count]: [{} / {}]",
self.peer,
fetch_head,
fetch_last,
fetch.len(),
self.active_chain.tip_number(),
inflight_peer_count,
inflight_total_count,
);
}

Some(
Expand Down
4 changes: 3 additions & 1 deletion sync/src/synchronizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,9 @@ impl Synchronizer {
continue;
}
if let Err(err) = nc.disconnect(*peer, "sync disconnect") {
debug!("synchronizer disconnect error: {:?}", err);
debug!("synchronizer disconnect peer-{}, error: {:?}", *peer, err);
} else {
debug!("synchronizer disconnect peer-{}", *peer);
}
}

Expand Down
22 changes: 18 additions & 4 deletions sync/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ckb_constant::sync::{
RETRY_ASK_TX_TIMEOUT_INCREASE, SUSPEND_SYNC_TIME,
};
use ckb_error::{is_internal_db_error, Error as CKBError};
use ckb_logger::{debug, error, trace};
use ckb_logger::{debug, error, info, trace};
use ckb_network::{CKBProtocolContext, PeerIndex, SupportProtocols};
use ckb_shared::{shared::Shared, Snapshot};
use ckb_shared::{BlockStatus, HeaderView};
Expand Down Expand Up @@ -263,6 +263,11 @@ impl HeadersSyncController {
}
}

#[derive(Clone, Default, Debug)]
pub struct PeerTrace {
connected_at: u64,
}

#[derive(Clone, Default, Debug)]
pub struct PeerState {
pub headers_sync_controller: Option<HeadersSyncController>,
Expand All @@ -275,17 +280,20 @@ pub struct PeerState {
// use on ibd concurrent block download
// save `get_headers` locator hashes here
pub unknown_header_list: Vec<Byte32>,

pub trace: PeerTrace,
}

impl PeerState {
pub fn new(peer_flags: PeerFlags) -> PeerState {
pub fn new(peer_flags: PeerFlags, connected_at: u64) -> PeerState {
PeerState {
headers_sync_controller: None,
peer_flags,
chain_sync: ChainSyncState::default(),
best_known_header: None,
last_common_header: None,
unknown_header_list: Vec::new(),
trace: PeerTrace { connected_at },
}
}

Expand Down Expand Up @@ -850,7 +858,7 @@ impl Peers {
state.sync_connected();
})
.or_insert_with(|| {
let mut state = PeerState::new(peer_flags);
let mut state = PeerState::new(peer_flags, unix_time_as_millis());
state.sync_connected();
state
});
Expand All @@ -859,7 +867,7 @@ impl Peers {
pub fn relay_connected(&self, peer: PeerIndex) {
self.state
.entry(peer)
.or_insert_with(|| PeerState::new(PeerFlags::default()));
.or_insert_with(|| PeerState::new(PeerFlags::default(), unix_time_as_millis()));
}

pub fn get_best_known_header(&self, pi: PeerIndex) -> Option<HeaderView> {
Expand Down Expand Up @@ -909,6 +917,12 @@ impl Peers {
);
}

info!(
"disconnected peer: {}, connection_duration: {}",
peer,
unix_time_as_millis() - peer_state.trace.connected_at
);

// Protection node disconnected
if peer_state.peer_flags.is_protect {
assert_ne!(
Expand Down
2 changes: 1 addition & 1 deletion util/app-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use clap::ArgMatches;
use std::{path::PathBuf, str::FromStr};

// 500_000 total difficulty
const MIN_CHAIN_WORK_500K: U256 = u256!("0x3314412053c82802a7");
const MIN_CHAIN_WORK_500K: U256 = u256!("0x331");

/// A struct including all the information to start the ckb process.
pub struct Setup {
Expand Down

0 comments on commit eff0b47

Please sign in to comment.