mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 15:11:03 +00:00
client/network-gossip: Move sink IO outside of state_machine (#5669)
* client/network-gossip: Move sink IO outside of state_machine `ConsensusGossip` is supposed to be a deterministic state machine. `GossipEngine` wrapping `ConsensusGossip` should handle IO operations. This commit moves the `message_sink` IO operations to `GossipEngine`. More specifically on incoming messages a `GossipEngine` calls `ConsensusGossip::on_incoming` to validate and register the messages. `ConsensusGossip` returns the valid messages which are then forwarded by `GossipEngine` to the upper layer via the `message_sinks`. * client/network-gossip: Adjust and extend tests * Update client/network-gossip/src/bridge.rs Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com>
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{Network, MessageIntent, Validator, ValidatorContext, ValidationResult};
|
||||
|
||||
use std::collections::{HashMap, HashSet, hash_map::Entry};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use std::iter;
|
||||
use std::time;
|
||||
@@ -24,7 +24,6 @@ use log::trace;
|
||||
use lru::LruCache;
|
||||
use libp2p::PeerId;
|
||||
use sp_runtime::traits::{Block as BlockT, Hash, HashFor};
|
||||
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender, TracingUnboundedReceiver};
|
||||
use sp_runtime::ConsensusEngineId;
|
||||
use sc_network::ObservedRole;
|
||||
use wasm_timer::Instant;
|
||||
@@ -51,7 +50,7 @@ struct PeerConsensus<H> {
|
||||
}
|
||||
|
||||
/// Topic stream message with sender.
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct TopicNotification {
|
||||
/// Message data.
|
||||
pub message: Vec<u8>,
|
||||
@@ -147,7 +146,6 @@ fn propagate<'a, B: BlockT, I>(
|
||||
/// Consensus network protocol handler. Manages statements and candidate requests.
|
||||
pub struct ConsensusGossip<B: BlockT> {
|
||||
peers: HashMap<PeerId, PeerConsensus<B::Hash>>,
|
||||
live_message_sinks: HashMap<B::Hash, Vec<TracingUnboundedSender<TopicNotification>>>,
|
||||
messages: Vec<MessageEntry<B>>,
|
||||
known_messages: LruCache<B::Hash, ()>,
|
||||
engine_id: ConsensusEngineId,
|
||||
@@ -160,7 +158,6 @@ impl<B: BlockT> ConsensusGossip<B> {
|
||||
pub fn new(validator: Arc<dyn Validator<B>>, engine_id: ConsensusEngineId) -> Self {
|
||||
ConsensusGossip {
|
||||
peers: HashMap::new(),
|
||||
live_message_sinks: HashMap::new(),
|
||||
messages: Default::default(),
|
||||
known_messages: LruCache::new(KNOWN_MESSAGES_CACHE_SIZE),
|
||||
engine_id,
|
||||
@@ -256,11 +253,6 @@ impl<B: BlockT> ConsensusGossip<B> {
|
||||
/// Prune old or no longer relevant consensus messages. Provide a predicate
|
||||
/// for pruning, which returns `false` when the items with a given topic should be pruned.
|
||||
pub fn collect_garbage(&mut self) {
|
||||
self.live_message_sinks.retain(|_, sinks| {
|
||||
sinks.retain(|sink| !sink.is_closed());
|
||||
!sinks.is_empty()
|
||||
});
|
||||
|
||||
let known_messages = &mut self.known_messages;
|
||||
let before = self.messages.len();
|
||||
|
||||
@@ -278,33 +270,24 @@ impl<B: BlockT> ConsensusGossip<B> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get data of valid, incoming messages for a topic (but might have expired meanwhile)
|
||||
pub fn messages_for(&mut self, topic: B::Hash)
|
||||
-> TracingUnboundedReceiver<TopicNotification>
|
||||
{
|
||||
let (tx, rx) = tracing_unbounded("mpsc_gossip_messages_for");
|
||||
for entry in self.messages.iter_mut().filter(|e| e.topic == topic) {
|
||||
tx.unbounded_send(TopicNotification {
|
||||
message: entry.message.clone(),
|
||||
sender: entry.sender.clone(),
|
||||
})
|
||||
.expect("receiver known to be live; qed");
|
||||
}
|
||||
|
||||
self.live_message_sinks.entry(topic).or_default().push(tx);
|
||||
|
||||
rx
|
||||
/// Get valid messages received in the past for a topic (might have expired meanwhile).
|
||||
pub fn messages_for(&mut self, topic: B::Hash) -> impl Iterator<Item = TopicNotification> + '_ {
|
||||
self.messages.iter().filter(move |e| e.topic == topic).map(|entry| TopicNotification {
|
||||
message: entry.message.clone(),
|
||||
sender: entry.sender.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Handle an incoming message for topic by who via protocol. Discard message if topic already
|
||||
/// known, the message is old, its source peers isn't a registered peer or the connection to
|
||||
/// them is broken.
|
||||
/// Register incoming messages and return the ones that are new and valid (according to a gossip
|
||||
/// validator) and should thus be forwarded to the upper layers.
|
||||
pub fn on_incoming(
|
||||
&mut self,
|
||||
network: &mut dyn Network<B>,
|
||||
who: PeerId,
|
||||
messages: Vec<Vec<u8>>,
|
||||
) {
|
||||
) -> Vec<(B::Hash, TopicNotification)> {
|
||||
let mut to_forward = vec![];
|
||||
|
||||
if !messages.is_empty() {
|
||||
trace!(target: "gossip", "Received {} messages from peer {}", messages.len(), who);
|
||||
}
|
||||
@@ -335,23 +318,19 @@ impl<B: BlockT> ConsensusGossip<B> {
|
||||
network.report_peer(who.clone(), rep::GOSSIP_SUCCESS);
|
||||
if let Some(ref mut peer) = self.peers.get_mut(&who) {
|
||||
peer.known_messages.insert(message_hash);
|
||||
if let Entry::Occupied(mut entry) = self.live_message_sinks.entry(topic) {
|
||||
trace!(target: "gossip", "Pushing consensus message to sinks for {}.", topic);
|
||||
entry.get_mut().retain(|sink| {
|
||||
if let Err(e) = sink.unbounded_send(TopicNotification {
|
||||
message: message.clone(),
|
||||
sender: Some(who.clone())
|
||||
}) {
|
||||
trace!(target: "gossip", "Error broadcasting message notification: {:?}", e);
|
||||
}
|
||||
!sink.is_closed()
|
||||
});
|
||||
if entry.get().is_empty() {
|
||||
entry.remove_entry();
|
||||
}
|
||||
}
|
||||
|
||||
to_forward.push((topic, TopicNotification {
|
||||
message: message.clone(),
|
||||
sender: Some(who.clone())
|
||||
}));
|
||||
|
||||
if keep {
|
||||
self.register_message_hashed(message_hash, topic, message, Some(who.clone()));
|
||||
self.register_message_hashed(
|
||||
message_hash,
|
||||
topic,
|
||||
message,
|
||||
Some(who.clone()),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
trace!(target:"gossip", "Ignored statement from unregistered peer {}", who);
|
||||
@@ -361,6 +340,8 @@ impl<B: BlockT> ConsensusGossip<B> {
|
||||
trace!(target:"gossip", "Discard message from peer {}", who);
|
||||
}
|
||||
}
|
||||
|
||||
to_forward
|
||||
}
|
||||
|
||||
/// Send all messages with given topic to a peer.
|
||||
@@ -437,7 +418,6 @@ impl<B: BlockT> ConsensusGossip<B> {
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
use sp_runtime::testing::{H256, Block as RawBlock, ExtrinsicWrapper};
|
||||
use futures::executor::block_on_stream;
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -518,16 +498,18 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_stream_include_those_sent_before_asking_for_stream() {
|
||||
fn message_stream_include_those_sent_before_asking() {
|
||||
let mut consensus = ConsensusGossip::<Block>::new(Arc::new(AllowAll), [0, 0, 0, 0]);
|
||||
|
||||
// Register message.
|
||||
let message = vec![4, 5, 6];
|
||||
let topic = HashFor::<Block>::hash(&[1,2,3]);
|
||||
|
||||
consensus.register_message(topic, message.clone());
|
||||
let mut stream = block_on_stream(consensus.messages_for(topic));
|
||||
|
||||
assert_eq!(stream.next(), Some(TopicNotification { message: message, sender: None }));
|
||||
assert_eq!(
|
||||
consensus.messages_for(topic).next(),
|
||||
Some(TopicNotification { message: message, sender: None }),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -544,22 +526,6 @@ mod tests {
|
||||
assert_eq!(consensus.messages.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_keep_multiple_subscribers_per_topic() {
|
||||
let mut consensus = ConsensusGossip::<Block>::new(Arc::new(AllowAll), [0, 0, 0, 0]);
|
||||
|
||||
let message = vec![4, 5, 6];
|
||||
let topic = HashFor::<Block>::hash(&[1, 2, 3]);
|
||||
|
||||
consensus.register_message(topic, message.clone());
|
||||
|
||||
let mut stream1 = block_on_stream(consensus.messages_for(topic));
|
||||
let mut stream2 = block_on_stream(consensus.messages_for(topic));
|
||||
|
||||
assert_eq!(stream1.next(), Some(TopicNotification { message: message.clone(), sender: None }));
|
||||
assert_eq!(stream2.next(), Some(TopicNotification { message, sender: None }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn peer_is_removed_on_disconnect() {
|
||||
struct TestNetwork;
|
||||
|
||||
Reference in New Issue
Block a user