From 2de7fd727af26eed1df75cc4a6c6b40b908d0f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= <elois@ifee.fr> Date: Sat, 1 Dec 2018 22:00:46 +0100 Subject: [PATCH] [ref] rework DursMsg enum & move dal to blockchain sublib --- .gitlab-ci.yml | 6 +- Cargo.lock | 142 ++++--- Cargo.toml | 4 +- bin/durs-server/Cargo.toml | 2 +- bin/durs-server/src/main.rs | 4 +- lib/core/core/Cargo.toml | 4 +- lib/core/core/lib.rs | 10 +- lib/core/core/router.rs | 208 +++++----- lib/core/message/Cargo.toml | 7 +- .../dal_event.rs => core/message/events.rs} | 19 +- lib/core/message/lib.rs | 130 +++---- lib/core/message/requests.rs | 61 +++ .../message/responses.rs} | 78 ++-- lib/core/module/lib.rs | 2 + lib/core/network/events.rs | 2 + lib/modules/blockchain/Cargo.toml | 29 -- .../blockchain/blockchain-dal}/Cargo.toml | 12 +- .../blockchain/blockchain-dal}/balance.rs | 0 .../blockchain/blockchain-dal}/block.rs | 0 .../blockchain/blockchain-dal}/certs.rs | 0 .../blockchain/blockchain-dal}/clippy.toml | 0 .../blockchain/blockchain-dal}/constants.rs | 0 .../blockchain-dal}/currency_params.rs | 0 .../blockchain/blockchain-dal}/identity.rs | 0 .../blockchain/blockchain-dal}/lib.rs | 6 - .../blockchain-dal}/parsers/certifications.rs | 0 .../blockchain/blockchain-dal}/parsers/mod.rs | 0 .../blockchain-dal}/parsers/revoked.rs | 0 .../blockchain/blockchain-dal}/sources.rs | 0 .../blockchain/blockchain-dal}/tools.rs | 0 .../blockchain-dal}/writers/block.rs | 0 .../blockchain-dal}/writers/certification.rs | 0 .../blockchain-dal}/writers/dividend.rs | 0 .../blockchain-dal}/writers/identity.rs | 0 .../blockchain/blockchain-dal}/writers/mod.rs | 0 .../blockchain-dal}/writers/requests.rs | 0 .../blockchain-dal}/writers/transaction.rs | 0 lib/modules/blockchain/blockchain/Cargo.toml | 29 ++ .../{ => blockchain}/apply_valid_block.rs | 10 +- .../{ => blockchain}/check_and_apply_block.rs | 17 +- .../blockchain/{ => blockchain}/clippy.toml | 0 .../blockchain/{ => blockchain}/dbex.rs | 16 +- .../blockchain/{ => blockchain}/lib.rs | 348 +++++++++-------- .../{ => blockchain}/revert_block.rs | 10 +- .../blockchain/{ => blockchain}/sync.rs | 20 +- .../blockchain/{ => blockchain}/ts_parsers.rs | 0 lib/modules/skeleton/Cargo.toml | 3 +- lib/modules/skeleton/lib.rs | 46 ++- lib/modules/tui/Cargo.toml | 5 +- lib/modules/tui/lib.rs | 126 +++--- lib/modules/ws2p-v1-legacy/Cargo.toml | 3 +- lib/modules/ws2p-v1-legacy/datas.rs | 40 +- lib/modules/ws2p-v1-legacy/lib.rs | 366 +++++++++--------- lib/modules/ws2p/ws2p/Cargo.toml | 2 +- lib/modules/ws2p/ws2p/lib.rs | 4 +- lib/modules/ws2p/ws2p/src/lib.rs | 4 +- 56 files changed, 911 insertions(+), 864 deletions(-) rename lib/{tools/dal/dal_event.rs => core/message/events.rs} (71%) create mode 100644 lib/core/message/requests.rs rename lib/{tools/dal/dal_requests.rs => core/message/responses.rs} (62%) delete mode 100644 lib/modules/blockchain/Cargo.toml rename lib/{tools/dal => modules/blockchain/blockchain-dal}/Cargo.toml (53%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/balance.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/block.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/certs.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/clippy.toml (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/constants.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/currency_params.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/identity.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/lib.rs (99%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/parsers/certifications.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/parsers/mod.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/parsers/revoked.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/sources.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/tools.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/block.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/certification.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/dividend.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/identity.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/mod.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/requests.rs (100%) rename lib/{tools/dal => modules/blockchain/blockchain-dal}/writers/transaction.rs (100%) create mode 100644 lib/modules/blockchain/blockchain/Cargo.toml rename lib/modules/blockchain/{ => blockchain}/apply_valid_block.rs (97%) rename lib/modules/blockchain/{ => blockchain}/check_and_apply_block.rs (92%) rename lib/modules/blockchain/{ => blockchain}/clippy.toml (100%) rename lib/modules/blockchain/{ => blockchain}/dbex.rs (95%) rename lib/modules/blockchain/{ => blockchain}/lib.rs (72%) rename lib/modules/blockchain/{ => blockchain}/revert_block.rs (97%) rename lib/modules/blockchain/{ => blockchain}/sync.rs (97%) rename lib/modules/blockchain/{ => blockchain}/ts_parsers.rs (100%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f558ee5e..5605205b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -101,9 +101,9 @@ tests:win64:stable: - cargo test --package duniter-module --target=x86_64-pc-windows-gnu - cargo test --package duniter-network --target=x86_64-pc-windows-gnu - cargo test --package duniter-conf --target=x86_64-pc-windows-gnu - - cargo test --package duniter-dal --target=x86_64-pc-windows-gnu - - cargo test --package duniter-message --target=x86_64-pc-windows-gnu - - cargo test --package duniter-blockchain --target=x86_64-pc-windows-gnu + - cargo test --package durs-blockchain-dal --target=x86_64-pc-windows-gnu + - cargo test --package durs-message --target=x86_64-pc-windows-gnu + - cargo test --package durs-blockchain --target=x86_64-pc-windows-gnu - cargo test --package duniter-core --target=x86_64-pc-windows-gnu #- cargo test --package durs-skeleton-module --target=x86_64-pc-windows-gnu diff --git a/Cargo.lock b/Cargo.lock index b5bc2225..6963bf7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -243,30 +243,6 @@ dependencies = [ "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "duniter-blockchain" -version = "0.1.0-a0.1" -dependencies = [ - "dirs 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "dubp-documents 0.11.0", - "duniter-conf 0.1.0-a0.1", - "duniter-dal 0.1.0-a0.1", - "duniter-message 0.1.0-a0.1", - "duniter-module 0.1.0-a0.1", - "duniter-network 0.1.0-a0.1", - "dup-crypto 0.5.0", - "durs-network-documents 0.3.0", - "durs-wot 0.8.0-a0.9", - "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rustbreak 2.0.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.21 (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)", -] - [[package]] name = "duniter-conf" version = "0.1.0-a0.1" @@ -287,12 +263,12 @@ name = "duniter-core" version = "0.1.0-a0.1" dependencies = [ "dirs 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "duniter-blockchain 0.1.0-a0.1", "duniter-conf 0.1.0-a0.1", - "duniter-message 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", + "durs-blockchain 0.1.0-a0.1", + "durs-message 0.1.0-a0.1", "durs-network-documents 0.3.0", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "log-panics 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -305,28 +281,24 @@ dependencies = [ ] [[package]] -name = "duniter-dal" +name = "duniter-module" version = "0.1.0-a0.1" dependencies = [ "dubp-documents 0.11.0", - "duniter-module 0.1.0-a0.1", "dup-crypto 0.5.0", - "durs-wot 0.8.0-a0.9", - "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rustbreak 2.0.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)", + "durs-network-documents 0.3.0", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "duniter-message" +name = "duniter-network" version = "0.1.0-a0.1" dependencies = [ "dubp-documents 0.11.0", - "duniter-dal 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", - "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", "durs-network-documents 0.3.0", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", @@ -335,73 +307,80 @@ dependencies = [ ] [[package]] -name = "duniter-module" -version = "0.1.0-a0.1" +name = "dup-crypto" +version = "0.5.0" dependencies = [ - "dubp-documents 0.11.0", - "dup-crypto 0.5.0", - "durs-network-documents 0.3.0", + "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto-wasm 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "durs" +version = "0.1.0-a1" +dependencies = [ + "duniter-core 0.1.0-a0.1", + "durs-tui 0.1.0-a0.1", + "durs-ws2p 0.1.0-a0.1", + "durs-ws2p-v1-legacy 0.1.0-a0.1", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "duniter-network" +name = "durs-blockchain" version = "0.1.0-a0.1" dependencies = [ + "dirs 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dubp-documents 0.11.0", + "duniter-conf 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", + "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", + "durs-blockchain-dal 0.1.0-a0.1", + "durs-message 0.1.0-a0.1", "durs-network-documents 0.3.0", + "durs-wot 0.8.0-a0.9", + "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustbreak 2.0.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.21 (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)", ] [[package]] -name = "duniter-tui" +name = "durs-blockchain-dal" version = "0.1.0-a0.1" dependencies = [ "dubp-documents 0.11.0", - "duniter-conf 0.1.0-a0.1", - "duniter-dal 0.1.0-a0.1", - "duniter-message 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", - "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", - "durs-network-documents 0.3.0", + "durs-wot 0.8.0-a0.9", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustbreak 2.0.0-rc3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", - "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "dup-crypto" -version = "0.5.0" +name = "durs-message" +version = "0.1.0-a0.1" dependencies = [ - "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", - "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rust-crypto-wasm 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "dubp-documents 0.11.0", + "duniter-module 0.1.0-a0.1", + "duniter-network 0.1.0-a0.1", + "dup-crypto 0.5.0", + "durs-network-documents 0.3.0", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "durs" -version = "0.1.0-a1" -dependencies = [ - "duniter-core 0.1.0-a0.1", - "duniter-tui 0.1.0-a0.1", - "durs-ws2p 0.1.0-a0.1", - "durs-ws2p-v1-legacy 0.1.0-a0.1", - "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -426,16 +405,34 @@ name = "durs-skeleton" version = "0.1.0" dependencies = [ "duniter-conf 0.1.0-a0.1", - "duniter-dal 0.1.0-a0.1", - "duniter-message 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", + "durs-message 0.1.0-a0.1", + "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "durs-tui" +version = "0.1.0-a0.1" +dependencies = [ + "dubp-documents 0.11.0", + "duniter-conf 0.1.0-a0.1", + "duniter-module 0.1.0-a0.1", + "duniter-network 0.1.0-a0.1", + "dup-crypto 0.5.0", + "durs-message 0.1.0-a0.1", + "durs-network-documents 0.3.0", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -456,10 +453,10 @@ dependencies = [ "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "dubp-documents 0.11.0", "duniter-conf 0.1.0-a0.1", - "duniter-message 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", + "durs-message 0.1.0-a0.1", "durs-network-documents 0.3.0", "durs-ws2p-messages 0.1.0-a0.1", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -493,11 +490,10 @@ dependencies = [ "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "dubp-documents 0.11.0", "duniter-conf 0.1.0-a0.1", - "duniter-dal 0.1.0-a0.1", - "duniter-message 0.1.0-a0.1", "duniter-module 0.1.0-a0.1", "duniter-network 0.1.0-a0.1", "dup-crypto 0.5.0", + "durs-message 0.1.0-a0.1", "durs-network-documents 0.3.0", "durs-wot 0.8.0-a0.9", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index bfeb8090..400aeae3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,14 +6,14 @@ members = [ "lib/core/message", "lib/core/module", "lib/core/network", - "lib/modules/blockchain", + "lib/modules/blockchain/blockchain", + "lib/modules/blockchain/blockchain-dal", "lib/modules/skeleton", "lib/modules/tui", "lib/modules/ws2p-v1-legacy", "lib/modules/ws2p/ws2p", "lib/modules/ws2p/ws2p-messages", "lib/tools/crypto", - "lib/tools/dal", "lib/tools/documents", "lib/tools/network-documents", "lib/tools/wot", diff --git a/bin/durs-server/Cargo.toml b/bin/durs-server/Cargo.toml index 8ba372c4..c6c59d30 100644 --- a/bin/durs-server/Cargo.toml +++ b/bin/durs-server/Cargo.toml @@ -13,7 +13,7 @@ durs-ws2p-v1-legacy = { path = "../../lib/modules/ws2p-v1-legacy" } structopt= "0.2.*" [target.'cfg(unix)'.dependencies] -duniter-tui = { path = "../../lib/modules/tui" } +durs-tui = { path = "../../lib/modules/tui" } [features] ssl = ["durs-ws2p-v1-legacy/ssl"] diff --git a/bin/durs-server/src/main.rs b/bin/durs-server/src/main.rs index 05ae5d3f..175f5ea7 100644 --- a/bin/durs-server/src/main.rs +++ b/bin/durs-server/src/main.rs @@ -32,7 +32,7 @@ extern crate duniter_core; #[cfg(unix)] -extern crate duniter_tui; +extern crate durs_tui; //extern crate durs_skeleton; extern crate durs_ws2p_v1_legacy; //extern crate durs_ws2p; @@ -40,7 +40,7 @@ extern crate structopt; pub use duniter_core::{cli::DursOpt, DuRsConf, DuniterCore, UserCommand}; #[cfg(unix)] -pub use duniter_tui::TuiModule; +pub use durs_tui::TuiModule; //pub use durs_skeleton::SkeletonModule; pub use durs_ws2p_v1_legacy::WS2PModule; //pub use durs_ws2p::WS2Pv2Module; diff --git a/lib/core/core/Cargo.toml b/lib/core/core/Cargo.toml index 4183713a..d1922462 100644 --- a/lib/core/core/Cargo.toml +++ b/lib/core/core/Cargo.toml @@ -10,10 +10,10 @@ path = "lib.rs" [dependencies] dirs = "1.0.2" -duniter-blockchain = { path = "../../modules/blockchain" } +durs-blockchain = { path = "../../modules/blockchain/blockchain" } duniter-conf = { path = "../conf" } dup-crypto = { path = "../../tools/crypto" } -duniter-message = { path = "../message" } +durs-message = { path = "../message" } duniter-module = { path = "../module" } duniter-network = { path = "../network" } durs-network-documents = { path = "../../tools/network-documents" } diff --git a/lib/core/core/lib.rs b/lib/core/core/lib.rs index cb1d8cf4..b83cdb66 100644 --- a/lib/core/core/lib.rs +++ b/lib/core/core/lib.rs @@ -34,12 +34,12 @@ extern crate log; extern crate structopt; extern crate dirs; -extern crate duniter_blockchain; extern crate duniter_conf; -extern crate duniter_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; +extern crate durs_blockchain; +extern crate durs_message; extern crate durs_network_documents; extern crate log_panics; extern crate serde_json; @@ -50,11 +50,11 @@ pub mod change_conf; pub mod cli; pub mod router; -use duniter_blockchain::{BlockchainModule, DBExQuery, DBExTxQuery, DBExWotQuery}; pub use duniter_conf::{ChangeGlobalConf, DuRsConf, DuniterKeyPairs, KEYPAIRS_FILENAME}; -use duniter_message::*; use duniter_module::*; use duniter_network::{NetworkModule, SyncEndpoint, SyncParams}; +use durs_blockchain::{BlockchainModule, DBExQuery, DBExTxQuery, DBExWotQuery}; +use durs_message::*; use log::Level; use simplelog::*; //use std::error::Error; @@ -229,7 +229,7 @@ impl<'a, 'b: 'a> DuniterCore<'b, 'a, DuRsConf> { &mut self, sup_apps: Vec<App<'a, 'b>>, sup_apps_fn: Option<&Fn(&str, &ArgMatches) -> bool>, - external_followers: Vec<mpsc::Sender<DursMsgContent>>, + external_followers: Vec<mpsc::Sender<DursMsg>>, ) -> bool { // Inject core subcommands //let core_cli_conf = inject_core_subcommands(self.cli_conf.0.clone()); diff --git a/lib/core/core/router.rs b/lib/core/core/router.rs index 51c3758a..98cbfd2e 100644 --- a/lib/core/core/router.rs +++ b/lib/core/core/router.rs @@ -17,8 +17,8 @@ use duniter_conf; use duniter_conf::DuRsConf; -use duniter_message::*; use duniter_module::*; +use durs_message::*; use durs_network_documents::network_endpoint::EndpointEnum; use std::collections::HashMap; use std::sync::mpsc; @@ -29,16 +29,23 @@ use std::time::SystemTime; static MAX_REGISTRATION_DELAY: &'static u64 = &20; +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +enum DursMsgReceiver { + Role(ModuleRole), + Event(ModuleEvent), + One(ModuleStaticName), +} + /// Start broadcasting thread fn start_broadcasting_thread( start_time: SystemTime, run_duration_in_secs: u64, receiver: &mpsc::Receiver<RouterThreadMessage<DursMsg>>, - external_followers: &[mpsc::Sender<DursMsgContent>], + _external_followers: &[mpsc::Sender<DursMsg>], ) { // Define variables let mut modules_senders: HashMap<ModuleStaticName, mpsc::Sender<DursMsg>> = HashMap::new(); - let mut pool_msgs: HashMap<DursMsgReceiver, Vec<DursMsgContent>> = HashMap::new(); + let mut pool_msgs: HashMap<DursMsgReceiver, Vec<DursMsg>> = HashMap::new(); let mut events_subscriptions: HashMap<ModuleEvent, Vec<ModuleStaticName>> = HashMap::new(); let mut roles: HashMap<ModuleRole, Vec<ModuleStaticName>> = HashMap::new(); let mut registrations_count = 0; @@ -69,14 +76,12 @@ fn start_broadcasting_thread( .get(&DursMsgReceiver::Event(event)) .unwrap_or(&Vec::with_capacity(0)) { - module_sender - .send(DursMsg(DursMsgReceiver::Event(event), msg.clone())) - .unwrap_or_else(|_| { - panic!( - "Fatal error: fail to relay DursMsg to {:?} !", - module_static_name - ) - }); + module_sender.send(msg.clone()).unwrap_or_else(|_| { + panic!( + "Fatal error: fail to relay DursMsg to {:?} !", + module_static_name + ) + }); } // Store event subscription events_subscriptions @@ -91,14 +96,12 @@ fn start_broadcasting_thread( .get(&DursMsgReceiver::Role(role)) .unwrap_or(&Vec::with_capacity(0)) { - module_sender - .send(DursMsg(DursMsgReceiver::Role(role), msg.clone())) - .unwrap_or_else(|_| { - panic!( - "Fatal error: fail to relay DursMsg to {:?} !", - module_static_name - ) - }); + module_sender.send(msg.clone()).unwrap_or_else(|_| { + panic!( + "Fatal error: fail to relay DursMsg to {:?} !", + module_static_name + ) + }); } // Store sender roles roles @@ -133,10 +136,7 @@ fn start_broadcasting_thread( .to_vec(); // Send endpoints to receivers send_msg_to_several_receivers( - DursMsg( - DursMsgReceiver::Role(ModuleRole::InterNodesNetwork), - DursMsgContent::Endpoints(local_node_endpoints.clone()), - ), + DursMsg::ModulesEndpoints(local_node_endpoints.clone()), &receivers, &modules_senders, ); @@ -144,45 +144,10 @@ fn start_broadcasting_thread( // Add this sender to modules_senders modules_senders.insert(module_static_name, module_sender); } - RouterThreadMessage::ModuleMessage(msg) => match msg.0 { - DursMsgReceiver::One(_) => {} - DursMsgReceiver::All => { - for (module_static_name, module_sender) in &modules_senders { - module_sender.send(msg.clone()).unwrap_or_else(|_| { - panic!( - "Fatal error: fail to relay DursMsg to {:?} !", - module_static_name - ) - }); - } - // Detect stop message - let stop = if let DursMsgContent::Stop() = msg.1 { - true - } else { - false - }; - // Send message to external followers - for external_follower in external_followers { - external_follower.send(msg.1.clone()).expect( - "Fatal error: fail to relay DursMsg to external followers !", - ); - } - // Send message to all modules - send_msg_to_several_receivers( - msg, - &modules_senders - .keys() - .cloned() - .collect::<Vec<ModuleStaticName>>(), - &modules_senders, - ); - // Stop thread if its requested - if stop { - break; - } - } - DursMsgReceiver::Event(event) => { - // the node to be started less than 20 seconds ago, + RouterThreadMessage::ModuleMessage(msg) => match msg { + DursMsg::Stop => break, + DursMsg::Event { event_type, .. } => { + // the node to be started less than MAX_REGISTRATION_DELAY seconds ago, // keep the message in memory to be able to send it back to modules not yet plugged store_msg_in_pool( start_time, @@ -192,14 +157,14 @@ fn start_broadcasting_thread( ); // Get list of receivers let receivers = events_subscriptions - .get(&event) + .get(&event_type) .unwrap_or(&Vec::with_capacity(0)) .to_vec(); // Send msg to receivers send_msg_to_several_receivers(msg, &receivers, &modules_senders) } - DursMsgReceiver::Role(role) => { - // If the node to be started less than 20 seconds ago, + DursMsg::Request { req_to: role, .. } => { + // If the node to be started less than MAX_REGISTRATION_DELAY seconds ago, // keep the message in memory to be able to send it back to modules not yet plugged store_msg_in_pool( start_time, @@ -213,6 +178,7 @@ fn start_broadcasting_thread( // Send msg to receivers send_msg_to_several_receivers(msg, &receivers, &modules_senders) } + _ => {} // Others DursMsg variants }, } } @@ -241,11 +207,11 @@ fn start_broadcasting_thread( } /// Start conf thread -fn start_conf_thread(profile: &str, mut conf: DuRsConf, receiver: &mpsc::Receiver<DursMsgContent>) { +fn start_conf_thread(profile: &str, mut conf: DuRsConf, receiver: &mpsc::Receiver<DursMsg>) { loop { match receiver.recv() { Ok(msg) => { - if let DursMsgContent::SaveNewModuleConf(module_static_name, new_json_conf) = msg { + if let DursMsg::SaveNewModuleConf(module_static_name, new_json_conf) = msg { conf.set_module_conf(module_static_name.to_string(), new_json_conf); duniter_conf::write_conf_file(&profile, &conf) .expect("Fail to write new module conf in conf file ! "); @@ -276,7 +242,7 @@ fn send_msg_to_several_receivers( }); } } - // Send message by move to the last module to be revceive + // Send message by move to the last module to be receive if !receivers.is_empty() { if let Some(module_sender) = modules_senders.get(&receivers[0]) { module_sender.send(msg).unwrap_or_else(|_| { @@ -286,22 +252,30 @@ fn send_msg_to_several_receivers( } } -/// If the node to be started less than 20 seconds ago, +/// If the node to be started less than MAX_REGISTRATION_DELAY seconds ago, /// keep the message in memory to be able to send it back to modules not yet plugged fn store_msg_in_pool( start_time: SystemTime, run_duration_in_secs: u64, msg: DursMsg, - pool_msgs: &mut HashMap<DursMsgReceiver, Vec<DursMsgContent>>, + pool_msgs: &mut HashMap<DursMsgReceiver, Vec<DursMsg>>, ) { if run_duration_in_secs > 0 && SystemTime::now() .duration_since(start_time) .expect("Duration error !") .as_secs() - < 20 + < *MAX_REGISTRATION_DELAY { - pool_msgs.entry(msg.0).or_insert_with(Vec::new).push(msg.1); + let msg_recv = match msg { + DursMsg::Event { event_type, .. } => Some(DursMsgReceiver::Event(event_type)), + DursMsg::Request { req_to, .. } => Some(DursMsgReceiver::Role(req_to)), + DursMsg::Response { res_to, .. } => Some(DursMsgReceiver::One(res_to)), + _ => None, + }; + if let Some(msg_recv) = msg_recv { + pool_msgs.entry(msg_recv).or_insert_with(Vec::new).push(msg); + } } else if !pool_msgs.is_empty() { // Clear pool_msgs pool_msgs.clear(); @@ -313,7 +287,7 @@ pub fn start_router( run_duration_in_secs: u64, profile: String, conf: DuRsConf, - external_followers: Vec<mpsc::Sender<DursMsgContent>>, + external_followers: Vec<mpsc::Sender<DursMsg>>, ) -> mpsc::Sender<RouterThreadMessage<DursMsg>> { let start_time = SystemTime::now(); @@ -342,10 +316,8 @@ pub fn start_router( }); // Create conf thread channel - let (conf_sender, conf_receiver): ( - mpsc::Sender<DursMsgContent>, - mpsc::Receiver<DursMsgContent>, - ) = mpsc::channel(); + let (conf_sender, conf_receiver): (mpsc::Sender<DursMsg>, mpsc::Receiver<DursMsg>) = + mpsc::channel(); // Create conf thread thread::spawn(move || { @@ -354,7 +326,7 @@ pub fn start_router( // Define variables let mut modules_senders: HashMap<ModuleStaticName, mpsc::Sender<DursMsg>> = HashMap::new(); - let mut pool_msgs: HashMap<ModuleStaticName, Vec<DursMsgContent>> = HashMap::new(); + let mut pool_msgs: HashMap<ModuleStaticName, Vec<DursMsg>> = HashMap::new(); // Wait to receiver modules senders loop { @@ -382,17 +354,12 @@ pub fn start_router( // Send pending messages destined specifically to this module if let Some(msgs) = pool_msgs.remove(&module_static_name) { for msg in msgs { - module_sender - .send(DursMsg( - DursMsgReceiver::One(module_static_name), - msg, - )) - .unwrap_or_else(|_| { - panic!( - "Fatal error: fail to relay DursMsg to {:?} !", - module_static_name - ) - }); + module_sender.send(msg).unwrap_or_else(|_| { + panic!( + "Fatal error: fail to relay DursMsg to {:?} !", + module_static_name + ) + }); } } // Add this sender to modules_senders @@ -418,39 +385,40 @@ pub fn start_router( } RouterThreadMessage::ModuleMessage(msg) => { trace!("Router thread receive ModuleMessage({:?})", msg); - match msg.0 { - DursMsgReceiver::All => { - let stop = if let DursMsgContent::Stop() = msg.1 { - true - } else { - false - }; + match msg { + DursMsg::Stop => { + // Relay stop signal to broadcasting thread broadcasting_sender .send(RouterThreadMessage::ModuleMessage(msg)) .expect("Fail to relay message to broadcasting thread !"); - if stop { - break; + // Relay stop message to all modules + for module_sender in modules_senders.values() { + if module_sender.send(DursMsg::Stop).is_err() { + warn!("Fail to relay stop to modules !"); + } } + break; } - DursMsgReceiver::Role(role) => - // If the message is intended for role "ChangeConf", forward it to the conf thread - { - if let ModuleRole::ChangeConf = role { - conf_sender - .send(msg.1) - .expect("Fail to reach conf thread !"); - } else { - broadcasting_sender - .send(RouterThreadMessage::ModuleMessage(msg)) - .expect( - "Fail to relay specific role message to broadcasting thread !", - ); - } + DursMsg::SaveNewModuleConf(_, _) => { + // Forward it to the conf thread + conf_sender + .send(msg) + .expect("Fail to reach conf thread !"); } - DursMsgReceiver::Event(_module_event) => broadcasting_sender + DursMsg::Request{ .. } => { + broadcasting_sender + .send(RouterThreadMessage::ModuleMessage(msg)) + .expect( + "Fail to relay specific role message to broadcasting thread !", + ); + } + DursMsg::Event{ .. } => broadcasting_sender .send(RouterThreadMessage::ModuleMessage(msg)) .expect("Fail to relay specific event message to broadcasting thread !"), - DursMsgReceiver::One(module_static_name) => { + DursMsg::Response { + res_to: module_static_name, + .. + } => { if let Some(module_sender) = modules_senders.get(&module_static_name) { @@ -464,12 +432,12 @@ pub fn start_router( .duration_since(start_time) .expect("Duration error !") .as_secs() - < 20 + < *MAX_REGISTRATION_DELAY { pool_msgs .entry(module_static_name) .or_insert_with(Vec::new) - .push(msg.1); + .push(msg); } else { if !pool_msgs.is_empty() { pool_msgs = HashMap::with_capacity(0); @@ -480,6 +448,9 @@ pub fn start_router( ); } } + DursMsg::ModulesEndpoints(_) => { + warn!("A module try to send reserved router message: ModulesEndpoints."); + } } } } @@ -500,10 +471,7 @@ pub fn start_router( > run_duration_in_secs { broadcasting_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - DursMsgReceiver::All, - DursMsgContent::Stop(), - ))) + .send(RouterThreadMessage::ModuleMessage(DursMsg::Stop)) .expect("Fail to relay stop message to broadcasting thread !"); break; } diff --git a/lib/core/message/Cargo.toml b/lib/core/message/Cargo.toml index 967ff847..c0293fa5 100644 --- a/lib/core/message/Cargo.toml +++ b/lib/core/message/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "duniter-message" +name = "durs-message" version = "0.1.0-a0.1" -authors = ["librelois <elois@duniter.org>"] -description = "message model for the Duniter project." +authors = ["librelois <elois@ifee.fr>"] +description = "message model for the Durs project." license = "AGPL-3.0" [lib] @@ -10,7 +10,6 @@ path = "lib.rs" [dependencies] dup-crypto = { path = "../../tools/crypto" } -duniter-dal = { path = "../../tools/dal" } dubp-documents= { path = "../../tools/documents" } duniter-module = { path = "../module" } duniter-network = { path = "../network" } diff --git a/lib/tools/dal/dal_event.rs b/lib/core/message/events.rs similarity index 71% rename from lib/tools/dal/dal_event.rs rename to lib/core/message/events.rs index 398f6193..0bf0863c 100644 --- a/lib/tools/dal/dal_event.rs +++ b/lib/core/message/events.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2018 The Duniter Project Developers. +// Copyright (C) 2018 The Durs Project Developers. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -15,10 +15,25 @@ use dubp_documents::v10::block::BlockDocument; use dubp_documents::*; +use duniter_network::events::NetworkEvent; +use *; + +/// The DURS event message. +#[derive(Debug, Clone)] +pub enum DursEvent { + /// Arbitrary datas. + ArbitraryDatas(ArbitraryDatas), + /// Blockchain event + BlockchainEvent(BlockchainEvent), + /// Network event + NetworkEvent(NetworkEvent), + /// Client API event + ReceiveValidDocsFromClient(Vec<DUBPDocument>), +} #[derive(Debug, Clone)] /// Event to be transmitted to the other modules -pub enum DALEvent { +pub enum BlockchainEvent { /// Stack up new valid block in local blockchain StackUpValidBlock(Box<BlockDocument>, Blockstamp), /// Revert blocks in local blockchain diff --git a/lib/core/message/lib.rs b/lib/core/message/lib.rs index ad073925..de0476ac 100644 --- a/lib/core/message/lib.rs +++ b/lib/core/message/lib.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2018 The Duniter Project Developers. +// Copyright (C) 2018 The Durs Project Developers. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -13,8 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -//! Defined the few global types used by all modules, -//! as well as the DuniterModule trait that all modules must implement. +//! Define the format of the messages exchanged between the DURS modules. #![cfg_attr(feature = "strict", deny(warnings))] #![deny( @@ -30,7 +29,6 @@ )] extern crate dubp_documents; -extern crate duniter_dal; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; @@ -38,91 +36,71 @@ extern crate durs_network_documents; extern crate serde; extern crate serde_json; -use dubp_documents::BlockId; -use dubp_documents::DUBPDocument; -use duniter_dal::dal_event::DALEvent; -use duniter_dal::dal_requests::{DALRequest, DALResponse}; use duniter_module::*; -use duniter_network::{ - events::NetworkEvent, - requests::{NetworkResponse, OldNetworkRequest}, -}; -use dup_crypto::hashs::Hash; -use dup_crypto::keys::Sig; use durs_network_documents::network_endpoint::EndpointEnum; -#[derive(Debug, Clone)] -/// Message exchanged between Durs modules -pub struct DursMsg(pub DursMsgReceiver, pub DursMsgContent); +/// Define modules events +pub mod events; -impl ModuleMessage for DursMsg {} +/// Define modules requests +pub mod requests; -/// The recipient of a message -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum DursMsgReceiver { - /// Message for all modules - All, - /// Message for one specific module - One(ModuleStaticName), - /// Message for all modules who play a specific role - Role(ModuleRole), - /// Message for all modules that are subscribed to a specific type of event - Event(ModuleEvent), -} +/// Define requests responses +pub mod responses; +use events::*; +use requests::*; +use responses::*; + +/// Message exchanged between Durs modules #[derive(Debug, Clone)] -/// Content of message exchanged between Durs modules -pub enum DursMsgContent { - /// Request - Request(DursReq), - /// Brut text message - Text(String), - /// Brut binary message - Binary(Vec<u8>), +pub enum DursMsg { + /// Durs module event + Event { + /// Event type + event_type: ModuleEvent, + /// Event content + event_content: DursEvent, + }, + /// Durs modules requests + Request { + /// The requester + req_from: ModuleStaticName, + /// Recipient + req_to: ModuleRole, + /// Request id (Must be unique for a given requester) + req_id: ModuleReqId, + /// Request content + req_content: DursReqContent, + }, + /// Durs modules request response + Response { + /// The module that answers the request + res_from: ModuleStaticName, + /// The requester + res_to: ModuleStaticName, + /// Request id (Must be unique for a given requester) + req_id: ModuleReqId, + /// Response content + res_content: DursResContent, + }, + /// Stop signal + Stop, /// New configuration of a module to save SaveNewModuleConf(ModuleStaticName, serde_json::Value), - /// List of local node endpoints - Endpoints(Vec<EndpointEnum>), - /// Response of DALRequest - DALResponse(Box<DALResponse>), - /// Blockchain event - DALEvent(DALEvent), - /// Request to the network module - OldNetworkRequest(OldNetworkRequest), - /// Network event - NetworkEvent(NetworkEvent), - /// Response of OldNetworkRequest - NetworkResponse(NetworkResponse), - /// Pow module response - ProverResponse(BlockId, Sig, u64), - /// Client API event - ReceiveDocsFromClient(Vec<DUBPDocument>), - /// Stop signal - Stop(), + /// List of all endpoints declared by the modules + ModulesEndpoints(Vec<EndpointEnum>), } -#[derive(Debug, Clone)] -/// Durs modules requests -pub struct DursReq { - /// Requester - pub requester: ModuleStaticName, - /// Request unique id - pub id: ModuleReqId, - /// Request content - pub content: DursReqContent, -} +impl ModuleMessage for DursMsg {} +/// Arbitrary datas #[derive(Debug, Clone)] -/// Modules request content -pub enum DursReqContent { - /// Network request (Not yet implemented) - NetworkRequest(), - /// Blockchain datas request - DALRequest(DALRequest), - /// Request to the pow module - ProverRequest(BlockId, Hash), - /// Brut text request +pub enum ArbitraryDatas { + /// Arbitrary text message Text(String), - /// Brut binary request + /// Arbitrary json message + Json(serde_json::Value), + /// Arbitrary binary message Binary(Vec<u8>), } diff --git a/lib/core/message/requests.rs b/lib/core/message/requests.rs new file mode 100644 index 00000000..0e5e0107 --- /dev/null +++ b/lib/core/message/requests.rs @@ -0,0 +1,61 @@ +// Copyright (C) 2018 The Durs Project Developers. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use dubp_documents::BlockId; +use duniter_network::requests::OldNetworkRequest; +use dup_crypto::hashs::Hash; +use dup_crypto::keys::*; +use *; + +#[derive(Debug, Clone)] +/// Modules request content +pub enum DursReqContent { + /// Request to the old network module + OldNetworkRequest(OldNetworkRequest), + /// Network request (Not yet implemented) + NetworkRequest(), + /// Blockchain datas request + BlockchainRequest(BlockchainRequest), + /// Mem pool datas request + MemPoolRequest(MemPoolRequest), + /// Request to the pow module + ProverRequest(BlockId, Hash), + /// Arbitrary datas + ArbitraryDatas(ArbitraryDatas), +} + +#[derive(Debug, Clone, PartialEq)] +/// Inter-module Blockchain request for blockchain data +pub enum BlockchainRequest { + /// Current block + CurrentBlock(), + /// Block by number + BlockByNumber(u64), + /// Chunk (block pack) + Chunk(u64, usize), + /// Usernames corresponding to the public keys in parameter + UIDs(Vec<PubKey>), +} + +#[derive(Debug, Copy, Clone)] +/// Inter-module request for mem pool data +pub enum MemPoolRequest { + /// All pending identities with their pending certifications + AllPendingIdentities(usize), + /// All pending identities without their pending certifications + AllPendingIdentitiesWithoutCerts(usize), + /// All pending datas for given pubkey + PendingWotDatasForPubkey(PubKey), +} diff --git a/lib/tools/dal/dal_requests.rs b/lib/core/message/responses.rs similarity index 62% rename from lib/tools/dal/dal_requests.rs rename to lib/core/message/responses.rs index dc5cfa3d..54479281 100644 --- a/lib/tools/dal/dal_requests.rs +++ b/lib/core/message/responses.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2018 The Duniter Project Developers. +// Copyright (C) 2018 The Durs Project Developers. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -13,51 +13,30 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -extern crate duniter_module; -extern crate serde; - -use self::duniter_module::ModuleReqId; use dubp_documents::v10::block::BlockDocument; use dubp_documents::v10::certification::CertificationDocument; use dubp_documents::v10::identity::IdentityDocument; use dubp_documents::v10::membership::MembershipDocument; use dubp_documents::v10::revocation::RevocationDocument; +use dubp_documents::BlockId; use dubp_documents::Blockstamp; +use duniter_module::ModuleReqId; +use duniter_network::requests::NetworkResponse; use dup_crypto::hashs::Hash; use dup_crypto::keys::*; use std::collections::HashMap; -#[derive(Debug, Copy, Clone)] -/// Inter-module DAL request for pool data -pub enum DALReqPendings { - /// All pending identities with their pending certifications - AllPendingIdentities(usize), - /// All pending identities without their pending certifications - AllPendingIdentitiesWithoutCerts(usize), - /// All pending datas for given pubkey - PendingWotDatasForPubkey(PubKey), -} - -#[derive(Debug, Clone, PartialEq)] -/// Inter-module DAL request for blockchain data -pub enum DALReqBlockchain { - /// Current block - CurrentBlock(), - /// Block by number - BlockByNumber(u64), - /// Chunk (block pack) - Chunk(u64, usize), - /// Usernames corresponding to the public keys in parameter - UIDs(Vec<PubKey>), -} - +/// Durs request response message #[derive(Debug, Clone)] -/// Inter-module DAL request -pub enum DALRequest { - /// Inter-module DAL request for blockchain data - BlockchainRequest(DALReqBlockchain), - /// Inter-module DAL request for pool data - PendingsRequest(DALReqPendings), +pub enum DursResContent { + /// BlockchainResponse + BlockchainResponse(BlockchainResponse), + /// MemPoolResponse + MemPoolResponse(MemPoolResponse), + /// Response of OldNetworkRequest + NetworkResponse(NetworkResponse), + /// Pow module response + ProverResponse(BlockId, Sig, u64), } #[derive(Debug, Clone)] @@ -76,19 +55,8 @@ pub struct PendingIdtyDatas { } #[derive(Debug, Clone)] -/// Response to a DALReqPendings request -pub enum DALResPendings { - /// All pending identities with their pending certifications - AllPendingIdentities(ModuleReqId, HashMap<Hash, PendingIdtyDatas>), - /// All pending identities without their pending certifications - AllPendingIdentitiesWithoutCerts(ModuleReqId, HashMap<Hash, PendingIdtyDatas>), - /// All pending datas for given pubkey - PendingWotDatasForPubkey(ModuleReqId, Box<PendingIdtyDatas>), -} - -#[derive(Debug, Clone)] -/// Response to a DALReqBlockchain request -pub enum DALResBlockchain { +/// Response to a BlockchainReqBlockchain request +pub enum BlockchainResponse { /// Current block CurrentBlock(ModuleReqId, Box<BlockDocument>, Blockstamp), /// Block by number @@ -100,10 +68,12 @@ pub enum DALResBlockchain { } #[derive(Debug, Clone)] -/// Response to a DAL request -pub enum DALResponse { - /// Response to a DALReqBlockchain request - Blockchain(Box<DALResBlockchain>), - /// Response to a DALReqPendings request - Pendings(Box<DALResPendings>), +/// Response to a MemPoolRequest request +pub enum MemPoolResponse { + /// All pending identities with their pending certifications + AllPendingIdentities(ModuleReqId, HashMap<Hash, PendingIdtyDatas>), + /// All pending identities without their pending certifications + AllPendingIdentitiesWithoutCerts(ModuleReqId, HashMap<Hash, PendingIdtyDatas>), + /// All pending datas for given pubkey + PendingWotDatasForPubkey(ModuleReqId, Box<PendingIdtyDatas>), } diff --git a/lib/core/module/lib.rs b/lib/core/module/lib.rs index 251afbda..569dcaff 100644 --- a/lib/core/module/lib.rs +++ b/lib/core/module/lib.rs @@ -199,6 +199,8 @@ pub enum ModuleEvent { NewValidHeadFromNetwork, /// Change in connections with other nodes (disconnection of a connection or establishment of a new connection) ConnectionsChangeNodeNetwork, + /// A new self peer card is generated + NewSelfPeer, /// A new valid peer record has been received from the network NewValidPeerFromNodeNetwork, } diff --git a/lib/core/network/events.rs b/lib/core/network/events.rs index 2628ae12..5cb4a8c6 100644 --- a/lib/core/network/events.rs +++ b/lib/core/network/events.rs @@ -25,6 +25,8 @@ use NodeFullId; pub enum NetworkEvent { /// A connection has changed state(`u32` is the new state, `Option<String>` est l'uid du noeud) ConnectionStateChange(NodeFullId, u32, Option<String>, String), + /// Generate new self peer card + NewSelfPeer(PeerCard), /// Receiving Pending Documents ReceiveDocuments(Vec<BlockchainDocument>), /// Receipt of peer cards diff --git a/lib/modules/blockchain/Cargo.toml b/lib/modules/blockchain/Cargo.toml deleted file mode 100644 index a4ffd84c..00000000 --- a/lib/modules/blockchain/Cargo.toml +++ /dev/null @@ -1,29 +0,0 @@ -[package] -name = "duniter-blockchain" -version = "0.1.0-a0.1" -authors = ["librelois <elois@ifee.fr>"] -description = "Blockchain module for the Duniter project." -license = "AGPL-3.0" - -[lib] -path = "lib.rs" - -[dependencies] -dirs = "1.0.2" -duniter-conf = { path = "../../core/conf" } -dup-crypto = { path = "../../tools/crypto" } -duniter-dal = { path = "../../tools/dal" } -dubp-documents= { path = "../../tools/documents" } -durs-network-documents = { path = "../../tools/network-documents" } -duniter-message = { path = "../../core/message" } -duniter-module = { path = "../../core/module" } -duniter-network = { path = "../../core/network" } -durs-wot = { path = "../../tools/wot" } -log = "0.4.*" -num_cpus = "1.8.*" -pbr = "1.0.*" -rustbreak = {version = "2.0.0-rc3", features = ["bin_enc"]} -serde = "1.0.*" -serde_json = "1.0.*" -sqlite = "0.23.*" -threadpool = "1.7.*" \ No newline at end of file diff --git a/lib/tools/dal/Cargo.toml b/lib/modules/blockchain/blockchain-dal/Cargo.toml similarity index 53% rename from lib/tools/dal/Cargo.toml rename to lib/modules/blockchain/blockchain-dal/Cargo.toml index 28d9ee14..f91ec6ff 100644 --- a/lib/tools/dal/Cargo.toml +++ b/lib/modules/blockchain/blockchain-dal/Cargo.toml @@ -1,18 +1,18 @@ [package] -name = "duniter-dal" +name = "durs-blockchain-dal" version = "0.1.0-a0.1" authors = ["librelois <elois@ifee.fr>"] -description = "Data Access Layer for the Duniter project." +description = "Data Access Layer for Durs Blockchain module." license = "AGPL-3.0" [lib] path = "lib.rs" [dependencies] -dup-crypto = { path = "../crypto" } -dubp-documents= { path = "../documents" } -duniter-module = { path = "../../core/module" } -durs-wot = { path = "../wot" } +dup-crypto = { path = "../../../tools/crypto" } +dubp-documents= { path = "../../../tools/documents" } +duniter-module = { path = "../../../core/module" } +durs-wot = { path = "../../../tools/wot" } log = "0.4.*" rustbreak = {version = "2.0.0-rc3", features = ["bin_enc"]} serde = "1.0.*" diff --git a/lib/tools/dal/balance.rs b/lib/modules/blockchain/blockchain-dal/balance.rs similarity index 100% rename from lib/tools/dal/balance.rs rename to lib/modules/blockchain/blockchain-dal/balance.rs diff --git a/lib/tools/dal/block.rs b/lib/modules/blockchain/blockchain-dal/block.rs similarity index 100% rename from lib/tools/dal/block.rs rename to lib/modules/blockchain/blockchain-dal/block.rs diff --git a/lib/tools/dal/certs.rs b/lib/modules/blockchain/blockchain-dal/certs.rs similarity index 100% rename from lib/tools/dal/certs.rs rename to lib/modules/blockchain/blockchain-dal/certs.rs diff --git a/lib/tools/dal/clippy.toml b/lib/modules/blockchain/blockchain-dal/clippy.toml similarity index 100% rename from lib/tools/dal/clippy.toml rename to lib/modules/blockchain/blockchain-dal/clippy.toml diff --git a/lib/tools/dal/constants.rs b/lib/modules/blockchain/blockchain-dal/constants.rs similarity index 100% rename from lib/tools/dal/constants.rs rename to lib/modules/blockchain/blockchain-dal/constants.rs diff --git a/lib/tools/dal/currency_params.rs b/lib/modules/blockchain/blockchain-dal/currency_params.rs similarity index 100% rename from lib/tools/dal/currency_params.rs rename to lib/modules/blockchain/blockchain-dal/currency_params.rs diff --git a/lib/tools/dal/identity.rs b/lib/modules/blockchain/blockchain-dal/identity.rs similarity index 100% rename from lib/tools/dal/identity.rs rename to lib/modules/blockchain/blockchain-dal/identity.rs diff --git a/lib/tools/dal/lib.rs b/lib/modules/blockchain/blockchain-dal/lib.rs similarity index 99% rename from lib/tools/dal/lib.rs rename to lib/modules/blockchain/blockchain-dal/lib.rs index 653b97fa..0bb46a20 100644 --- a/lib/tools/dal/lib.rs +++ b/lib/modules/blockchain/blockchain-dal/lib.rs @@ -57,12 +57,6 @@ pub mod constants; /// Currency parameters operations pub mod currency_params; -/// Define DAL events to be transmitted to other modules -pub mod dal_event; - -/// Defined module requests for DAL -pub mod dal_requests; - /// Identity operations pub mod identity; diff --git a/lib/tools/dal/parsers/certifications.rs b/lib/modules/blockchain/blockchain-dal/parsers/certifications.rs similarity index 100% rename from lib/tools/dal/parsers/certifications.rs rename to lib/modules/blockchain/blockchain-dal/parsers/certifications.rs diff --git a/lib/tools/dal/parsers/mod.rs b/lib/modules/blockchain/blockchain-dal/parsers/mod.rs similarity index 100% rename from lib/tools/dal/parsers/mod.rs rename to lib/modules/blockchain/blockchain-dal/parsers/mod.rs diff --git a/lib/tools/dal/parsers/revoked.rs b/lib/modules/blockchain/blockchain-dal/parsers/revoked.rs similarity index 100% rename from lib/tools/dal/parsers/revoked.rs rename to lib/modules/blockchain/blockchain-dal/parsers/revoked.rs diff --git a/lib/tools/dal/sources.rs b/lib/modules/blockchain/blockchain-dal/sources.rs similarity index 100% rename from lib/tools/dal/sources.rs rename to lib/modules/blockchain/blockchain-dal/sources.rs diff --git a/lib/tools/dal/tools.rs b/lib/modules/blockchain/blockchain-dal/tools.rs similarity index 100% rename from lib/tools/dal/tools.rs rename to lib/modules/blockchain/blockchain-dal/tools.rs diff --git a/lib/tools/dal/writers/block.rs b/lib/modules/blockchain/blockchain-dal/writers/block.rs similarity index 100% rename from lib/tools/dal/writers/block.rs rename to lib/modules/blockchain/blockchain-dal/writers/block.rs diff --git a/lib/tools/dal/writers/certification.rs b/lib/modules/blockchain/blockchain-dal/writers/certification.rs similarity index 100% rename from lib/tools/dal/writers/certification.rs rename to lib/modules/blockchain/blockchain-dal/writers/certification.rs diff --git a/lib/tools/dal/writers/dividend.rs b/lib/modules/blockchain/blockchain-dal/writers/dividend.rs similarity index 100% rename from lib/tools/dal/writers/dividend.rs rename to lib/modules/blockchain/blockchain-dal/writers/dividend.rs diff --git a/lib/tools/dal/writers/identity.rs b/lib/modules/blockchain/blockchain-dal/writers/identity.rs similarity index 100% rename from lib/tools/dal/writers/identity.rs rename to lib/modules/blockchain/blockchain-dal/writers/identity.rs diff --git a/lib/tools/dal/writers/mod.rs b/lib/modules/blockchain/blockchain-dal/writers/mod.rs similarity index 100% rename from lib/tools/dal/writers/mod.rs rename to lib/modules/blockchain/blockchain-dal/writers/mod.rs diff --git a/lib/tools/dal/writers/requests.rs b/lib/modules/blockchain/blockchain-dal/writers/requests.rs similarity index 100% rename from lib/tools/dal/writers/requests.rs rename to lib/modules/blockchain/blockchain-dal/writers/requests.rs diff --git a/lib/tools/dal/writers/transaction.rs b/lib/modules/blockchain/blockchain-dal/writers/transaction.rs similarity index 100% rename from lib/tools/dal/writers/transaction.rs rename to lib/modules/blockchain/blockchain-dal/writers/transaction.rs diff --git a/lib/modules/blockchain/blockchain/Cargo.toml b/lib/modules/blockchain/blockchain/Cargo.toml new file mode 100644 index 00000000..a45de95c --- /dev/null +++ b/lib/modules/blockchain/blockchain/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "durs-blockchain" +version = "0.1.0-a0.1" +authors = ["librelois <elois@ifee.fr>"] +description = "Blockchain module for the Durs project." +license = "AGPL-3.0" + +[lib] +path = "lib.rs" + +[dependencies] +dirs = "1.0.2" +duniter-conf = { path = "../../../core/conf" } +dup-crypto = { path = "../../../tools/crypto" } +durs-blockchain-dal = { path = "../blockchain-dal" } +dubp-documents= { path = "../../../tools/documents" } +durs-network-documents = { path = "../../../tools/network-documents" } +durs-message = { path = "../../../core/message" } +duniter-module = { path = "../../../core/module" } +duniter-network = { path = "../../../core/network" } +durs-wot = { path = "../../../tools/wot" } +log = "0.4.*" +num_cpus = "1.8.*" +pbr = "1.0.*" +rustbreak = {version = "2.0.0-rc3", features = ["bin_enc"]} +serde = "1.0.*" +serde_json = "1.0.*" +sqlite = "0.23.*" +threadpool = "1.7.*" \ No newline at end of file diff --git a/lib/modules/blockchain/apply_valid_block.rs b/lib/modules/blockchain/blockchain/apply_valid_block.rs similarity index 97% rename from lib/modules/blockchain/apply_valid_block.rs rename to lib/modules/blockchain/blockchain/apply_valid_block.rs index 4e1154f5..bd443de1 100644 --- a/lib/modules/blockchain/apply_valid_block.rs +++ b/lib/modules/blockchain/blockchain/apply_valid_block.rs @@ -17,11 +17,11 @@ use dubp_documents::v10::transaction::{TxAmount, TxBase}; use dubp_documents::v10::BlockDocument; use dubp_documents::BlockId; use dubp_documents::Document; -use duniter_dal::block::DALBlock; -use duniter_dal::sources::SourceAmount; -use duniter_dal::writers::requests::*; -use duniter_dal::{BinDB, ForkId}; use dup_crypto::keys::*; +use durs_blockchain_dal::block::DALBlock; +use durs_blockchain_dal::sources::SourceAmount; +use durs_blockchain_dal::writers::requests::*; +use durs_blockchain_dal::{BinDB, ForkId}; use durs_wot::data::{NewLinkResult, RemLinkResult}; use durs_wot::{NodeId, WebOfTrust}; use std::collections::{HashMap, HashSet}; @@ -237,7 +237,7 @@ pub fn apply_valid_block<W: WebOfTrust>( (centralities.iter().sum::<u64>() as f64 / centralities.len() as f64) as usize; // Register the state of the wot let max_connectivity = currency_params.max_connectivity(); - duniter_dal::register_wot_state( + durs_blockchain_dal::register_wot_state( db, &WotState { block_number: block.number.0, diff --git a/lib/modules/blockchain/check_and_apply_block.rs b/lib/modules/blockchain/blockchain/check_and_apply_block.rs similarity index 92% rename from lib/modules/blockchain/check_and_apply_block.rs rename to lib/modules/blockchain/blockchain/check_and_apply_block.rs index 6ba3095a..4c01613c 100644 --- a/lib/modules/blockchain/check_and_apply_block.rs +++ b/lib/modules/blockchain/blockchain/check_and_apply_block.rs @@ -18,10 +18,10 @@ use std::collections::HashMap; use apply_valid_block::*; use dubp_documents::Document; use dubp_documents::{BlockHash, BlockId, Blockstamp, PreviousBlockstamp}; -use duniter_dal::block::DALBlock; -use duniter_dal::*; use duniter_network::documents::NetworkBlock; use dup_crypto::keys::*; +use durs_blockchain_dal::block::DALBlock; +use durs_blockchain_dal::*; use *; #[derive(Debug, Copy, Clone)] @@ -86,13 +86,14 @@ pub fn check_and_apply_block<W: WebOfTrust>( ); // Detect expire_certs let blocks_expiring = Vec::with_capacity(0); - let expire_certs = duniter_dal::certs::find_expire_certs(certs_db, blocks_expiring)?; + let expire_certs = + durs_blockchain_dal::certs::find_expire_certs(certs_db, blocks_expiring)?; // Try stack up block let mut old_fork_id = None; let block_doc = match *block { Block::NetworkBlock(network_block) => complete_network_block(network_block, true)?, Block::LocalBlock(block_doc) => { - old_fork_id = duniter_dal::block::get_fork_id_of_blockstamp( + old_fork_id = durs_blockchain_dal::block::get_fork_id_of_blockstamp( &blocks_databases.forks_blocks_db, &block_doc.blockstamp(), )?; @@ -147,7 +148,7 @@ pub fn check_and_apply_block<W: WebOfTrust>( block: block_doc, expire_certs: None, }; - duniter_dal::writers::block::write( + durs_blockchain_dal::writers::block::write( &blocks_databases.blockchain_db, &blocks_databases.forks_db, &blocks_databases.forks_blocks_db, @@ -156,7 +157,7 @@ pub fn check_and_apply_block<W: WebOfTrust>( false, false, ) - .expect("duniter_dal::writers::block::write() : DALError") + .expect("durs_blockchain_dal::writers::block::write() : DALError") } Block::LocalBlock(block_doc) => { let old_fork_id = None; @@ -166,7 +167,7 @@ pub fn check_and_apply_block<W: WebOfTrust>( block: block_doc.clone(), expire_certs: None, }; - duniter_dal::writers::block::write( + durs_blockchain_dal::writers::block::write( &blocks_databases.blockchain_db, &blocks_databases.forks_db, &blocks_databases.forks_blocks_db, @@ -175,7 +176,7 @@ pub fn check_and_apply_block<W: WebOfTrust>( false, false, ) - .expect("duniter_dal::writers::block::write() : DALError") + .expect("durs_blockchain_dal::writers::block::write() : DALError") } }; } else { diff --git a/lib/modules/blockchain/clippy.toml b/lib/modules/blockchain/blockchain/clippy.toml similarity index 100% rename from lib/modules/blockchain/clippy.toml rename to lib/modules/blockchain/blockchain/clippy.toml diff --git a/lib/modules/blockchain/dbex.rs b/lib/modules/blockchain/blockchain/dbex.rs similarity index 95% rename from lib/modules/blockchain/dbex.rs rename to lib/modules/blockchain/blockchain/dbex.rs index 4e039bb1..656545af 100644 --- a/lib/modules/blockchain/dbex.rs +++ b/lib/modules/blockchain/blockchain/dbex.rs @@ -14,9 +14,9 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use dubp_documents::v10::transaction::*; -use duniter_dal::identity::DALIdentity; use duniter_module::DuniterConf; use dup_crypto::keys::*; +use durs_blockchain_dal::identity::DALIdentity; use durs_wot::data::rusty::RustyWebOfTrust; use durs_wot::operations::distance::{DistanceCalculator, WotDistance, WotDistanceParameters}; use std::time::*; @@ -80,7 +80,7 @@ pub fn dbex_tx<DC: DuniterConf>(profile: &str, conf: &DC, _csv: bool, query: &DB DBExTxQuery::Balance(ref address_str) => { let pubkey = if let Ok(ed25519_pubkey) = ed25519::PublicKey::from_base58(address_str) { PubKey::Ed25519(ed25519_pubkey) - } else if let Some(pubkey) = duniter_dal::identity::get_pubkey_from_uid( + } else if let Some(pubkey) = durs_blockchain_dal::identity::get_pubkey_from_uid( &wot_databases.identities_db, address_str, ) @@ -92,7 +92,7 @@ pub fn dbex_tx<DC: DuniterConf>(profile: &str, conf: &DC, _csv: bool, query: &DB return; }; let address = UTXOConditionsGroup::Single(TransactionOutputCondition::Sig(pubkey)); - let address_balance = duniter_dal::balance::get_address_balance( + let address_balance = durs_blockchain_dal::balance::get_address_balance( ¤cy_databases.balances_db, &address, ) @@ -270,9 +270,11 @@ pub fn dbex_wot<DC: DuniterConf>(profile: &str, conf: &DC, csv: bool, query: &DB } DBExWotQuery::MemberDatas(ref uid) => { println!(" Members count = {}.", members_count); - if let Some(pubkey) = - duniter_dal::identity::get_pubkey_from_uid(&wot_databases.identities_db, uid) - .expect("get_pubkey_from_uid() : DALError !") + if let Some(pubkey) = durs_blockchain_dal::identity::get_pubkey_from_uid( + &wot_databases.identities_db, + uid, + ) + .expect("get_pubkey_from_uid() : DALError !") { let wot_id = wot_index[&pubkey]; println!( @@ -307,7 +309,7 @@ pub fn dbex_wot<DC: DuniterConf>(profile: &str, conf: &DC, csv: bool, query: &DB .expect("Fail to get links source !"); println!("Certifiers : {}", sources.len()); for (i, source) in sources.iter().enumerate() { - let source_uid = duniter_dal::identity::get_uid( + let source_uid = durs_blockchain_dal::identity::get_uid( &wot_databases.identities_db, *(wot_reverse_index[&source]), ) diff --git a/lib/modules/blockchain/lib.rs b/lib/modules/blockchain/blockchain/lib.rs similarity index 72% rename from lib/modules/blockchain/lib.rs rename to lib/modules/blockchain/blockchain/lib.rs index ac4246d2..c1e66949 100644 --- a/lib/modules/blockchain/lib.rs +++ b/lib/modules/blockchain/blockchain/lib.rs @@ -35,11 +35,11 @@ extern crate log; extern crate dirs; extern crate dubp_documents; extern crate duniter_conf; -extern crate duniter_dal; -extern crate duniter_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; +extern crate durs_blockchain_dal; +extern crate durs_message; extern crate durs_network_documents; extern crate durs_wot; extern crate serde; @@ -65,14 +65,6 @@ pub use dbex::{DBExQuery, DBExTxQuery, DBExWotQuery}; use dubp_documents::v10::{BlockDocument, V10Document}; use dubp_documents::*; use dubp_documents::{DUBPDocument, Document}; -use duniter_dal::block::DALBlock; -use duniter_dal::currency_params::CurrencyParameters; -use duniter_dal::dal_event::DALEvent; -use duniter_dal::dal_requests::{DALReqBlockchain, DALRequest, DALResBlockchain, DALResponse}; -use duniter_dal::identity::DALIdentity; -use duniter_dal::writers::requests::BlocksDBsWriteQuery; -use duniter_dal::*; -use duniter_message::*; use duniter_module::*; use duniter_network::{ documents::{BlockchainDocument, NetworkBlock}, @@ -80,6 +72,15 @@ use duniter_network::{ requests::{NetworkResponse, OldNetworkRequest}, }; use dup_crypto::keys::*; +use durs_blockchain_dal::block::DALBlock; +use durs_blockchain_dal::currency_params::CurrencyParameters; +use durs_blockchain_dal::identity::DALIdentity; +use durs_blockchain_dal::writers::requests::BlocksDBsWriteQuery; +use durs_blockchain_dal::*; +use durs_message::events::*; +use durs_message::requests::*; +use durs_message::responses::*; +use durs_message::*; use durs_network_documents::NodeFullId; use durs_wot::data::rusty::RustyWebOfTrust; use durs_wot::operations::distance::RustyDistanceCalculator; @@ -168,18 +169,20 @@ impl BlockchainModule { let currency_databases = CurrencyV10DBs::open(Some(&db_path)); // Get current blockstamp - let current_blockstamp = duniter_dal::block::get_current_blockstamp(&blocks_databases) - .expect("Fatal error : fail to read Blockchain DB !"); + let current_blockstamp = + durs_blockchain_dal::block::get_current_blockstamp(&blocks_databases) + .expect("Fatal error : fail to read Blockchain DB !"); // Get currency parameters - let currency_params = - duniter_dal::currency_params::get_currency_params(&blocks_databases.blockchain_db) - .expect("Fatal error : fail to read Blockchain DB !") - .unwrap_or_default(); + let currency_params = durs_blockchain_dal::currency_params::get_currency_params( + &blocks_databases.blockchain_db, + ) + .expect("Fatal error : fail to read Blockchain DB !") + .unwrap_or_default(); // Get forks states let forks_states = if let Some(current_blockstamp) = current_blockstamp { - duniter_dal::block::get_forks(&blocks_databases.forks_db, current_blockstamp) + durs_blockchain_dal::block::get_forks(&blocks_databases.forks_db, current_blockstamp) .expect("Fatal error : fail to read Forks DB !") } else { vec![] @@ -272,35 +275,44 @@ impl BlockchainModule { requests_ids } /// Send network request - fn request_network(&self, _req_id: ModuleReqId, request: &OldNetworkRequest) -> ModuleReqId { + fn request_network(&self, req_id: ModuleReqId, request: &OldNetworkRequest) -> ModuleReqId { self.router_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - DursMsgReceiver::Role(ModuleRole::InterNodesNetwork), - DursMsgContent::OldNetworkRequest(*request), - ))) + .send(RouterThreadMessage::ModuleMessage(DursMsg::Request { + req_from: BlockchainModule::name(), + req_to: ModuleRole::InterNodesNetwork, + req_id, + req_content: DursReqContent::OldNetworkRequest(*request), + })) .unwrap_or_else(|_| panic!("Fail to send OldNetworkRequest to router")); request.get_req_id() } /// Send blockchain event - fn send_event(&self, event: &DALEvent) { + fn send_event(&self, event: &BlockchainEvent) { let module_event = match event { - DALEvent::StackUpValidBlock(_, _) => ModuleEvent::NewValidBlock, - DALEvent::RevertBlocks(_) => ModuleEvent::RevertBlocks, + BlockchainEvent::StackUpValidBlock(_, _) => ModuleEvent::NewValidBlock, + BlockchainEvent::RevertBlocks(_) => ModuleEvent::RevertBlocks, _ => return, }; self.router_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - DursMsgReceiver::Event(module_event), - DursMsgContent::DALEvent(event.clone()), - ))) - .unwrap_or_else(|_| panic!("Fail to send DalEvent to router")); + .send(RouterThreadMessage::ModuleMessage(DursMsg::Event { + event_type: module_event, + event_content: DursEvent::BlockchainEvent(event.clone()), + })) + .unwrap_or_else(|_| panic!("Fail to send BlockchainEvent to router")); } - fn send_req_response(&self, requester: DursMsgReceiver, response: &DALResponse) { + fn send_req_response( + &self, + requester: ModuleStaticName, + req_id: ModuleReqId, + response: &BlockchainResponse, + ) { self.router_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - requester, - DursMsgContent::DALResponse(Box::new(response.clone())), - ))) + .send(RouterThreadMessage::ModuleMessage(DursMsg::Response { + res_from: BlockchainModule::name(), + res_to: requester, + req_id, + res_content: DursResContent::BlockchainResponse(response.clone()), + })) .unwrap_or_else(|_| panic!("Fail to send ReqRes to router")); } fn receive_network_documents<W: WebOfTrust>( @@ -349,7 +361,7 @@ impl BlockchainModule { if let BlocksDBsWriteQuery::WriteBlock(_, _, _, block_hash) = block_req { info!("StackUpValidBlock({})", block_doc.number.0); - self.send_event(&DALEvent::StackUpValidBlock( + self.send_event(&BlockchainEvent::StackUpValidBlock( Box::new(block_doc.clone()), Blockstamp { id: block_doc.number, @@ -359,7 +371,7 @@ impl BlockchainModule { } current_blockstamp = network_block.blockstamp(); // Update forks states - self.forks_states = duniter_dal::block::get_forks( + self.forks_states = durs_blockchain_dal::block::get_forks( &self.blocks_databases.forks_db, current_blockstamp, ) @@ -380,11 +392,11 @@ impl BlockchainModule { "RefusedBlock({})", network_block.uncompleted_block_doc().number.0 ); - self.send_event(&DALEvent::RefusedPendingDoc(DUBPDocument::V10( - Box::new(V10Document::Block(Box::new( + self.send_event(&BlockchainEvent::RefusedPendingDoc( + DUBPDocument::V10(Box::new(V10Document::Block(Box::new( network_block.uncompleted_block_doc().clone(), - ))), - ))); + )))), + )); } } } @@ -434,7 +446,7 @@ impl BlockchainModule { DUBPDocument::V10(ref doc_v10) => match doc_v10.deref() { _ => {} }, - _ => self.send_event(&DALEvent::RefusedPendingDoc(document.clone())), + _ => self.send_event(&BlockchainEvent::RefusedPendingDoc(document.clone())), } } } @@ -465,7 +477,7 @@ impl BlockchainModule { { current_blockstamp = block.blockstamp(); // Update forks states - self.forks_states = duniter_dal::block::get_forks( + self.forks_states = durs_blockchain_dal::block::get_forks( &self.blocks_databases.forks_db, current_blockstamp, ) @@ -522,7 +534,7 @@ impl BlockchainModule { // Get current block let mut current_blockstamp = - duniter_dal::block::get_current_blockstamp(&self.blocks_databases) + durs_blockchain_dal::block::get_current_blockstamp(&self.blocks_databases) .expect("Fatal error : fail to read ForksV10DB !") .unwrap_or_default(); @@ -574,145 +586,160 @@ impl BlockchainModule { } } match blockchain_receiver.recv_timeout(Duration::from_millis(1000)) { - Ok(ref message) => { - match (*message).1 { - DursMsgContent::Request(ref request) => { - if let DursReqContent::DALRequest(ref dal_request) = request.content { - match dal_request { - DALRequest::BlockchainRequest(ref blockchain_req) => { - match *blockchain_req { - DALReqBlockchain::CurrentBlock() => { - debug!("BlockchainModule : receive DALReqBc::CurrentBlock()"); + Ok(ref durs_message) => { + match *durs_message { + DursMsg::Request { + req_from, + req_to: _, + req_id, + ref req_content, + } => { + if let DursReqContent::BlockchainRequest(ref blockchain_req) = + req_content + { + match *blockchain_req { + BlockchainRequest::CurrentBlock() => { + debug!( + "BlockchainModule : receive DALReqBc::CurrentBlock()" + ); - if let Some(current_block) = + if let Some(current_block) = DALBlock::get_block( &self.blocks_databases.blockchain_db, None, ¤t_blockstamp, ).expect( "Fatal error : get_block : fail to read LocalBlockchainV10DB !", - ) { + ) + { debug!("BlockchainModule : send_req_response(CurrentBlock({}))", current_blockstamp); - self.send_req_response(DursMsgReceiver::One(request.requester), &DALResponse::Blockchain(Box::new( - DALResBlockchain::CurrentBlock( - request.id, + self.send_req_response(req_from, req_id, &BlockchainResponse::CurrentBlock( + req_id, Box::new(current_block.block), current_blockstamp, ), - ))); + ); } else { - warn!("BlockchainModule : Req : fail to get current_block in bdd !"); + warn!("BlockchainModule : Req : fail to get current_block in bdd !"); } - } - DALReqBlockchain::UIDs(ref pubkeys) => { - self.send_req_response(DursMsgReceiver::One(request.requester), &DALResponse::Blockchain(Box::new( - DALResBlockchain::UIDs( - request.id, + } + BlockchainRequest::UIDs(ref pubkeys) => { + self.send_req_response(req_from, req_id, &BlockchainResponse::UIDs( + req_id, pubkeys .iter() - .map(|p| { - ( - *p, - duniter_dal::identity::get_uid(&self.wot_databases.identities_db, *p) - .expect("Fatal error : get_uid : Fail to read WotV10DB !") - ) - }) + .map(|p| ( + *p, + durs_blockchain_dal::identity::get_uid(&self.wot_databases.identities_db, *p) + .expect("Fatal error : get_uid : Fail to read WotV10DB !") + )) .collect(), ), - ))); - } - _ => {} - } + ); } - DALRequest::PendingsRequest(ref _pending_req) => {} + _ => {} } } } - DursMsgContent::NetworkEvent(ref network_event) => match *network_event { - NetworkEvent::ReceiveDocuments(ref network_docs) => { - let new_current_blockstamp = self.receive_network_documents( - network_docs, - ¤t_blockstamp, - &mut wot_index, - &wot_db, - ); - current_blockstamp = new_current_blockstamp; + DursMsg::Event { + event_type: _, + ref event_content, + } => match *event_content { + DursEvent::NetworkEvent(ref network_event) => match *network_event { + NetworkEvent::ReceiveDocuments(ref network_docs) => { + let new_current_blockstamp = self.receive_network_documents( + network_docs, + ¤t_blockstamp, + &mut wot_index, + &wot_db, + ); + current_blockstamp = new_current_blockstamp; + } + NetworkEvent::ReceiveHeads(_) => {} + _ => {} + }, + DursEvent::ReceiveValidDocsFromClient(ref docs) => { + self.receive_documents(docs); } - NetworkEvent::ReceiveHeads(_) => {} - _ => {} + _ => {} // Others modules events }, - DursMsgContent::NetworkResponse(ref network_response) => { - debug!("BlockchainModule : receive NetworkEvent::ReqResponse() !"); - if let Some(request) = - pending_network_requests.remove(&network_response.get_req_id()) - { - match request { - OldNetworkRequest::GetConsensus(_) => { - if let NetworkResponse::Consensus(_, response) = - *network_response.deref() - { - if let Ok(blockstamp) = response { - consensus = blockstamp; - if current_blockstamp.id.0 > consensus.id.0 + 2 { - // Find free fork id - let free_fork_id = ForkId(49); - // Get last dal_block - let last_dal_block_id = - BlockId(current_blockstamp.id.0 - 1); - let last_dal_block = self - .blocks_databases - .blockchain_db - .read(|db| db.get(&last_dal_block_id).cloned()) - .expect("Fail to read blockchain DB.") - .expect( - "Fatal error : not foutn last dal block !", - ); - revert_block::revert_block( - &last_dal_block, - &mut wot_index, - &wot_db, - Some(free_fork_id), - &self - .currency_databases - .tx_db - .read(|db| db.clone()) - .expect("Fail to read TxDB."), - ) - .expect("Fail to revert block"); + DursMsg::Response { + res_from: _, + res_to: _, + ref req_id, + ref res_content, + } => match *res_content { + DursResContent::NetworkResponse(ref network_response) => { + debug!("BlockchainModule : receive NetworkResponse() !"); + if let Some(request) = pending_network_requests.remove(req_id) { + match request { + OldNetworkRequest::GetConsensus(_) => { + if let NetworkResponse::Consensus(_, response) = + *network_response.deref() + { + if let Ok(blockstamp) = response { + consensus = blockstamp; + if current_blockstamp.id.0 > consensus.id.0 + 2 + { + // Find free fork id + let free_fork_id = ForkId(49); + // Get last dal_block + let last_dal_block_id = + BlockId(current_blockstamp.id.0 - 1); + let last_dal_block = self + .blocks_databases + .blockchain_db + .read(|db| db.get(&last_dal_block_id).cloned()) + .expect("Fail to read blockchain DB.") + .expect( + "Fatal error : not foutn last dal block !", + ); + revert_block::revert_block( + &last_dal_block, + &mut wot_index, + &wot_db, + Some(free_fork_id), + &self + .currency_databases + .tx_db + .read(|db| db.clone()) + .expect("Fail to read TxDB."), + ) + .expect("Fail to revert block"); + } } } } - } - OldNetworkRequest::GetBlocks(_, _, _, _) => { - if let NetworkResponse::Chunk(_, _, ref blocks) = - *network_response.deref() - { - let new_current_blockstamp = self.receive_blocks( - blocks, - ¤t_blockstamp, - &mut wot_index, - &wot_db, - ); - if current_blockstamp != new_current_blockstamp { - current_blockstamp = new_current_blockstamp; - // Update forks states - self.forks_states = duniter_dal::block::get_forks( - &self.blocks_databases.forks_db, - current_blockstamp, - ) - .expect("get_forks() : DALError"); + OldNetworkRequest::GetBlocks(_, _, _, _) => { + if let NetworkResponse::Chunk(_, _, ref blocks) = + *network_response.deref() + { + let new_current_blockstamp = self.receive_blocks( + blocks, + ¤t_blockstamp, + &mut wot_index, + &wot_db, + ); + if current_blockstamp != new_current_blockstamp { + current_blockstamp = new_current_blockstamp; + // Update forks states + self.forks_states = + durs_blockchain_dal::block::get_forks( + &self.blocks_databases.forks_db, + current_blockstamp, + ) + .expect("get_forks() : DALError"); + } } } + _ => {} } - _ => {} } } - } - DursMsgContent::ReceiveDocsFromClient(ref docs) => { - self.receive_documents(docs); - } - DursMsgContent::Stop() => break, - _ => {} + _ => {} // Others DursResContent variants + }, + DursMsg::Stop => break, + _ => {} // Others DursMsg variants } } Err(e) => match e { @@ -731,12 +758,13 @@ impl BlockchainModule { { last_get_stackables_blocks = now; loop { - let stackable_blocks = duniter_dal::block::DALBlock::get_stackables_blocks( - &self.blocks_databases.forks_db, - &self.blocks_databases.forks_blocks_db, - ¤t_blockstamp, - ) - .expect("Fatal error : Fail to read ForksV10DB !"); + let stackable_blocks = + durs_blockchain_dal::block::DALBlock::get_stackables_blocks( + &self.blocks_databases.forks_db, + &self.blocks_databases.forks_blocks_db, + ¤t_blockstamp, + ) + .expect("Fatal error : Fail to read ForksV10DB !"); if stackable_blocks.is_empty() { break; } else { @@ -825,11 +853,11 @@ pub fn complete_network_block( let mut block_doc = network_block_v10.uncompleted_block_doc.clone(); trace!("complete_network_block #{}...", block_doc.number); block_doc.certifications = - duniter_dal::parsers::certifications::parse_certifications_into_compact( + durs_blockchain_dal::parsers::certifications::parse_certifications_into_compact( &network_block_v10.certifications, ); trace!("Success to complete certs."); - block_doc.revoked = duniter_dal::parsers::revoked::parse_revocations_into_compact( + block_doc.revoked = durs_blockchain_dal::parsers::revoked::parse_revocations_into_compact( &network_block_v10.revoked, ); trace!("Success to complete certs & revocations."); diff --git a/lib/modules/blockchain/revert_block.rs b/lib/modules/blockchain/blockchain/revert_block.rs similarity index 97% rename from lib/modules/blockchain/revert_block.rs rename to lib/modules/blockchain/blockchain/revert_block.rs index 64cfb889..b1d7f7e8 100644 --- a/lib/modules/blockchain/revert_block.rs +++ b/lib/modules/blockchain/blockchain/revert_block.rs @@ -16,12 +16,12 @@ use dubp_documents::v10::block::TxDocOrTxHash; use dubp_documents::v10::transaction::{TxAmount, TxBase}; use dubp_documents::Document; -use duniter_dal::block::DALBlock; -use duniter_dal::sources::SourceAmount; -use duniter_dal::writers::requests::*; -use duniter_dal::writers::transaction::DALTxV10; -use duniter_dal::{BinDB, ForkId, TxV10Datas}; use dup_crypto::keys::*; +use durs_blockchain_dal::block::DALBlock; +use durs_blockchain_dal::sources::SourceAmount; +use durs_blockchain_dal::writers::requests::*; +use durs_blockchain_dal::writers::transaction::DALTxV10; +use durs_blockchain_dal::{BinDB, ForkId, TxV10Datas}; use durs_wot::data::{NewLinkResult, RemLinkResult}; use durs_wot::{NodeId, WebOfTrust}; use std::collections::HashMap; diff --git a/lib/modules/blockchain/sync.rs b/lib/modules/blockchain/blockchain/sync.rs similarity index 97% rename from lib/modules/blockchain/sync.rs rename to lib/modules/blockchain/blockchain/sync.rs index 480d8577..6dbdb6a5 100644 --- a/lib/modules/blockchain/sync.rs +++ b/lib/modules/blockchain/blockchain/sync.rs @@ -21,12 +21,12 @@ extern crate threadpool; use self::pbr::ProgressBar; use self::threadpool::ThreadPool; use dubp_documents::{BlockHash, BlockId}; -use duniter_dal::currency_params::CurrencyParameters; -use duniter_dal::writers::requests::*; -use duniter_dal::ForkId; use duniter_network::documents::NetworkBlock; use dup_crypto::hashs::Hash; use dup_crypto::keys::*; +use durs_blockchain_dal::currency_params::CurrencyParameters; +use durs_blockchain_dal::writers::requests::*; +use durs_blockchain_dal::ForkId; use durs_wot::NodeId; use std::collections::{HashMap, VecDeque}; use std::fs; @@ -158,7 +158,7 @@ pub fn sync_ts<DC: DuniterConf>( debug!("Get local current blockstamp..."); let db_path = duniter_conf::get_blockchain_db_path(&profile_copy, ¤cy); let blocks_databases = BlocksV10DBs::open(Some(&db_path)); - let current_blockstamp: Blockstamp = duniter_dal::block::get_current_blockstamp( + let current_blockstamp: Blockstamp = durs_blockchain_dal::block::get_current_blockstamp( &blocks_databases, ).expect("ForksV10DB : RustBreakError !") .unwrap_or_default(); @@ -268,9 +268,10 @@ pub fn sync_ts<DC: DuniterConf>( // Get local current blockstamp debug!("Get local current blockstamp..."); - let mut current_blockstamp: Blockstamp = duniter_dal::block::get_current_blockstamp(&databases) - .expect("ForksV10DB : RustBreakError !") - .unwrap_or_default(); + let mut current_blockstamp: Blockstamp = + durs_blockchain_dal::block::get_current_blockstamp(&databases) + .expect("ForksV10DB : RustBreakError !") + .unwrap_or_default(); debug!("Success to get local current blockstamp."); // Node is already synchronized ? @@ -528,8 +529,9 @@ pub fn sync_ts<DC: DuniterConf>( blocks_not_expiring.pop_front(); } // Find expire_certs - let expire_certs = duniter_dal::certs::find_expire_certs(&certs_db, blocks_expiring) - .expect("find_expire_certs() : DALError"); + let expire_certs = + durs_blockchain_dal::certs::find_expire_certs(&certs_db, blocks_expiring) + .expect("find_expire_certs() : DALError"); // Apply block let apply_valid_block_begin = SystemTime::now(); if let Ok(ValidBlockApplyReqs(block_req, wot_db_reqs, currency_db_reqs)) = diff --git a/lib/modules/blockchain/ts_parsers.rs b/lib/modules/blockchain/blockchain/ts_parsers.rs similarity index 100% rename from lib/modules/blockchain/ts_parsers.rs rename to lib/modules/blockchain/blockchain/ts_parsers.rs diff --git a/lib/modules/skeleton/Cargo.toml b/lib/modules/skeleton/Cargo.toml index 7619cafe..a83af23f 100644 --- a/lib/modules/skeleton/Cargo.toml +++ b/lib/modules/skeleton/Cargo.toml @@ -10,8 +10,7 @@ path = "lib.rs" [dependencies] duniter-conf = { path = "../../core/conf" } -duniter-dal = { path = "../../tools/dal" } -duniter-message = { path = "../../core/message" } +durs-message = { path = "../../core/message" } duniter-module = { path = "../../core/module" } duniter-network = { path = "../../core/network" } dup-crypto = { path = "../../tools/crypto" } diff --git a/lib/modules/skeleton/lib.rs b/lib/modules/skeleton/lib.rs index 81dd950e..fb15ed45 100644 --- a/lib/modules/skeleton/lib.rs +++ b/lib/modules/skeleton/lib.rs @@ -36,17 +36,16 @@ extern crate serde_derive; extern crate structopt; extern crate duniter_conf; -extern crate duniter_dal; -extern crate duniter_message; extern crate duniter_module; extern crate dup_crypto; +extern crate durs_message; extern crate serde; extern crate serde_json; use duniter_conf::DuRsConf; -use duniter_dal::dal_event::DALEvent; -use duniter_message::*; use duniter_module::*; +use durs_message::events::*; +use durs_message::*; use std::ops::Deref; use std::sync::mpsc; use std::thread; @@ -218,7 +217,7 @@ impl DuniterModule<DuRsConf, DursMsg> for SkeletonModule { loop { match proxy_receiver.recv() { Ok(message) => { - let stop = if let DursMsg(_, DursMsgContent::Stop()) = message { + let stop = if let DursMsg::Stop = message { true } else { false @@ -252,10 +251,9 @@ impl DuniterModule<DuRsConf, DursMsg> for SkeletonModule { // Get messages match skeleton_receiver.recv_timeout(Duration::from_millis(250)) { Ok(ref message) => match *message { - SkeletonMsg::DursMsg(ref duniter_message) => { - match (*duniter_message.deref()).1 // Match Durs message content - { - DursMsgContent::Stop() => { + SkeletonMsg::DursMsg(ref durs_message) => { + match durs_message.deref() { + DursMsg::Stop => { // Relay stop signal to all child threads let _result_stop_propagation: Result< (), @@ -263,21 +261,33 @@ impl DuniterModule<DuRsConf, DursMsg> for SkeletonModule { > = datas .child_threads .iter() - .map(|t| t.send(SkeletonMsg::DursMsg(Box::new(DursMsg(DursMsgReceiver::All, DursMsgContent::Stop()))))) + .map(|t| t.send(SkeletonMsg::DursMsg(Box::new(DursMsg::Stop)))) .collect(); // Relay stop signal to router - let _result = router_sender.send(RouterThreadMessage::ModuleMessage(DursMsg(DursMsgReceiver::All, DursMsgContent::Stop()))); + let _result = router_sender + .send(RouterThreadMessage::ModuleMessage(DursMsg::Stop)); // Break main loop break; } - DursMsgContent::DALEvent(ref dal_event) => match *dal_event { - DALEvent::StackUpValidBlock(ref _block, ref _blockstamp) => { - // Do something when the node has stacked a new block at its local blockchain - } - DALEvent::RevertBlocks(ref _blocks) => { - // Do something when the node has destacked blocks from its local blockchain (roll back) + DursMsg::Event { + event_type: _, + ref event_content, + } => match *event_content { + DursEvent::BlockchainEvent(ref blockchain_event) => { + match *blockchain_event { + BlockchainEvent::StackUpValidBlock( + ref _block, + ref _blockstamp, + ) => { + // Do something when the node has stacked a new block at its local blockchain + } + BlockchainEvent::RevertBlocks(ref _blocks) => { + // Do something when the node has destacked blocks from its local blockchain (roll back) + } + _ => {} // Do nothing for events that don't concern your module. + } } - _ => {} // Do nothing for events that don't concern your module. + _ => {} // Do nothing for DursEvent variants that don't concern your module. }, _ => {} // Do nothing for DursMsgContent variants that don't concern your module. } diff --git a/lib/modules/tui/Cargo.toml b/lib/modules/tui/Cargo.toml index 3a8e00cf..c469074b 100644 --- a/lib/modules/tui/Cargo.toml +++ b/lib/modules/tui/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "duniter-tui" +name = "durs-tui" version = "0.1.0-a0.1" authors = ["librelois <elois@ifee.fr>"] description = "Terminal user interface for Duniter-Rs." @@ -11,9 +11,8 @@ path = "lib.rs" [dependencies] duniter-conf = { path = "../../core/conf" } dup-crypto = { path = "../../tools/crypto" } -duniter-dal = { path = "../../tools/dal" } dubp-documents= { path = "../../tools/documents" } -duniter-message = { path = "../../core/message" } +durs-message = { path = "../../core/message" } duniter-module = { path = "../../core/module" } duniter-network = { path = "../../core/network" } durs-network-documents = { path = "../../tools/network-documents" } diff --git a/lib/modules/tui/lib.rs b/lib/modules/tui/lib.rs index 731fa83f..acbe49d3 100644 --- a/lib/modules/tui/lib.rs +++ b/lib/modules/tui/lib.rs @@ -38,21 +38,20 @@ extern crate structopt; extern crate dubp_documents; extern crate duniter_conf; -extern crate duniter_dal; -extern crate duniter_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; +extern crate durs_message; extern crate durs_network_documents; extern crate serde; extern crate serde_json; extern crate termion; use duniter_conf::DuRsConf; -use duniter_dal::dal_event::DALEvent; -use duniter_message::*; use duniter_module::*; use duniter_network::events::NetworkEvent; +use durs_message::events::*; +use durs_message::*; use durs_network_documents::network_head::NetworkHead; use durs_network_documents::NodeFullId; use std::collections::HashMap; @@ -116,7 +115,7 @@ pub struct Connection { /// Data that the Tui module needs to cache pub struct TuiModuleDatas { /// Sender of all other modules - pub followers: Vec<mpsc::Sender<DursMsgContent>>, + pub followers: Vec<mpsc::Sender<DursMsg>>, /// HEADs cache content pub heads_cache: HashMap<NodeFullId, NetworkHead>, /// Position of the 1st head displayed on the screen @@ -433,7 +432,7 @@ impl DuniterModule<DuRsConf, DursMsg> for TuiModule { let (proxy_sender, proxy_receiver): (mpsc::Sender<DursMsg>, mpsc::Receiver<DursMsg>) = mpsc::channel(); - // Launch a proxy thread that transform DursMsgContent() to TuiMess::DursMsgContent(DursMsgContent()) + // Launch a proxy thread that transform DursMsg() to TuiMess::DursMsg(DursMsg()) let tui_sender_clone = tui_sender.clone(); thread::spawn(move || { // Send proxy sender to main @@ -456,7 +455,7 @@ impl DuniterModule<DuRsConf, DursMsg> for TuiModule { loop { match proxy_receiver.recv() { Ok(message) => { - let stop = if let DursMsg(_, DursMsgContent::Stop()) = message { + let stop = if let DursMsg::Stop = message { true } else { false @@ -516,8 +515,8 @@ impl DuniterModule<DuRsConf, DursMsg> for TuiModule { // Get messages match tui_receiver.recv_timeout(Duration::from_millis(250)) { Ok(ref message) => match *message { - TuiMess::DursMsg(ref duniter_message) => match (*duniter_message.deref()).1 { - DursMsgContent::Stop() => { + TuiMess::DursMsg(ref durs_message) => match durs_message.deref() { + DursMsg::Stop => { writeln!( stdout, "{}{}{}{}{}", @@ -528,53 +527,59 @@ impl DuniterModule<DuRsConf, DursMsg> for TuiModule { clear::All, ) .unwrap(); - let _result_stop_propagation: Result< - (), - mpsc::SendError<DursMsgContent>, - > = tui - .followers - .iter() - .map(|f| f.send(DursMsgContent::Stop())) - .collect(); + let _result_stop_propagation: Result<(), mpsc::SendError<DursMsg>> = + tui.followers + .iter() + .map(|f| f.send(DursMsg::Stop)) + .collect(); break; } - DursMsgContent::DALEvent(ref dal_event) => match *dal_event { - DALEvent::StackUpValidBlock(ref _block, ref _blockstamp) => {} - DALEvent::RevertBlocks(ref _blocks) => {} - _ => {} - }, - DursMsgContent::NetworkEvent(ref network_event) => match *network_event { - NetworkEvent::ConnectionStateChange( - ref node_full_id, - ref status, - ref uid, - ref url, - ) => { - if let Some(conn) = tui.connections_status.get(&node_full_id) { - if *status == 12 && (*conn).status != 12 { - tui.established_conns_count += 1; - } else if *status != 12 - && (*conn).status == 12 - && tui.established_conns_count > 0 - { - tui.established_conns_count -= 1; - } - }; - tui.connections_status.insert( - *node_full_id, - Connection { - status: *status, - url: url.clone(), - uid: uid.clone(), - }, - ); - } - NetworkEvent::ReceiveHeads(ref heads) => { - heads - .iter() - .map(|h| tui.heads_cache.insert(h.node_full_id(), h.clone())) - .for_each(drop); - } + DursMsg::Event { + event_type: _, + ref event_content, + } => match *event_content { + DursEvent::BlockchainEvent(ref dal_event) => match *dal_event { + BlockchainEvent::StackUpValidBlock(ref _block, ref _blockstamp) => { + } + BlockchainEvent::RevertBlocks(ref _blocks) => {} + _ => {} + }, + DursEvent::NetworkEvent(ref network_event) => match *network_event { + NetworkEvent::ConnectionStateChange( + ref node_full_id, + ref status, + ref uid, + ref url, + ) => { + if let Some(conn) = tui.connections_status.get(&node_full_id) { + if *status == 12 && (*conn).status != 12 { + tui.established_conns_count += 1; + } else if *status != 12 + && (*conn).status == 12 + && tui.established_conns_count > 0 + { + tui.established_conns_count -= 1; + } + }; + tui.connections_status.insert( + *node_full_id, + Connection { + status: *status, + url: url.clone(), + uid: uid.clone(), + }, + ); + } + NetworkEvent::ReceiveHeads(ref heads) => { + heads + .iter() + .map(|h| { + tui.heads_cache.insert(h.node_full_id(), h.clone()) + }) + .for_each(drop); + } + _ => {} + }, _ => {} }, _ => {} @@ -592,14 +597,11 @@ impl DuniterModule<DuRsConf, DursMsg> for TuiModule { clear::All, ) .unwrap(); - let _result_stop_propagation: Result< - (), - mpsc::SendError<DursMsgContent>, - > = tui - .followers - .iter() - .map(|f| f.send(DursMsgContent::Stop())) - .collect(); + let _result_stop_propagation: Result<(), mpsc::SendError<DursMsg>> = + tui.followers + .iter() + .map(|f| f.send(DursMsg::Stop)) + .collect(); break; } Event::Mouse(ref me) => match *me { diff --git a/lib/modules/ws2p-v1-legacy/Cargo.toml b/lib/modules/ws2p-v1-legacy/Cargo.toml index 4b65d262..8d5d0df7 100644 --- a/lib/modules/ws2p-v1-legacy/Cargo.toml +++ b/lib/modules/ws2p-v1-legacy/Cargo.toml @@ -12,10 +12,9 @@ path = "lib.rs" byteorder = "1.2.3" duniter-conf = { path = "../../core/conf" } dup-crypto = { path = "../../tools/crypto" } -duniter-dal = { path = "../../tools/dal" } dubp-documents= { path = "../../tools/documents" } durs-network-documents = { path = "../../tools/network-documents" } -duniter-message = { path = "../../core/message" } +durs-message = { path = "../../core/message" } duniter-module = { path = "../../core/module" } duniter-network = { path = "../../core/network" } durs-wot = { path = "../../tools/wot" } diff --git a/lib/modules/ws2p-v1-legacy/datas.rs b/lib/modules/ws2p-v1-legacy/datas.rs index fecfb7ae..a63f3c8e 100644 --- a/lib/modules/ws2p-v1-legacy/datas.rs +++ b/lib/modules/ws2p-v1-legacy/datas.rs @@ -15,10 +15,10 @@ use constants::*; use dubp_documents::Blockstamp; -use duniter_dal::dal_requests::DALRequest; -use duniter_message::*; use duniter_network::*; use dup_crypto::keys::*; +use durs_message::requests::BlockchainRequest; +use durs_message::*; use durs_network_documents::network_endpoint::*; use durs_network_documents::network_head::*; use durs_network_documents::*; @@ -61,32 +61,33 @@ impl WS2PModuleDatas { } Ok(conn) } - pub fn send_dal_request(&mut self, req: &DALRequest) { + pub fn send_dal_request(&mut self, req: &BlockchainRequest) { self.count_dal_requests += 1; if self.count_dal_requests == std::u32::MAX { self.count_dal_requests = 0; } self.router_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - DursMsgReceiver::Role(ModuleRole::BlockchainDatas), - DursMsgContent::Request(DursReq { - requester: WS2PModule::name(), - id: ModuleReqId(self.count_dal_requests), - content: DursReqContent::DALRequest(req.clone()), - }), - ))) + .send(RouterThreadMessage::ModuleMessage(DursMsg::Request { + req_from: WS2PModule::name(), + req_to: ModuleRole::BlockchainDatas, + req_id: ModuleReqId(self.count_dal_requests), + req_content: DursReqContent::BlockchainRequest(req.clone()), + })) .expect("Fail to send message to router !"); } pub fn send_network_req_response( &self, requester: ModuleStaticName, + req_id: ModuleReqId, response: NetworkResponse, ) { self.router_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - DursMsgReceiver::One(requester), - DursMsgContent::NetworkResponse(response), - ))) + .send(RouterThreadMessage::ModuleMessage(DursMsg::Response { + res_from: WS2PModule::name(), + res_to: requester, + req_id, + res_content: DursResContent::NetworkResponse(response), + })) .expect("Fail to send message to router !"); } pub fn send_network_event(&self, event: &NetworkEvent) { @@ -107,12 +108,13 @@ impl WS2PModuleDatas { } NetworkEvent::ReceiveHeads(_) => ModuleEvent::NewValidHeadFromNetwork, NetworkEvent::ReceivePeers(_) => ModuleEvent::NewValidPeerFromNodeNetwork, + NetworkEvent::NewSelfPeer(_) => ModuleEvent::NewSelfPeer, }; self.router_sender - .send(RouterThreadMessage::ModuleMessage(DursMsg( - DursMsgReceiver::Event(module_event), - DursMsgContent::NetworkEvent(event.clone()), - ))) + .send(RouterThreadMessage::ModuleMessage(DursMsg::Event { + event_type: module_event, + event_content: DursEvent::NetworkEvent(event.clone()), + })) .expect("Fail to send network event to router !"); } pub fn get_network_consensus(&self) -> Result<Blockstamp, NetworkConsensusError> { diff --git a/lib/modules/ws2p-v1-legacy/lib.rs b/lib/modules/ws2p-v1-legacy/lib.rs index d689171e..9d6a8f80 100644 --- a/lib/modules/ws2p-v1-legacy/lib.rs +++ b/lib/modules/ws2p-v1-legacy/lib.rs @@ -39,11 +39,10 @@ extern crate structopt; extern crate byteorder; extern crate dubp_documents; extern crate duniter_conf; -extern crate duniter_dal; -extern crate duniter_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; +extern crate durs_message; extern crate durs_network_documents; extern crate rand; extern crate sqlite; @@ -67,15 +66,16 @@ use constants::*; use datas::*; use dubp_documents::Blockstamp; use duniter_conf::DuRsConf; -use duniter_dal::dal_event::DALEvent; -use duniter_dal::dal_requests::{DALReqBlockchain, DALRequest, DALResBlockchain, DALResponse}; -use duniter_message::*; use duniter_module::*; use duniter_network::documents::*; use duniter_network::events::*; use duniter_network::requests::*; use duniter_network::*; use dup_crypto::keys::*; +use durs_message::events::*; +use durs_message::requests::*; +use durs_message::responses::*; +use durs_message::*; use durs_network_documents::network_endpoint::*; use durs_network_documents::network_head::*; use durs_network_documents::*; @@ -343,7 +343,7 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { loop { match proxy_receiver.recv() { Ok(message) => { - let stop = if let DursMsgContent::Stop() = message.1 { + let stop = if let DursMsg::Stop = message { true } else { false @@ -408,189 +408,207 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { .recv_timeout(Duration::from_millis(200)) { Ok(message) => match message { - WS2PThreadSignal::DursMsg(ref duniter_mesage) => { - match (*duniter_mesage.deref()).1 { - DursMsgContent::Stop() => break, - DursMsgContent::OldNetworkRequest(ref request) => match *request { - OldNetworkRequest::GetBlocks( - ref req_id, - ref receiver, - ref count, - ref from, - ) => { - if *receiver == NodeFullId::default() { - let mut receiver_index = 0; - let mut real_receiver = NodeFullId::default(); - for (ws2p_full_id, (_ep, state)) in - ws2p_module.ws2p_endpoints.clone() - { - if let WS2PConnectionState::Established = state { - if receiver_index == next_receiver { - real_receiver = ws2p_full_id; - break; - } - receiver_index += 1; - } - } - if real_receiver == NodeFullId::default() { - next_receiver = 0; + WS2PThreadSignal::DursMsg(ref durs_mesage) => { + match *durs_mesage.deref() { + DursMsg::Stop => break, + DursMsg::Request { + req_from: _, + req_to: _, + req_id: _, + ref req_content, + } => match *req_content { + DursReqContent::OldNetworkRequest(ref request) => match *request { + OldNetworkRequest::GetBlocks( + ref req_id, + ref receiver, + ref count, + ref from, + ) => { + if *receiver == NodeFullId::default() { + let mut receiver_index = 0; + let mut real_receiver = NodeFullId::default(); for (ws2p_full_id, (_ep, state)) in ws2p_module.ws2p_endpoints.clone() { if let WS2PConnectionState::Established = state { - real_receiver = ws2p_full_id; - break; + if receiver_index == next_receiver { + real_receiver = ws2p_full_id; + break; + } + receiver_index += 1; } } + if real_receiver == NodeFullId::default() { + next_receiver = 0; + for (ws2p_full_id, (_ep, state)) in + ws2p_module.ws2p_endpoints.clone() + { + if let WS2PConnectionState::Established = state + { + real_receiver = ws2p_full_id; + break; + } + } + } else { + next_receiver += 1; + } + if real_receiver != NodeFullId::default() { + let _blocks_request_result = ws2p_module + .send_request_to_specific_node( + &real_receiver, + &OldNetworkRequest::GetBlocks( + *req_id, *receiver, *count, *from, + ), + ); + } } else { - next_receiver += 1; - } - if real_receiver != NodeFullId::default() { let _blocks_request_result = ws2p_module .send_request_to_specific_node( - &real_receiver, + &receiver, &OldNetworkRequest::GetBlocks( *req_id, *receiver, *count, *from, ), ); } - } else { - let _blocks_request_result = ws2p_module - .send_request_to_specific_node( - &receiver, - &OldNetworkRequest::GetBlocks( - *req_id, *receiver, *count, *from, - ), - ); } - } - OldNetworkRequest::GetEndpoints(ref _request) => {} - _ => {} + OldNetworkRequest::GetEndpoints(ref _request) => {} + _ => {} + }, + _ => {} // Others DursReqContent variants }, - DursMsgContent::DALEvent(ref dal_event) => match *dal_event { - DALEvent::StackUpValidBlock(ref _block, ref blockstamp) => { - current_blockstamp = *blockstamp; - debug!( - "WS2PModule : current_blockstamp = {}", - current_blockstamp - ); - ws2p_module.my_head = Some(heads::generate_my_head( - &key_pair, - NodeId(soft_meta_datas.conf.my_node_id()), - soft_meta_datas.soft_name, - soft_meta_datas.soft_version, - ¤t_blockstamp, - None, - )); - ws2p_module.send_network_event(&NetworkEvent::ReceiveHeads( - vec![ws2p_module.my_head.clone().unwrap()], - )); - // Send my head to all connections - let my_json_head = serializer::serialize_head( - ws2p_module.my_head.clone().unwrap(), - ); - trace!("Send my HEAD: {:#?}", my_json_head); - let _results: Result<(), ws::Error> = ws2p_module - .websockets - .iter_mut() - .map(|ws| { - (ws.1).0.send(Message::text( - json!({ - "name": "HEAD", - "body": { - "heads": [my_json_head] - } - }) - .to_string(), - )) - }) - .collect(); - } - DALEvent::RevertBlocks(ref _blocks) => {} - _ => {} + DursMsg::Event { + event_type: _, + ref event_content, + } => match *event_content { + DursEvent::BlockchainEvent(ref dal_event) => match *dal_event { + BlockchainEvent::StackUpValidBlock( + ref _block, + ref blockstamp, + ) => { + current_blockstamp = *blockstamp; + debug!( + "WS2PModule : current_blockstamp = {}", + current_blockstamp + ); + ws2p_module.my_head = Some(heads::generate_my_head( + &key_pair, + NodeId(soft_meta_datas.conf.my_node_id()), + soft_meta_datas.soft_name, + soft_meta_datas.soft_version, + ¤t_blockstamp, + None, + )); + ws2p_module.send_network_event( + &NetworkEvent::ReceiveHeads(vec![ws2p_module + .my_head + .clone() + .unwrap()]), + ); + // Send my head to all connections + let my_json_head = serializer::serialize_head( + ws2p_module.my_head.clone().unwrap(), + ); + trace!("Send my HEAD: {:#?}", my_json_head); + let _results: Result<(), ws::Error> = ws2p_module + .websockets + .iter_mut() + .map(|ws| { + (ws.1).0.send(Message::text( + json!({ + "name": "HEAD", + "body": { + "heads": [my_json_head] + } + }) + .to_string(), + )) + }) + .collect(); + } + BlockchainEvent::RevertBlocks(ref _blocks) => {} + _ => {} + }, + _ => {} // Others DursEvent variants }, - DursMsgContent::DALResponse(ref dal_res) => match *dal_res.deref() { - DALResponse::Blockchain(ref dal_res_bc) => { - match *dal_res_bc.deref() { - DALResBlockchain::CurrentBlock( - ref _requester_full_id, - ref current_block, - ref current_blockstamp_, - ) => { - let _current_block = current_block.deref(); - debug!( - "WS2PModule : receive DALResBc::CurrentBlock({})", - current_blockstamp - ); - current_blockstamp = *current_blockstamp_; - if ws2p_module.my_head.is_none() { - ws2p_module.my_head = - Some(heads::generate_my_head( - &key_pair, - NodeId(soft_meta_datas.conf.my_node_id()), - soft_meta_datas.soft_name, - soft_meta_datas.soft_version, - ¤t_blockstamp, - None, - )); - } - ws2p_module.send_network_event( - &NetworkEvent::ReceiveHeads(vec![ws2p_module - .my_head - .clone() - .unwrap()]), - ); + DursMsg::Response { + res_from: _, + res_to: _, + req_id: _, + ref res_content, + } => match *res_content { + DursResContent::BlockchainResponse(ref bc_res) => match *bc_res + .deref() + { + BlockchainResponse::CurrentBlock( + ref _requester_full_id, + ref current_block, + ref current_blockstamp_, + ) => { + let _current_block = current_block.deref(); + debug!( + "WS2PModule : receive DALResBc::CurrentBlock({})", + current_blockstamp + ); + current_blockstamp = *current_blockstamp_; + if ws2p_module.my_head.is_none() { + ws2p_module.my_head = Some(heads::generate_my_head( + &key_pair, + NodeId(soft_meta_datas.conf.my_node_id()), + soft_meta_datas.soft_name, + soft_meta_datas.soft_version, + ¤t_blockstamp, + None, + )); } - DALResBlockchain::UIDs(ref _req_id, ref uids) => { - // Add uids to heads - for head in ws2p_module.heads_cache.values_mut() { - if let Some(uid_option) = uids.get(&head.pubkey()) { - if let Some(ref uid) = *uid_option { - head.set_uid(uid); - ws2p_module - .uids_cache - .insert(head.pubkey(), uid.to_string()); - } else { - ws2p_module - .uids_cache - .remove(&head.pubkey()); - } - } - } - // Resent heads to other modules - ws2p_module.send_network_event( - &NetworkEvent::ReceiveHeads( + ws2p_module.send_network_event( + &NetworkEvent::ReceiveHeads(vec![ws2p_module + .my_head + .clone() + .unwrap()]), + ); + } + BlockchainResponse::UIDs(ref _req_id, ref uids) => { + // Add uids to heads + for head in ws2p_module.heads_cache.values_mut() { + if let Some(uid_option) = uids.get(&head.pubkey()) { + if let Some(ref uid) = *uid_option { + head.set_uid(uid); ws2p_module - .heads_cache - .values() - .cloned() - .collect(), - ), - ); - // Resent to other modules connections that match receive uids - for (node_full_id, (ep, conn_state)) in - &ws2p_module.ws2p_endpoints - { - if let Some(uid_option) = uids.get(&node_full_id.1) - { - ws2p_module.send_network_event( - &NetworkEvent::ConnectionStateChange( - *node_full_id, - *conn_state as u32, - uid_option.clone(), - ep.get_url(false, false) - .expect("Endpoint unreachable !"), - ), - ); + .uids_cache + .insert(head.pubkey(), uid.to_string()); + } else { + ws2p_module.uids_cache.remove(&head.pubkey()); } } } - _ => {} + // Resent heads to other modules + ws2p_module.send_network_event( + &NetworkEvent::ReceiveHeads( + ws2p_module.heads_cache.values().cloned().collect(), + ), + ); + // Resent to other modules connections that match receive uids + for (node_full_id, (ep, conn_state)) in + &ws2p_module.ws2p_endpoints + { + if let Some(uid_option) = uids.get(&node_full_id.1) { + ws2p_module.send_network_event( + &NetworkEvent::ConnectionStateChange( + *node_full_id, + *conn_state as u32, + uid_option.clone(), + ep.get_url(false, false) + .expect("Endpoint unreachable !"), + ), + ); + } + } } - } - DALResponse::Pendings(_) => {} + _ => {} // Others BlockchainResponse variants + }, + _ => {} // Others DursResContent variants }, - _ => {} + _ => {} // Others DursMsg variants } } WS2PThreadSignal::WS2PConnectionMessage(ws2p_conn_message) => match ws2p_module @@ -612,9 +630,9 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { ), ); if ws2p_module.uids_cache.get(&ws2p_full_id.1).is_none() { - ws2p_module.send_dal_request(&DALRequest::BlockchainRequest( - DALReqBlockchain::UIDs(vec![ws2p_full_id.1]), - )); + ws2p_module.send_dal_request(&BlockchainRequest::UIDs(vec![ + ws2p_full_id.1, + ])); } ws2p_module.send_network_event(&NetworkEvent::ConnectionStateChange( ws2p_full_id, @@ -691,8 +709,8 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { } WS2PSignal::Heads(ws2p_full_id, heads) => { trace!("WS2PSignal::Heads({}, {:?})", ws2p_full_id, heads.len()); - ws2p_module.send_dal_request(&DALRequest::BlockchainRequest( - DALReqBlockchain::UIDs(heads.iter().map(|h| h.pubkey()).collect()), + ws2p_module.send_dal_request(&BlockchainRequest::UIDs( + heads.iter().map(|h| h.pubkey()).collect(), )); ws2p_module.send_network_event(&NetworkEvent::ReceiveHeads( heads @@ -725,6 +743,7 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { if let Some(block) = parse_json_block(&response) { ws2p_module.send_network_req_response( req.get_req_full_id().0, + req.get_req_full_id().1, NetworkResponse::CurrentBlock( ModuleReqFullId(WS2PModule::name(), req_id), recipient_full_id, @@ -875,11 +894,11 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { if let Some(&(ref ep, ref state)) = ws2p_module.ws2p_endpoints.get(&ep_full_id) { - /*let dal_endpoint = duniter_dal::endpoint::DALEndpoint::new( + /*let dal_endpoint = durs_blockchain_dal::endpoint::DALEndpoint::new( state.clone() as u32, ep.node_uuid().unwrap().0, ep.pubkey(), - duniter_dal::endpoint::string_to_api(&ep.api().0).unwrap(), + durs_blockchain_dal::endpoint::string_to_api(&ep.api().0).unwrap(), 1, ep.to_string(), received_time.duration_since(UNIX_EPOCH).unwrap(), @@ -918,11 +937,10 @@ impl DuniterModule<DuRsConf, DursMsg> for WS2PModule { mod tests { extern crate dubp_documents; extern crate duniter_conf; - extern crate duniter_dal; - extern crate duniter_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; + extern crate durs_message; use super::parsers::blocks::parse_json_block; use super::*; diff --git a/lib/modules/ws2p/ws2p/Cargo.toml b/lib/modules/ws2p/ws2p/Cargo.toml index 13383868..e2ecfdb7 100644 --- a/lib/modules/ws2p/ws2p/Cargo.toml +++ b/lib/modules/ws2p/ws2p/Cargo.toml @@ -15,7 +15,7 @@ dup-crypto = { path = "../../../tools/crypto" } dubp-documents= { path = "../../../tools/documents" } durs-network-documents = { path = "../../../tools/network-documents" } durs-ws2p-messages = { path = "../ws2p-messages" } -duniter-message= { path = "../../../core/message" } +durs-message= { path = "../../../core/message" } duniter-module = { path = "../../../core/module" } duniter-network = { path = "../../../core/network" } log = "0.4.*" diff --git a/lib/modules/ws2p/ws2p/lib.rs b/lib/modules/ws2p/ws2p/lib.rs index 190b7b1a..1faada49 100644 --- a/lib/modules/ws2p/ws2p/lib.rs +++ b/lib/modules/ws2p/ws2p/lib.rs @@ -37,7 +37,7 @@ extern crate structopt; extern crate bincode; extern crate dubp_documents; extern crate duniter_conf; -extern crate duniter_message; +extern crate durs_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; @@ -51,7 +51,7 @@ pub mod services; use constants::*; use duniter_conf::DuRsConf; -use duniter_message::DursMsg; +use durs_message::*; use duniter_module::*; use duniter_network::*; use durs_network_documents::network_endpoint::*; diff --git a/lib/modules/ws2p/ws2p/src/lib.rs b/lib/modules/ws2p/ws2p/src/lib.rs index b03629a0..e9c3ef04 100644 --- a/lib/modules/ws2p/ws2p/src/lib.rs +++ b/lib/modules/ws2p/ws2p/src/lib.rs @@ -37,10 +37,10 @@ extern crate structopt; extern crate bincode; extern crate dubp_documents; extern crate duniter_conf; -extern crate duniter_message; extern crate duniter_module; extern crate duniter_network; extern crate dup_crypto; +extern crate durs_message; extern crate durs_network_documents; extern crate durs_ws2p_messages; @@ -51,9 +51,9 @@ pub mod services; use constants::*; use duniter_conf::DuRsConf; -use duniter_message::DursMsg; use duniter_module::*; use duniter_network::*; +use durs_message::DursMsg; use durs_network_documents::network_endpoint::*; use std::sync::mpsc; -- GitLab