Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 16 additions & 62 deletions client/network-gossip/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use crate::Network;
use crate::state_machine::{ConsensusGossip, Validator, TopicNotification};
use crate::{Network, Validator};
use crate::state_machine::{ConsensusGossip, TopicNotification};

use sc_network::Context;
use sc_network::message::generic::ConsensusMessage;
use sc_network::{Event, ReputationChange};

Expand All @@ -36,37 +35,29 @@ pub struct GossipEngine<B: BlockT> {

struct GossipEngineInner<B: BlockT> {
state_machine: ConsensusGossip<B>,
context: Box<dyn Context<B> + Send>,
context_ext: Box<dyn ContextExt<B> + Send>,
network: Box<dyn Network<B> + Send>,
}

impl<B: BlockT> GossipEngine<B> {
/// Create a new instance.
pub fn new<N: Network<B> + Send + Clone + 'static>(
network: N,
mut network: N,
executor: &impl futures::task::Spawn,
engine_id: ConsensusEngineId,
validator: Arc<dyn Validator<B>>,
) -> Self where B: 'static {
let mut state_machine = ConsensusGossip::new();
let mut context = Box::new(ContextOverService {
network: network.clone(),
});
let context_ext = Box::new(ContextOverService {
network: network.clone(),
});

// We grab the event stream before registering the notifications protocol, otherwise we
// might miss events.
let event_stream = network.event_stream();

network.register_notifications_protocol(engine_id);
state_machine.register_validator(&mut *context, engine_id, validator);
state_machine.register_validator(&mut network, engine_id, validator);

let inner = Arc::new(Mutex::new(GossipEngineInner {
state_machine,
context,
context_ext,
network: Box::new(network),
}));

let gossip_engine = GossipEngine {
Expand All @@ -82,7 +73,7 @@ impl<B: BlockT> GossipEngine<B> {
if let Some(inner) = inner.upgrade() {
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.tick(&mut *inner.context);
inner.state_machine.tick(&mut *inner.network);
} else {
// We reach this branch if the `Arc<GossipEngineInner>` has no reference
// left. We can now let the task end.
Expand All @@ -107,21 +98,21 @@ impl<B: BlockT> GossipEngine<B> {
}
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.new_peer(&mut *inner.context, remote, roles);
inner.state_machine.new_peer(&mut *inner.network, remote, roles);
}
Event::NotificationsStreamClosed { remote, engine_id: msg_engine_id } => {
if msg_engine_id != engine_id {
continue;
}
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.peer_disconnected(&mut *inner.context, remote);
inner.state_machine.peer_disconnected(&mut *inner.network, remote);
},
Event::NotificationsReceived { remote, messages } => {
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.on_incoming(
&mut *inner.context,
&mut *inner.network,
remote,
messages.into_iter()
.filter_map(|(engine, data)| if engine == engine_id {
Expand Down Expand Up @@ -149,7 +140,7 @@ impl<B: BlockT> GossipEngine<B> {
}

pub fn report(&self, who: PeerId, reputation: ReputationChange) {
self.inner.lock().context.report_peer(who, reputation);
self.inner.lock().network.report_peer(who, reputation);
}

/// Registers a message without propagating it to any peers. The message
Expand All @@ -174,7 +165,7 @@ impl<B: BlockT> GossipEngine<B> {
pub fn broadcast_topic(&self, topic: B::Hash, force: bool) {
let mut inner = self.inner.lock();
let inner = &mut *inner;
inner.state_machine.broadcast_topic(&mut *inner.context, topic, force);
inner.state_machine.broadcast_topic(&mut *inner.network, topic, force);
}

/// Get data of valid, incoming messages for a topic (but might have expired meanwhile).
Expand All @@ -193,7 +184,7 @@ impl<B: BlockT> GossipEngine<B> {
) {
let mut inner = self.inner.lock();
let inner = &mut *inner;
inner.state_machine.send_topic(&mut *inner.context, who, topic, self.engine_id, force)
inner.state_machine.send_topic(&mut *inner.network, who, topic, self.engine_id, force)
}

/// Multicast a message to all peers.
Expand All @@ -210,7 +201,7 @@ impl<B: BlockT> GossipEngine<B> {

let mut inner = self.inner.lock();
let inner = &mut *inner;
inner.state_machine.multicast(&mut *inner.context, topic, message, force)
inner.state_machine.multicast(&mut *inner.network, topic, message, force)
}

/// Send addressed message to the given peers. The message is not kept or multicast
Expand All @@ -220,7 +211,7 @@ impl<B: BlockT> GossipEngine<B> {
let inner = &mut *inner;

for who in &who {
inner.state_machine.send_message(&mut *inner.context, who, ConsensusMessage {
inner.state_machine.send_message(&mut *inner.network, who, ConsensusMessage {
engine_id: self.engine_id,
data: data.clone(),
});
Expand All @@ -232,7 +223,7 @@ impl<B: BlockT> GossipEngine<B> {
/// Note: this method isn't strictly related to gossiping and should eventually be moved
/// somewhere else.
pub fn announce(&self, block: B::Hash, associated_data: Vec<u8>) {
self.inner.lock().context_ext.announce(block, associated_data);
self.inner.lock().network.announce(block, associated_data);
}
}

Expand All @@ -244,40 +235,3 @@ impl<B: BlockT> Clone for GossipEngine<B> {
}
}
}

struct ContextOverService<N> {
network: N,
}

impl<B: BlockT, N: Network<B>> Context<B> for ContextOverService<N> {
fn report_peer(&mut self, who: PeerId, reputation: ReputationChange) {
self.network.report_peer(who, reputation);
}

fn disconnect_peer(&mut self, who: PeerId) {
self.network.disconnect_peer(who)
}

fn send_consensus(&mut self, who: PeerId, messages: Vec<ConsensusMessage>) {
for message in messages {
self.network.write_notification(who.clone(), message.engine_id, message.data);
}
}

fn send_chain_specific(&mut self, _: PeerId, _: Vec<u8>) {
log::error!(
target: "sub-libp2p",
"send_chain_specific has been called in a context where it shouldn't"
);
}
}

trait ContextExt<B: BlockT> {
fn announce(&self, block: B::Hash, associated_data: Vec<u8>);
}

impl<B: BlockT, N: Network<B>> ContextExt<B> for ContextOverService<N> {
fn announce(&self, block: B::Hash, associated_data: Vec<u8>) {
Network::announce(&self.network, block, associated_data)
}
}
6 changes: 3 additions & 3 deletions client/network-gossip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@
//! used to inform peers of a current view of protocol state.

pub use self::bridge::GossipEngine;
pub use self::state_machine::{TopicNotification, MessageIntent};
pub use self::state_machine::{Validator, ValidatorContext, ValidationResult};
pub use self::state_machine::DiscardAll;
pub use self::state_machine::TopicNotification;
pub use self::validator::{DiscardAll, MessageIntent, Validator, ValidatorContext, ValidationResult};

use sc_network::{specialization::NetworkSpecialization, Event, ExHashT, NetworkService, PeerId, ReputationChange};
use sp_runtime::{traits::Block as BlockT, ConsensusEngineId};
use std::sync::Arc;

mod bridge;
mod state_machine;
mod validator;

/// Abstraction over a network.
pub trait Network<B: BlockT> {
Expand Down
Loading