Skip to content
Snippets Groups Projects
Commit 13f3845f authored by Éloïs's avatar Éloïs
Browse files

[ref] core:dbex: refactor dbex & add blockchain queries

wip
parent 6298000d
No related branches found
No related tags found
1 merge request!195Elois/rml13
...@@ -19,7 +19,7 @@ use crate::commands::DursExecutableCoreCommand; ...@@ -19,7 +19,7 @@ use crate::commands::DursExecutableCoreCommand;
use crate::dbex; use crate::dbex;
use crate::errors::DursCoreError; use crate::errors::DursCoreError;
use crate::DursCore; use crate::DursCore;
use durs_blockchain::{DBExQuery, DBExTxQuery, DBExWotQuery}; use durs_blockchain::dbex::{DbExBcQuery, DbExQuery, DbExTxQuery, DbExWotQuery};
use durs_conf::DuRsConf; use durs_conf::DuRsConf;
#[derive(StructOpt, Debug, Clone)] #[derive(StructOpt, Debug, Clone)]
...@@ -70,6 +70,12 @@ pub enum DbExSubCommand { ...@@ -70,6 +70,12 @@ pub enum DbExSubCommand {
raw(setting = "structopt::clap::AppSettings::ColoredHelp") raw(setting = "structopt::clap::AppSettings::ColoredHelp")
)] )]
MembersOpt(MembersOpt), MembersOpt(MembersOpt),
/// BlocksOpt
#[structopt(
name = "blocks",
raw(setting = "structopt::clap::AppSettings::ColoredHelp")
)]
BlocksOpt(BlocksOpt),
} }
#[derive(StructOpt, Debug, Copy, Clone)] #[derive(StructOpt, Debug, Copy, Clone)]
...@@ -109,6 +115,10 @@ pub struct BalanceOpt { ...@@ -109,6 +115,10 @@ pub struct BalanceOpt {
pub address: String, pub address: String,
} }
#[derive(StructOpt, Debug, Copy, Clone)]
/// BlocksOpt
pub struct BlocksOpt {}
impl DursExecutableCoreCommand for DbExOpt { impl DursExecutableCoreCommand for DbExOpt {
fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> { fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> {
let profile_path = durs_core.soft_meta_datas.profile_path; let profile_path = durs_core.soft_meta_datas.profile_path;
...@@ -122,7 +132,7 @@ impl DursExecutableCoreCommand for DbExOpt { ...@@ -122,7 +132,7 @@ impl DursExecutableCoreCommand for DbExOpt {
DbExSubCommand::DistanceOpt(distance_opts) => dbex( DbExSubCommand::DistanceOpt(distance_opts) => dbex(
profile_path, profile_path,
self.csv, self.csv,
&DBExQuery::WotQuery(DBExWotQuery::AllDistances(distance_opts.reverse)), &DbExQuery::WotQuery(DbExWotQuery::AllDistances(distance_opts.reverse)),
), ),
DbExSubCommand::ForksOpt(_forks_opts) => { DbExSubCommand::ForksOpt(_forks_opts) => {
dbex(profile_path, self.csv, &DBExQuery::ForkTreeQuery) dbex(profile_path, self.csv, &DBExQuery::ForkTreeQuery)
...@@ -130,23 +140,28 @@ impl DursExecutableCoreCommand for DbExOpt { ...@@ -130,23 +140,28 @@ impl DursExecutableCoreCommand for DbExOpt {
DbExSubCommand::MemberOpt(member_opts) => dbex( DbExSubCommand::MemberOpt(member_opts) => dbex(
profile_path, profile_path,
self.csv, self.csv,
&DBExQuery::WotQuery(DBExWotQuery::MemberDatas(member_opts.uid)), &DbExQuery::WotQuery(DbExWotQuery::MemberDatas(member_opts.uid)),
), ),
DbExSubCommand::MembersOpt(members_opts) => { DbExSubCommand::MembersOpt(members_opts) => {
if members_opts.expire { if members_opts.expire {
dbex( dbex(
profile_path, profile_path,
self.csv, self.csv,
&DBExQuery::WotQuery(DBExWotQuery::ExpireMembers(members_opts.reverse)), &DbExQuery::WotQuery(DbExWotQuery::ExpireMembers(members_opts.reverse)),
); );
} else { } else {
dbex( dbex(
profile_path, profile_path,
self.csv, self.csv,
&DBExQuery::WotQuery(DBExWotQuery::ListMembers(members_opts.reverse)), &DbExQuery::WotQuery(DbExWotQuery::ListMembers(members_opts.reverse)),
); );
} }
} }
DbExSubCommand::BlocksOpt(_blocks_opts) => dbex(
profile_path,
self.csv,
&DbExQuery::BcQuery(DbExBcQuery::CountBlocksPerIssuer),
),
} }
Ok(()) Ok(())
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Databases explorer
use crate::*; use crate::*;
use dubp_block_doc::block::BlockDocumentTrait; use dubp_block_doc::block::BlockDocumentTrait;
use dubp_common_doc::BlockNumber; use dubp_common_doc::BlockNumber;
...@@ -27,9 +29,23 @@ use unwrap::unwrap; ...@@ -27,9 +29,23 @@ use unwrap::unwrap;
pub static EMPTY_BLOCKCHAIN: &'static str = pub static EMPTY_BLOCKCHAIN: &'static str =
"No blockchain, please sync your node to get a blockchain."; "No blockchain, please sync your node to get a blockchain.";
#[derive(Debug, Copy, Clone)]
/// Query for blockchain databases explorer
pub enum DbExBcQuery {
/// Count blocks per issuer
CountBlocksPerIssuer,
}
#[derive(Debug, Clone)]
/// Query for tx databases explorer
pub enum DbExTxQuery {
/// Ask balance of an address (pubkey or uid)
Balance(String),
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
/// Query for wot databases explorer /// Query for wot databases explorer
pub enum DBExWotQuery { pub enum DbExWotQuery {
/// Ask distance of all members /// Ask distance of all members
AllDistances(bool), AllDistances(bool),
/// Show members expire date /// Show members expire date
...@@ -40,32 +56,63 @@ pub enum DBExWotQuery { ...@@ -40,32 +56,63 @@ pub enum DBExWotQuery {
MemberDatas(String), MemberDatas(String),
} }
#[derive(Debug, Clone)]
/// Query for tx databases explorer
pub enum DBExTxQuery {
/// Ask balance of an address (pubkey or uid)
Balance(String),
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
/// Query for databases explorer /// Query for databases explorer
pub enum DBExQuery { pub enum DbExQuery {
/// Blockchain query
BcQuery(DbExBcQuery),
/// Fork tree query /// Fork tree query
ForkTreeQuery, ForkTreeQuery,
/// Tx query /// Tx query
TxQuery(DBExTxQuery), TxQuery(DbExTxQuery),
/// Wot query /// Wot query
WotQuery(DBExWotQuery), WotQuery(DbExWotQuery),
} }
/// Execute DbExQuery
pub fn dbex(profile_path: PathBuf, csv: bool, query: &DBExQuery) { pub fn dbex(profile_path: PathBuf, csv: bool, query: &DBExQuery) {
match *query { 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::TxQuery(ref tx_query) => dbex_tx(profile_path, csv, tx_query),
DBExQuery::WotQuery(ref wot_query) => dbex_wot(profile_path, csv, wot_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> {
// Get db path
let db_path = durs_conf::get_blockchain_db_path(profile_path, &conf.currency());
// Open databases
let load_dbs_begin = SystemTime::now();
let blocks_db = BlocksV10DBs::open(Some(&db_path));
let load_dbs_duration = SystemTime::now()
.duration_since(load_dbs_begin)
.expect("duration_since error !");
println!(
"Databases loaded in {}.{:03} seconds.",
load_dbs_duration.as_secs(),
load_dbs_duration.subsec_millis()
);
if let Some(current_blockstamp) =
durs_blockchain_dal::readers::block::get_current_blockstamp(&blocks_db)?
{
println!("Current block: #{}", current_blockstamp);
}
Ok(())
}
pub fn dbex_fork_tree(profile_path: PathBuf, _csv: bool) { pub fn dbex_fork_tree(profile_path: PathBuf, _csv: bool) {
// Get db path // Get db path
let db_path = durs_conf::get_blockchain_db_path(profile_path); let db_path = durs_conf::get_blockchain_db_path(profile_path);
...@@ -115,7 +162,7 @@ pub fn dbex_tx(profile_path: PathBuf, _csv: bool, query: &DBExTxQuery) { ...@@ -115,7 +162,7 @@ pub fn dbex_tx(profile_path: PathBuf, _csv: bool, query: &DBExTxQuery) {
); );
let req_process_begin = SystemTime::now(); let req_process_begin = SystemTime::now();
match *query { match *query {
DBExTxQuery::Balance(ref address_str) => { DbExTxQuery::Balance(ref address_str) => {
let pubkey = if let Ok(ed25519_pubkey) = ed25519::PublicKey::from_base58(address_str) { let pubkey = if let Ok(ed25519_pubkey) = ed25519::PublicKey::from_base58(address_str) {
PubKey::Ed25519(ed25519_pubkey) PubKey::Ed25519(ed25519_pubkey)
} else if let Some(pubkey) = } else if let Some(pubkey) =
...@@ -214,7 +261,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) { ...@@ -214,7 +261,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) {
.len(); .len();
match *query { match *query {
DBExWotQuery::AllDistances(ref reverse) => { DbExWotQuery::AllDistances(ref reverse) => {
println!("compute distances..."); println!("compute distances...");
let compute_distances_begin = SystemTime::now(); let compute_distances_begin = SystemTime::now();
let mut distances_datas: Vec<(NodeId, WotDistance)> = wot_db let mut distances_datas: Vec<(NodeId, WotDistance)> = wot_db
...@@ -269,7 +316,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) { ...@@ -269,7 +316,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) {
compute_distances_duration.subsec_millis() compute_distances_duration.subsec_millis()
); );
} }
DBExWotQuery::ExpireMembers(ref reverse) => { DbExWotQuery::ExpireMembers(ref reverse) => {
// Open blockchain database // Open blockchain database
let blockchain_db = open_file_db::<LocalBlockchainV10Datas>(&db_path, "blockchain.db") let blockchain_db = open_file_db::<LocalBlockchainV10Datas>(&db_path, "blockchain.db")
.expect("Fail to open blockchain db"); .expect("Fail to open blockchain db");
...@@ -313,7 +360,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) { ...@@ -313,7 +360,7 @@ pub fn dbex_wot(profile_path: PathBuf, csv: bool, query: &DBExWotQuery) {
println!("{}, {}", wot_uid_index[&node_id], expire_date); println!("{}, {}", wot_uid_index[&node_id], expire_date);
} }
} }
DBExWotQuery::MemberDatas(ref uid) => { DbExWotQuery::MemberDatas(ref uid) => {
println!(" Members count = {}.", members_count); println!(" Members count = {}.", members_count);
if let Some(pubkey) = durs_blockchain_dal::readers::identity::get_pubkey_from_uid( if let Some(pubkey) = durs_blockchain_dal::readers::identity::get_pubkey_from_uid(
&wot_databases.identities_db, &wot_databases.identities_db,
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
extern crate log; extern crate log;
mod constants; mod constants;
mod dbex; pub mod dbex;
mod dubp; mod dubp;
mod dunp; mod dunp;
mod events; mod events;
...@@ -50,7 +50,7 @@ use std::sync::mpsc; ...@@ -50,7 +50,7 @@ use std::sync::mpsc;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::constants::*; use crate::constants::*;
pub use crate::dbex::{DBExQuery, DBExTxQuery, DBExWotQuery}; use crate::dbex::DbExQuery;
use crate::dubp::apply::ValidBlockApplyReqs; use crate::dubp::apply::ValidBlockApplyReqs;
use crate::dubp::*; use crate::dubp::*;
use crate::fork::*; use crate::fork::*;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment