Adds construct_simple_protocol macro for simplifying the creation of protocols (#897)

* Make `on_message` take the message as `&mut Option<_>`

* Make `ConsensusGossip` implement `Specialization`

* Move `new_session` into `ConsensusGossip`

* Adds `construct_simple_protocol` macro for simplifying the creation of protocols
This commit is contained in:
Bastian Köcher
2018-10-12 13:10:36 +02:00
committed by Gav Wood
parent 671b0e0007
commit db427cb45c
7 changed files with 167 additions and 74 deletions
+1 -1
View File
@@ -275,7 +275,7 @@ impl<P: AuthoringApi + Send + Sync + 'static> Network for ConsensusNetwork<P> {
// spin up a task in the background that processes all incoming statements
// TODO: propagate statements on a timer?
let process_task = self.network.with_spec(|spec, _ctx| {
spec.new_consensus(parent_hash);
spec.consensus_gossip.new_session(parent_hash);
MessageProcessTask {
inner_stream: spec.consensus_gossip.messages_for(parent_hash),
bft_messages: bft_send,
+6 -69
View File
@@ -21,6 +21,7 @@
#![warn(unused_extern_crates)]
extern crate substrate_bft as bft;
#[macro_use]
extern crate substrate_network;
extern crate substrate_primitives;
@@ -36,79 +37,15 @@ extern crate log;
pub mod consensus;
use node_primitives::{Block, Hash, Header};
use substrate_network::{NodeIndex, Context, Severity};
use node_primitives::{Block, Hash};
use substrate_network::consensus_gossip::ConsensusGossip;
use substrate_network::{message, generic_message};
use substrate_network::specialization::Specialization;
use substrate_network::StatusMessage as GenericFullStatus;
type FullStatus = GenericFullStatus<Block>;
/// Specialization of the network service for the node protocol.
pub type NetworkService = ::substrate_network::Service<Block, Protocol, Hash>;
/// Demo protocol attachment for substrate.
pub struct Protocol {
consensus_gossip: ConsensusGossip<Block>,
live_consensus: Option<Hash>,
}
impl Protocol {
/// Instantiate a node protocol handler.
pub fn new() -> Self {
Protocol {
consensus_gossip: ConsensusGossip::new(),
live_consensus: None,
}
}
/// Note new consensus session.
fn new_consensus(&mut self, parent_hash: Hash) {
let old_consensus = self.live_consensus.take();
self.live_consensus = Some(parent_hash);
self.consensus_gossip
.collect_garbage(|topic| old_consensus.as_ref().map_or(true, |h| topic != h));
}
}
impl Specialization<Block> for Protocol {
fn status(&self) -> Vec<u8> {
Vec::new()
}
fn on_connect(&mut self, ctx: &mut Context<Block>, who: NodeIndex, status: FullStatus) {
self.consensus_gossip.new_peer(ctx, who, status.roles);
}
fn on_disconnect(&mut self, ctx: &mut Context<Block>, who: NodeIndex) {
self.consensus_gossip.peer_disconnected(ctx, who);
}
fn on_message(&mut self, ctx: &mut Context<Block>, who: NodeIndex, message: message::Message<Block>) {
match message {
generic_message::Message::BftMessage(msg) => {
trace!(target: "node-network", "BFT message from {}: {:?}", who, msg);
// TODO: check signature here? what if relevant block is unknown?
self.consensus_gossip.on_bft_message(ctx, who, msg)
}
generic_message::Message::ChainSpecific(_) => {
trace!(target: "node-network", "Bad message from {}", who);
ctx.report_peer(who, Severity::Bad("Invalid node protocol message format"));
}
_ => {}
}
}
fn on_abort(&mut self) {
self.consensus_gossip.abort();
}
fn maintain_peers(&mut self, _ctx: &mut Context<Block>) {
self.consensus_gossip.collect_garbage(|_| true);
}
fn on_block_imported(&mut self, _ctx: &mut Context<Block>, _hash: Hash, _header: &Header) {
construct_simple_protocol! {
/// Demo protocol attachment for substrate.
pub struct Protocol where Block = Block {
consensus_gossip: ConsensusGossip<Block>,
}
}