diff --git a/neon/native/src/server.rs b/neon/native/src/server.rs index d1804c53fe75b7ea30397839c74ddab058208d38..259f9b6b0cfe2d42549870c024200c8f415e28c1 100644 --- a/neon/native/src/server.rs +++ b/neon/native/src/server.rs @@ -21,7 +21,7 @@ use dubp::documents::{ }; use dubp::documents_parser::prelude::*; use dubp::{common::crypto::hashs::Hash, crypto::keys::ed25519::Ed25519KeyPair}; -use duniter_server::{DuniterConf, DuniterServer, GvaConf}; +use duniter_server::{DuniterConf, DuniterMode, DuniterServer, GvaConf}; use neon::declare_types; use neon::prelude::*; use serde::{Deserialize, Serialize}; @@ -40,7 +40,6 @@ declare_types! { let rust_server_conf_stringified: RustServerConfStringified = neon_serde::from_value(&mut cx, rust_server_conf_js)?; let gva_conf = rust_server_conf_stringified.gva; - let command_name = rust_server_conf_stringified.command_name; let currency = rust_server_conf_stringified.currency; let server_pubkey = if let Some(self_keypair_str) = rust_server_conf_stringified.self_keypair { into_neon_res(&mut cx, crate::crypto::keypair_from_expanded_base58_secret_key(&self_keypair_str))? @@ -73,12 +72,20 @@ declare_types! { } else { None }; + let duniter_mode = if let Ok(duniter_mode) = std::env::var("DUNITER_MODE") { + match duniter_mode.as_str() { + "sync" => DuniterMode::Sync, + _ => DuniterMode::Start, + } + } else { + return cx.throw_error("Env var DUNITER_MODE not exist or contain invalid utf8"); + }; into_neon_res( &mut cx, if let Some(home_path) = home_path_opt { - DuniterServer::start(command_name, conf, currency, Some(home_path.as_path()), std::env!("CARGO_PKG_VERSION")) + DuniterServer::start(conf, currency, duniter_mode, Some(home_path.as_path()), std::env!("CARGO_PKG_VERSION")) } else { - DuniterServer::start(command_name, conf, currency, None, std::env!("CARGO_PKG_VERSION")) + DuniterServer::start(conf, currency, duniter_mode, None, std::env!("CARGO_PKG_VERSION")) }.map(|server| RustServer { server }) ) } @@ -463,7 +470,6 @@ pub struct PeerCardStringified { #[derive(Deserialize, Serialize)] #[serde(rename_all = "camelCase")] struct RustServerConfStringified { - command_name: Option<String>, currency: String, gva: Option<GvaConf>, self_keypair: Option<String>, diff --git a/rust-bins/duniter-launcher/src/daemon.rs b/rust-bins/duniter-launcher/src/daemon.rs index a558c33d606a61fcae7f3118bb294391cec0f722..6e3c524f810dd91b02cd70b365fb737192bbb330 100644 --- a/rust-bins/duniter-launcher/src/daemon.rs +++ b/rust-bins/duniter-launcher/src/daemon.rs @@ -24,6 +24,7 @@ pub fn start(prod: bool, profile_path: &Path, duniter_js_args: &[String]) -> Res .args(duniter_js_args) .stdout(Stdio::null()) .stderr(Stdio::null()) + .env("DUNITER_MODE", "start") .spawn()?; let pid = child.id(); diff --git a/rust-bins/duniter-launcher/src/main.rs b/rust-bins/duniter-launcher/src/main.rs index 64661c1cc98c79ac5ce075f580926ecd44bc5b88..d83279acdf5502df868e42ab92ca72d15ba8fb51 100644 --- a/rust-bins/duniter-launcher/src/main.rs +++ b/rust-bins/duniter-launcher/src/main.rs @@ -245,7 +245,17 @@ fn main() -> Result<()> { duniter_js_command.current_dir(DUNITER_JS_CURRENT_DIR); } //println!("TMP duniter_ts_args={:?}", duniter_ts_args); - let exit_code_opt = duniter_js_command.args(duniter_ts_args).status()?.code(); + let mode = match args.command { + DuniterCommand::DirectStart { .. } + | DuniterCommand::DirectWebstart { .. } => "start", + DuniterCommand::Sync(_) => "sync", + _ => "other", + }; + let exit_code_opt = duniter_js_command + .args(duniter_ts_args) + .env("DUNITER_MODE", mode) + .status()? + .code(); if let Some(exit_code) = exit_code_opt { std::process::exit(exit_code); } else { diff --git a/rust-libs/duniter-conf/src/lib.rs b/rust-libs/duniter-conf/src/lib.rs index a966b2ab8b169588cc98fca9cba60747877e84b8..5038209c6570f174b9464dcba08ef77006fec46e 100644 --- a/rust-libs/duniter-conf/src/lib.rs +++ b/rust-libs/duniter-conf/src/lib.rs @@ -44,3 +44,11 @@ impl Default for DuniterConf { } } } + +/// Duniter mode +#[derive(Clone, Copy, Debug)] +#[non_exhaustive] +pub enum DuniterMode { + Start, + Sync, +} diff --git a/rust-libs/duniter-module/src/lib.rs b/rust-libs/duniter-module/src/lib.rs index 661900595c2527868843b8b0b5e7dc84f17180bc..9cf71883519a70b4c42d8a46702051e403090809 100644 --- a/rust-libs/duniter-module/src/lib.rs +++ b/rust-libs/duniter-module/src/lib.rs @@ -28,7 +28,7 @@ use dubp::{ crypto::{hashs::Hash, keys::ed25519::PublicKey}, documents::transaction::TransactionDocumentV10, }; -use duniter_conf::DuniterConf; +use duniter_conf::{DuniterConf, DuniterMode}; use duniter_dbs::{kv_typed::prelude::*, FileBackend, SharedDbs}; use duniter_mempools::Mempools; use std::path::Path; @@ -45,7 +45,7 @@ pub trait DuniterModule: 'static + Sized { /// in this case it must be reimplemented because the default implementation panics. fn apply_block( _block: &DubpBlockV10, - _conf: &duniter_conf::DuniterConf, + _conf: &DuniterConf, _profile_path_opt: Option<&Path>, ) -> KvResult<()> { unreachable!() @@ -55,7 +55,7 @@ pub trait DuniterModule: 'static + Sized { /// in this case it must be reimplemented because the default implementation panics. fn revert_block( _block: &DubpBlockV10, - _conf: &duniter_conf::DuniterConf, + _conf: &DuniterConf, _profile_path_opt: Option<&Path>, ) -> KvResult<()> { unreachable!() @@ -66,6 +66,7 @@ pub trait DuniterModule: 'static + Sized { currency: &str, dbs_pool: &fast_threadpool::ThreadPoolAsyncHandler<SharedDbs<FileBackend>>, mempools: Mempools, + mode: DuniterMode, profile_path_opt: Option<&Path>, software_version: &'static str, ) -> anyhow::Result<(Self, Vec<Endpoint>)>; @@ -204,12 +205,13 @@ macro_rules! plug_duniter_modules { currency: String, dbs_pool: fast_threadpool::ThreadPoolAsyncHandler<SharedDbs<FileBackend>>, mempools: duniter_mempools::Mempools, + mode: DuniterMode, profile_path_opt: Option<std::path::PathBuf>, software_version: &'static str, ) -> anyhow::Result<()> { let mut all_endpoints = Vec::<String>::new(); $( - let ([<$M:snake>], mut endpoints) =<$M>::init(conf, ¤cy, &dbs_pool, mempools, profile_path_opt.as_deref(), software_version) + let ([<$M:snake>], mut endpoints) =<$M>::init(conf, ¤cy, &dbs_pool, mempools, mode, profile_path_opt.as_deref(), software_version) .with_context(|| format!("Fail to init module '{}'", stringify!($M)))?; all_endpoints.append(&mut endpoints); )* @@ -289,6 +291,7 @@ mod tests { _currency: &str, _dbs_pool: &fast_threadpool::ThreadPoolAsyncHandler<SharedDbs<FileBackend>>, _mempools: Mempools, + _mode: DuniterMode, profile_path_opt: Option<&Path>, _software_version: &'static str, ) -> anyhow::Result<(Self, Vec<Endpoint>)> { @@ -312,6 +315,7 @@ mod tests { _currency: &str, _dbs_pool: &fast_threadpool::ThreadPoolAsyncHandler<SharedDbs<FileBackend>>, _mempools: Mempools, + _mode: DuniterMode, _profile_path_opt: Option<&Path>, _software_version: &'static str, ) -> anyhow::Result<(Self, Vec<Endpoint>)> { @@ -338,6 +342,7 @@ mod tests { Mempools { txs: TxsMempool::new(0), }, + DuniterMode::Sync, None, "", ) diff --git a/rust-libs/duniter-server/src/legacy/dunp.rs b/rust-libs/duniter-server/src/legacy/dunp.rs index 85f0f37d5fadf7a4788ced540988adfa1b5e2c7f..3433c911281ca86655b8f72306f1543f88d011ac 100644 --- a/rust-libs/duniter-server/src/legacy/dunp.rs +++ b/rust-libs/duniter-server/src/legacy/dunp.rs @@ -90,7 +90,7 @@ mod tests { #[test] fn test_receive_new_heads() -> anyhow::Result<()> { - let server = DuniterServer::test(DuniterConf::default())?; + let server = DuniterServer::test(DuniterConf::default(), DuniterMode::Start)?; let dbs = server.get_shared_dbs(); let head = ( @@ -119,7 +119,7 @@ mod tests { #[test] fn test_save_peer() -> anyhow::Result<()> { use duniter_dbs::databases::dunp_v1::DunpV1DbReadable as _; - let server = DuniterServer::test(DuniterConf::default())?; + let server = DuniterServer::test(DuniterConf::default(), DuniterMode::Start)?; let dbs = server.get_shared_dbs(); let peer = PeerCardDbV1 { diff --git a/rust-libs/duniter-server/src/lib.rs b/rust-libs/duniter-server/src/lib.rs index a934f14633f4aee5ac5f9050eac403ccc66a3aab..f8d98098964ea52de01d5e0581be4c11eaba4568 100644 --- a/rust-libs/duniter-server/src/lib.rs +++ b/rust-libs/duniter-server/src/lib.rs @@ -24,7 +24,7 @@ mod legacy; -pub use duniter_conf::{gva_conf::GvaConf, DuniterConf}; +pub use duniter_conf::{gva_conf::GvaConf, DuniterConf, DuniterMode}; use duniter_dbs::databases::dunp_v1::DunpV1DbWritable; pub use duniter_dbs::{ kv_typed::prelude::KvResult, smallvec, DunpHeadDbV1, DunpNodeIdV1Db, PeerCardDbV1, @@ -67,12 +67,6 @@ cfg_if::cfg_if! { } } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum DuniterCommand { - Sync, - Start, -} - pub struct DuniterServer { bc_db: BcV2Db<FileBackend>, conf: DuniterConf, @@ -90,16 +84,13 @@ impl DuniterServer { self.shared_dbs.clone() } pub fn start( - command_name: Option<String>, conf: DuniterConf, currency: String, + duniter_mode: DuniterMode, profile_path_opt: Option<&Path>, software_version: &'static str, ) -> anyhow::Result<DuniterServer> { - let command = match command_name.unwrap_or_default().as_str() { - "sync" => DuniterCommand::Sync, - _ => DuniterCommand::Start, - }; + log::info!("mode={:?}", duniter_mode); let txs_mempool = TxsMempool::new(conf.txs_mempool_size); @@ -128,7 +119,8 @@ impl DuniterServer { let threadpool = fast_threadpool::ThreadPool::start(ThreadPoolConfig::default(), shared_dbs.clone()); - if command != DuniterCommand::Sync && conf.gva.is_some() { + if conf.gva.is_some() { + log::info!("start duniter modules..."); let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build()?; @@ -142,6 +134,7 @@ impl DuniterServer { currency, threadpool_async_handler, Mempools { txs: txs_mempool }, + duniter_mode, profile_path_opt_clone, software_version, )) @@ -161,11 +154,14 @@ impl DuniterServer { }) } #[cfg(test)] - pub(crate) fn test(conf: DuniterConf) -> anyhow::Result<DuniterServer> { + pub(crate) fn test( + conf: DuniterConf, + duniter_mode: DuniterMode, + ) -> anyhow::Result<DuniterServer> { DuniterServer::start( - None, conf, "test".to_owned(), + duniter_mode, None, duniter_module::SOFTWARE_NAME, ) diff --git a/rust-libs/modules/gva/src/lib.rs b/rust-libs/modules/gva/src/lib.rs index 809b5953fff887a739d780b5ef1e3de92b223e74..35ca803e27c39dd13087d9cd905e9d6dc9561f7d 100644 --- a/rust-libs/modules/gva/src/lib.rs +++ b/rust-libs/modules/gva/src/lib.rs @@ -22,16 +22,17 @@ unused_import_braces )] -pub use duniter_conf::gva_conf::GvaConf; - mod anti_spam; mod warp_; +pub use duniter_conf::gva_conf::GvaConf; + use async_graphql::http::GraphQLPlaygroundConfig; use dubp::common::crypto::keys::{ed25519::PublicKey, KeyPair as _}; use dubp::common::prelude::*; use dubp::documents::transaction::TransactionDocumentV10; use dubp::{block::DubpBlockV10, crypto::hashs::Hash}; +use duniter_conf::DuniterMode; use duniter_dbs::databases::txs_mp_v2::TxsMpV2DbReadable; use duniter_dbs::prelude::*; use duniter_dbs::{kv_typed::prelude::*, FileBackend}; @@ -50,6 +51,7 @@ pub struct GvaModule { dbs_pool: fast_threadpool::ThreadPoolAsyncHandler<SharedDbs<FileBackend>>, gva_db_ro: &'static GvaV1DbRo<FileBackend>, mempools: Mempools, + mode: DuniterMode, self_pubkey: PublicKey, software_version: &'static str, } @@ -79,6 +81,7 @@ impl duniter_module::DuniterModule for GvaModule { currency: &str, dbs_pool: &fast_threadpool::ThreadPoolAsyncHandler<SharedDbs<FileBackend>>, mempools: Mempools, + mode: duniter_conf::DuniterMode, profile_path_opt: Option<&Path>, software_version: &'static str, ) -> anyhow::Result<(Self, Vec<duniter_module::Endpoint>)> { @@ -115,6 +118,7 @@ impl duniter_module::DuniterModule for GvaModule { dbs_pool: dbs_pool.to_owned(), gva_db_ro: get_gva_db_ro(profile_path_opt), mempools, + mode, self_pubkey: conf.self_key_pair.public_key(), software_version, }, @@ -131,21 +135,24 @@ impl duniter_module::DuniterModule for GvaModule { dbs_pool, gva_db_ro, mempools, + mode, self_pubkey, software_version, } = self; - if let Some(conf) = conf { - GvaModule::start_inner( - conf, - currency, - dbs_pool, - gva_db_ro, - mempools, - self_pubkey, - software_version, - ) - .await + if let DuniterMode::Start = mode { + if let Some(conf) = conf { + GvaModule::start_inner( + conf, + currency, + dbs_pool, + gva_db_ro, + mempools, + self_pubkey, + software_version, + ) + .await + } } } Ok(()) @@ -328,6 +335,7 @@ mod tests { "", &threadpool.into_async_handler(), Mempools::default(), + duniter_conf::DuniterMode::Start, None, "test", )? diff --git a/rust-libs/tests/duniter-integration-tests/src/lib.rs b/rust-libs/tests/duniter-integration-tests/src/lib.rs index ac460624c47fa944e4f92173a294238131652396..c8172e6ea2ed9fe3e0e73898743ea2adf628e8cc 100644 --- a/rust-libs/tests/duniter-integration-tests/src/lib.rs +++ b/rust-libs/tests/duniter-integration-tests/src/lib.rs @@ -34,8 +34,8 @@ mod tests { #[test] fn test_txs_history() -> anyhow::Result<()> { + std::env::set_var("DUNITER_MODE", "start"); let server = DuniterServer::start( - None, DuniterConf { gva: None, self_key_pair: Ed25519KeyPair::generate_random() @@ -43,6 +43,7 @@ mod tests { txs_mempool_size: 200, }, "currency_test".to_owned(), + DuniterMode::Start, None, "test", )?;