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)?;