Skip to content

Commit

Permalink
Add NewBestBlockNumber event.
Browse files Browse the repository at this point in the history
  • Loading branch information
shamil-gadelshin committed Apr 14, 2024
1 parent db67e2f commit 756241c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions substrate/client/network/sync/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,9 @@ where
ToServiceCommand::SetSyncForkRequest(peers, hash, number) => {
self.strategy.set_sync_fork_request(peers, &hash, number);
},
ToServiceCommand::NewBestBlockNumber(number) => {
self.strategy.update_common_number_for_peers(number);
},
ToServiceCommand::EventStream(tx) => self.event_streams.push(tx),
ToServiceCommand::RequestJustification(hash, number) =>
self.strategy.request_justification(&hash, number),
Expand Down
5 changes: 5 additions & 0 deletions substrate/client/network/sync/src/service/syncing_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::{

/// Commands send to `SyncingEngine`
pub enum ToServiceCommand<B: BlockT> {
NewBestBlockNumber(NumberFor<B>),
SetSyncForkRequest(Vec<PeerId>, B::Hash, NumberFor<B>),
RequestJustification(B::Hash, NumberFor<B>),
ClearJustificationRequests,
Expand Down Expand Up @@ -91,6 +92,10 @@ impl<B: BlockT> SyncingService<B> {
rx.await
}

pub fn new_best_number(&self, number: NumberFor<B>) {
let _ = self.tx.unbounded_send(ToServiceCommand::NewBestBlockNumber(number));
}

/// Get best seen block.
pub async fn best_seen_block(&self) -> Result<Option<NumberFor<B>>, oneshot::Canceled> {
let (tx, rx) = oneshot::channel();
Expand Down
9 changes: 9 additions & 0 deletions substrate/client/network/sync/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ where
}
}

pub fn update_common_number_for_peers(&mut self, number: NumberFor<B>) {
match self {
SyncingStrategy::WarpSyncStrategy(_) => {},
SyncingStrategy::StateSyncStrategy(_) => {},
SyncingStrategy::ChainSyncStrategy(strategy) =>
strategy.update_common_number_for_peers(number),
}
}

/// Request extra justification.
pub fn request_justification(&mut self, hash: &B::Hash, number: NumberFor<B>) {
match self {
Expand Down
10 changes: 10 additions & 0 deletions substrate/client/network/sync/src/strategy/chain_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,16 @@ where
}
}

pub fn update_common_number_for_peers(&mut self, new_common: NumberFor<B>) {
for peer in self.peers.values_mut() {
if peer.best_number >= new_common {
peer.update_common_number(new_common);
} else {
peer.update_common_number(peer.best_number);
}
}
}

/// Called when a block has been queued for import.
///
/// Updates our internal state for best queued block and then goes
Expand Down

0 comments on commit 756241c

Please sign in to comment.