Import headers from BlockAnnounce message on light nodes (#2731)

* import headers from announce message on light

* lines width

* added comments
This commit is contained in:
Svyatoslav Nikolsky
2019-05-31 10:28:09 +03:00
committed by Gavin Wood
parent 1d6696a0ec
commit 0d9fad431b
4 changed files with 262 additions and 96 deletions
+2 -2
View File
@@ -521,7 +521,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Synchronize with import queue.
#[cfg(any(test, feature = "test-helpers"))]
fn import_queue_sync(&self) {
pub fn import_queue_sync(&self) {
self.import_queue.synchronize();
let _ = self.net_proto_channel.wait_sync();
}
@@ -675,7 +675,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
/// Push blocks to the peer (simplified: with or without a TX) starting from
/// given hash.
fn push_blocks_at(&self, at: BlockId<Block>, count: usize, with_tx: bool) -> H256 {
pub fn push_blocks_at(&self, at: BlockId<Block>, count: usize, with_tx: bool) -> H256 {
let mut nonce = 0;
if with_tx {
self.generate_blocks_at(at, count, BlockOrigin::File, |mut builder| {
+33
View File
@@ -452,3 +452,36 @@ fn can_not_sync_from_light_peer() {
// check that light #1 has disconnected from #2
assert_eq!(net.peer(1).protocol_status().num_peers, 1);
}
#[test]
fn light_peer_imports_header_from_announce() {
let _ = ::env_logger::try_init();
fn import_with_announce(net: &mut TestNet, hash: H256) {
let header = net.peer(0).client().header(&BlockId::Hash(hash)).unwrap().unwrap();
net.peer(1).receive_message(
&net.peer(0).peer_id,
message::generic::Message::BlockAnnounce(message::generic::BlockAnnounce {
header,
}),
);
net.peer(1).import_queue_sync();
assert!(net.peer(1).client().header(&BlockId::Hash(hash)).unwrap().is_some());
}
// given the network with 1 full nodes (#0) and 1 light node (#1)
let mut net = TestNet::new(1);
net.add_light_peer(&Default::default());
// let them connect to each other
net.sync();
// check that NEW block is imported from announce message
let new_hash = net.peer(0).push_blocks(1, false);
import_with_announce(&mut net, new_hash);
// check that KNOWN STALE block is imported from announce message
let known_stale_hash = net.peer(0).push_blocks_at(BlockId::Number(0), 1, true);
import_with_announce(&mut net, known_stale_hash);
}