From 0b80d0d8d63d4110e75bce4d9ab2e15adfb79e7a Mon Sep 17 00:00:00 2001 From: ghassmo Date: Sun, 24 Apr 2022 21:10:13 +0300 Subject: [PATCH] bin/tau: add the option to run tau locally & add net::Setting to Args struct --- bin/tau/taud/src/main.rs | 51 ++++++++++++++++++------------------ bin/tau/taud/src/settings.rs | 23 ++++++++-------- bin/tau/taud_config.toml | 18 +++++++++++++ 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/bin/tau/taud/src/main.rs b/bin/tau/taud/src/main.rs index 9476cdd9e..8a247c5f1 100644 --- a/bin/tau/taud/src/main.rs +++ b/bin/tau/taud/src/main.rs @@ -11,7 +11,6 @@ use structopt_toml::StructOptToml; use darkfi::{ async_daemonize, - net::Settings as P2pSettings, raft::Raft, rpc::rpcserver::{listen_and_serve, RpcServerConfig}, util::{ @@ -32,33 +31,14 @@ use crate::{ error::TaudResult, jsonrpc::JsonRpcInterface, month_tasks::MonthTasks, - settings::{Args, CONFIG_FILE, CONFIG_FILE_CONTENTS}, + settings::{Args, Command, CONFIG_FILE, CONFIG_FILE_CONTENTS}, task_info::TaskInfo, }; async_daemonize!(realmain); async fn realmain(settings: Args, executor: Arc>) -> Result<()> { - let p2p_settings = P2pSettings { - inbound: settings.accept, - outbound_connections: settings.slots, - external_addr: settings.accept, - peers: settings.connect.clone(), - seeds: settings.seeds.clone(), - ..Default::default() - }; - let datastore_path = PathBuf::from(&settings.datastore); - // - //Raft - // - let datastore_raft = datastore_path.join("tau.db"); - let mut raft = Raft::::new(settings.accept, datastore_raft)?; - - let raft_sender = raft.get_broadcast(); - let commits = raft.get_commits(); - let initial_sync_raft_sender = raft_sender.clone(); - // // RPC // @@ -74,6 +54,29 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { let rpc_interface = Arc::new(JsonRpcInterface::new(rpc_snd, datastore_path.clone())); + let executor_cloned = executor.clone(); + let rpc_listener_taks = + executor_cloned.spawn(listen_and_serve(server_config, rpc_interface, executor.clone())); + + let net_settings = match settings.command { + Some(Command::Net(s)) => s, + None => { + warn!("run without connecting to raft and p2p network"); + rpc_listener_taks.await?; + return Ok(()) + } + }; + + // + //Raft + // + let datastore_raft = datastore_path.join("tau.db"); + let mut raft = Raft::::new(net_settings.inbound, datastore_raft)?; + + let raft_sender = raft.get_broadcast(); + let commits = raft.get_commits(); + let initial_sync_raft_sender = raft_sender.clone(); + let datastore_path_cloned = datastore_path.clone(); let recv_update: smol::Task> = executor.spawn(async move { info!(target: "tau", "Start initial sync"); @@ -105,10 +108,6 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { } }); - let executor_cloned = executor.clone(); - let rpc_listener_taks = - executor_cloned.spawn(listen_and_serve(server_config, rpc_interface, executor.clone())); - let (signal, shutdown) = async_channel::bounded::<()>(1); ctrlc_async::set_async_handler(async move { warn!(target: "tau", "taud start() Exit Signal"); @@ -120,7 +119,7 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { .unwrap(); // blocking - raft.start(p2p_settings.clone(), executor.clone(), shutdown.clone()).await?; + raft.start(net_settings, executor.clone(), shutdown.clone()).await?; Ok(()) } diff --git a/bin/tau/taud/src/settings.rs b/bin/tau/taud/src/settings.rs index a07627f3e..36058c6e7 100644 --- a/bin/tau/taud/src/settings.rs +++ b/bin/tau/taud/src/settings.rs @@ -1,5 +1,6 @@ use std::net::SocketAddr; +use darkfi::net; use serde::Deserialize; use structopt::StructOpt; use structopt_toml::StructOptToml; @@ -21,19 +22,17 @@ pub struct Args { /// Sets Datastore Path #[structopt(long, default_value = "~/.config/tau")] pub datastore: String, - /// Raft Accept address - #[structopt(short, long)] - pub accept: Option, - /// Raft Seed nodes (repeatable) - #[structopt(short, long)] - pub seeds: Vec, - /// Raft Manual connection (repeatable) - #[structopt(short, long)] - pub connect: Vec, - /// Raft Connection slots - #[structopt(long, default_value = "0")] - pub slots: u32, + #[structopt(subcommand)] + pub command: Option, /// Increase verbosity #[structopt(short, parse(from_occurrences))] pub verbose: u8, } + +#[derive(Clone, Debug, Deserialize, StructOpt)] +#[serde(tag = "type", content = "args")] +pub enum Command { + /// Raft net settings + /// Note: Wihtout passing this flag, tau will work locally + Net(net::Settings), +} diff --git a/bin/tau/taud_config.toml b/bin/tau/taud_config.toml index 8b1378917..24028be29 100644 --- a/bin/tau/taud_config.toml +++ b/bin/tau/taud_config.toml @@ -1 +1,19 @@ +# JSON-RPC listen URL +rpc_listen="127.0.0.1:8857" +# Sets Datastore Path +datastore="~/.config/tau" +# Raft net settings +[command] +type="Net" +[args] +inbound="127.0.0.1:11002" +outbound_connections=0 +manual_attempt_limit=0 +seed_query_timeout_seconds=8 +connect_timeout_seconds=10 +channel_handshake_seconds=4 +channel_heartbeat_seconds=10 +external_addr="127.0.0.1:11002" +peers=["127.0.0.1:11003"] +seeds=["127.0.0.1:11001"]