Skip to content

Commit

Permalink
feat: metric of reorg depth of blockchain tree (#3860)
Browse files Browse the repository at this point in the history
  • Loading branch information
int88 committed Jul 26, 2023
1 parent 9adab0b commit b5a44ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,12 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
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);
Expand Down Expand Up @@ -1094,6 +1097,11 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
}
}

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
Expand Down
2 changes: 2 additions & 0 deletions crates/blockchain-tree/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
5 changes: 5 additions & 0 deletions crates/storage/provider/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down

0 comments on commit b5a44ae

Please sign in to comment.