Add block announce validator. (#3346)

* Add `BlockAnnounceValidator` trait.

* Add associated data to block announcement.

* Make tests compile.

* Move validator into `sync.rs`.

* Smaller changes.

* Update core/network/src/protocol.rs

Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update core/network/src/protocol.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update core/network/src/test/sync.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Formatting.

* Remove assoc. data from `BlockImportNotification`.

* Use `Option<Vec<u8>>` for associated data.

* Update core/network/src/protocol/sync.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix type error.
This commit is contained in:
Toralf Wittner
2019-09-24 10:48:21 +02:00
committed by Bastian Köcher
parent 4888c253a3
commit af0d71d389
16 changed files with 207 additions and 89 deletions
+8 -5
View File
@@ -37,6 +37,7 @@ use client::{
use client::block_builder::BlockBuilder;
use client::backend::{AuxStore, Backend, Finalizer};
use crate::config::Roles;
use consensus::block_validation::DefaultBlockAnnounceValidator;
use consensus::import_queue::BasicQueue;
use consensus::import_queue::{
BoxBlockImport, BoxJustificationImport, Verifier, BoxFinalityProofImport,
@@ -254,8 +255,8 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
}
/// Announces an important block on the network.
pub fn announce_block(&self, hash: <Block as BlockT>::Hash) {
self.network.service().announce_block(hash);
pub fn announce_block(&self, hash: <Block as BlockT>::Hash, data: Vec<u8>) {
self.network.service().announce_block(hash, data);
}
/// Add blocks to the peer -- edit the block before adding
@@ -302,11 +303,11 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
Default::default()
};
self.block_import.import_block(import_block, cache).expect("block_import failed");
self.network.on_block_imported(hash, header, true);
self.network.on_block_imported(hash, header, Vec::new(), true);
at = hash;
}
self.network.service().announce_block(at.clone());
self.network.service().announce_block(at.clone(), Vec::new());
at
}
@@ -555,6 +556,7 @@ pub trait TestNetFactory: Sized {
protocol_id: ProtocolId::from(&b"test-protocol-name"[..]),
import_queue,
specialization: self::SpecializationFactory::create(),
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
}).unwrap();
self.mut_peers(|peers| {
@@ -628,6 +630,7 @@ pub trait TestNetFactory: Sized {
protocol_id: ProtocolId::from(&b"test-protocol-name"[..]),
import_queue,
specialization: self::SpecializationFactory::create(),
block_announce_validator: Box::new(DefaultBlockAnnounceValidator::new(client.clone()))
}).unwrap();
self.mut_peers(|peers| {
@@ -690,7 +693,7 @@ pub trait TestNetFactory: Sized {
// We poll `imported_blocks_stream`.
while let Ok(Async::Ready(Some(notification))) = peer.imported_blocks_stream.poll() {
peer.network.on_block_imported(notification.hash, notification.header, true);
peer.network.on_block_imported(notification.hash, notification.header, Vec::new(), true);
}
// We poll `finality_notification_stream`, but we only take the last event.
+2 -2
View File
@@ -444,7 +444,7 @@ fn can_sync_small_non_best_forks() {
assert!(net.peer(0).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
assert!(!net.peer(1).client().header(&BlockId::Hash(small_hash)).unwrap().is_some());
net.peer(0).announce_block(small_hash);
net.peer(0).announce_block(small_hash, Vec::new());
// after announcing, peer 1 downloads the block.
@@ -499,7 +499,7 @@ fn light_peer_imports_header_from_announce() {
let mut runtime = current_thread::Runtime::new().unwrap();
fn import_with_announce(net: &mut TestNet, runtime: &mut current_thread::Runtime, hash: H256) {
net.peer(0).announce_block(hash);
net.peer(0).announce_block(hash, Vec::new());
runtime.block_on(futures::future::poll_fn::<(), (), _>(|| {
net.poll();