Ver Fonte

script/research/blockchain-explorer: display blockchain statistis funcionality added

aggstam há 3 anos atrás
pai
commit
b50ff8bfc5

+ 4 - 1
script/research/blockchain-explorer/Cargo.toml

@@ -1,7 +1,7 @@
 [package]
 [package]
 name = "blockchain-explorer"
 name = "blockchain-explorer"
 version = "0.4.1"
 version = "0.4.1"
-description = "Command-line client to export blockchain sled database contents"
+description = "Command-line client to display statistics or export blockchain sled database contents"
 authors = ["Dyne.org foundation <foundation@dyne.org>"]
 authors = ["Dyne.org foundation <foundation@dyne.org>"]
 repository = "https://github.com/darkrenaissance/darkfi"
 repository = "https://github.com/darkrenaissance/darkfi"
 license = "AGPL-3.0-only"
 license = "AGPL-3.0-only"
@@ -15,3 +15,6 @@ clap = {version = "4.1.4", features = ["derive"]}
 darkfi = {path = "../../../", features = ["blockchain", "wallet", "rpc"]}
 darkfi = {path = "../../../", features = ["blockchain", "wallet", "rpc"]}
 darkfi-sdk = {path = "../../../src/sdk"}
 darkfi-sdk = {path = "../../../src/sdk"}
 sled = "0.34.7"
 sled = "0.34.7"
+
+[patch.crates-io]
+halo2_proofs = {git="https://github.com/parazyd/halo2", branch="vk-ser"}

+ 44 - 35
script/research/blockchain-explorer/src/main.rs

@@ -23,13 +23,13 @@ use darkfi::{
     blockchain::{
     blockchain::{
         block_store::{BlockOrderStore, BlockStore, HeaderStore},
         block_store::{BlockOrderStore, BlockStore, HeaderStore},
         slot_checkpoint_store::SlotCheckpointStore,
         slot_checkpoint_store::SlotCheckpointStore,
-        tx_store::{ErroneousTxStore, TxStore},
+        tx_store::TxStore,
         Blockchain,
         Blockchain,
     },
     },
     cli_desc,
     cli_desc,
     consensus::{
     consensus::{
         block::{Block, Header},
         block::{Block, Header},
-        constants::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
+        constants::{EPOCH_LENGTH, TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
         lead_info::LeadInfo,
         lead_info::LeadInfo,
     },
     },
     tx::Transaction,
     tx::Transaction,
@@ -52,6 +52,10 @@ struct Args {
     #[arg(short, long, default_value = "/blockchain/testnet")]
     #[arg(short, long, default_value = "/blockchain/testnet")]
     /// Node blockchain folder
     /// Node blockchain folder
     blockchain: String,
     blockchain: String,
+
+    #[arg(short, long)]
+    /// Export all contents into a JSON file
+    export: bool,
 }
 }
 
 
 #[derive(Debug)]
 #[derive(Debug)]
@@ -280,27 +284,6 @@ impl TxStoreInfo {
     }
     }
 }
 }
 
 
-#[derive(Debug)]
-struct ErroneousTxStoreInfo {
-    _transactions: Vec<blake3::Hash>,
-}
-
-impl ErroneousTxStoreInfo {
-    pub fn new(erroneoustxstore: &ErroneousTxStore) -> ErroneousTxStoreInfo {
-        let mut _transactions = Vec::new();
-        let result = erroneoustxstore.get_all();
-        match result {
-            Ok(iter) => {
-                for hash in iter.iter() {
-                    _transactions.push(hash.clone());
-                }
-            }
-            Err(e) => println!("Error: {:?}", e),
-        }
-        ErroneousTxStoreInfo { _transactions }
-    }
-}
-
 #[derive(Debug)]
 #[derive(Debug)]
 struct BlockchainInfo {
 struct BlockchainInfo {
     _headers: HeaderStoreInfo,
     _headers: HeaderStoreInfo,
@@ -308,7 +291,6 @@ struct BlockchainInfo {
     _order: BlockOrderStoreInfo,
     _order: BlockOrderStoreInfo,
     _slot_checkpoints: SlotCheckpointStoreInfo,
     _slot_checkpoints: SlotCheckpointStoreInfo,
     _transactions: TxStoreInfo,
     _transactions: TxStoreInfo,
-    _erroneous_txs: ErroneousTxStoreInfo,
 }
 }
 
 
 impl BlockchainInfo {
 impl BlockchainInfo {
@@ -318,19 +300,42 @@ impl BlockchainInfo {
         let _order = BlockOrderStoreInfo::new(&blockchain.order);
         let _order = BlockOrderStoreInfo::new(&blockchain.order);
         let _slot_checkpoints = SlotCheckpointStoreInfo::new(&blockchain.slot_checkpoints);
         let _slot_checkpoints = SlotCheckpointStoreInfo::new(&blockchain.slot_checkpoints);
         let _transactions = TxStoreInfo::new(&blockchain.transactions);
         let _transactions = TxStoreInfo::new(&blockchain.transactions);
-        let _erroneous_txs = ErroneousTxStoreInfo::new(&blockchain.erroneous_txs);
-        BlockchainInfo {
-            _headers,
-            _blocks,
-            _order,
-            _slot_checkpoints,
-            _transactions,
-            _erroneous_txs,
-        }
+        BlockchainInfo { _headers, _blocks, _order, _slot_checkpoints, _transactions }
     }
     }
 }
 }
 
 
-fn generate(folder: &str, node: &str, blockchain: &str) -> Result<()> {
+fn statistics(folder: &str, node: &str, blockchain: &str) -> Result<()> {
+    println!("Retrieving blockchain statistics for {node}...");
+
+    // Node folder
+    let folder = folder.to_owned() + node;
+
+    // Initialize or load sled database
+    let path = folder.to_owned() + blockchain;
+    let db_path = expand_path(&path).unwrap();
+    let sled_db = sled::open(&db_path)?;
+
+    // Retrieve statistics
+    let blockchain =
+        Blockchain::new(&sled_db, *TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES)?;
+    let slot = blockchain.last_slot_checkpoint()?.slot;
+    let epoch = slot / EPOCH_LENGTH as u64;
+    let (_, block) = blockchain.last()?;
+    let blocks = blockchain.len();
+    let txs = blockchain.txs_len();
+    drop(sled_db);
+
+    // Print statistics
+    println!("Latest slot: {slot}");
+    println!("Epoch: {epoch}");
+    println!("Latest block: {block}");
+    println!("Total blocks: {blocks}");
+    println!("Total transactions: {txs}");
+
+    Ok(())
+}
+
+fn export(folder: &str, node: &str, blockchain: &str) -> Result<()> {
     println!("Exporting data for {node}...");
     println!("Exporting data for {node}...");
 
 
     // Node folder
     // Node folder
@@ -361,7 +366,11 @@ fn main() -> Result<()> {
     println!("Node folder path: {}", args.path);
     println!("Node folder path: {}", args.path);
     // Export data for each node
     // Export data for each node
     for node in args.node {
     for node in args.node {
-        generate(&args.path, &node, &args.blockchain)?;
+        if args.export {
+            export(&args.path, &node, &args.blockchain)?;
+            continue
+        }
+        statistics(&args.path, &node, &args.blockchain)?;
     }
     }
 
 
     Ok(())
     Ok(())

+ 1 - 0
src/blockchain/block_store.rs

@@ -328,6 +328,7 @@ impl BlockOrderStore {
         self.0.len()
         self.0.len()
     }
     }
 
 
+    /// Check if sled contains any records
     pub fn is_empty(&self) -> bool {
     pub fn is_empty(&self) -> bool {
         self.0.len() == 0
         self.0.len() == 0
     }
     }

+ 6 - 0
src/blockchain/mod.rs

@@ -184,6 +184,12 @@ impl Blockchain {
         self.order.len()
         self.order.len()
     }
     }
 
 
+    /// Retrieve stored txs count
+    pub fn txs_len(&self) -> usize {
+        self.transactions.len()
+    }
+
+    /// Check if blockchain contains any blocks
     pub fn is_empty(&self) -> bool {
     pub fn is_empty(&self) -> bool {
         self.order.len() == 0
         self.order.len() == 0
     }
     }

+ 5 - 0
src/blockchain/tx_store.rs

@@ -108,6 +108,11 @@ impl TxStore {
 
 
         Ok(txs)
         Ok(txs)
     }
     }
+
+    /// Retrieve records count
+    pub fn len(&self) -> usize {
+        self.0.len()
+    }
 }
 }
 
 
 /// The `PendingTxStore` is a `sled` tree storing all the node pending
 /// The `PendingTxStore` is a `sled` tree storing all the node pending