From b5a44aeb3764265eb26b5c9794371710513f4875 Mon Sep 17 00:00:00 2001 From: int88 <106391185+int88@users.noreply.github.com> Date: Thu, 27 Jul 2023 07:00:38 +0800 Subject: [PATCH] feat: metric of reorg depth of blockchain tree (#3860) --- crates/blockchain-tree/src/blockchain_tree.rs | 10 +++++++++- crates/blockchain-tree/src/metrics.rs | 2 ++ crates/storage/provider/src/chain.rs | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index ce92f48b2839..8edee773badf 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -1001,9 +1001,12 @@ impl BlockchainTree old: Arc::new(old_canon_chain.clone()), new: Arc::new(new_canon_chain.clone()), }; + let reorg_depth = old_canon_chain.len(); + // insert old canon chain self.insert_chain(AppendableChain::new(old_canon_chain)); - self.metrics.reorgs.increment(1); + + self.update_reorg_metrics(reorg_depth as f64); } else { // error here to confirm that we are reverting nothing from db. error!(target: "blockchain_tree", "Reverting nothing from db on block: #{:?}", block_hash); @@ -1094,6 +1097,11 @@ impl BlockchainTree } } + fn update_reorg_metrics(&mut self, reorg_depth: f64) { + self.metrics.reorgs.increment(1); + self.metrics.latest_reorg_depth.set(reorg_depth); + } + /// Update blockchain tree chains (canonical and sidechains) and sync metrics. /// /// NOTE: this method should not be called during the pipeline sync, because otherwise the sync diff --git a/crates/blockchain-tree/src/metrics.rs b/crates/blockchain-tree/src/metrics.rs index fd48307d4be2..b49ad3c5b921 100644 --- a/crates/blockchain-tree/src/metrics.rs +++ b/crates/blockchain-tree/src/metrics.rs @@ -13,6 +13,8 @@ pub struct TreeMetrics { pub canonical_chain_height: Gauge, /// The number of reorgs pub reorgs: Counter, + /// The latest reorg depth + pub latest_reorg_depth: Gauge, /// Longest sidechain height pub longest_sidechain_height: Gauge, } diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index b5d596c0164b..1633d360f065 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -136,6 +136,11 @@ impl Chain { Self { state, blocks: block_num_hash } } + /// Returns length of the chain. + pub fn len(&self) -> usize { + self.blocks.len() + } + /// Get all receipts for the given block. pub fn receipts_by_block_hash(&self, block_hash: BlockHash) -> Option<&[Receipt]> { let num = self.block_number(block_hash)?;