diff --git a/Cargo.lock b/Cargo.lock index 8671f8c1da6e24ee007f6a0567839a0dd3281d2e..6aaa53b4a3f149b50199f4402d00354e29381baf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,7 +73,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -188,9 +188,9 @@ dependencies = [ "pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustbreak 2.0.0-rc2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "sqlite 0.23.9 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -202,9 +202,9 @@ dependencies = [ "duniter-crypto 0.1.2", "duniter-module 0.1.0", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -222,9 +222,8 @@ dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "sqlite 0.23.9 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -237,7 +236,7 @@ dependencies = [ "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -256,9 +255,9 @@ dependencies = [ "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustbreak 2.0.0-rc2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -272,7 +271,7 @@ dependencies = [ "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -285,9 +284,8 @@ dependencies = [ "duniter-documents 0.7.1", "duniter-module 0.1.0", "duniter-network 0.1.0", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -296,9 +294,9 @@ version = "0.1.0" dependencies = [ "duniter-crypto 0.1.2", "duniter-documents 0.7.1", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -311,9 +309,9 @@ dependencies = [ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -328,7 +326,7 @@ dependencies = [ "duniter-module 0.1.0", "duniter-network 0.1.0", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -339,7 +337,7 @@ dependencies = [ "bincode 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -360,9 +358,9 @@ dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "sqlite 0.23.9 (registry+https://github.com/rust-lang/crates.io-index)", "ws 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -371,6 +369,7 @@ dependencies = [ name = "durs" version = "0.1.0" dependencies = [ + "duniter-conf 0.1.0", "duniter-core 0.1.0", "duniter-tui 0.1.0", "duniter-ws2p 0.1.0", @@ -761,7 +760,7 @@ dependencies = [ "base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -786,7 +785,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.57" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -801,12 +800,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.17" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1162,9 +1161,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" -"checksum serde 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)" = "9478f147957b713a156ce5e4529d77275bbcfddc29563b794939b36230df8ca8" +"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95" "checksum serde_derive 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)" = "1bdba9c305f1aeff7e83e2ff0685a141780de943cee66bdd89b63913b2b69c86" -"checksum serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "f3ad6d546e765177cf3dded3c2e424a8040f870083a0e64064746b958ece9cb1" +"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum simplelog 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9cc12b39fdf4c9a07f88bffac2d628f0118ed5ac077a4b0feece61fadf1429e5" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" diff --git a/Cargo.toml b/Cargo.toml index 3eecdcaed45e67aa65b3fbca4941d536e73be393..9674d5b625837386d3be78a46382d5f93dd7227c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ description = "DUniter-RS (durs) is a new implementation of Duniter protocol and license = "AGPL-3.0" [dependencies] +duniter-conf = { path = "./conf" } duniter-core = { path = "./core" } duniter-tui = { path = "./tui", optional = true } duniter-ws2p = { path = "./ws2p" } diff --git a/blockchain/Cargo.toml b/blockchain/Cargo.toml index 416cd092d5c4e3ecd1194f38baa53dffce8116ee..300c2b50e32c01e861215c90fea3a6df9704c88a 100644 --- a/blockchain/Cargo.toml +++ b/blockchain/Cargo.toml @@ -24,6 +24,6 @@ rand = "0.4.2" rustbreak = {version = "2.0.0-rc2", features = ["bin_enc"]} serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" +serde_json = "1.0.20" sqlite = "0.23.9" threadpool = "1.7.1" \ No newline at end of file diff --git a/blockchain/dbex.rs b/blockchain/dbex.rs index f7937787d48066042ab78a2dc932b659aadf1139..ebb8d5965553c17ec3f8beb66c8cb1180a104b50 100644 --- a/blockchain/dbex.rs +++ b/blockchain/dbex.rs @@ -51,16 +51,16 @@ pub enum DBExQuery { TxQuery(DBExTxQuery), } -pub fn dbex(conf: &DuniterConf, query: &DBExQuery) { +pub fn dbex<DC: DuniterConf>(profile: &str, conf: &DC, query: &DBExQuery) { match *query { - DBExQuery::WotQuery(ref wot_query) => dbex_wot(conf, wot_query), - DBExQuery::TxQuery(ref tx_query) => dbex_tx(conf, tx_query), + DBExQuery::WotQuery(ref wot_query) => dbex_wot(profile, conf, wot_query), + DBExQuery::TxQuery(ref tx_query) => dbex_tx(profile, conf, tx_query), } } -pub fn dbex_tx(conf: &DuniterConf, query: &DBExTxQuery) { +pub fn dbex_tx<DC: DuniterConf>(profile: &str, conf: &DC, query: &DBExTxQuery) { // Get db path - let db_path = duniter_conf::get_blockchain_db_path(conf.profile().as_str(), &conf.currency()); + let db_path = duniter_conf::get_blockchain_db_path(profile, &conf.currency()); // Open databases let load_dbs_begin = SystemTime::now(); @@ -114,9 +114,9 @@ pub fn dbex_tx(conf: &DuniterConf, query: &DBExTxQuery) { ); } -pub fn dbex_wot(conf: &DuniterConf, query: &DBExWotQuery) { +pub fn dbex_wot<DC: DuniterConf>(profile: &str, conf: &DC, query: &DBExWotQuery) { // Get db path - let db_path = duniter_conf::get_blockchain_db_path(conf.profile().as_str(), &conf.currency()); + let db_path = duniter_conf::get_blockchain_db_path(profile, &conf.currency()); // Open databases let load_dbs_begin = SystemTime::now(); diff --git a/blockchain/lib.rs b/blockchain/lib.rs index 3365f3890e93ee3ddae55bfd4041bb1323a8dcd7..49bb2e27088b9c6941a063b1773228b370163de6 100644 --- a/blockchain/lib.rs +++ b/blockchain/lib.rs @@ -94,7 +94,7 @@ pub struct BlockchainModule { /// Subscribers pub followers: Vec<mpsc::Sender<DuniterMessage>>, /// Name of the user datas profile - pub conf_profile: String, + pub profile: String, /// Currency pub currency: Currency, // Currency parameters @@ -144,16 +144,16 @@ pub enum CompletedBlockError { impl BlockchainModule { /// Return module identifier pub fn id() -> ModuleId { - ModuleId::Str("blockchain") + ModuleId(String::from("blockchain")) } /// Loading blockchain configuration - pub fn load_blockchain_conf( - conf: &DuniterConf, + pub fn load_blockchain_conf<DC: DuniterConf>( + profile: &str, + conf: &DC, _keys: RequiredKeysContent, ) -> BlockchainModule { // Get db path - let db_path = - duniter_conf::get_blockchain_db_path(conf.profile().as_str(), &conf.currency()); + let db_path = duniter_conf::get_blockchain_db_path(profile, &conf.currency()); // Open databases let blocks_databases = BlocksV10DBs::open(&db_path, false); @@ -181,7 +181,7 @@ impl BlockchainModule { // Instanciate BlockchainModule BlockchainModule { followers: Vec::new(), - conf_profile: conf.profile(), + profile: profile.to_string(), currency: conf.currency(), currency_params, blocks_databases, @@ -192,11 +192,17 @@ impl BlockchainModule { } } /// Databases explorer - pub fn dbex(conf: &DuniterConf, req: &DBExQuery) { - dbex::dbex(conf, req); + pub fn dbex<DC: DuniterConf>(profile: &str, conf: &DC, req: &DBExQuery) { + dbex::dbex(profile, conf, req); } /// Synchronize blockchain from a duniter-ts database - pub fn sync_ts(conf: &DuniterConf, ts_profile: &str, cautious: bool, verif_inner_hash: bool) { + pub fn sync_ts<DC: DuniterConf>( + profile: &str, + conf: &DC, + ts_profile: &str, + cautious: bool, + verif_inner_hash: bool, + ) { // get db_ts_path let mut db_ts_path = match env::home_dir() { Some(path) => path, @@ -208,7 +214,7 @@ impl BlockchainModule { if !db_ts_path.as_path().exists() { panic!("Fatal error : duniter-ts database don't exist !"); } - sync::sync_ts(conf, db_ts_path, cautious, verif_inner_hash); + sync::sync_ts(profile, conf, db_ts_path, cautious, verif_inner_hash); } /// Request chunk from network (chunk = group of blocks) fn request_chunk(&self, req_id: &ModuleReqId, from: u32) -> (ModuleReqId, NetworkRequest) { @@ -218,7 +224,7 @@ impl BlockchainModule { *CHUNK_SIZE, from, ); - (self.request_network(req), req) + (self.request_network(&req), req) } /// Requests blocks from current to `to` fn request_blocks_to( @@ -259,10 +265,10 @@ impl BlockchainModule { requests_ids } /// Send network request - fn request_network(&self, request: NetworkRequest) -> ModuleReqId { + fn request_network(&self, request: &NetworkRequest) -> ModuleReqId { for follower in &self.followers { if follower - .send(DuniterMessage::NetworkRequest(request)) + .send(DuniterMessage::NetworkRequest(request.clone())) .is_err() { debug!("BlockchainModule : one follower is unreachable !"); @@ -505,8 +511,7 @@ impl BlockchainModule { info!("BlockchainModule::start_blockchain()"); // Get dbs path - let dbs_path = - duniter_conf::get_blockchain_db_path(self.conf_profile.as_str(), &self.currency); + let dbs_path = duniter_conf::get_blockchain_db_path(self.profile.as_str(), &self.currency); // Get wotb index let mut wotb_index: HashMap<PubKey, NodeId> = @@ -534,7 +539,7 @@ impl BlockchainModule { BlockchainModule::id(), ModuleReqId(pending_network_requests.len() as u32), )); - let req_id = self.request_network(req); + let req_id = self.request_network(&req); pending_network_requests.insert(req_id, req); // Request Blocks let now = SystemTime::now(); @@ -592,7 +597,7 @@ impl BlockchainModule { debug!("BlockchainModule : send_req_response(CurrentBlock({}))", current_blockstamp); self.send_req_response(&DALResponse::Blockchain(Box::new( DALResBlockchain::CurrentBlock( - *requester_full_id, + requester_full_id.clone(), Box::new(current_block.block), current_blockstamp, ), diff --git a/blockchain/sync.rs b/blockchain/sync.rs index b0766079ae4906c5486f05c56c0797506b3bb614..3c42f95b32e3a9137fe699001d54bdd8d5e28725 100644 --- a/blockchain/sync.rs +++ b/blockchain/sync.rs @@ -67,7 +67,13 @@ enum SyncJobsMess { } /// Sync from a duniter-ts database -pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_inner_hash: bool) { +pub fn sync_ts<DC: DuniterConf>( + profile: &str, + conf: &DC, + db_ts_path: PathBuf, + cautious: bool, + verif_inner_hash: bool, +) { // Get verification level let _verif_level = if cautious { println!("Start cautious sync..."); @@ -92,12 +98,11 @@ pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_in let pool = ThreadPool::new(nb_workers); // Determine db_ts_copy_path - let mut db_ts_copy_path = - duniter_conf::datas_path(&conf.profile().clone(), &conf.currency().clone()); + let mut db_ts_copy_path = duniter_conf::datas_path(profile, &conf.currency().clone()); db_ts_copy_path.push("tmp_db_ts_copy.db"); // Lauch ts thread - let profile_copy = conf.profile().clone(); + let profile_copy = String::from(profile); let sender_sync_thread_clone = sender_sync_thread.clone(); pool.execute(move || { let ts_job_begin = SystemTime::now(); @@ -247,10 +252,10 @@ pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_in conf.set_currency(currency.clone()); // Get databases path - let db_path = duniter_conf::get_blockchain_db_path(&conf.profile(), ¤cy); + let db_path = duniter_conf::get_blockchain_db_path(profile, ¤cy); // Write nex conf - duniter_conf::write_conf_file(&conf).expect("Fail to write new conf !"); + duniter_conf::write_conf_file(profile, &conf).expect("Fail to write new conf !"); // Open wot db let wot_db = open_wot_db::<RustyWebOfTrust>(&db_path).expect("Fail to open WotDB !"); @@ -267,7 +272,7 @@ pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_in // Instanciate blockchain module let blockchain_module = - BlockchainModule::load_blockchain_conf(&conf, RequiredKeysContent::None()); + BlockchainModule::load_blockchain_conf(profile, &conf, RequiredKeysContent::None()); // Node is already synchronized ? if target_blockstamp.id.0 < current_blockstamp.id.0 { @@ -388,7 +393,7 @@ pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_in }); // / Launch wot_worker thread - let profile_copy2 = conf.profile().clone(); + let profile_copy2 = String::from(profile); let currency_copy2 = currency.clone(); let sender_sync_thread_clone2 = sender_sync_thread.clone(); @@ -430,7 +435,7 @@ pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_in }); // Launch tx_worker thread - let profile_copy = conf.profile().clone(); + let profile_copy = String::from(profile); let currency_copy = conf.currency().clone(); let sender_sync_thread_clone = sender_sync_thread.clone(); pool.execute(move || { @@ -468,7 +473,7 @@ pub fn sync_ts(conf: &DuniterConf, db_ts_path: PathBuf, cautious: bool, verif_in let main_job_begin = SystemTime::now(); // Open currency_params_db - let dbs_path = duniter_conf::get_blockchain_db_path(&conf.profile(), &conf.currency()); + let dbs_path = duniter_conf::get_blockchain_db_path(profile, &conf.currency()); let currency_params_db = open_db::<CurrencyParamsV10Datas>(&dbs_path, "params.db").expect("Fail to open params db"); diff --git a/conf/Cargo.toml b/conf/Cargo.toml index 1d161db892363844a594080f3195de067aaa153f..82d52df89a94b14cd743c98beadc6a07b6993558 100644 --- a/conf/Cargo.toml +++ b/conf/Cargo.toml @@ -12,7 +12,7 @@ path = "lib.rs" rand = "0.4.2" serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" +serde_json = "1.0.20" duniter-crypto = { path = "../crypto" } duniter-module = { path = "../module" } diff --git a/conf/lib.rs b/conf/lib.rs index fe82e0af9cc144e5914b2c8a6919a961cc11709f..c7931d0f7c1023213bf3cf67e814c8ac889cfdf4 100644 --- a/conf/lib.rs +++ b/conf/lib.rs @@ -23,6 +23,8 @@ unused_qualifications )] +#[macro_use] +extern crate serde_derive; #[macro_use] extern crate serde_json; @@ -31,7 +33,7 @@ extern crate duniter_module; extern crate rand; extern crate serde; use duniter_crypto::keys::*; -use duniter_module::{Currency, DuniterConf, DuniterConfV1, RequiredKeys, RequiredKeysContent}; +use duniter_module::{Currency, DuniterConf, ModuleId, RequiredKeys, RequiredKeysContent}; use rand::Rng; use serde::ser::{Serialize, SerializeStruct, Serializer}; use std::env; @@ -45,6 +47,93 @@ static USER_DATAS_FOLDER: &'static str = "durs-dev"; /// If no currency is specified by the user, is the currency will be chosen by default pub static DEFAULT_CURRRENCY: &'static str = "g1"; +#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)] +/// Duniter configuration v1 +pub struct DuRsConfV1 { + /// Currency + pub currency: Currency, + /// Duniter node unique identifier + pub my_node_id: u32, + /// Configuration of modules in json format (obtained from the conf.json file) + pub modules: serde_json::Value, + /// Disabled modules + pub disabled: Vec<ModuleId>, + /// Enabled modules + pub enabled: Vec<ModuleId>, +} + +impl Default for DuRsConfV1 { + fn default() -> Self { + DuRsConfV1 { + currency: Currency::Str(String::from("g1")), + my_node_id: generate_random_node_id(), + modules: serde_json::Value::Null, + disabled: Vec::with_capacity(0), + enabled: Vec::with_capacity(0), + } + } +} + +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +/// Duniter node configuration +pub enum DuRsConf { + /// Duniter node configuration v1 + V1(DuRsConfV1), + /// Duniter node configuration v2 + V2(), +} + +impl Default for DuRsConf { + fn default() -> Self { + DuRsConf::V1(DuRsConfV1::default()) + } +} + +impl DuniterConf for DuRsConf { + fn version(&self) -> usize { + match *self { + DuRsConf::V1(ref _conf_v1) => 1, + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } + fn currency(&self) -> Currency { + match *self { + DuRsConf::V1(ref conf_v1) => conf_v1.currency.clone(), + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } + fn set_currency(&mut self, new_currency: Currency) { + match *self { + DuRsConf::V1(ref mut conf_v1) => conf_v1.currency = new_currency, + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } + fn my_node_id(&self) -> u32 { + match *self { + DuRsConf::V1(ref conf_v1) => conf_v1.my_node_id, + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } + fn disabled_modules(&self) -> Vec<ModuleId> { + match *self { + DuRsConf::V1(ref conf_v1) => conf_v1.disabled.clone(), + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } + fn enabled_modules(&self) -> Vec<ModuleId> { + match *self { + DuRsConf::V1(ref conf_v1) => conf_v1.enabled.clone(), + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } + fn modules(&self) -> serde_json::Value { + match *self { + DuRsConf::V1(ref conf_v1) => conf_v1.modules.clone(), + _ => panic!("Fail to load duniter conf : conf version not supported !"), + } + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq)] /// Keypairs filled in by the user (via a file or by direct entry in the terminal). pub struct DuniterKeyPairs { @@ -173,7 +262,7 @@ pub fn get_profile_path(profile: &str) -> PathBuf { } /// Load configuration. -pub fn load_conf(profile: &str) -> (DuniterConf, DuniterKeyPairs) { +pub fn load_conf(profile: &str) -> (DuRsConf, DuniterKeyPairs) { let mut profile_path = get_profile_path(profile); // Load conf @@ -190,15 +279,7 @@ pub fn load_conf(profile: &str) -> (DuniterConf, DuniterKeyPairs) { } /// Load configuration. at specified path -pub fn load_conf_at_path(profile: &str, profile_path: &PathBuf) -> (DuniterConf, DuniterKeyPairs) { - // Default conf - let mut conf = DuniterConfV1 { - profile: String::from(profile), - currency: Currency::Str(DEFAULT_CURRRENCY.to_string()), - my_node_id: generate_random_node_id(), - modules: serde_json::Value::Null, - }; - +pub fn load_conf_at_path(profile: &str, profile_path: &PathBuf) -> (DuRsConf, DuniterKeyPairs) { // Get KeyPairs let mut keypairs_path = profile_path.clone(); keypairs_path.push("keypairs.json"); @@ -249,13 +330,15 @@ pub fn load_conf_at_path(profile: &str, profile_path: &PathBuf) -> (DuniterConf, }; // Open conf file + let mut conf = DuRsConf::default(); let mut conf_path = profile_path.clone(); conf_path.push("conf.json"); if conf_path.as_path().exists() { if let Ok(mut f) = File::open(conf_path.as_path()) { let mut contents = String::new(); if f.read_to_string(&mut contents).is_ok() { - let json_conf: serde_json::Value = + conf = serde_json::from_str(&contents).expect("Conf: Fail to parse conf file !"); + /*let json_conf: serde_json::Value = serde_json::from_str(&contents).expect("Conf: Fail to parse conf file !"); if let Some(currency) = json_conf.get("currency") { conf.currency = Currency::Str( @@ -276,19 +359,18 @@ pub fn load_conf_at_path(profile: &str, profile_path: &PathBuf) -> (DuniterConf, }; if let Some(modules_conf) = json_conf.get("modules") { conf.modules = modules_conf.clone(); - }; + };*/ } } else { panic!("Fail to open conf file !"); } } else { // Create conf file with default conf - write_conf_file(&DuniterConf::V1(conf.clone())) - .expect("Fatal error : fail to write default conf file !"); + write_conf_file(profile, &conf).expect("Fatal error : fail to write default conf file !"); } // Return conf and keypairs - (DuniterConf::V1(conf), keypairs) + (conf, keypairs) } /// Save keypairs in profile folder @@ -309,11 +391,18 @@ pub fn write_keypairs_file( } /// Save configuration in profile folder -pub fn write_conf_file(conf: &DuniterConf) -> Result<(), std::io::Error> { - let mut conf_path = get_profile_path(&conf.profile()); +pub fn write_conf_file<DC: DuniterConf>(profile: &str, conf: &DC) -> Result<(), std::io::Error> { + let mut conf_path = get_profile_path(profile); conf_path.push("conf.json"); - match *conf { - DuniterConf::V1(ref conf_v1) => { + let mut f = try!(File::create(conf_path.as_path())); + f.write_all( + serde_json::to_string_pretty(conf) + .expect("Fatal error : fail to write default conf file !") + .as_bytes(), + )?; + f.sync_all()?; + /*match *conf { + DuRsConf::V1(ref conf_v1) => { let mut f = try!(File::create(conf_path.as_path())); try!( f.write_all( @@ -327,7 +416,7 @@ pub fn write_conf_file(conf: &DuniterConf) -> Result<(), std::io::Error> { _ => { panic!("Fatal error : Conf version is not supported !"); } - } + }*/ Ok(()) } diff --git a/conf/test/conf.json b/conf/test/conf.json index bf8541562facac5e99e634e0267272b2b254e6a4..64f117db007e568194dc5def0c4dc222b2290949 100644 --- a/conf/test/conf.json +++ b/conf/test/conf.json @@ -1,19 +1,25 @@ { - "currency": "g1", - "node_id": "357fb4b", - "modules": { - "tui": null, - "ws2p": { - "sync_peers": [{ - "pubkey": "D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx", - "ws2p_endpoints": ["WS2P c1c39a0a i3.ifee.fr 80 /ws2p"] - },{ - "pubkey": "BoZP6aqtErHjiKLosLrQxBafi4ATciyDZQ6XRQkNefqG", - "ws2p_endpoints": ["WS2P 15af24db g1.ifee.fr 80 /ws2p"] - },{ - "pubkey": "7v2J4badvfWQ6qwRdCwhhJfAsmKwoxRUNpJHiJHj7zef", - "ws2p_endpoints": ["WS2P b48824f0 g1.monnaielibreoccitanie.org 80 /ws2p"] - }] - } + "V1": { + "currency": { + "Str": "g1" + }, + "my_node_id": 1191678020, + "disabled": [], + "enabled": [], + "modules": { + "tui": null, + "ws2p": { + "sync_peers": [{ + "pubkey": "D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx", + "ws2p_endpoints": ["WS2P c1c39a0a i3.ifee.fr 80 /ws2p"] + },{ + "pubkey": "BoZP6aqtErHjiKLosLrQxBafi4ATciyDZQ6XRQkNefqG", + "ws2p_endpoints": ["WS2P 15af24db g1.ifee.fr 80 /ws2p"] + },{ + "pubkey": "7v2J4badvfWQ6qwRdCwhhJfAsmKwoxRUNpJHiJHj7zef", + "ws2p_endpoints": ["WS2P b48824f0 g1.monnaielibreoccitanie.org 80 /ws2p"] + }] + } + } } } diff --git a/core/Cargo.toml b/core/Cargo.toml index 34ca86adb66e3c8d828541a2f6fdefecb37a1b4b..a748b2c0c33057fd6c478ef8b601f81ce2c3a8cb 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -22,7 +22,6 @@ regex = "1.0.0" rust-crypto = "0.2.36" serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" simplelog = "0.5.2" sqlite = "0.23.9" threadpool = "1.7.1" diff --git a/core/lib.rs b/core/lib.rs index f7d815b167fa84c8b8fab2de6ce9410eb8fa1918..f61af82f94a1c4a177467e160b68e951ae55009c 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -33,14 +33,13 @@ extern crate duniter_conf; extern crate duniter_crypto; extern crate duniter_message; extern crate duniter_module; -extern crate serde_json; extern crate simplelog; extern crate sqlite; extern crate threadpool; use clap::{App, ArgMatches}; use duniter_blockchain::{BlockchainModule, DBExQuery, DBExTxQuery, DBExWotQuery}; -use duniter_conf::DuniterKeyPairs; +use duniter_conf::{DuRsConf, DuniterKeyPairs}; use duniter_message::DuniterMessage; use duniter_module::*; use log::Level; @@ -55,17 +54,19 @@ use threadpool::ThreadPool; #[derive(Debug)] /// Duniter Core Datas -pub struct DuniterCore { +pub struct DuniterCore<DC: DuniterConf> { /// Does the entered command require to launch server ? pub start: bool, /// Software name pub soft_name: &'static str, /// Soft version pub soft_version: &'static str, + /// User profile + pub profile: String, /// Keypairs pub keypairs: DuniterKeyPairs, /// Duniter configuration - pub conf: DuniterConf, + pub conf: DC, /// Run duration. Zero = infinite duration. pub run_duration_in_secs: u64, /// Sender channel of rooter thread @@ -76,9 +77,12 @@ pub struct DuniterCore { pub thread_pool: ThreadPool, } -impl DuniterCore { +impl DuniterCore<DuRsConf> { /// Instantiate Duniter classic node - pub fn new(soft_name: &'static str, soft_version: &'static str) -> Option<DuniterCore> { + pub fn new( + soft_name: &'static str, + soft_version: &'static str, + ) -> Option<DuniterCore<DuRsConf>> { DuniterCore::new_specialized_node(soft_name, soft_version, 0, vec![], vec![], None) } /// Instantiate Duniter specialize node @@ -89,7 +93,7 @@ impl DuniterCore { external_followers: Vec<mpsc::Sender<DuniterMessage>>, sup_apps: Vec<App<'a, 'b>>, sup_apps_fn: Option<&Fn(&str, &ArgMatches) -> ()>, - ) -> Option<DuniterCore> { + ) -> Option<DuniterCore<DuRsConf>> { // Get cli conf let yaml = load_yaml!("./cli/en.yml"); let cli_conf = App::from_yaml(yaml); @@ -115,6 +119,7 @@ impl DuniterCore { Some(start( soft_name, soft_version, + &profile, keypairs, conf, run_duration_in_secs, @@ -123,6 +128,7 @@ impl DuniterCore { } else if let Some(matches) = cli_args.subcommand_matches("sync_ts") { let ts_profile = matches.value_of("TS_PROFILE").unwrap_or("duniter_default"); sync_ts( + profile.as_str(), &conf, ts_profile, matches.is_present("cautious"), @@ -133,6 +139,7 @@ impl DuniterCore { let csv = matches.is_present("csv"); if let Some(distances_matches) = matches.subcommand_matches("distances") { dbex( + profile.as_str(), &conf, &DBExQuery::WotQuery(DBExWotQuery::AllDistances( distances_matches.is_present("reverse"), @@ -141,12 +148,14 @@ impl DuniterCore { } else if let Some(member_matches) = matches.subcommand_matches("member") { let uid = member_matches.value_of("UID").unwrap_or(""); dbex( + profile.as_str(), &conf, &DBExQuery::WotQuery(DBExWotQuery::MemberDatas(String::from(uid))), ); } else if let Some(members_matches) = matches.subcommand_matches("members") { if members_matches.is_present("expire") { dbex( + profile.as_str(), &conf, &DBExQuery::WotQuery(DBExWotQuery::ExpireMembers( members_matches.is_present("reverse"), @@ -155,6 +164,7 @@ impl DuniterCore { ); } else { dbex( + profile.as_str(), &conf, &DBExQuery::WotQuery(DBExWotQuery::ListMembers( members_matches.is_present("reverse"), @@ -164,6 +174,7 @@ impl DuniterCore { } else if let Some(balance_matches) = matches.subcommand_matches("balance") { let address = balance_matches.value_of("ADDRESS").unwrap_or(""); dbex( + &profile, &conf, &DBExQuery::TxQuery(DBExTxQuery::Balance(String::from(address))), ); @@ -235,6 +246,7 @@ impl DuniterCore { // Instantiate blockchain module and load is conf let mut blockchain_module = BlockchainModule::load_blockchain_conf( + &self.profile, &self.conf, RequiredKeysContent::MemberKeyPair(None), ); @@ -245,7 +257,7 @@ impl DuniterCore { } } /// Plug a module - pub fn plug<M: DuniterModule<DuniterMessage>>(&mut self) { + pub fn plug<M: DuniterModule<DuRsConf, DuniterMessage>>(&mut self) { if self.start { // Start module in a new thread let soft_name_clone = &(*self.soft_name); @@ -264,10 +276,12 @@ impl DuniterCore { }; let rooter_sender_clone = self.rooter_sender.clone(); let conf_clone = self.conf.clone(); + let profile_copy = self.profile.clone(); self.thread_pool.execute(move || { M::start( soft_name_clone, soft_version_clone, + &profile_copy, required_keys, &conf_clone, &module_conf, @@ -290,14 +304,15 @@ pub fn match_profile(cli_args: &ArgMatches) -> String { } /// Launch duniter server -pub fn start( +pub fn start<DC: DuniterConf>( soft_name: &'static str, soft_version: &'static str, + profile: &str, keypairs: DuniterKeyPairs, - conf: DuniterConf, + conf: DC, run_duration_in_secs: u64, external_followers: Vec<mpsc::Sender<DuniterMessage>>, -) -> DuniterCore { +) -> DuniterCore<DC> { info!("Starting Duniter-rs..."); // Create senders channel @@ -398,6 +413,7 @@ pub fn start( start: true, soft_name, soft_version, + profile: profile.to_string(), keypairs, conf, run_duration_in_secs, @@ -408,15 +424,21 @@ pub fn start( } /// Launch synchronisation from a duniter-ts database -pub fn sync_ts(conf: &DuniterConf, ts_profile: &str, cautious: bool, verif_inner_hash: bool) { +pub fn sync_ts<DC: DuniterConf>( + profile: &str, + conf: &DC, + ts_profile: &str, + cautious: bool, + verif_inner_hash: bool, +) { // Launch sync-ts - BlockchainModule::sync_ts(conf, ts_profile, cautious, verif_inner_hash); + BlockchainModule::sync_ts(profile, conf, ts_profile, cautious, verif_inner_hash); } /// Launch databases explorer -pub fn dbex(conf: &DuniterConf, query: &DBExQuery) { +pub fn dbex<DC: DuniterConf>(profile: &str, conf: &DC, query: &DBExQuery) { // Launch databases explorer - BlockchainModule::dbex(conf, query); + BlockchainModule::dbex(profile, conf, query); } /// Initialize logger diff --git a/dal/Cargo.toml b/dal/Cargo.toml index 626ac9d121774507169f2b131a424eae9f684be6..8a80ab5067c7ec193a8f1f441c2efe0515274ae1 100644 --- a/dal/Cargo.toml +++ b/dal/Cargo.toml @@ -22,7 +22,7 @@ rust-crypto = "0.2.36" regex = "1.0.0" serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" +serde_json = "1.0.20" [features] exp = [] diff --git a/dal/block.rs b/dal/block.rs index c9efb14adcc2da9327b03db892f7dd3ff96047fd..3e8c9fff22c79a6c8388afb6cef7a637a94447bd 100644 --- a/dal/block.rs +++ b/dal/block.rs @@ -1,6 +1,5 @@ extern crate rustbreak; extern crate serde; -extern crate serde_json; use super::constants::MAX_FORKS; use duniter_crypto::keys::*; diff --git a/dal/dal_requests.rs b/dal/dal_requests.rs index 851f458e382b5de09236b079ee4d9c3a33cd4bca..eb1f30a990d43567e2719b0e5e3284d954cc7cec 100644 --- a/dal/dal_requests.rs +++ b/dal/dal_requests.rs @@ -9,7 +9,7 @@ use duniter_documents::blockchain::v10::documents::{ use duniter_documents::{Blockstamp, Hash}; use std::collections::HashMap; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub enum DALReqPendings { AllPendingIdentyties(ModuleReqFullId, usize), AllPendingIdentytiesWithoutCerts(ModuleReqFullId, usize), diff --git a/dal/lib.rs b/dal/lib.rs index 2996d71959d74baad762e2ae4ab3d8f345d37ae2..29a15beeddff480ffd5ed361d210fd70b1db20d7 100644 --- a/dal/lib.rs +++ b/dal/lib.rs @@ -193,7 +193,8 @@ impl CurrencyV10DBs<FileBackend> { pub fn open(db_path: &PathBuf) -> CurrencyV10DBs<FileBackend> { CurrencyV10DBs { du_db: open_db::<DUsV10Datas>(&db_path, "du.db").expect("Fail to open DUsV10DB"), - tx_db: open_db::<TxV10Datas>(&db_path, "tx.db").expect("Fail to open TxV10DB"), + tx_db: open_db::<TxV10Datas>(&db_path, "tx.db") + .expect(&format!("Fail to open TxV10DB : {:?} ", db_path.as_path())), utxos_db: open_db::<UTXOsV10Datas>(&db_path, "sources.db") .expect("Fail to open UTXOsV10DB"), balances_db: open_db::<BalancesV10Datas>(&db_path, "balances.db") diff --git a/message/Cargo.toml b/message/Cargo.toml index 22215113b0f077a87fa8ef8e0cd81d09447de306..86b913c81609e6c2669212a9598aeca390d8025e 100644 --- a/message/Cargo.toml +++ b/message/Cargo.toml @@ -16,7 +16,6 @@ duniter-module = { path = "../module" } duniter-network = { path = "../network" } serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" [features] # Treat warnings as a build error. diff --git a/message/lib.rs b/message/lib.rs index 03539027ee88412e5a8af143d0864e425c7ef59a..92c657143783a32cdea26a080e08a032b08814e5 100644 --- a/message/lib.rs +++ b/message/lib.rs @@ -29,7 +29,6 @@ extern crate duniter_documents; extern crate duniter_module; extern crate duniter_network; extern crate serde; -extern crate serde_json; use std::sync::mpsc; @@ -46,8 +45,6 @@ use duniter_network::{NetworkEvent, NetworkRequest}; pub enum DuniterMessage { /// Brut text message Text(String), - /// Brut json message - Json(serde_json::Value), /// Brut binary message Binary(Vec<u8>), /// Subscriptions to the module feed diff --git a/module/Cargo.toml b/module/Cargo.toml index fac259acc0818ee663139dcd5931ee61662997b5..cdcc7302e05e6a9eb9e7429aee4aa80a4c6df2f5 100644 --- a/module/Cargo.toml +++ b/module/Cargo.toml @@ -13,7 +13,7 @@ duniter-crypto = { path = "../crypto" } duniter-documents = { path = "../documents" } serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" +serde_json = "1.0.20" [features] # Treat warnings as a build error. diff --git a/module/clippy.toml b/module/clippy.toml new file mode 100644 index 0000000000000000000000000000000000000000..1e5837c771e0a191552d45213377a9b856114fb4 --- /dev/null +++ b/module/clippy.toml @@ -0,0 +1 @@ +too-many-arguments-threshold = 10 \ No newline at end of file diff --git a/module/lib.rs b/module/lib.rs index 05e74ea3a70b93df07fa550fdb16671c44cfad52..7707d3127cebf2484cedd1e1ee2e03599caf5be6 100644 --- a/module/lib.rs +++ b/module/lib.rs @@ -23,16 +23,20 @@ unused_qualifications )] +#[macro_use] +extern crate serde_derive; + extern crate duniter_crypto; extern crate serde; extern crate serde_json; use duniter_crypto::keys::{KeyPair, KeyPairEnum}; -use serde::ser::{Serialize, SerializeStruct, Serializer}; +use serde::de::DeserializeOwned; +use serde::ser::{Serialize, Serializer}; use std::fmt::Debug; use std::sync::mpsc; -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash, Serialize)] /// Store Currency pub enum Currency { /// Currency in string format @@ -50,25 +54,17 @@ impl ToString for Currency { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash, Serialize)] /// Store module identifier -pub enum ModuleId { - /// Module in static str format because module name must be know at compile time - Str(&'static str), - /// Module in binary format - Bin([u8; 2]), -} +pub struct ModuleId(pub String); impl ToString for ModuleId { fn to_string(&self) -> String { - match *self { - ModuleId::Str(module_id_str) => String::from(module_id_str), - ModuleId::Bin(_) => panic!("ModuleId binary format is not implemented !"), - } + self.0.clone() } } -impl Serialize for ModuleId { +/*impl Serialize for ModuleId { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, @@ -79,7 +75,7 @@ impl Serialize for ModuleId { }; serializer.serialize_str(module_id_string.as_str()) } -} +}*/ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] /// Identifier of an inter-module request @@ -94,7 +90,7 @@ impl Serialize for ModuleReqId { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] /// Several modules can simultaneously send requests with the same identifier. /// To identify each request in a unique way, we must therefore also take into account the identifier of the module performing the request. pub struct ModuleReqFullId(pub ModuleId, pub ModuleReqId); @@ -114,90 +110,27 @@ impl ToString for ModuleReqFullId { } }*/ -#[derive(Debug, Clone, PartialEq)] -/// Duniter configuration v1 -pub struct DuniterConfV1 { - /// Name of datas folder in ~/.config/durs/ - pub profile: String, - /// Currency - pub currency: Currency, - /// Duniter node unique identifier - pub my_node_id: u32, - /// Configuration of modules in json format (obtained from the conf.json file) - pub modules: serde_json::Value, -} - -impl Serialize for DuniterConfV1 { - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where - S: Serializer, - { - let mut state = serializer.serialize_struct("DuniterConfV1", 3)?; - - // Currency - state.serialize_field("currency", self.currency.to_string().as_str())?; - - // Node id - state.serialize_field("node_id", &format!("{:x}", self.my_node_id))?; - - // Modules - state.serialize_field("modules", &self.modules)?; - - // End - state.end() - } -} - -#[derive(Debug, Clone, PartialEq)] -/// Duniter node configuration -pub enum DuniterConf { - /// Duniter node configuration v1 - V1(DuniterConfV1), - /// Duniter node configuration v2 - V2(), -} - -impl DuniterConf { - /// Get profile - pub fn profile(&self) -> String { - match *self { - DuniterConf::V1(ref conf_v1) => conf_v1.profile.clone(), - _ => panic!("Fail to load duniter conf : conf version not supported !"), - } - } +/// Duniter configuration +pub trait DuniterConf: Clone + Debug + Default + PartialEq + Serialize + DeserializeOwned { + /// Get conf version profile + fn version(&self) -> usize; /// Get currency - pub fn currency(&self) -> Currency { - match *self { - DuniterConf::V1(ref conf_v1) => conf_v1.currency.clone(), - _ => panic!("Fail to load duniter conf : conf version not supported !"), - } - } + fn currency(&self) -> Currency; /// Set currency - pub fn set_currency(&mut self, new_currency: Currency) { - match *self { - DuniterConf::V1(ref mut conf_v1) => conf_v1.currency = new_currency, - _ => panic!("Fail to load duniter conf : conf version not supported !"), - } - } + fn set_currency(&mut self, new_currency: Currency); /// Get node id - pub fn my_node_id(&self) -> u32 { - match *self { - DuniterConf::V1(ref conf_v1) => conf_v1.my_node_id, - _ => panic!("Fail to load duniter conf : conf version not supported !"), - } - } + fn my_node_id(&self) -> u32; + /// Get disabled modules + fn disabled_modules(&self) -> Vec<ModuleId>; + /// Get enabled modules + fn enabled_modules(&self) -> Vec<ModuleId>; /// Get modules conf - pub fn modules(&self) -> serde_json::Value { - match *self { - DuniterConf::V1(ref conf_v1) => conf_v1.modules.clone(), - _ => panic!("Fail to load duniter conf : conf version not supported !"), - } - } + fn modules(&self) -> serde_json::Value; } /// The different modules of Duniter-rs can exchange messages with the type of their choice, /// provided that this type implements the ModuleMessage trait. -pub trait ModuleMessage: Debug + Clone {} +pub trait ModuleMessage: Clone + Debug {} #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] /// Type returned by module initialization function @@ -259,7 +192,7 @@ pub enum ModulePriority { } /// All Duniter-rs modules must implement this trait. -pub trait DuniterModule<M: ModuleMessage> { +pub trait DuniterModule<DC: DuniterConf, M: ModuleMessage> { /// Returns the module identifier fn id() -> ModuleId; /// Returns the module priority @@ -272,8 +205,9 @@ pub trait DuniterModule<M: ModuleMessage> { fn start( soft_name: &str, soft_version: &str, + profile: &str, keys: RequiredKeysContent, - conf: &DuniterConf, + conf: &DC, module_conf: &serde_json::Value, main_sender: mpsc::Sender<RooterThreadMessage<M>>, load_conf_only: bool, diff --git a/network/Cargo.toml b/network/Cargo.toml index 940ce6a2afa3726da4b321223e00698198fc974f..91b638382602f067e4efdd106158fe6341727cb1 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -17,7 +17,7 @@ regex = "1.0.0" rust-crypto = "0.2.36" serde = "1.0.57" serde_derive = "1.0.57" -serde_json = "1.0.17" +serde_json = "1.0.20" [features] # Treat warnings as a build error. diff --git a/network/lib.rs b/network/lib.rs index 62d54d7fba9967f3b0998dbb1ffbcc816648ac54..60478a07f67c1d716d5ab809a98484600791cf0b 100644 --- a/network/lib.rs +++ b/network/lib.rs @@ -195,7 +195,7 @@ pub enum NetworkConsensusError { Fork(), } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] /// Type containing a request addressed to the network module pub enum NetworkRequest { /// Get a current block of a specific node @@ -222,7 +222,7 @@ impl NetworkRequest { | NetworkRequest::GetRequirementsPending(ref req_id, _, _) | NetworkRequest::GetConsensus(ref req_id) | NetworkRequest::GetHeadsCache(ref req_id) - | NetworkRequest::GetEndpoints(ref req_id) => *req_id, + | NetworkRequest::GetEndpoints(ref req_id) => req_id.clone(), } } /// Get request identitifier @@ -270,7 +270,7 @@ impl NetworkResponse { | NetworkResponse::Chunk(ref req_id, _, _) | NetworkResponse::PendingDocuments(ref req_id, _) | NetworkResponse::Consensus(ref req_id, _) - | NetworkResponse::HeadsCache(ref req_id, _) => *req_id, + | NetworkResponse::HeadsCache(ref req_id, _) => req_id.clone(), } } /// Get request identifier diff --git a/network/network_endpoint.rs b/network/network_endpoint.rs index 759088e1df601bfefc22737fabca266131eba354..9098f4c69fbd9d28e738f867bf258f4f12c97e1b 100644 --- a/network/network_endpoint.rs +++ b/network/network_endpoint.rs @@ -21,7 +21,6 @@ extern crate duniter_documents; extern crate duniter_module; extern crate regex; extern crate serde; -extern crate serde_json; use self::regex::Regex; use super::{NodeFullId, NodeUUID}; diff --git a/network/network_peer.rs b/network/network_peer.rs index c2eb4d33e6e51fab1d85334a173247f37268044b..b539af01dcaaff77a0b3f9e4557f0065aa7b5649 100644 --- a/network/network_peer.rs +++ b/network/network_peer.rs @@ -20,7 +20,6 @@ extern crate duniter_crypto; extern crate duniter_documents; extern crate duniter_module; extern crate serde; -extern crate serde_json; use super::network_endpoint::NetworkEndpoint; use duniter_crypto::keys::*; diff --git a/src/main.rs b/src/main.rs index 938abebfb094f4ad0d98440ec975644e4859872f..2c9748b753cca19bed36b90d3c2a173191380309 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,12 +21,13 @@ trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces, unused_qualifications )] - +extern crate duniter_conf; extern crate duniter_core; #[cfg(feature = "tui")] extern crate duniter_tui; extern crate duniter_ws2p; +pub use duniter_conf::DuRsConf; pub use duniter_core::DuniterCore; #[cfg(feature = "tui")] pub use duniter_tui::TuiModule; @@ -39,7 +40,7 @@ fn main() { let soft_version = env!("CARGO_PKG_VERSION"); // Run duniter core - if let Some(mut duniter_core) = DuniterCore::new(soft_name, soft_version) { + if let Some(mut duniter_core) = DuniterCore::<DuRsConf>::new(soft_name, soft_version) { //duniter_core.plug::<DasaModule>(); //duniter_core.plug::<GuiModule>(); //duniter_core.plug::<GvaModule>(); @@ -53,8 +54,8 @@ fn main() { /// Plug TUI Module #[cfg(feature = "tui")] -fn plug_tui_module(duniter_core: &mut DuniterCore) { +fn plug_tui_module(duniter_core: &mut DuniterCore<DuRsConf>) { duniter_core.plug::<TuiModule>(); } #[cfg(not(feature = "tui"))] -fn plug_tui_module(_duniter_core: &mut DuniterCore) {} +fn plug_tui_module(_duniter_core: &mut DuniterCore<DuRsConf>) {} diff --git a/tui/Cargo.toml b/tui/Cargo.toml index 7dae06ddce856515bf73c89b9b93e6bcb2380923..5b3bc0a26cafa512d153ffeb4924df0a87811b42 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -17,7 +17,7 @@ duniter-message = { path = "../message" } duniter-module = { path = "../module" } duniter-network = { path = "../network" } log = "0.4.1" -serde_json = "1.0.17" +serde_json = "1.0.20" termion = "1.5.1" [features] diff --git a/tui/lib.rs b/tui/lib.rs index 6458841de95c0f03a62e2c183ae6b692528d43e1..55028786e5443704466389bbab4b07d6b0506fce 100644 --- a/tui/lib.rs +++ b/tui/lib.rs @@ -36,6 +36,7 @@ extern crate duniter_network; extern crate serde_json; extern crate termion; +use duniter_conf::DuRsConf; use duniter_dal::dal_event::DALEvent; use duniter_message::DuniterMessage; use duniter_module::*; @@ -347,9 +348,9 @@ impl Default for TuiModule { } } -impl DuniterModule<DuniterMessage> for TuiModule { +impl DuniterModule<DuRsConf, DuniterMessage> for TuiModule { fn id() -> ModuleId { - ModuleId::Str("tui") + ModuleId(String::from("tui")) } fn priority() -> ModulePriority { ModulePriority::Recommended() @@ -363,8 +364,9 @@ impl DuniterModule<DuniterMessage> for TuiModule { fn start( _soft_name: &str, _soft_version: &str, + _profile: &str, _keys: RequiredKeysContent, - _conf: &DuniterConf, + _conf: &DuRsConf, module_conf: &serde_json::Value, main_sender: mpsc::Sender<RooterThreadMessage<DuniterMessage>>, load_conf_only: bool, diff --git a/ws2p/Cargo.toml b/ws2p/Cargo.toml index 89e831f711682a8f1323dee5eb20e6f7620a9ef8..2b4b0f4debae043a864f4cab75c1652ddbc8ce63 100644 --- a/ws2p/Cargo.toml +++ b/ws2p/Cargo.toml @@ -25,7 +25,7 @@ rust-crypto = "0.2.36" sqlite = "0.23.9" serde = "1.0.24" serde_derive = "1.0.24" -serde_json = "1.0.9" +serde_json = "1.0.20" ws = "0.7.6" [features] diff --git a/ws2p/datas.rs b/ws2p/datas.rs index c9341419c3cf86284262d715736b06c6410bc96f..216b9f448b4a7797e56ca92ecddbac88c3dfc0fb 100644 --- a/ws2p/datas.rs +++ b/ws2p/datas.rs @@ -57,7 +57,7 @@ impl WS2PModuleDatas { Ok(conn) } pub fn parse_ws2p_conf( - duniter_conf: &DuniterConf, + duniter_conf: &DuRsConf, ws2p_json_conf: &serde_json::Value, ) -> WS2PConf { let mut sync_endpoints = Vec::new(); @@ -396,7 +396,7 @@ impl WS2PModuleDatas { { return WS2PSignal::ReqResponse( req_id, - *ws2p_request, + ws2p_request.clone(), *recipient_fulld_id, response, ); @@ -549,7 +549,11 @@ impl WS2PModuleDatas { ))?; self.requests_awaiting_response.insert( ws2p_request.get_req_id(), - (*ws2p_request, *receiver_ws2p_full_id, SystemTime::now()), + ( + ws2p_request.clone(), + *receiver_ws2p_full_id, + SystemTime::now(), + ), ); debug!( "send request {} to {}", diff --git a/ws2p/lib.rs b/ws2p/lib.rs index 035c2fc40ecbe836d33f1dce55697d6f97d0e931..5a25d52b0e902eb6aed2987d246e87d22c84ff56 100644 --- a/ws2p/lib.rs +++ b/ws2p/lib.rs @@ -59,6 +59,7 @@ use ack_message::WS2PAckMessageV1; use connect_message::WS2PConnectMessageV1; use constants::*; use datas::*; +use duniter_conf::DuRsConf; use duniter_crypto::keys::*; use duniter_dal::dal_event::DALEvent; use duniter_dal::dal_requests::{DALReqBlockchain, DALRequest, DALResBlockchain, DALResponse}; @@ -146,9 +147,9 @@ impl Default for WS2PModule { } } -impl DuniterModule<DuniterMessage> for WS2PModule { +impl DuniterModule<DuRsConf, DuniterMessage> for WS2PModule { fn id() -> ModuleId { - ModuleId::Str("ws2p") + ModuleId(String::from("ws2p")) } fn priority() -> ModulePriority { ModulePriority::Essential() @@ -170,8 +171,9 @@ impl DuniterModule<DuniterMessage> for WS2PModule { fn start( soft_name: &str, soft_version: &str, + profile: &str, keys: RequiredKeysContent, - duniter_conf: &DuniterConf, + duniter_conf: &DuRsConf, module_conf: &serde_json::Value, rooter_sender: mpsc::Sender<RooterThreadMessage<DuniterMessage>>, load_conf_only: bool, @@ -246,8 +248,7 @@ impl DuniterModule<DuniterMessage> for WS2PModule { }); // open ws2p bdd - let mut db_path = - duniter_conf::datas_path(duniter_conf.profile().as_str(), &duniter_conf.currency()); + let mut db_path = duniter_conf::datas_path(profile, &duniter_conf.currency()); db_path.push("ws2p.db"); let db = WS2PModuleDatas::open_db(&db_path).expect("Fatal error : fail to open WS2P DB !"); @@ -367,7 +368,10 @@ impl DuniterModule<DuniterMessage> for WS2PModule { .send_request_to_specific_node( &real_receiver, &NetworkRequest::GetBlocks( - *req_id, *receiver, *count, *from, + req_id.clone(), + *receiver, + *count, + *from, ), ); } @@ -376,7 +380,10 @@ impl DuniterModule<DuniterMessage> for WS2PModule { .send_request_to_specific_node( &receiver, &NetworkRequest::GetBlocks( - *req_id, *receiver, *count, *from, + req_id.clone(), + *receiver, + *count, + *from, ), ); }