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:
Bastian Köcher
2020-11-26 17:33:17 +01:00
committed by GitHub
parent 769f1981cb
commit d698d01378
7 changed files with 84 additions and 55 deletions
+32 -6
View File
@@ -279,7 +279,7 @@ impl<D> Peer<D> {
where F: FnMut(BlockBuilder<Block, PeersFullClient, substrate_test_runtime_client::Backend>) -> Block
{
let best_hash = self.client.info().best_hash;
self.generate_blocks_at(BlockId::Hash(best_hash), count, origin, edit_block, false)
self.generate_blocks_at(BlockId::Hash(best_hash), count, origin, edit_block, false, true)
}
/// Add blocks to the peer -- edit the block before adding. The chain will
@@ -291,6 +291,7 @@ impl<D> Peer<D> {
origin: BlockOrigin,
mut edit_block: F,
headers_only: bool,
inform_sync_about_new_best_block: bool,
) -> H256 where F: FnMut(BlockBuilder<Block, PeersFullClient, substrate_test_runtime_client::Backend>) -> Block {
let full_client = self.client.as_full()
.expect("blocks could only be generated by full clients");
@@ -328,7 +329,12 @@ impl<D> Peer<D> {
at = hash;
}
self.network.update_chain();
if inform_sync_about_new_best_block {
self.network.new_best_block_imported(
at,
full_client.header(&BlockId::Hash(at)).ok().flatten().unwrap().number().clone(),
);
}
self.network.service().announce_block(at.clone(), Vec::new());
at
}
@@ -342,18 +348,36 @@ impl<D> Peer<D> {
/// Push blocks to the peer (simplified: with or without a TX)
pub fn push_headers(&mut self, count: usize) -> H256 {
let best_hash = self.client.info().best_hash;
self.generate_tx_blocks_at(BlockId::Hash(best_hash), count, false, true)
self.generate_tx_blocks_at(BlockId::Hash(best_hash), count, false, true, true)
}
/// Push blocks to the peer (simplified: with or without a TX) starting from
/// given hash.
pub fn push_blocks_at(&mut self, at: BlockId<Block>, count: usize, with_tx: bool) -> H256 {
self.generate_tx_blocks_at(at, count, with_tx, false)
self.generate_tx_blocks_at(at, count, with_tx, false, true)
}
/// Push blocks to the peer (simplified: with or without a TX) starting from
/// given hash without informing the sync protocol about the new best block.
pub fn push_blocks_at_without_informing_sync(
&mut self,
at: BlockId<Block>,
count: usize,
with_tx: bool,
) -> H256 {
self.generate_tx_blocks_at(at, count, with_tx, false, false)
}
/// Push blocks/headers to the peer (simplified: with or without a TX) starting from
/// given hash.
fn generate_tx_blocks_at(&mut self, at: BlockId<Block>, count: usize, with_tx: bool, headers_only:bool) -> H256 {
fn generate_tx_blocks_at(
&mut self,
at: BlockId<Block>,
count: usize,
with_tx: bool,
headers_only: bool,
inform_sync_about_new_best_block: bool,
) -> H256 {
let mut nonce = 0;
if with_tx {
self.generate_blocks_at(
@@ -370,7 +394,8 @@ impl<D> Peer<D> {
nonce = nonce + 1;
builder.build().unwrap().block
},
headers_only
headers_only,
inform_sync_about_new_best_block,
)
} else {
self.generate_blocks_at(
@@ -379,6 +404,7 @@ impl<D> Peer<D> {
BlockOrigin::File,
|builder| builder.build().unwrap().block,
headers_only,
inform_sync_about_new_best_block,
)
}
}