Skip to content

Commit

Permalink
body sync, don't request blocks form peers that are not suppose to ha…
Browse files Browse the repository at this point in the history
…ve them.
  • Loading branch information
bayk committed Dec 4, 2024
1 parent 95048c4 commit 404fc46
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions servers/src/mwc/sync/body_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,24 @@ impl BodySync {
let head = self.chain.head()?;
let header_head = self.chain.header_head()?;

let max_avail_height = cmp::min(best_height, header_head.height);

// Last few blocks no need to sync, new mined blocks will be synced regular way
if head.height > header_head.height.saturating_sub(3) {
if head.height > max_avail_height.saturating_sub(3) {
// sync is done, we are ready.
return Ok(SyncResponse::new(
SyncRequestResponses::BodyReady,
Capabilities::UNKNOWN,
format!(
"head.height={} vs header_head.height={}",
head.height, header_head.height
"head.height={} vs max_avail_height={}",
head.height, max_avail_height
),
));
}

let archive_height = Chain::height_2_archive_height(best_height);

let head = self.chain.head()?;
let header_head = self.chain.header_head()?;
let fork_point = self.chain.fork_point()?;

if !self.chain.archive_mode() {
Expand Down Expand Up @@ -191,7 +192,7 @@ impl BodySync {
// Don't collect more than 500 blocks in the cache. The block size limit is 1.5MB, so total cache mem can be up to 750 Mb which is ok
let max_height = cmp::min(
fork_point.height + (self.pibd_params.get_orphans_num_limit() / 2) as u64,
header_head.height,
max_avail_height,
);
let mut current = self.chain.get_header_by_height(max_height)?;

Expand Down Expand Up @@ -293,8 +294,15 @@ impl BodySync {
sync_peers: &mut SyncPeers,
) -> Result<(), chain::Error> {
// request_series naturally from head to tail, but requesting better to send from tail to the head....
let mut peers = peers.clone();
for (hash, height) in self.request_series.iter().rev() {
if self.is_need_request_block(&hash)? {
// For tip we don't want request data from the peers that don't have anuthing.
peers.retain(|p| p.info.live_info.read().height >= *height);
if peers.is_empty() {
*need_request = 0;
return Ok(());
}
// can request a block...
let peer = peers.choose(rng).expect("Peers can't be empty");
if let Err(e) = peer.send_block_request(hash.clone(), chain::Options::SYNC) {
Expand Down

0 comments on commit 404fc46

Please sign in to comment.