mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 03:18:01 +00:00
Inform sync explicitly about new best block (#7604)
* Inform sync explicitly about new best block Instead of "fishing" the new best block out of the processed blocks, we now tell sync directly that there is a new best block. It also makes sure that we update the corresponding sync handshake to the new best block. This is required for parachains as they first import blocks and declare the new best block after being made aware of it by the relay chain. * Adds test * Make sure async stuff had time to run
This commit is contained in:
@@ -528,10 +528,9 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
self.sync.num_sync_requests()
|
||||
}
|
||||
|
||||
/// Sync local state with the blockchain state.
|
||||
pub fn update_chain(&mut self) {
|
||||
let info = self.context_data.chain.info();
|
||||
self.sync.update_chain_info(&info.best_hash, info.best_number);
|
||||
/// Inform sync about new best imported block.
|
||||
pub fn new_best_block_imported(&mut self, hash: B::Hash, number: NumberFor<B>) {
|
||||
self.sync.update_chain_info(&hash, number);
|
||||
self.behaviour.set_legacy_handshake_message(
|
||||
build_status_message(&self.config, &self.context_data.chain),
|
||||
);
|
||||
@@ -541,11 +540,6 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
);
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
@@ -1258,18 +1252,6 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
count: usize,
|
||||
results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>
|
||||
) {
|
||||
let new_best = results.iter().rev().find_map(|r| match r {
|
||||
(Ok(BlockImportResult::ImportedUnknown(n, aux, _)), hash) if aux.is_new_best => Some((*n, hash.clone())),
|
||||
_ => None,
|
||||
});
|
||||
if let Some((best_num, best_hash)) = new_best {
|
||||
self.sync.update_chain_info(&best_hash, best_num);
|
||||
self.behaviour.set_legacy_handshake_message(build_status_message(&self.config, &self.context_data.chain));
|
||||
self.behaviour.set_notif_protocol_handshake(
|
||||
&self.block_announces_protocol,
|
||||
BlockAnnouncesHandshake::build(&self.config, &self.context_data.chain).encode()
|
||||
);
|
||||
}
|
||||
let results = self.sync.on_blocks_processed(
|
||||
imported,
|
||||
count,
|
||||
|
||||
@@ -484,11 +484,9 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
|
||||
self.network_service.user_protocol_mut().on_block_finalized(hash, &header);
|
||||
}
|
||||
|
||||
/// This should be called when blocks are added to the
|
||||
/// chain by something other than the import queue.
|
||||
/// Currently this is only useful for tests.
|
||||
pub fn update_chain(&mut self) {
|
||||
self.network_service.user_protocol_mut().update_chain();
|
||||
/// Inform the network service about new best imported block.
|
||||
pub fn new_best_block_imported(&mut self, hash: B::Hash, number: NumberFor<B>) {
|
||||
self.network_service.user_protocol_mut().new_best_block_imported(hash, number);
|
||||
}
|
||||
|
||||
/// Returns the local `PeerId`.
|
||||
@@ -1012,21 +1010,11 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
|
||||
self.num_connected.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// This function should be called when blocks are added to the chain by something other
|
||||
/// than the import queue.
|
||||
///
|
||||
/// > **Important**: This function is a hack and can be removed at any time. Do **not** use it.
|
||||
pub fn update_chain(&self) {
|
||||
/// Inform the network service about new best imported block.
|
||||
pub fn new_best_block_imported(&self, hash: B::Hash, number: NumberFor<B>) {
|
||||
let _ = self
|
||||
.to_worker
|
||||
.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));
|
||||
.unbounded_send(ServiceToWorkerMsg::NewBestBlockImported(hash, number));
|
||||
}
|
||||
|
||||
/// Utility function to extract `PeerId` from each `Multiaddr` for priority group updates.
|
||||
@@ -1181,8 +1169,7 @@ enum ServiceToWorkerMsg<B: BlockT, H: ExHashT> {
|
||||
protocol_name: Cow<'static, str>,
|
||||
},
|
||||
DisconnectPeer(PeerId),
|
||||
UpdateChain,
|
||||
OwnBlockImported(B::Hash, NumberFor<B>),
|
||||
NewBestBlockImported(B::Hash, NumberFor<B>),
|
||||
}
|
||||
|
||||
/// Main network worker. Must be polled in order for the network to advance.
|
||||
@@ -1319,10 +1306,8 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
|
||||
this.network_service.register_notifications_protocol(protocol_name),
|
||||
ServiceToWorkerMsg::DisconnectPeer(who) =>
|
||||
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),
|
||||
ServiceToWorkerMsg::NewBestBlockImported(hash, number) =>
|
||||
this.network_service.user_protocol_mut().new_best_block_imported(hash, number),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user