Skip to content

Commit

Permalink
Extract transactions handling from protocol.rs (paritytech#8110)
Browse files Browse the repository at this point in the history
* Extract transactions handling from protocol.rs

* Oops, boolean

* Do this better

* Update client/network/src/transactions.rs

Co-authored-by: Nikolay Volf <[email protected]>

* [WIP] Fix handshake

* Finish handshake change

* Bugfix

Co-authored-by: Nikolay Volf <[email protected]>
  • Loading branch information
tomaka and NikVolf authored Feb 18, 2021
1 parent ca63242 commit 2c99434
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 344 deletions.
34 changes: 17 additions & 17 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
discovery::{DiscoveryBehaviour, DiscoveryConfig, DiscoveryOut},
protocol::{message::Roles, CustomMessageOutcome, NotificationsSink, Protocol},
peer_info, request_responses, light_client_requests,
ObservedRole, DhtEvent, ExHashT,
ObservedRole, DhtEvent,
};

use bytes::Bytes;
Expand Down Expand Up @@ -54,9 +54,9 @@ pub use crate::request_responses::{
/// General behaviour of the network. Combines all protocols together.
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourOut<B>", poll_method = "poll")]
pub struct Behaviour<B: BlockT, H: ExHashT> {
pub struct Behaviour<B: BlockT> {
/// All the substrate-specific protocols.
substrate: Protocol<B, H>,
substrate: Protocol<B>,
/// Periodically pings and identifies the nodes we are connected to, and store information in a
/// cache.
peer_info: peer_info::PeerInfoBehaviour,
Expand Down Expand Up @@ -172,10 +172,10 @@ pub enum BehaviourOut<B: BlockT> {
Dht(DhtEvent, Duration),
}

impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
impl<B: BlockT> Behaviour<B> {
/// Builds a new `Behaviour`.
pub fn new(
substrate: Protocol<B, H>,
substrate: Protocol<B>,
user_agent: String,
local_public_key: PublicKey,
light_client_request_sender: light_client_requests::sender::LightClientRequestSender<B>,
Expand Down Expand Up @@ -256,12 +256,12 @@ impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
}

/// Returns a shared reference to the user protocol.
pub fn user_protocol(&self) -> &Protocol<B, H> {
pub fn user_protocol(&self) -> &Protocol<B> {
&self.substrate
}

/// Returns a mutable reference to the user protocol.
pub fn user_protocol_mut(&mut self) -> &mut Protocol<B, H> {
pub fn user_protocol_mut(&mut self) -> &mut Protocol<B> {
&mut self.substrate
}

Expand Down Expand Up @@ -294,15 +294,15 @@ fn reported_roles_to_observed_role(roles: Roles) -> ObservedRole {
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<void::Void> for
Behaviour<B, H> {
impl<B: BlockT> NetworkBehaviourEventProcess<void::Void> for
Behaviour<B> {
fn inject_event(&mut self, event: void::Void) {
void::unreachable(event)
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for
Behaviour<B, H> {
impl<B: BlockT> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for
Behaviour<B> {
fn inject_event(&mut self, event: CustomMessageOutcome<B>) {
match event {
CustomMessageOutcome::BlockImport(origin, blocks) =>
Expand Down Expand Up @@ -362,7 +362,7 @@ Behaviour<B, H> {
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<request_responses::Event> for Behaviour<B, H> {
impl<B: BlockT> NetworkBehaviourEventProcess<request_responses::Event> for Behaviour<B> {
fn inject_event(&mut self, event: request_responses::Event) {
match event {
request_responses::Event::InboundRequest { peer, protocol, result } => {
Expand All @@ -386,8 +386,8 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<request_responses::Even
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<peer_info::PeerInfoEvent>
for Behaviour<B, H> {
impl<B: BlockT> NetworkBehaviourEventProcess<peer_info::PeerInfoEvent>
for Behaviour<B> {
fn inject_event(&mut self, event: peer_info::PeerInfoEvent) {
let peer_info::PeerInfoEvent::Identified {
peer_id,
Expand Down Expand Up @@ -416,8 +416,8 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<peer_info::PeerInfoEven
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<DiscoveryOut>
for Behaviour<B, H> {
impl<B: BlockT> NetworkBehaviourEventProcess<DiscoveryOut>
for Behaviour<B> {
fn inject_event(&mut self, out: DiscoveryOut) {
match out {
DiscoveryOut::UnroutablePeer(_peer_id) => {
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<DiscoveryOut>
}
}

impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
impl<B: BlockT> Behaviour<B> {
fn poll<TEv>(
&mut self,
cx: &mut Context,
Expand Down
3 changes: 3 additions & 0 deletions client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub struct Params<B: BlockT, H: ExHashT> {
/// default.
pub executor: Option<Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>>,

/// How to spawn the background task dedicated to the transactions handler.
pub transactions_handler_executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>,

/// Network layer configuration.
pub network_config: NetworkConfiguration,

Expand Down
1 change: 1 addition & 0 deletions client/network/src/gossip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn build_test_full_node(network_config: config::NetworkConfiguration)
let worker = NetworkWorker::new(config::Params {
role: config::Role::Full,
executor: None,
transactions_handler_executor: Box::new(|task| { async_std::task::spawn(task); }),
network_config,
chain: client.clone(),
on_demand: None,
Expand Down
1 change: 1 addition & 0 deletions client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub mod config;
pub mod error;
pub mod gossip;
pub mod network_state;
pub mod transactions;

#[doc(inline)]
pub use libp2p::{multiaddr, Multiaddr, PeerId};
Expand Down
Loading

0 comments on commit 2c99434

Please sign in to comment.