Skip to content

Commit

Permalink
fix(tree): update metrics only on canonical/side chain changes (#3671)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Jul 9, 2023
1 parent f116040 commit b68116c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 6 additions & 2 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,8 +1084,12 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
}
}

/// Update blockchain tree and sync metrics
pub(crate) fn update_metrics(&mut self) {
/// 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
/// checkpoint metric will get overwritten. Buffered blocks metrics are updated in
/// [BlockBuffer] during the pipeline sync.
pub(crate) fn update_chains_metrics(&mut self) {
let height = self.canonical_chain().tip().number;

self.metrics.sidechains.set(self.chains.len() as f64);
Expand Down
16 changes: 8 additions & 8 deletions crates/blockchain-tree/src/shareable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTreeEngine
{
fn buffer_block(&self, block: SealedBlockWithSenders) -> Result<(), InsertBlockError> {
let mut tree = self.tree.write();
let res = tree.buffer_block(block);
tree.update_metrics();
res
// Blockchain tree metrics shouldn't be updated here, see
// `BlockchainTree::update_chains_metrics` documentation.
tree.buffer_block(block)
}

fn insert_block(
Expand All @@ -55,38 +55,38 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTreeEngine
trace!(target: "blockchain_tree", hash=?block.hash, number=block.number, parent_hash=?block.parent_hash, "Inserting block");
let mut tree = self.tree.write();
let res = tree.insert_block(block);
tree.update_metrics();
tree.update_chains_metrics();
res
}

fn finalize_block(&self, finalized_block: BlockNumber) {
trace!(target: "blockchain_tree", ?finalized_block, "Finalizing block");
let mut tree = self.tree.write();
tree.finalize_block(finalized_block);
tree.update_metrics();
tree.update_chains_metrics();
}

fn restore_canonical_hashes(&self, last_finalized_block: BlockNumber) -> Result<(), Error> {
trace!(target: "blockchain_tree", ?last_finalized_block, "Restoring canonical hashes for last finalized block");
let mut tree = self.tree.write();
let res = tree.restore_canonical_hashes(last_finalized_block);
tree.update_metrics();
tree.update_chains_metrics();
res
}

fn make_canonical(&self, block_hash: &BlockHash) -> Result<CanonicalOutcome, Error> {
trace!(target: "blockchain_tree", ?block_hash, "Making block canonical");
let mut tree = self.tree.write();
let res = tree.make_canonical(block_hash);
tree.update_metrics();
tree.update_chains_metrics();
res
}

fn unwind(&self, unwind_to: BlockNumber) -> Result<(), Error> {
trace!(target: "blockchain_tree", ?unwind_to, "Unwinding to block number");
let mut tree = self.tree.write();
let res = tree.unwind(unwind_to);
tree.update_metrics();
tree.update_chains_metrics();
res
}
}
Expand Down

0 comments on commit b68116c

Please sign in to comment.