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,
)
}
}
+35
View File
@@ -779,3 +779,38 @@ fn wait_until_deferred_block_announce_validation_is_ready() {
net.block_until_idle();
}
}
/// When we don't inform the sync protocol about the best block, a node will not sync from us as the
/// handshake is not does not contain our best block.
#[test]
fn sync_to_tip_requires_that_sync_protocol_is_informed_about_best_block() {
sp_tracing::try_init_simple();
log::trace!(target: "sync", "Test");
let mut net = TestNet::new(1);
// Produce some blocks
let block_hash = net.peer(0).push_blocks_at_without_informing_sync(BlockId::Number(0), 3, true);
// Add a node and wait until they are connected
net.add_full_peer_with_config(Default::default());
net.block_until_connected();
net.block_until_idle();
// The peer should not have synced the block.
assert!(!net.peer(1).has_block(&block_hash));
// Make sync protocol aware of the best block
net.peer(0).network_service().new_best_block_imported(block_hash, 3);
net.block_until_idle();
// Connect another node that should now sync to the tip
net.add_full_peer_with_config(Default::default());
net.block_until_connected();
while !net.peer(2).has_block(&block_hash) {
net.block_until_idle();
}
// However peer 1 should still not have the block.
assert!(!net.peer(1).has_block(&block_hash));
}