-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! DSN config and implementation for the gateway. | ||
|
||
use clap::Parser; | ||
use parking_lot::Mutex; | ||
use std::sync::Arc; | ||
use subspace_networking::libp2p::kad::Mode; | ||
use subspace_networking::libp2p::Multiaddr; | ||
use subspace_networking::{construct, Config, KademliaMode, Node, NodeRunner, WeakNode}; | ||
|
||
/// Configuration for network stack | ||
#[derive(Debug, Parser)] | ||
pub(crate) struct NetworkArgs { | ||
/// Multiaddrs of bootstrap nodes to connect to on startup, multiple are supported | ||
#[arg(long)] | ||
pub(crate) bootstrap_nodes: Vec<Multiaddr>, | ||
|
||
/// Keeps non-global (private, shared, loopback..) addresses in the Kademlia DHT. | ||
#[arg(long, default_value_t = false)] | ||
pub(crate) allow_private_ips: bool, | ||
|
||
/// Multiaddrs of reserved nodes to maintain a connection to, multiple are supported | ||
#[arg(long)] | ||
pub(crate) reserved_peers: Vec<Multiaddr>, | ||
|
||
/// Maximum established outgoing swarm connection limit. | ||
#[arg(long, default_value_t = 100)] | ||
pub(crate) out_connections: u32, | ||
|
||
/// Maximum pending outgoing swarm connection limit. | ||
#[arg(long, default_value_t = 100)] | ||
pub(crate) pending_out_connections: u32, | ||
} | ||
|
||
/// Create a DSN network client with the supplied configuration. | ||
pub(crate) fn configure_network( | ||
NetworkArgs { | ||
bootstrap_nodes, | ||
allow_private_ips, | ||
reserved_peers, | ||
out_connections, | ||
pending_out_connections, | ||
}: NetworkArgs, | ||
) -> anyhow::Result<(Node, NodeRunner<()>)> { | ||
let maybe_weak_node = Arc::new(Mutex::new(None::<WeakNode>)); | ||
|
||
// TODO: | ||
// - store keypair on disk and allow CLI override | ||
// - cache known peers on disk | ||
// - get default dsnBootstrapNodes from chainspec? | ||
// - prometheus telemetry | ||
let default_config = Config::<()>::default(); | ||
let config = Config { | ||
reserved_peers, | ||
allow_non_global_addresses_in_dht: allow_private_ips, | ||
max_established_outgoing_connections: out_connections, | ||
max_pending_outgoing_connections: pending_out_connections, | ||
bootstrap_addresses: bootstrap_nodes, | ||
kademlia_mode: KademliaMode::Static(Mode::Client), | ||
..default_config | ||
}; | ||
|
||
let (node, node_runner) = construct(config)?; | ||
maybe_weak_node.lock().replace(node.downgrade()); | ||
|
||
Ok((node, node_runner)) | ||
} |