From 9cd7a8fcfd02cc2f5a314aba207b9b4ba5745da7 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 8 Aug 2025 16:21:00 +0400 Subject: [PATCH 1/3] update new_mithril_update and live_sync_backfill_and_purge --- rust/cardano-chain-follower/src/chain_sync.rs | 16 +++++----------- rust/cardano-chain-follower/src/stats/mod.rs | 4 ---- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/rust/cardano-chain-follower/src/chain_sync.rs b/rust/cardano-chain-follower/src/chain_sync.rs index f590a3df3a..09d86c5bea 100644 --- a/rust/cardano-chain-follower/src/chain_sync.rs +++ b/rust/cardano-chain-follower/src/chain_sync.rs @@ -451,13 +451,13 @@ async fn live_sync_backfill_and_purge( return; }; + stats::new_mithril_update(cfg.chain, update.tip.slot_or_default()); + debug!( "Before Backfill: Size of the Live Chain is: {} Blocks", live_chain_length(cfg.chain) ); - let live_chain_head: Point; - loop { // We will re-attempt backfill, until its successful. // Backfill is atomic, it either fully works, or none of the live-chain is changed. @@ -467,19 +467,11 @@ async fn live_sync_backfill_and_purge( sleep(Duration::from_secs(10)).await; } - if let Some(head_point) = get_live_head_point(cfg.chain) { - live_chain_head = head_point; + if get_live_head_point(cfg.chain).is_some() { break; } } - stats::new_mithril_update( - cfg.chain, - update.tip.slot_or_default(), - live_chain_length(cfg.chain) as u64, - live_chain_head.slot_or_default(), - ); - debug!( "After Backfill: Size of the Live Chain is: {} Blocks", live_chain_length(cfg.chain) @@ -496,6 +488,8 @@ async fn live_sync_backfill_and_purge( return; }; + stats::new_mithril_update(cfg.chain, update.tip.slot_or_default()); + // We can't get an update sender until the sync is released. if update_sender.is_none() { update_sender = get_chain_update_tx_queue(cfg.chain).await; diff --git a/rust/cardano-chain-follower/src/stats/mod.rs b/rust/cardano-chain-follower/src/stats/mod.rs index bdc51a25b3..5bfbee9e93 100644 --- a/rust/cardano-chain-follower/src/stats/mod.rs +++ b/rust/cardano-chain-follower/src/stats/mod.rs @@ -193,8 +193,6 @@ pub(crate) fn new_live_block( pub(crate) fn new_mithril_update( chain: Network, mithril_tip: Slot, - total_live_blocks: u64, - tip_slot: Slot, ) { // This will actually always succeed. let Some(stats) = lookup_stats(chain) else { @@ -209,8 +207,6 @@ pub(crate) fn new_mithril_update( chain_stats.mithril.updates = chain_stats.mithril.updates.saturating_add(1); chain_stats.mithril.tip = mithril_tip; - chain_stats.live.blocks = total_live_blocks; - chain_stats.live.tip = tip_slot; } /// When did we start the backfill. From 5d0e8c04d5e8b607ec7e93fea156b31bd5ae2879 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 8 Aug 2025 17:42:39 +0400 Subject: [PATCH 2/3] wip --- rust/cardano-chain-follower/src/chain_sync.rs | 18 +++++++++++++----- rust/cardano-chain-follower/src/stats/mod.rs | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/rust/cardano-chain-follower/src/chain_sync.rs b/rust/cardano-chain-follower/src/chain_sync.rs index 09d86c5bea..b5ed873fd6 100644 --- a/rust/cardano-chain-follower/src/chain_sync.rs +++ b/rust/cardano-chain-follower/src/chain_sync.rs @@ -451,7 +451,11 @@ async fn live_sync_backfill_and_purge( return; }; - stats::new_mithril_update(cfg.chain, update.tip.slot_or_default()); + stats::new_mithril_update( + cfg.chain, + update.tip.slot_or_default(), + live_chain_length(cfg.chain) as u64, + ); debug!( "Before Backfill: Size of the Live Chain is: {} Blocks", @@ -488,8 +492,6 @@ async fn live_sync_backfill_and_purge( return; }; - stats::new_mithril_update(cfg.chain, update.tip.slot_or_default()); - // We can't get an update sender until the sync is released. if update_sender.is_none() { update_sender = get_chain_update_tx_queue(cfg.chain).await; @@ -504,9 +506,15 @@ async fn live_sync_backfill_and_purge( error!("Mithril Purge Failed: {}", error); } + let new_live_chain_length = live_chain_length(cfg.chain); + stats::new_mithril_update( + cfg.chain, + update.tip.slot_or_default(), + new_live_chain_length as u64, + ); + debug!( - "After Purge: Size of the Live Chain is: {} Blocks: Triggering Sleeping Followers.", - live_chain_length(cfg.chain) + "After Purge: Size of the Live Chain is: {new_live_chain_length} Blocks: Triggering Sleeping Followers.", ); // Trigger any sleeping followers that data has changed. diff --git a/rust/cardano-chain-follower/src/stats/mod.rs b/rust/cardano-chain-follower/src/stats/mod.rs index 5bfbee9e93..1973ca7566 100644 --- a/rust/cardano-chain-follower/src/stats/mod.rs +++ b/rust/cardano-chain-follower/src/stats/mod.rs @@ -193,6 +193,7 @@ pub(crate) fn new_live_block( pub(crate) fn new_mithril_update( chain: Network, mithril_tip: Slot, + total_live_blocks: u64, ) { // This will actually always succeed. let Some(stats) = lookup_stats(chain) else { @@ -207,6 +208,7 @@ pub(crate) fn new_mithril_update( chain_stats.mithril.updates = chain_stats.mithril.updates.saturating_add(1); chain_stats.mithril.tip = mithril_tip; + chain_stats.live.blocks = total_live_blocks; } /// When did we start the backfill. From d2f786016a74e329eaddc7dca490911f715e8943 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 8 Aug 2025 23:30:19 +0400 Subject: [PATCH 3/3] wip --- rust/cardano-chain-follower/src/chain_sync.rs | 22 +++++++------------ rust/cardano-chain-follower/src/stats/mod.rs | 19 +++++++++++++++- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/rust/cardano-chain-follower/src/chain_sync.rs b/rust/cardano-chain-follower/src/chain_sync.rs index b5ed873fd6..6cbea0ca44 100644 --- a/rust/cardano-chain-follower/src/chain_sync.rs +++ b/rust/cardano-chain-follower/src/chain_sync.rs @@ -451,11 +451,7 @@ async fn live_sync_backfill_and_purge( return; }; - stats::new_mithril_update( - cfg.chain, - update.tip.slot_or_default(), - live_chain_length(cfg.chain) as u64, - ); + stats::new_mithril_update(cfg.chain, update.tip.slot_or_default()); debug!( "Before Backfill: Size of the Live Chain is: {} Blocks", @@ -476,10 +472,10 @@ async fn live_sync_backfill_and_purge( } } - debug!( - "After Backfill: Size of the Live Chain is: {} Blocks", - live_chain_length(cfg.chain) - ); + let new_live_chain_length = live_chain_length(cfg.chain); + stats::new_live_total_blocks(cfg.chain, new_live_chain_length as u64); + + debug!("After Backfill: Size of the Live Chain is: {new_live_chain_length} Blocks",); // Once Backfill is completed OK we can use the Blockchain data for Syncing and Querying sync_ready.signal(); @@ -497,6 +493,8 @@ async fn live_sync_backfill_and_purge( update_sender = get_chain_update_tx_queue(cfg.chain).await; } + stats::new_mithril_update(cfg.chain, update.tip.slot_or_default()); + debug!("Mithril Tip has advanced to: {update:?} : PURGE NEEDED"); let update_point: Point = update.tip.clone(); @@ -507,11 +505,7 @@ async fn live_sync_backfill_and_purge( } let new_live_chain_length = live_chain_length(cfg.chain); - stats::new_mithril_update( - cfg.chain, - update.tip.slot_or_default(), - new_live_chain_length as u64, - ); + stats::new_live_total_blocks(cfg.chain, new_live_chain_length as u64); debug!( "After Purge: Size of the Live Chain is: {new_live_chain_length} Blocks: Triggering Sleeping Followers.", diff --git a/rust/cardano-chain-follower/src/stats/mod.rs b/rust/cardano-chain-follower/src/stats/mod.rs index 1973ca7566..85cc6f389f 100644 --- a/rust/cardano-chain-follower/src/stats/mod.rs +++ b/rust/cardano-chain-follower/src/stats/mod.rs @@ -193,7 +193,6 @@ pub(crate) fn new_live_block( pub(crate) fn new_mithril_update( chain: Network, mithril_tip: Slot, - total_live_blocks: u64, ) { // This will actually always succeed. let Some(stats) = lookup_stats(chain) else { @@ -208,6 +207,24 @@ pub(crate) fn new_mithril_update( chain_stats.mithril.updates = chain_stats.mithril.updates.saturating_add(1); chain_stats.mithril.tip = mithril_tip; +} + +/// Track the current total live blocks count +pub(crate) fn new_live_total_blocks( + chain: Network, + total_live_blocks: u64, +) { + // This will actually always succeed. + let Some(stats) = lookup_stats(chain) else { + return; + }; + + let Ok(mut chain_stats) = stats.write() else { + // Worst case if this fails (it never should) is we stop updating stats. + error!("Stats RwLock should never be able to error."); + return; + }; + chain_stats.live.blocks = total_live_blocks; }