Skip to content

Commit

Permalink
Refactor subspace-networking module structure. (#1867)
Browse files Browse the repository at this point in the history
Refactor the module structure of the `subspace-networking` crate: gather custom protocols and request handlers in the “protocols” module.
  • Loading branch information
shamil-gadelshin authored and vedhavyas committed Aug 25, 2023
1 parent 94c5e64 commit b2f7cf7
Show file tree
Hide file tree
Showing 32 changed files with 87 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use subspace_networking::libp2p::multiaddr::Protocol;
use subspace_networking::utils::multihash::ToMultihash;
use subspace_networking::utils::strip_peer_id;
use subspace_networking::{
create, Config, NetworkingParametersManager, Node, NodeRunner, PeerInfo, PeerInfoProvider,
construct, Config, NetworkingParametersManager, Node, NodeRunner, PeerInfo, PeerInfoProvider,
PieceByIndexRequest, PieceByIndexRequestHandler, PieceByIndexResponse,
SegmentHeaderBySegmentIndexesRequestHandler, SegmentHeaderRequest, SegmentHeaderResponse,
};
Expand Down Expand Up @@ -186,7 +186,7 @@ pub(super) fn configure_dsn(
..default_config
};

create(config)
construct(config)
.map(|(node, node_runner)| {
node.on_new_listener(Arc::new({
let node = node.clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-networking/examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub async fn configure_dsn(
enable_autonat: false,
..default_config
};
let (node, mut node_runner_1) = subspace_networking::create(config).unwrap();
let (node, mut node_runner_1) = subspace_networking::construct(config).unwrap();

let (node_address_sender, node_address_receiver) = oneshot::channel();
let on_new_listener_handler = node.on_new_listener(Arc::new({
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-networking/examples/get-peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() {
allow_non_global_addresses_in_dht: true,
..Config::default()
};
let (node_1, mut node_runner_1) = subspace_networking::create(config_1).unwrap();
let (node_1, mut node_runner_1) = subspace_networking::construct(config_1).unwrap();

println!("Node 1 ID is {}", node_1.id());

Expand Down Expand Up @@ -48,7 +48,7 @@ async fn main() {
..Config::default()
};

let (node_2, mut node_runner_2) = subspace_networking::create(config_2).unwrap();
let (node_2, mut node_runner_2) = subspace_networking::construct(config_2).unwrap();

println!("Node 2 ID is {}", node_2.id());

Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-networking/examples/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn main() {
metrics: Some(metrics),
..Config::default()
};
let (node_1, mut node_runner_1) = subspace_networking::create(config_1).unwrap();
let (node_1, mut node_runner_1) = subspace_networking::construct(config_1).unwrap();

// Init prometheus
let prometheus_metrics_server_address = "127.0.0.1:63000".parse().unwrap();
Expand Down Expand Up @@ -76,7 +76,7 @@ async fn main() {
..Config::default()
};

let (node_2, mut node_runner_2) = subspace_networking::create(config_2).unwrap();
let (node_2, mut node_runner_2) = subspace_networking::construct(config_2).unwrap();

println!("Node 2 ID is {}", node_2.id());

Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-networking/examples/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() {
allow_non_global_addresses_in_dht: true,
..Config::default()
};
let (node_1, mut node_runner_1) = subspace_networking::create(config_1).unwrap();
let (node_1, mut node_runner_1) = subspace_networking::construct(config_1).unwrap();

println!("Node 1 ID is {}", node_1.id());

Expand Down Expand Up @@ -55,7 +55,7 @@ async fn main() {
..Config::default()
};

let (node_2, mut node_runner_2) = subspace_networking::create(config_2).unwrap();
let (node_2, mut node_runner_2) = subspace_networking::construct(config_2).unwrap();

println!("Node 2 ID is {}", node_2.id());

Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-networking/examples/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() {
)],
..Config::default()
};
let (node_1, mut node_runner_1) = subspace_networking::create(config_1).unwrap();
let (node_1, mut node_runner_1) = subspace_networking::construct(config_1).unwrap();

println!("Node 1 ID is {}", node_1.id());

Expand Down Expand Up @@ -72,7 +72,7 @@ async fn main() {
..Config::default()
};

let (node_2, mut node_runner_2) = subspace_networking::create(config_2).unwrap();
let (node_2, mut node_runner_2) = subspace_networking::construct(config_2).unwrap();

println!("Node 2 ID is {}", node_2.id());

Expand Down
20 changes: 11 additions & 9 deletions crates/subspace-networking/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ pub(crate) mod persistent_parameters;
#[cfg(test)]
mod tests;

use crate::connected_peers::{
use crate::protocols::connected_peers::{
Behaviour as ConnectedPeersBehaviour, Config as ConnectedPeersConfig,
Event as ConnectedPeersEvent,
};
use crate::peer_info::{
use crate::protocols::peer_info::{
Behaviour as PeerInfoBehaviour, Config as PeerInfoConfig, Event as PeerInfoEvent,
};
use crate::request_responses::{
Event as RequestResponseEvent, RequestHandler, RequestResponsesBehaviour,
use crate::protocols::request_response::request_response_factory::{
Event as RequestResponseEvent, RequestHandler, RequestResponseFactoryBehaviour,
};
use crate::reserved_peers::{
use crate::protocols::reserved_peers::{
Behaviour as ReservedPeersBehaviour, Config as ReservedPeersConfig, Event as ReservedPeersEvent,
};
use crate::PeerInfoProvider;
Expand Down Expand Up @@ -75,7 +75,7 @@ pub(crate) struct Behavior<RecordStore> {
pub(crate) kademlia: Kademlia<RecordStore>,
pub(crate) gossipsub: Toggle<Gossipsub>,
pub(crate) ping: Ping,
pub(crate) request_response: RequestResponsesBehaviour,
pub(crate) request_response: RequestResponseFactoryBehaviour,
pub(crate) connection_limits: ConnectionLimitsBehaviour,
pub(crate) block_list: BlockListBehaviour,
pub(crate) reserved_peers: ReservedPeersBehaviour,
Expand Down Expand Up @@ -119,9 +119,11 @@ where
kademlia,
gossipsub,
ping: Ping::default(),
request_response: RequestResponsesBehaviour::new(config.request_response_protocols)
//TODO: Convert to an error.
.expect("RequestResponse protocols registration failed."),
request_response: RequestResponseFactoryBehaviour::new(
config.request_response_protocols,
)
//TODO: Convert to an error.
.expect("RequestResponse protocols registration failed."),
connection_limits: ConnectionLimitsBehaviour::new(config.connection_limits),
block_list: BlockListBehaviour::default(),
reserved_peers: ReservedPeersBehaviour::new(config.reserved_peers),
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-networking/src/behavior/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async fn test_async_handler_works_with_pending_internal_future() {
)],
..Config::default()
};
let (node_1, mut node_runner_1) = crate::create(config_1).unwrap();
let (node_1, mut node_runner_1) = crate::construct(config_1).unwrap();

let (node_1_address_sender, node_1_address_receiver) = oneshot::channel();
let on_new_listener_handler = node_1.on_new_listener(Arc::new({
Expand Down Expand Up @@ -225,7 +225,7 @@ async fn test_async_handler_works_with_pending_internal_future() {
..Config::default()
};

let (node_2, mut node_runner_2) = crate::create(config_2).unwrap();
let (node_2, mut node_runner_2) = crate::construct(config_2).unwrap();

let bootstrap_fut = Box::pin({
let node = node_2.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
..Config::new(protocol_version.to_string(), keypair, (), None)
};
let (node, mut node_runner) =
subspace_networking::create(config).expect("Networking stack creation failed.");
subspace_networking::construct(config).expect("Networking stack creation failed.");

node.on_new_listener(Arc::new({
let node_id = node.id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::behavior::persistent_parameters::{
NetworkingParametersRegistry, StubNetworkingParametersManager,
};
use crate::behavior::{Behavior, BehaviorConfig};
use crate::connected_peers::Config as ConnectedPeersConfig;
use crate::create::temporary_bans::TemporaryBans;
use crate::create::transport::build_transport;
use crate::constructor::temporary_bans::TemporaryBans;
use crate::constructor::transport::build_transport;
use crate::node::Node;
use crate::node_runner::{NodeRunner, NodeRunnerConfig};
use crate::peer_info::PeerInfoProvider;
use crate::request_responses::RequestHandler;
use crate::reserved_peers::Config as ReservedPeersConfig;
use crate::protocols::connected_peers::Config as ConnectedPeersConfig;
use crate::protocols::peer_info::PeerInfoProvider;
use crate::protocols::request_response::request_response_factory::RequestHandler;
use crate::protocols::reserved_peers::Config as ReservedPeersConfig;
use crate::shared::Shared;
use crate::utils::{strip_peer_id, ResizableSemaphore};
use crate::{PeerInfo, PeerInfoConfig};
Expand Down Expand Up @@ -390,7 +390,7 @@ pub fn peer_id(keypair: &identity::Keypair) -> PeerId {
}

/// Create a new network node and node runner instances.
pub fn create<LocalRecordProvider>(
pub fn construct<LocalRecordProvider>(
config: Config<LocalRecordProvider>,
) -> Result<(Node, NodeRunner<LocalRecordProvider>), CreationError>
where
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::create::temporary_bans::TemporaryBans;
use crate::constructor::temporary_bans::TemporaryBans;
use crate::CreationError;
use futures::future::Either;
use libp2p::core::multiaddr::{Multiaddr, Protocol};
Expand Down
21 changes: 10 additions & 11 deletions crates/subspace-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
#![warn(missing_docs)]

mod behavior;
mod connected_peers;
mod create;
mod constructor;
mod node;
mod node_runner;
mod peer_info;
mod request_handlers;
mod request_responses;
mod reserved_peers;
mod protocols;

mod shared;
pub mod utils;

Expand All @@ -37,16 +34,18 @@ pub use crate::node::{
GetClosestPeersError, Node, SendRequestError, SubscribeError, TopicSubscription,
};
pub use crate::node_runner::NodeRunner;
pub use crate::peer_info::{
pub use crate::protocols::peer_info::{
Config as PeerInfoConfig, Notification, NotificationHandler, PeerInfo, PeerInfoProvider,
};
pub use create::{create, peer_id, Config, CreationError, LocalRecordProvider};
pub use constructor::{construct, peer_id, Config, CreationError, LocalRecordProvider};
pub use libp2p;
pub use request_handlers::generic_request_handler::{GenericRequest, GenericRequestHandler};
pub use request_handlers::piece_by_index::{
pub use protocols::request_response::handlers::generic_request_handler::{
GenericRequest, GenericRequestHandler,
};
pub use protocols::request_response::handlers::piece_by_index::{
PieceByIndexRequest, PieceByIndexRequestHandler, PieceByIndexResponse,
};
pub use request_handlers::segment_header::{
pub use protocols::request_response::handlers::segment_header::{
SegmentHeaderBySegmentIndexesRequestHandler, SegmentHeaderRequest, SegmentHeaderResponse,
};
pub use shared::NewPeerInfo;
Expand Down
9 changes: 5 additions & 4 deletions crates/subspace-networking/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::request_handlers::generic_request_handler::GenericRequest;
use crate::protocols::request_response::handlers::generic_request_handler::GenericRequest;
use crate::protocols::request_response::request_response_factory;
pub use crate::shared::NewPeerInfo;
use crate::shared::{Command, CreatedSubscription, Shared};
use crate::utils::multihash::Multihash;
use crate::utils::{HandlerFn, ResizableSemaphorePermit};
use crate::{request_responses, NewPeerInfo};
use bytes::Bytes;
use event_listener_primitives::HandlerId;
use futures::channel::mpsc::SendError;
Expand Down Expand Up @@ -189,7 +190,7 @@ pub enum SendRequestError {
NodeRunnerDropped,
/// Underlying protocol returned an error, impossible to get response.
#[error("Underlying protocol returned an error: {0}")]
ProtocolFailure(#[from] request_responses::RequestFailure),
ProtocolFailure(#[from] request_response_factory::RequestFailure),
/// Underlying protocol returned an incorrect format, impossible to get response.
#[error("Received incorrectly formatted response: {0}")]
IncorrectResponseFormat(#[from] parity_scale_codec::Error),
Expand Down Expand Up @@ -513,7 +514,7 @@ impl Node {
Ok(())
}

/// Callback is called when we receive new [`crate::peer_info::PeerInfo`]
/// Callback is called when we receive new [`crate::protocols::peer_info::PeerInfo`]
pub fn on_peer_info(&self, callback: HandlerFn<NewPeerInfo>) -> HandlerId {
self.shared.handlers.new_peer_info.add(callback)
}
Expand Down
20 changes: 11 additions & 9 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ use crate::behavior::persistent_parameters::{
use crate::behavior::{
Behavior, Event, GeneralConnectedPeersInstance, SpecialConnectedPeersInstance,
};
use crate::connected_peers::Event as ConnectedPeersEvent;
use crate::create;
use crate::create::temporary_bans::TemporaryBans;
use crate::create::{
use crate::constructor;
use crate::constructor::temporary_bans::TemporaryBans;
use crate::constructor::{
ConnectedPeersHandler, LocalOnlyRecordStore, KADEMLIA_CONCURRENT_TASKS_BOOST_PER_PEER,
REGULAR_CONCURRENT_TASKS_BOOST_PER_PEER,
};
use crate::peer_info::{Event as PeerInfoEvent, PeerInfoSuccess};
use crate::request_responses::{Event as RequestResponseEvent, IfDisconnected};
use crate::protocols::connected_peers::Event as ConnectedPeersEvent;
use crate::protocols::peer_info::{Event as PeerInfoEvent, PeerInfoSuccess};
use crate::protocols::request_response::request_response_factory::{
Event as RequestResponseEvent, IfDisconnected,
};
use crate::shared::{Command, CreatedSubscription, NewPeerInfo, Shared};
use crate::utils::{
is_global_address_or_dns, strip_peer_id, PeerAddress, ResizableSemaphorePermit,
Expand Down Expand Up @@ -98,7 +100,7 @@ enum BootstrapCommandState {
#[must_use = "Node does not function properly unless its runner is driven forward"]
pub struct NodeRunner<LocalRecordProvider>
where
LocalRecordProvider: create::LocalRecordProvider + Send + Sync + 'static,
LocalRecordProvider: constructor::LocalRecordProvider + Send + Sync + 'static,
{
/// Should non-global addresses be added to the DHT?
allow_non_global_addresses_in_dht: bool,
Expand Down Expand Up @@ -154,7 +156,7 @@ where
// Helper struct for NodeRunner configuration (clippy requirement).
pub(crate) struct NodeRunnerConfig<LocalRecordProvider>
where
LocalRecordProvider: create::LocalRecordProvider + Send + Sync + 'static,
LocalRecordProvider: constructor::LocalRecordProvider + Send + Sync + 'static,
{
pub(crate) allow_non_global_addresses_in_dht: bool,
pub(crate) command_receiver: mpsc::Receiver<Command>,
Expand All @@ -175,7 +177,7 @@ where

impl<LocalRecordProvider> NodeRunner<LocalRecordProvider>
where
LocalRecordProvider: create::LocalRecordProvider + Send + Sync + 'static,
LocalRecordProvider: constructor::LocalRecordProvider + Send + Sync + 'static,
{
pub(crate) fn new(
NodeRunnerConfig {
Expand Down
4 changes: 4 additions & 0 deletions crates/subspace-networking/src/protocols.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub(crate) mod connected_peers;
pub mod peer_info;
pub mod request_response;
pub(crate) mod reserved_peers;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod handler;
mod protocol;

use crate::peer_info::handler::HandlerInEvent;
use crate::protocols::peer_info::handler::HandlerInEvent;
use handler::Handler;
pub use handler::{Config, PeerInfoError, PeerInfoSuccess};
use libp2p::core::{Endpoint, Multiaddr};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::peer_info::{protocol, PeerInfo};
use crate::protocols::peer_info::{protocol, PeerInfo};
use futures::future::BoxFuture;
use futures::prelude::*;
use libp2p::core::upgrade::ReadyUpgrade;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module defines low-level functions for working with inbound and outbound streams.

use crate::peer_info::PeerInfo;
use crate::protocols::peer_info::PeerInfo;
use futures::prelude::*;
use parity_scale_codec::{Decode, Encode};
use std::io;
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-networking/src/protocols/request_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) mod handlers;
pub(crate) mod request_response_factory;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig, RequestHandler};
use crate::protocols::request_response::request_response_factory::{
IncomingRequest, OutgoingResponse, ProtocolConfig, RequestHandler,
};
use async_trait::async_trait;
use futures::channel::mpsc;
use futures::prelude::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Handle (i.e. answer) incoming pieces requests from a remote peer received via
//! `RequestResponsesBehaviour` with generic [`GenericRequestHandler`].

use crate::request_handlers::generic_request_handler::{GenericRequest, GenericRequestHandler};
use super::generic_request_handler::{GenericRequest, GenericRequestHandler};
use parity_scale_codec::{Decode, Encode};
use subspace_core_primitives::{Piece, PieceIndex};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Handle (i.e. answer) incoming segment headers requests from a remote peer received via
//! `RequestResponsesBehaviour` with generic [`GenericRequestHandler`].

use crate::request_handlers::generic_request_handler::{GenericRequest, GenericRequestHandler};
use super::generic_request_handler::{GenericRequest, GenericRequestHandler};
use parity_scale_codec::{Decode, Encode};
use subspace_core_primitives::{SegmentHeader, SegmentIndex};

Expand Down
Loading

0 comments on commit b2f7cf7

Please sign in to comment.