Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Update sync chain info on own block import (#6424)
Browse files Browse the repository at this point in the history
Before we only updated the chain info of sync when we have imported
something using the import queue. However, if you import your own
blocks, this is not done using the import queue and so sync is not
updated. If we don't do this, it can lead to sync switching to "major
sync" mode because sync is not informed about new blocks. This
especially happens on Cumulus, where a collator is selected multiple
times to include its block into the relay chain and thus, sync switches
to major sync mode while the node is still building blocks.
  • Loading branch information
bkchr authored Jun 19, 2020
1 parent 18707b3 commit 26aec42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
self.sync.update_chain_info(&info.best_hash, info.best_number);
}

/// Inform sync about an own imported block.
pub fn own_block_imported(&mut self, hash: B::Hash, number: NumberFor<B>) {
self.sync.update_chain_info(&hash, number);
}

fn update_peer_info(&mut self, who: &PeerId) {
if let Some(info) = self.sync.peer_info(who) {
if let Some(ref mut peer) = self.context_data.peers.get_mut(who) {
Expand Down
9 changes: 9 additions & 0 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
.unbounded_send(ServiceToWorkerMsg::UpdateChain);
}

/// Inform the network service about an own imported block.
pub fn own_block_imported(&self, hash: B::Hash, number: NumberFor<B>) {
let _ = self
.to_worker
.unbounded_send(ServiceToWorkerMsg::OwnBlockImported(hash, number));
}
}

impl<B: BlockT + 'static, H: ExHashT> sp_consensus::SyncOracle
Expand Down Expand Up @@ -812,6 +818,7 @@ enum ServiceToWorkerMsg<B: BlockT, H: ExHashT> {
},
DisconnectPeer(PeerId),
UpdateChain,
OwnBlockImported(B::Hash, NumberFor<B>),
}

/// Main network worker. Must be polled in order for the network to advance.
Expand Down Expand Up @@ -1142,6 +1149,8 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
this.network_service.user_protocol_mut().disconnect_peer(&who),
ServiceToWorkerMsg::UpdateChain =>
this.network_service.user_protocol_mut().update_chain(),
ServiceToWorkerMsg::OwnBlockImported(hash, number) =>
this.network_service.user_protocol_mut().own_block_imported(hash, number),
}
}

Expand Down
9 changes: 8 additions & 1 deletion client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use sc_network::{NetworkService, NetworkStatus, network_state::NetworkState, Pee
use log::{log, warn, debug, error, Level};
use codec::{Encode, Decode};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::Block as BlockT;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use parity_util_mem::MallocSizeOf;
use sp_utils::{status_sinks, mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}};

Expand Down Expand Up @@ -382,6 +382,13 @@ fn build_network_future<
if announce_imported_blocks {
network.service().announce_block(notification.hash, Vec::new());
}

if let sp_consensus::BlockOrigin::Own = notification.origin {
network.service().own_block_imported(
notification.hash,
notification.header.number().clone(),
);
}
}

// We poll `finality_notification_stream`, but we only take the last event.
Expand Down

0 comments on commit 26aec42

Please sign in to comment.