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
@@ -127,7 +127,7 @@ pub trait Network<Block: BlockT>: Clone + Send + 'static {
fn report(&self, who: network::PeerId, cost_benefit: i32);
/// Inform peers that a block with given hash should be downloaded.
fn announce(&self, block: Block::Hash);
fn announce(&self, block: Block::Hash, associated_data: Vec<u8>);
}
/// Create a unique topic for a round and set-id combo.
@@ -197,8 +197,8 @@ impl<B, S, H> Network<B> for Arc<NetworkService<B, S, H>> where
self.report_peer(who, cost_benefit)
}
fn announce(&self, block: B::Hash) {
self.announce_block(block)
fn announce(&self, block: B::Hash, associated_data: Vec<u8>) {
self.announce_block(block, associated_data)
}
}
@@ -727,7 +727,7 @@ impl<Block: BlockT, N: Network<Block>> Sink for OutgoingMessages<Block, N>
);
// send the target block hash to the background block announcer
self.announce_sender.send(target_hash);
self.announce_sender.send(target_hash, Vec::new());
// propagate the message to peers
let topic = round_topic::<Block>(self.round, self.set_id);
@@ -118,7 +118,7 @@ pub(super) fn neighbor_packet_worker<B, N>(net: N) -> (
/// A background worker for performing block announcements.
struct BlockAnnouncer<B: BlockT, N> {
net: N,
block_rx: mpsc::UnboundedReceiver<B::Hash>,
block_rx: mpsc::UnboundedReceiver<(B::Hash, Vec<u8>)>,
latest_voted_blocks: VecDeque<B::Hash>,
reannounce_after: Duration,
delay: Delay,
@@ -199,8 +199,8 @@ impl<B: BlockT, N: Network<B>> Future for BlockAnnouncer<B, N> {
match self.block_rx.poll().expect("unbounded receivers do not error; qed") {
Async::Ready(None) => return Ok(Async::Ready(())),
Async::Ready(Some(block)) => {
if self.note_block(block) {
self.net.announce(block);
if self.note_block(block.0) {
self.net.announce(block.0, block.1);
self.reset_delay();
}
},
@@ -229,7 +229,7 @@ impl<B: BlockT, N: Network<B>> Future for BlockAnnouncer<B, N> {
);
for block in self.latest_voted_blocks.iter() {
self.net.announce(*block);
self.net.announce(*block, Vec::new());
}
},
Ok(Async::NotReady) => return Ok(Async::NotReady),
@@ -240,15 +240,12 @@ impl<B: BlockT, N: Network<B>> Future for BlockAnnouncer<B, N> {
/// A sender used to send block hashes to announce to a background job.
#[derive(Clone)]
pub(super) struct BlockAnnounceSender<B: BlockT>(mpsc::UnboundedSender<B::Hash>);
pub(super) struct BlockAnnounceSender<B: BlockT>(mpsc::UnboundedSender<(B::Hash, Vec<u8>)>);
impl<B: BlockT> BlockAnnounceSender<B> {
/// Send a block hash for the background worker to announce.
pub fn send(
&self,
block: B::Hash,
) {
if let Err(err) = self.0.unbounded_send(block) {
pub fn send(&self, block: B::Hash, associated_data: Vec<u8>) {
if let Err(err) = self.0.unbounded_send((block, associated_data)) {
debug!(target: "afg", "Failed to send block to background announcer: {:?}", err);
}
}
@@ -88,7 +88,7 @@ impl super::Network<Block> for TestNetwork {
}
/// Inform peers that a block with given hash should be downloaded.
fn announce(&self, block: Hash) {
fn announce(&self, block: Hash, _associated_data: Vec<u8>) {
let _ = self.sender.unbounded_send(Event::Announce(block));
}
}
@@ -559,7 +559,7 @@ fn periodically_reannounce_voted_blocks_on_stall() {
for _ in 0..=12 {
let hash = Hash::random();
hashes.lock().push(hash);
announce_sender.send(hash);
announce_sender.send(hash, Vec::new());
}
// we should see an event for each of those announcements