Skip to content

Commit

Permalink
Add basic DSN client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Oct 17, 2024
1 parent 5f711a4 commit c1dc6b4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/subspace-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ clap = { version = "4.5.18", features = ["derive"] }
fdlimit = "0.3.0"
futures = "0.3.30"
mimalloc = "0.1.43"
parking_lot = "0.12.2"
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
supports-color = "3.0.1"
thiserror = "1.0.64"
tokio = { version = "1.40.0", features = ["macros"] }
Expand Down
24 changes: 21 additions & 3 deletions crates/subspace-gateway/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Gateway run command.
//! This is the primary command for the gateway.

mod dsn;

use crate::commands::run::dsn::NetworkArgs;
use crate::commands::shutdown_signal;
use clap::Parser;
use futures::{select, FutureExt};
Expand All @@ -19,8 +22,14 @@ pub struct RunOptions {
#[derive(Debug, Parser)]
pub(super) struct GatewayOptions {
/// Enable development mode.
#[arg(long)]
///
/// Implies following flags (unless customized):
/// * `--allow-private-ips`
#[arg(long, verbatim_doc_comment)]
dev: bool,

#[clap(flatten)]
dsn: NetworkArgs,
}

/// Default run command for gateway
Expand All @@ -29,14 +38,23 @@ pub async fn run(run_options: RunOptions) -> anyhow::Result<()> {
let signal = shutdown_signal();

let RunOptions {
gateway: GatewayOptions { dev: _ },
gateway: GatewayOptions { dev, mut dsn },
} = run_options;

// Development mode handling is limited to this section
{
if dev {
dsn.allow_private_ips = true;
}
}

info!("Subspace Gateway");
info!("✌️ version {}", env!("CARGO_PKG_VERSION"));
info!("❤️ by {}", env!("CARGO_PKG_AUTHORS"));

let dsn_fut = future::pending::<()>();
let (_dsn_node, mut dsn_node_runner) = dsn::configure_network(dsn)?;
let dsn_fut = dsn_node_runner.run();

let rpc_fut = future::pending::<()>();

// This defines order in which things are dropped
Expand Down
66 changes: 66 additions & 0 deletions crates/subspace-gateway/src/commands/run/dsn.rs
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))
}

0 comments on commit c1dc6b4

Please sign in to comment.