From cc67c3a0ff387193155e5da6e217dec3714e8144 Mon Sep 17 00:00:00 2001 From: Jean-Marie PEDURAND <jm81@hotmail.fr> Date: Sat, 25 May 2019 18:04:01 +0200 Subject: [PATCH] [feat] durs-blockchain : add blocks sub command --- Cargo.lock | 67 ++++++++++++++++ lib/core/core/src/commands/dbex.rs | 16 ++-- lib/core/core/src/lib.rs | 4 +- .../blockchain-dal/src/readers/identity.rs | 1 - lib/modules/blockchain/blockchain/Cargo.toml | 2 + lib/modules/blockchain/blockchain/src/dbex.rs | 76 +++++++++++++++---- lib/modules/blockchain/blockchain/src/lib.rs | 5 +- 7 files changed, 145 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76aa68ce..6cac3155 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,6 +128,17 @@ dependencies = [ "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bstr" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-automata 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "byte-tools" version = "0.3.1" @@ -279,6 +290,26 @@ name = "cryptoxide" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "csv" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bstr 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "csv-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "csv-core" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "difference" version = "2.0.0" @@ -467,6 +498,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", @@ -796,6 +828,11 @@ name = "either" version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "encode_unicode" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "failure" version = "0.1.5" @@ -1016,6 +1053,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "memchr" version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "memoffset" @@ -1231,6 +1271,19 @@ dependencies = [ "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "prettytable-rs" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.2.3" @@ -1452,6 +1505,14 @@ dependencies = [ "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex-automata" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex-syntax" version = "0.6.11" @@ -1987,6 +2048,7 @@ dependencies = [ "checksum blake2b_simd 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "461f4b879a8eb70c1debf7d0788a9a5ff15f1ea9d25925fea264ef4258bed6b2" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-padding 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6d4dc3af3ee2e12f3e5d224e5e1e3d73668abbeb69e566d361f7d5563a4fdf09" +"checksum bstr 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94cdf78eb7e94c566c1f5dbe2abf8fc70a548fc902942a48c4b3a98b48ca9ade" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" @@ -2005,10 +2067,13 @@ dependencies = [ "checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" "checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" "checksum cryptoxide 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b486f0f6730b6972b7831f0faf2dc7c3280aab69bc07357c7a2f24042b945e36" +"checksum csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37519ccdfd73a75821cac9319d4fce15a81b9fcf75f951df5b9988aa3a0af87d" +"checksum csv-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9b5cadb6b25c77aeff80ba701712494213f4a8418fcda2ee11b6560c3ad0bf4c" "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" "checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" +"checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" @@ -2062,6 +2127,7 @@ dependencies = [ "checksum pest_meta 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f249ea6de7c7b7aba92b4ff4376a994c6dbd98fd2166c89d5c4947397ecb574d" "checksum pkg-config 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c1d2cfa5a714db3b5f24f0915e74fcdf91d09d496ba61329705dda7774d2af" "checksum pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a029430f0d744bc3d15dd474d591bed2402b645d024583082b9f63bb936dac6" +"checksum prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0fd04b170004fa2daccf418a7f8253aaf033c27760b5f225889024cf66d7ac2e" "checksum proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cd07deb3c6d1d9ff827999c7f9b04cdfd66b1b17ae508e14fe47b620f2282ae0" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c5c2380ae88876faae57698be9e9775e3544decad214599c3a6266cca6ac802" @@ -2086,6 +2152,7 @@ dependencies = [ "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d" "checksum regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88c3d9193984285d544df4a30c23a4e62ead42edf70a4452ceb76dac1ce05c26" +"checksum regex-automata 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "92b73c2a1770c255c240eaa4ee600df1704a38dc3feaa6e949e7fcd4f8dc09f9" "checksum regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b143cceb2ca5e56d5671988ef8b15615733e7ee16cd348e064333b251b89343f" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum rpassword 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b273c91bd242ca03ad6d71c143b6f17a48790e61f21a6c78568fa2b6774a24a4" diff --git a/lib/core/core/src/commands/dbex.rs b/lib/core/core/src/commands/dbex.rs index 85efee91..8fbe0f3b 100644 --- a/lib/core/core/src/commands/dbex.rs +++ b/lib/core/core/src/commands/dbex.rs @@ -46,6 +46,12 @@ pub enum DbExSubCommand { raw(setting = "structopt::clap::AppSettings::ColoredHelp") )] BalanceOpt(BalanceOpt), + /// Display blocks current frame + #[structopt( + name = "blocks", + raw(setting = "structopt::clap::AppSettings::ColoredHelp") + )] + BlocksOpt(BlocksOpt), /// Web of Trust distances explorer #[structopt( name = "distance", @@ -70,12 +76,6 @@ pub enum DbExSubCommand { raw(setting = "structopt::clap::AppSettings::ColoredHelp") )] MembersOpt(MembersOpt), - /// BlocksOpt - #[structopt( - name = "blocks", - raw(setting = "structopt::clap::AppSettings::ColoredHelp") - )] - BlocksOpt(BlocksOpt), } #[derive(StructOpt, Debug, Copy, Clone)] @@ -127,7 +127,7 @@ impl DursExecutableCoreCommand for DbExOpt { DbExSubCommand::BalanceOpt(balance_opts) => dbex( profile_path, self.csv, - &DBExQuery::TxQuery(DBExTxQuery::Balance(balance_opts.address)), + &DbExQuery::TxQuery(DbExTxQuery::Balance(balance_opts.address)), ), DbExSubCommand::DistanceOpt(distance_opts) => dbex( profile_path, @@ -135,7 +135,7 @@ impl DursExecutableCoreCommand for DbExOpt { &DbExQuery::WotQuery(DbExWotQuery::AllDistances(distance_opts.reverse)), ), DbExSubCommand::ForksOpt(_forks_opts) => { - dbex(profile_path, self.csv, &DBExQuery::ForkTreeQuery) + dbex(profile_path, self.csv, &DbExQuery::ForkTreeQuery) } DbExSubCommand::MemberOpt(member_opts) => dbex( profile_path, diff --git a/lib/core/core/src/lib.rs b/lib/core/core/src/lib.rs index cb07c8d0..b17037a9 100644 --- a/lib/core/core/src/lib.rs +++ b/lib/core/core/src/lib.rs @@ -43,7 +43,7 @@ use crate::commands::*; use crate::constants::DEFAULT_USER_PROFILE; use crate::errors::DursCoreError; use dubp_currency_params::CurrencyName; -use durs_blockchain::{BlockchainModule, DBExQuery}; +use durs_blockchain::{dbex::DbExQuery, BlockchainModule}; use durs_common_tools::fatal_error; pub use durs_conf::{ constants::KEYPAIRS_FILENAME, keys::*, ChangeGlobalConf, DuRsConf, DuniterKeyPairs, @@ -589,7 +589,7 @@ pub fn get_module_conf<M: DursModule<DuRsConf, DursMsg>>( } /// Launch databases explorer -pub fn dbex(profile_path: PathBuf, csv: bool, query: &DBExQuery) { +pub fn dbex(profile_path: PathBuf, csv: bool, query: &DbExQuery) { // Launch databases explorer BlockchainModule::dbex(profile_path, csv, query); } diff --git a/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs b/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs index 78539356..0e931916 100644 --- a/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs +++ b/lib/modules/blockchain/blockchain-dal/src/readers/identity.rs @@ -169,7 +169,6 @@ mod test { &mock_identities, &get_identities(&identities_db, filters, BlockNumber(5))? )); - // Test by pubkey filter filters = IdentitiesFilter::by_pubkey(pubkey('A')); assert_eq!( diff --git a/lib/modules/blockchain/blockchain/Cargo.toml b/lib/modules/blockchain/blockchain/Cargo.toml index 24c25ab3..576240b6 100644 --- a/lib/modules/blockchain/blockchain/Cargo.toml +++ b/lib/modules/blockchain/blockchain/Cargo.toml @@ -29,12 +29,14 @@ json-pest-parser = { path = "../../../tools/json-pest-parser" } log = "0.4.*" num_cpus = "1.10.*" pbr = "1.0.*" +prettytable-rs = "0.8.0" rayon = "1.0.3" serde = "1.0.*" serde_json = "1.0.*" threadpool = "1.7.*" unwrap = "1.2.1" + [dev-dependencies] dup-crypto-tests-tools = { path = "../../../tests-tools/crypto-tests-tools" } dubp-user-docs-tests-tools = { path = "../../../tests-tools/user-docs-tests-tools" } \ No newline at end of file diff --git a/lib/modules/blockchain/blockchain/src/dbex.rs b/lib/modules/blockchain/blockchain/src/dbex.rs index 8762d281..15534e05 100644 --- a/lib/modules/blockchain/blockchain/src/dbex.rs +++ b/lib/modules/blockchain/blockchain/src/dbex.rs @@ -23,12 +23,18 @@ use dup_crypto::keys::*; use durs_wot::data::rusty::RustyWebOfTrust; use durs_wot::data::WebOfTrust; use durs_wot::operations::distance::{DistanceCalculator, WotDistance, WotDistanceParameters}; +use prettytable::Table; use std::time::*; use unwrap::unwrap; +/// Error message for empty blockchain case pub static EMPTY_BLOCKCHAIN: &'static str = "No blockchain, please sync your node to get a blockchain."; +static PUB_KEY: &'static str = "PUBKEY"; +static BLOCK: &'static str = "BLOCK"; +static USERNAME: &'static str = "USERNAME"; + #[derive(Debug, Copy, Clone)] /// Query for blockchain databases explorer pub enum DbExBcQuery { @@ -70,30 +76,27 @@ pub enum DbExQuery { } /// Execute DbExQuery -pub fn dbex(profile_path: PathBuf, csv: bool, query: &DBExQuery) { +pub fn dbex(profile_path: PathBuf, csv: bool, query: &DbExQuery) { match *query { - DBExQuery::ForkTreeQuery => dbex_fork_tree(profile_path, csv), + DbExQuery::ForkTreeQuery => dbex_fork_tree(profile_path, csv), DbExQuery::BcQuery(bc_query) => { dbex_bc(profile_path, csv, bc_query).expect("Error: fail to open DB.") } - DBExQuery::TxQuery(ref tx_query) => dbex_tx(profile_path, csv, tx_query), - DBExQuery::WotQuery(ref wot_query) => dbex_wot(profile_path, csv, wot_query), + DbExQuery::TxQuery(ref tx_query) => dbex_tx(profile_path, csv, tx_query), + DbExQuery::WotQuery(ref wot_query) => dbex_wot(profile_path, csv, wot_query), } } /// Execute DbExBcQuery -pub fn dbex_bc<DC: DursConfTrait>( - profile_path: PathBuf, - conf: &DC, - _csv: bool, - _query: DbExBcQuery, -) -> Result<(), DALError> { +pub fn dbex_bc(profile_path: PathBuf, _csv: bool, _query: DbExBcQuery) -> Result<(), DALError> { // Get db path - let db_path = durs_conf::get_blockchain_db_path(profile_path, &conf.currency()); + let db_path = durs_conf::get_blockchain_db_path(profile_path); // Open databases let load_dbs_begin = SystemTime::now(); let blocks_db = BlocksV10DBs::open(Some(&db_path)); + //let forks_dbs = ForksDBs::open(Some(&db_path)); + let wot_databases = WotsV10DBs::open(Some(&db_path)); let load_dbs_duration = SystemTime::now() .duration_since(load_dbs_begin) @@ -107,12 +110,55 @@ pub fn dbex_bc<DC: DursConfTrait>( if let Some(current_blockstamp) = durs_blockchain_dal::readers::block::get_current_blockstamp(&blocks_db)? { - println!("Current block: #{}", current_blockstamp); + println!("Current block: #{}.", current_blockstamp); + if let Some(current_block) = durs_blockchain_dal::readers::block::get_block( + &blocks_db.blockchain_db, + None, + ¤t_blockstamp, + )? { + let map_pubkey = durs_blockchain_dal::readers::block::get_current_frame( + ¤t_block, + &blocks_db.blockchain_db, + )?; + + let mut vec = map_pubkey.iter().collect::<Vec<(&PubKey, &usize)>>(); + vec.sort_by(|a, b| b.1.cmp(&a.1)); + + if _csv { + println!("{},{},{}", &BLOCK, &USERNAME, &PUB_KEY); + for (pub_key, v) in &vec { + if let Ok(Some(identity)) = durs_blockchain_dal::readers::identity::get_identity( + &wot_databases.identities_db, + &pub_key, + ) { + println!( + "{},{},{}", + v, + identity.idty_doc.username(), + pub_key.to_string() + ); + } + } + } else { + let mut table = Table::new(); + table.add_row(row![&BLOCK, &USERNAME, &PUB_KEY]); + for (pub_key, v) in &vec { + if let Ok(Some(identity)) = durs_blockchain_dal::readers::identity::get_identity( + &wot_databases.identities_db, + &pub_key, + ) { + table.add_row(row![v, identity.idty_doc.username(), pub_key.to_string()]); + } + } + table.printstd(); + } + } } Ok(()) } +/// Print fork tree pub fn dbex_fork_tree(profile_path: PathBuf, _csv: bool) { // Get db path let db_path = durs_conf::get_blockchain_db_path(profile_path); @@ -143,7 +189,8 @@ pub fn dbex_fork_tree(profile_path: PathBuf, _csv: bool) { } } -pub fn dbex_tx(profile_path: PathBuf, _csv: bool, query: &DBExTxQuery) { +/// Execute DbExTxQuery +pub fn dbex_tx(profile_path: PathBuf, _csv: bool, query: &DbExTxQuery) { // Get db path let db_path = durs_conf::get_blockchain_db_path(profile_path); @@ -202,7 +249,8 @@ pub fn dbex_tx(profile_path: PathBuf, _csv: bool, query: &DBExTxQuery) { ); } -pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) { +/// Execute DbExWotQuery +pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DbExWotQuery) { // Get db path let db_path = durs_conf::get_blockchain_db_path(profile_path.clone()); diff --git a/lib/modules/blockchain/blockchain/src/lib.rs b/lib/modules/blockchain/blockchain/src/lib.rs index b6de26b5..a9ef9a07 100644 --- a/lib/modules/blockchain/blockchain/src/lib.rs +++ b/lib/modules/blockchain/blockchain/src/lib.rs @@ -33,6 +33,9 @@ #[macro_use] extern crate log; +#[macro_use] +extern crate prettytable; + mod constants; pub mod dbex; mod dubp; @@ -236,7 +239,7 @@ impl BlockchainModule { } } /// Databases explorer - pub fn dbex(profile_path: PathBuf, csv: bool, req: &DBExQuery) { + pub fn dbex(profile_path: PathBuf, csv: bool, req: &DbExQuery) { dbex::dbex(profile_path, csv, req); } /// Synchronize blockchain from local duniter json files -- GitLab