diff --git a/bin/dunitrust-server/src/cli.rs b/bin/dunitrust-server/src/cli.rs index 75813c3e187073466f7e4284d8b8015c018045c5..dbc7a026568ba713612723f78c1250138e4245dd 100644 --- a/bin/dunitrust-server/src/cli.rs +++ b/bin/dunitrust-server/src/cli.rs @@ -15,7 +15,7 @@ //! Command line options for classic Dunitrust nodes (no specialization). -use durs_conf::DuRsConf; +use durs_conf::{DuRsConf, DuniterKeyPairs}; use durs_core::commands::dbex::DbExOpt; use durs_core::commands::keys::KeysOpt; use durs_core::commands::modules::{DisableOpt, EnableOpt, ListModulesOpt}; @@ -68,14 +68,18 @@ impl CommandNeedKeypairs for DursCliOpt {} impl ExecutableModuleCommand for DursCliOpt { /// Execute command - fn execute_module_command(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { + fn execute_module_command( + self, + durs_core: DursCore<DuRsConf>, + keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError> { match self.cmd { DursCliSubCommand::Ws2p1(module_opts) => { - DursCore::execute_module_command::<WS2Pv1Module>(durs_core, module_opts) + DursCore::execute_module_command::<WS2Pv1Module>(durs_core, &keypairs, module_opts) } #[cfg(not(target_arch = "arm"))] DursCliSubCommand::Gva(module_opts) => { - DursCore::execute_module_command::<GvaModule>(durs_core, module_opts) + DursCore::execute_module_command::<GvaModule>(durs_core, &keypairs, module_opts) } _ => unreachable!(), } diff --git a/lib/core/conf/src/keypairs.rs b/lib/core/conf/src/keypairs.rs index 0a6b7fc812c45c845b1bff6ad7317c537e01b784..643a4be4612a072fe74fd314ae878e0cebbcd257 100644 --- a/lib/core/conf/src/keypairs.rs +++ b/lib/core/conf/src/keypairs.rs @@ -69,21 +69,21 @@ impl DuniterKeyPairs { /// Returns only the keys indicated as required pub fn get_required_keys_content( required_keys: RequiredKeys, - keypairs: DuniterKeyPairs, + keypairs: &DuniterKeyPairs, ) -> RequiredKeysContent { match required_keys { RequiredKeys::MemberKeyPair => { - RequiredKeysContent::MemberKeyPair(keypairs.member_keypair) + RequiredKeysContent::MemberKeyPair(keypairs.member_keypair.clone()) } - RequiredKeys::MemberPublicKey => { - RequiredKeysContent::MemberPublicKey(if let Some(keys) = keypairs.member_keypair { + RequiredKeys::MemberPublicKey => RequiredKeysContent::MemberPublicKey( + if let Some(keys) = keypairs.member_keypair.clone() { Some(keys.public_key()) } else { None - }) - } + }, + ), RequiredKeys::NetworkKeyPair => { - RequiredKeysContent::NetworkKeyPair(keypairs.network_keypair) + RequiredKeysContent::NetworkKeyPair(keypairs.network_keypair.clone()) } RequiredKeys::NetworkPublicKey => { RequiredKeysContent::NetworkPublicKey(keypairs.network_keypair.public_key()) diff --git a/lib/core/conf/src/modules_conf.rs b/lib/core/conf/src/modules_conf.rs index 521571481c7a08a3cddf2064ec59e86ba7b8edc1..63945c758b58b618d47d32059bef1abc27257630 100644 --- a/lib/core/conf/src/modules_conf.rs +++ b/lib/core/conf/src/modules_conf.rs @@ -103,7 +103,7 @@ pub fn get_module_conf_and_keys<M: DursModule<DuRsConf, DursMsg>>( currency_name: Option<&CurrencyName>, global_conf: &<DuRsConf as DursConfTrait>::GlobalConf, module_conf_json: Option<serde_json::Value>, - keypairs: DuniterKeyPairs, + keypairs: &DuniterKeyPairs, ) -> Result<ModuleConfsAndKeys<M>, ModuleConfError> { Ok(( ModulesConf::get_module_conf::<M>(currency_name, global_conf, module_conf_json)?, @@ -159,7 +159,7 @@ mod tests { None, &DuRsGlobalConf::V2(DuRsGlobalConfV2::default()), None, - keypairs(), + &keypairs(), )?; assert_eq!( @@ -189,7 +189,7 @@ mod tests { None, &DuRsGlobalConf::V2(DuRsGlobalConfV2::default()), Some(json_conf), - keypairs(), + &keypairs(), )?; assert_eq!( @@ -226,7 +226,7 @@ mod tests { None, &DuRsGlobalConf::V2(DuRsGlobalConfV2::default()), Some(json_conf), - keypairs(), + &keypairs(), )?; assert_eq!( diff --git a/lib/core/core/src/commands/dbex.rs b/lib/core/core/src/commands/dbex.rs index b431c32e084b9371ad9113d1a073090e90e94968..060e1f16c1629dfb14d28a25fee7a43ff29dcbb9 100644 --- a/lib/core/core/src/commands/dbex.rs +++ b/lib/core/core/src/commands/dbex.rs @@ -20,7 +20,7 @@ use crate::dbex; use crate::errors::DursCoreError; use crate::DursCore; use durs_bc::dbex::{DbExBcQuery, DbExQuery, DbExTxQuery, DbExWotQuery}; -use durs_conf::DuRsConf; +use durs_conf::{DuRsConf, DuniterKeyPairs}; #[derive(StructOpt, Debug, Clone)] #[structopt(name = "dbex", setting(structopt::clap::AppSettings::ColoredHelp))] @@ -101,7 +101,11 @@ pub struct BalanceOpt { pub struct BlocksOpt {} impl DursExecutableCoreCommand for DbExOpt { - fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { + fn execute( + self, + durs_core: DursCore<DuRsConf>, + _keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError> { let profile_path = durs_core.soft_meta_datas.profile_path; match self.subcommand { diff --git a/lib/core/core/src/commands/keys.rs b/lib/core/core/src/commands/keys.rs index 842eaaa0e499f69f2068c9f10f4804e8a8bc6331..0ad07dda0ec564f540b1e42b1ede8b0e9dc2d390 100644 --- a/lib/core/core/src/commands/keys.rs +++ b/lib/core/core/src/commands/keys.rs @@ -21,7 +21,7 @@ use crate::DursCore; use clap::arg_enum; use durs_conf::keypairs::cli::*; use durs_conf::ui::ui_cli::CLI; -use durs_conf::DuRsConf; +use durs_conf::{DuRsConf, DuniterKeyPairs}; #[derive(StructOpt, Debug, Clone, Copy)] #[structopt( @@ -136,11 +136,14 @@ pub struct WizardOpt {} pub struct ShowOpt {} impl DursExecutableCoreCommand for KeysOpt { - fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { + fn execute( + self, + durs_core: DursCore<DuRsConf>, + keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError> { let cli = CLI {}; let profile_path = durs_core.soft_meta_datas.profile_path; let keypairs_file = durs_core.options.keypairs_file; - let keypairs = durs_core.keypairs; match self.subcommand { KeysSubCommand::Wizard(_) => { diff --git a/lib/core/core/src/commands/mod.rs b/lib/core/core/src/commands/mod.rs index cb87feef646cde3a347df59458411756b4c6ef31..c2c0bcd838a18de1a1b1617219a7c277c54221ef 100644 --- a/lib/core/core/src/commands/mod.rs +++ b/lib/core/core/src/commands/mod.rs @@ -25,7 +25,7 @@ use crate::constants::DEFAULT_USER_PROFILE; use crate::errors::DursCoreError; use crate::DursCore; pub use dbex::*; -use durs_conf::DuRsConf; +use durs_conf::{DuRsConf, DuniterKeyPairs}; use durs_dbs_tools::kv_db_old::KvFileDbHandler; pub use durs_network::cli::sync::SyncOpt; pub use keys::KeysOpt; @@ -66,13 +66,21 @@ impl DursCoreOptions { /// Dunitrust executable command pub trait DursExecutableCoreCommand { /// Execute Dunitrust command - fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError>; + fn execute( + self, + durs_core: DursCore<DuRsConf>, + keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError>; } /// Executable module command pub trait ExecutableModuleCommand: CommandNeedKeypairs { /// Execute module command - fn execute_module_command(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError>; + fn execute_module_command( + self, + durs_core: DursCore<DuRsConf>, + keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError>; } /// Do this command use the keypairs @@ -116,13 +124,19 @@ impl<T: ExecutableModuleCommand> DursCommand<T> { plug_modules: PlugFunc, ) -> Result<(), DursCoreError> where - PlugFunc: FnMut(&mut DursCore<DuRsConf>) -> Result<(), DursCoreError>, + PlugFunc: FnMut(&mut DursCore<DuRsConf>, DuniterKeyPairs) -> Result<(), DursCoreError>, { let profile_path = self.options.define_profile_path(); let bc_db = self.open_bc_db(&profile_path)?; let durs_core = DursCore::<DuRsConf>::init(soft_name, soft_version, self.options, 0)?; + let keypairs = durs_conf::keypairs::load_keypairs_from_file( + &profile_path, + &durs_core.options.keypairs_file, + ) + .map_err(DursCoreError::LoadConfError)?; + match self.command { DursCommandEnum::Core(core_cmd) => DursCore::execute_core_command( bc_db, @@ -130,8 +144,9 @@ impl<T: ExecutableModuleCommand> DursCommand<T> { plug_modules, profile_path, durs_core, + keypairs, ), - DursCommandEnum::Other(cmd) => cmd.execute_module_command(durs_core), + DursCommandEnum::Other(cmd) => cmd.execute_module_command(durs_core, keypairs), } } } diff --git a/lib/core/core/src/commands/modules.rs b/lib/core/core/src/commands/modules.rs index 5b08d1d5b4bc35e6c5ffbd2699e05559769c80c0..98abe93a1058c063ae4c1b926910f7b6968ab308 100644 --- a/lib/core/core/src/commands/modules.rs +++ b/lib/core/core/src/commands/modules.rs @@ -18,7 +18,7 @@ use crate::commands::{CommandNeedKeypairs, DursExecutableCoreCommand}; use crate::errors::DursCoreError; use crate::DursCore; -use durs_conf::{ChangeGlobalConf, DuRsConf}; +use durs_conf::{ChangeGlobalConf, DuRsConf, DuniterKeyPairs}; use durs_module::*; use std::collections::HashSet; @@ -35,7 +35,11 @@ impl CommandNeedKeypairs for EnableOpt {} impl DursExecutableCoreCommand for EnableOpt { #[inline] - fn execute(self, mut durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { + fn execute( + self, + mut durs_core: DursCore<DuRsConf>, + _keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError> { crate::change_conf::change_global_conf( &durs_core.soft_meta_datas.profile_path.clone(), &mut durs_core.soft_meta_datas.conf, @@ -57,7 +61,11 @@ impl CommandNeedKeypairs for DisableOpt {} impl DursExecutableCoreCommand for DisableOpt { #[inline] - fn execute(self, mut durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { + fn execute( + self, + mut durs_core: DursCore<DuRsConf>, + _keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError> { crate::change_conf::change_global_conf( &durs_core.soft_meta_datas.profile_path.clone(), &mut durs_core.soft_meta_datas.conf, diff --git a/lib/core/core/src/commands/reset.rs b/lib/core/core/src/commands/reset.rs index 4dc34ae8965ba490e9cd6f895722eb6d3f672129..cfc7cfb13a606f38a0116c8a0a7d6d133e383925 100644 --- a/lib/core/core/src/commands/reset.rs +++ b/lib/core/core/src/commands/reset.rs @@ -19,7 +19,7 @@ use super::InvalidInput; use crate::commands::{CommandNeedKeypairs, DursExecutableCoreCommand}; use crate::errors::DursCoreError; use crate::DursCore; -use durs_conf::DuRsConf; +use durs_conf::{DuRsConf, DuniterKeyPairs}; use std::fs; use std::str::FromStr; @@ -64,7 +64,11 @@ impl CommandNeedKeypairs for ResetOpt { } impl DursExecutableCoreCommand for ResetOpt { - fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { + fn execute( + self, + durs_core: DursCore<DuRsConf>, + _keypairs: DuniterKeyPairs, + ) -> Result<(), DursCoreError> { let profile_path = durs_core.soft_meta_datas.profile_path; match self.reset_type { diff --git a/lib/core/core/src/lib.rs b/lib/core/core/src/lib.rs index ab12c9a1841a83e1b04c286a1888321ecca6dcb9..2ab78c331a36041d6f3af2b3348b1203faf8b948 100644 --- a/lib/core/core/src/lib.rs +++ b/lib/core/core/src/lib.rs @@ -63,9 +63,9 @@ use unwrap::unwrap; macro_rules! durs_plug { ( [ $( $NetworkModule:ty ),* ], [ $( $Module:ty ),* ] ) => { { - |core| { - $(core.plug::<$Module>()?;)* - $(core.plug_network::<$NetworkModule>()?;)* + |core, keypairs| { + $(core.plug::<$Module>(&keypairs)?;)* + $(core.plug_network::<$NetworkModule>(&keypairs)?;)* Ok(()) } } @@ -82,8 +82,6 @@ pub struct DursCore<DC: DursConfTrait> { server_command: Option<ServerMode>, /// Software meta datas pub soft_meta_datas: SoftwareMetaDatas<DC>, - /// Keypairs - pub keypairs: DuniterKeyPairs, /// Run duration. Zero = infinite duration. pub run_duration_in_secs: u64, /// Sender channel of router thread @@ -111,6 +109,7 @@ impl DursCore<DuRsConf> { /// Execute module command pub fn execute_module_command<M: DursModule<DuRsConf, DursMsg>>( mut durs_core: DursCore<DuRsConf>, + keypairs: &DuniterKeyPairs, module_command: M::ModuleOpt, ) -> Result<(), DursCoreError> { // Load module conf and keys @@ -127,7 +126,7 @@ impl DursCore<DuRsConf> { durs_core.currency_name.as_ref(), &durs_core.soft_meta_datas.conf.get_global_conf(), module_conf_json, - durs_core.keypairs, + &keypairs, ) .map_err(|e| DursCoreError::PlugModuleError { module_name: M::name(), @@ -159,16 +158,17 @@ impl DursCore<DuRsConf> { mut plug_modules: PlugFunc, profile_path: PathBuf, mut durs_core: DursCore<DuRsConf>, + keypairs: DuniterKeyPairs, ) -> Result<(), DursCoreError> where - PlugFunc: FnMut(&mut DursCore<DuRsConf>) -> Result<(), DursCoreError>, + PlugFunc: FnMut(&mut DursCore<DuRsConf>, DuniterKeyPairs) -> Result<(), DursCoreError>, { /* * CORE COMMAND PROCESSING */ match core_command { - DursCoreCommand::DisableOpt(opts) => opts.execute(durs_core), - DursCoreCommand::EnableOpt(opts) => opts.execute(durs_core), + DursCoreCommand::DisableOpt(opts) => opts.execute(durs_core, keypairs), + DursCoreCommand::EnableOpt(opts) => opts.execute(durs_core, keypairs), DursCoreCommand::ListModulesOpt(opts) => { durs_core.server_command = Some(ServerMode::ListModules(opts)); @@ -177,7 +177,7 @@ impl DursCore<DuRsConf> { profile_path, durs_core.soft_meta_datas.conf.clone(), )); - plug_modules(&mut durs_core) + plug_modules(&mut durs_core, keypairs) } DursCoreCommand::StartOpt(_opts) => { durs_core.server_command = Some(ServerMode::Start()); @@ -187,7 +187,7 @@ impl DursCore<DuRsConf> { profile_path, durs_core.soft_meta_datas.conf.clone(), )); - plug_modules(&mut durs_core)?; + plug_modules(&mut durs_core, keypairs)?; durs_core.start(bc_db) } DursCoreCommand::SyncOpt(opts) => { @@ -209,15 +209,15 @@ impl DursCore<DuRsConf> { profile_path, durs_core.soft_meta_datas.conf.clone(), )); - plug_modules(&mut durs_core)?; + plug_modules(&mut durs_core, keypairs)?; durs_core.start(bc_db) } else { Err(DursCoreError::SyncWithoutSource) } } - DursCoreCommand::DbExOpt(opts) => opts.execute(durs_core), - DursCoreCommand::ResetOpt(opts) => opts.execute(durs_core), - DursCoreCommand::KeysOpt(opts) => opts.execute(durs_core), + DursCoreCommand::DbExOpt(opts) => opts.execute(durs_core, keypairs), + DursCoreCommand::ResetOpt(opts) => opts.execute(durs_core, keypairs), + DursCoreCommand::KeysOpt(opts) => opts.execute(durs_core, keypairs), } } /// Initialize Dunitrust core @@ -241,12 +241,6 @@ impl DursCore<DuRsConf> { // Load global conf let conf = durs_conf::load_conf(profile_path.clone()).map_err(DursCoreError::LoadConfError)?; - let keypairs = durs_conf::keypairs::load_keypairs_from_file( - &profile_path, - &durs_core_opts.keypairs_file, - ) - .map_err(DursCoreError::LoadConfError)?; - info!("Success to load global conf."); // Get currency name @@ -258,7 +252,6 @@ impl DursCore<DuRsConf> { // Instanciate durs core Ok(DursCore { currency_name, - keypairs, options: durs_core_opts, modules_names: Vec::new(), network_modules_count: 0, @@ -369,8 +362,9 @@ impl DursCore<DuRsConf> { /// Plug a network module pub fn plug_network<NM: NetworkModule<DuRsConf, DursMsg>>( &mut self, + keypairs: &DuniterKeyPairs, ) -> Result<(), DursCoreError> { - self.plug_network_::<NM>() + self.plug_network_::<NM>(keypairs) .map_err(|error| DursCoreError::PlugModuleError { module_name: NM::name(), error, @@ -378,6 +372,7 @@ impl DursCore<DuRsConf> { } fn plug_network_<NM: NetworkModule<DuRsConf, DursMsg>>( &mut self, + keypairs: &DuniterKeyPairs, ) -> Result<(), PlugModuleError> { let enabled = enabled::<DuRsConf, DursMsg, NM>(&self.soft_meta_datas.conf); if enabled { @@ -415,7 +410,7 @@ impl DursCore<DuRsConf> { self.currency_name.as_ref(), &soft_meta_datas.conf.get_global_conf(), module_conf_json, - self.keypairs.clone(), + &keypairs, )?; let sync_params = network_sync.clone(); @@ -451,16 +446,19 @@ impl DursCore<DuRsConf> { Ok(()) } } else { - self.plug_::<NM>(true) + self.plug_::<NM>(keypairs, true) } } else { - self.plug_::<NM>(true) + self.plug_::<NM>(keypairs, true) } } #[inline] /// Plug a module - pub fn plug<M: DursModule<DuRsConf, DursMsg>>(&mut self) -> Result<(), DursCoreError> { - self.plug_::<M>(false) + pub fn plug<M: DursModule<DuRsConf, DursMsg>>( + &mut self, + keypairs: &DuniterKeyPairs, + ) -> Result<(), DursCoreError> { + self.plug_::<M>(keypairs, false) .map_err(|error| DursCoreError::PlugModuleError { module_name: M::name(), error, @@ -470,6 +468,7 @@ impl DursCore<DuRsConf> { /// Plug a module pub fn plug_<M: DursModule<DuRsConf, DursMsg>>( &mut self, + keypairs: &DuniterKeyPairs, is_network_module: bool, ) -> Result<(), PlugModuleError> { let enabled = enabled::<DuRsConf, DursMsg, M>(&self.soft_meta_datas.conf); @@ -500,7 +499,7 @@ impl DursCore<DuRsConf> { self.currency_name.as_ref(), &soft_meta_datas.conf.get_global_conf(), module_conf_json, - self.keypairs.clone(), + keypairs, )?; let thread_builder = thread::Builder::new().name(M::name().0.into());