Просмотр исходного кода

script/research/seedd: implementation of multiple P2P seeds in single instance.

aggstam 4 лет назад
Родитель
Сommit
d6a9ef2ff3

+ 2 - 0
script/research/seedd/.gitignore

@@ -0,0 +1,2 @@
+/target
+Cargo.lock

+ 27 - 0
script/research/seedd/Cargo.toml

@@ -0,0 +1,27 @@
+[package]
+name = "seedd"
+version = "0.3.0"
+edition = "2021"
+
+[dependencies.darkfi]
+path = "../../../"
+features = ["net"]
+
+[dependencies]
+async-channel = "1.6.1"
+async-executor = "1.4.1"
+async-std = "1.12.0"
+ctrlc-async = {version = "3.2.2", default-features = false, features = ["async-std", "termination"]}
+easy-parallel = "3.2.0"
+futures-lite = "1.12.0"
+log = "0.4.17"
+simplelog = "0.12.0"
+url = "2.2.2"
+
+# Argument parsing
+serde = "1.0.138"
+serde_derive = "1.0.138"
+structopt = "0.3.26"
+structopt-toml = "0.5.0"
+
+[workspace]

+ 47 - 0
script/research/seedd/README.md

@@ -0,0 +1,47 @@
+seedd
+==========
+
+A tool to deploy multiple P2P network seed nodes for darkfi applications, in a single daemon.
+
+## Usage
+
+```
+seedd 0.3.0
+Defines the network specific settings
+
+USAGE:
+    seedd [FLAGS] [OPTIONS]
+
+FLAGS:
+        --darkfid    Darkfid activation flag
+    -h, --help       Prints help information
+        --ircd       Ircd activation flag
+        --taud       Taud activation flag
+    -V, --version    Prints version information
+    -v               Increase verbosity (-vvv supported)
+
+OPTIONS:
+    -c, --config <config>    Configuration file to use
+        --url <url>          Daemon published url, common for all enabled networks [default: tcp://127.0.0.1]
+```
+
+On first execution, daemon will create default config file ~/.config/darkfi/seedd_config.toml.
+Configuration must be verified, and applications should be configured accordingly.
+
+Run seedd as follows:
+
+```
+% cargo run -- --darkfid --taud --ircd
+17:00:19 [INFO] Starting seed network node for darkfid at: tcp://127.0.0.1:7650
+17:00:19 [WARN] Skipping seed sync process since no seeds are configured.
+17:00:19 [INFO] Starting seed network node for ircd at: tcp://127.0.0.1:8760
+17:00:19 [INFO] Starting inbound session on tcp://127.0.0.1:7650
+17:00:19 [WARN] Skipping seed sync process since no seeds are configured.
+17:00:19 [INFO] Starting seed network node for taud at: tcp://127.0.0.1:9870
+17:00:19 [INFO] Starting inbound session on tcp://127.0.0.1:8760
+17:00:19 [WARN] Skipping seed sync process since no seeds are configured.
+17:00:19 [INFO] Starting inbound session on tcp://127.0.0.1:9870
+17:00:19 [INFO] Starting 0 outbound connection slots.
+17:00:19 [INFO] Starting 0 outbound connection slots.
+17:00:19 [INFO] Starting 0 outbound connection slots
+```

+ 55 - 0
script/research/seedd/seedd_config.toml

@@ -0,0 +1,55 @@
+## seedd configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+##
+## The default values are left commented. They can be overridden either by
+## uncommenting, or by using the command-line.
+
+# Daemon published url, common for all enabled networks
+url = "tcp://127.0.0.1"
+
+# Darkfid activation flag
+#darkfid = true
+
+# Ircd activation flag
+#ircd = true
+
+# Taud activation flag
+#taud = true
+
+# Following specific network settings shouldn't be commended even if 
+# their corresponding network is not enabled.
+
+# Darkfid network settings.
+[darkfid_opts]
+# Network port
+port = 7650
+
+# Seed nodes to connect to
+seeds = []
+
+# Peers to connect to
+peers = []
+
+# Ircd network settings.
+[ircd_opts]
+# Network port
+port = 8760
+
+# Seed nodes to connect to
+seeds = []
+
+# Peers to connect to
+peers = []
+
+# Taud network settings.
+[taud_opts]
+# Network port
+port = 9870
+
+# Seed nodes to connect to
+seeds = []
+
+# Peers to connect to
+peers = []

+ 64 - 0
script/research/seedd/src/config.rs

@@ -0,0 +1,64 @@
+use serde_derive::Deserialize;
+use structopt::StructOpt;
+use structopt_toml::StructOptToml;
+use url::Url;
+
+use darkfi::cli_desc;
+
+#[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
+#[serde(default)]
+#[structopt(name = "seedd", about = cli_desc!())]
+pub struct Args {
+    #[structopt(short, long)]
+    /// Configuration file to use
+    pub config: Option<String>,
+
+    #[structopt(long, default_value = "tcp://127.0.0.1")]
+    /// Daemon published url, common for all enabled networks
+    pub url: Url,
+
+    #[structopt(long)]
+    /// Darkfid activation flag
+    pub darkfid: bool,
+
+    #[structopt(flatten)]
+    /// Darkfid network specific settings
+    pub darkfid_opts: NetOpt,
+
+    #[structopt(long)]
+    /// Ircd activation flag
+    pub ircd: bool,
+
+    #[structopt(flatten)]
+    /// Ircd network specific settings
+    pub ircd_opts: NetOpt,
+
+    #[structopt(long)]
+    /// Taud activation flag
+    pub taud: bool,
+
+    #[structopt(flatten)]
+    /// Taud network specific settings
+    pub taud_opts: NetOpt,
+
+    #[structopt(short, parse(from_occurrences))]
+    /// Increase verbosity (-vvv supported)
+    pub verbose: u8,
+}
+
+/// Defines the network specific settings
+#[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
+#[structopt()]
+pub struct NetOpt {
+    #[structopt(skip)]
+    /// Specific port the network will use
+    pub port: u16,
+
+    #[structopt(skip)]
+    /// Connect to seeds (repeatable flag)
+    pub seeds: Vec<Url>,
+
+    #[structopt(skip)]
+    /// Connect to peers (repeatable flag)
+    pub peers: Vec<Url>,
+}

+ 103 - 0
script/research/seedd/src/main.rs

@@ -0,0 +1,103 @@
+use async_executor::Executor;
+use async_std::sync::Arc;
+use futures_lite::future;
+use log::{error, info};
+use structopt_toml::StructOptToml;
+use url::Url;
+
+use darkfi::{
+    async_daemonize, net,
+    util::{
+        cli::{get_log_config, get_log_level, spawn_config},
+        path::get_config_path,
+    },
+    Result,
+};
+
+mod config;
+use config::{Args, NetOpt};
+
+// TODO: disable unregistered protocols message subscription warning
+
+const CONFIG_FILE: &str = "seedd_config.toml";
+const CONFIG_FILE_CONTENTS: &str = include_str!("../seedd_config.toml");
+
+async fn spawn_network(
+    name: &str,
+    mut url: Url,
+    opts: NetOpt,
+    ex: Arc<Executor<'_>>,
+) -> Result<()> {
+    url.set_port(Some(opts.port))?;
+    let network_settings = net::Settings {
+        inbound: Some(url.clone()),
+        external_addr: Some(url.clone()),
+        seeds: opts.seeds,
+        peers: opts.peers,
+        outbound_connections: 0,
+        ..Default::default()
+    };
+
+    let p2p = net::P2p::new(network_settings).await;
+
+    info!("Starting seed network node for {} at: {}", name, url);
+    p2p.clone().start(ex.clone()).await?;
+    let _ex = ex.clone();
+    ex.spawn(async move {
+        if let Err(e) = p2p.run(_ex).await {
+            error!("Failed starting P2P network seed: {}", e);
+        }
+    })
+    .detach();
+
+    Ok(())
+}
+
+async_daemonize!(realmain);
+async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
+    // We use this handler to block this function after detaching all
+    // tasks, and to catch a shutdown signal, where we can clean up and
+    // exit gracefully.
+    let (signal, shutdown) = async_channel::bounded::<()>(1);
+    ctrlc_async::set_async_handler(async move {
+        signal.send(()).await.unwrap();
+    })
+    .unwrap();
+
+    // Verify any daemon network is enabled
+    let check = args.darkfid || args.ircd || args.taud;
+    if !check {
+        info!("No daemon network is enabled!");
+        return Ok(())
+    }
+
+    // Spawn darkfid network, if configured
+    if args.darkfid {
+        if let Err(e) =
+            spawn_network("darkfid", args.url.clone(), args.darkfid_opts, ex.clone()).await
+        {
+            error!("Failed starting darkfid P2P network seed: {}", e);
+        }
+    }
+
+    // Spawn ircd network, if configured
+    if args.ircd {
+        if let Err(e) = spawn_network("ircd", args.url.clone(), args.ircd_opts, ex.clone()).await {
+            error!("Failed starting ircd P2P network seed: {}", e);
+        }
+    }
+
+    // Spawn taud network, if configured
+    if args.taud {
+        if let Err(e) = spawn_network("taud", args.url, args.taud_opts, ex).await {
+            error!("Failed starting taud P2P network seed: {}", e);
+        }
+    }
+
+    // Wait for SIGINT
+    shutdown.recv().await?;
+    print!("\r");
+    info!("Caught termination signal, cleaning up and exiting...");
+
+    Ok(())
+}