Skip to content

Commit

Permalink
fix(core): set reorg metrics on header sync rewind (#4087)
Browse files Browse the repository at this point in the history
Description
---
Sets `tari_base_node::blockchain::reorgs` and `tari_base_node::blockchain::tip_height` metrics on header sync rewind.

Motivation and Context
---
Header sync is designed to handle syncing from nodes that are on a forked chain, and so can rewind and reorg the blockchain. This PR sets the correct metrics for a header sync rewind.

How Has This Been Tested?
---
Not explicitly tested in this PR (reorgs tested in general and in cucumber tests). This case is fairly rare and difficult to test manually (unit tests should be implemented for header sync! but this is out of scope). The correct metrics should be seen on grafana over the next month.
  • Loading branch information
sdbondi authored May 16, 2022
1 parent 810d9ff commit 354bae2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use randomx_rs::RandomXFlag;
use crate::{
base_node::{
comms_interface::BlockEvent,
metrics,
state_machine_service::states::{BlockSyncInfo, HorizonStateSync, StateEvent, StateInfo, StatusInfo},
sync::{BlockSynchronizer, SyncPeer},
BaseNodeStateMachine,
Expand Down Expand Up @@ -67,13 +68,15 @@ impl BlockSync {
let local_nci = shared.local_node_interface.clone();
let randomx_vm_cnt = shared.get_randomx_vm_cnt();
let randomx_vm_flags = shared.get_randomx_vm_flags();
let tip_height_metric = metrics::tip_height();
synchronizer.on_progress(move |block, remote_tip_height, sync_peer| {
let local_height = block.height();
local_nci.publish_block_event(BlockEvent::ValidBlockAdded(
block.block().clone().into(),
BlockAddResult::Ok(block),
));

tip_height_metric.set(local_height as i64);
let _result = status_event_sender.send(StatusInfo {
bootstrapped,
state_info: StateInfo::BlockSync(BlockSyncInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use tari_common_types::chain_metadata::ChainMetadata;
use crate::{
base_node::{
comms_interface::BlockEvent,
metrics,
state_machine_service::states::{BlockSyncInfo, StateEvent, StateInfo, StatusInfo},
sync::{BlockHeaderSyncError, HeaderSynchronizer, SyncPeer},
BaseNodeStateMachine,
Expand Down Expand Up @@ -112,8 +113,13 @@ impl HeaderSyncState {
});

let local_nci = shared.local_node_interface.clone();
synchronizer.on_rewind(move |blocks| {
local_nci.publish_block_event(BlockEvent::BlockSyncRewind(blocks));
synchronizer.on_rewind(move |removed| {
if let Some(fork_height) = removed.last().map(|b| b.height() - 1) {
metrics::tip_height().set(fork_height as i64);
metrics::reorg(fork_height, 0, removed.len()).inc();
}

local_nci.publish_block_event(BlockEvent::BlockSyncRewind(removed));
});

let timer = Instant::now();
Expand Down

0 comments on commit 354bae2

Please sign in to comment.