From a161034a76b16cd6426cf6a2f1c66e6c6e063912 Mon Sep 17 00:00:00 2001 From: Micaiah Reid Date: Tue, 18 Jun 2024 12:32:21 -0400 Subject: [PATCH] chore: update predicate scanning status on every trigger (#603) The predicate status is used to report data to users and to pick back up where the service left off on a restart. This change updates the scanning predicate status to be updated on every successful trigger of a scanning predicate, to be sure we don't scan blocks twice on a restart (if the status was set to a previous block, we'd start on that block at startup) --- components/chainhook-cli/src/scan/bitcoin.rs | 9 ++++++++- components/chainhook-cli/src/scan/stacks.rs | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/components/chainhook-cli/src/scan/bitcoin.rs b/components/chainhook-cli/src/scan/bitcoin.rs index 886f60998..b3d3185ce 100644 --- a/components/chainhook-cli/src/scan/bitcoin.rs +++ b/components/chainhook-cli/src/scan/bitcoin.rs @@ -98,9 +98,14 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate( let mut last_scanned_block_confirmations = 0; let http_client = build_http_client(); + let mut loop_did_trigger = false; while let Some(current_block_height) = block_heights_to_scan.pop_front() { if let Some(ref mut predicates_db_conn) = predicates_db_conn { - if number_of_blocks_scanned % 10 == 0 || number_of_blocks_scanned == 0 { + if number_of_blocks_scanned % 100 == 0 + || number_of_blocks_scanned == 0 + // if the last loop did trigger a predicate, update the status + || loop_did_trigger + { set_predicate_scanning_status( &predicate_spec.key(), number_of_blocks_to_scan, @@ -112,6 +117,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate( ); } } + loop_did_trigger = false; if current_block_height > chain_tip { let prev_chain_tip = chain_tip; @@ -177,6 +183,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate( Ok(actions) => { if actions > 0 { number_of_times_triggered += 1; + loop_did_trigger = true } actions_triggered += actions; Ok(()) diff --git a/components/chainhook-cli/src/scan/stacks.rs b/components/chainhook-cli/src/scan/stacks.rs index 009aa16da..b626da29d 100644 --- a/components/chainhook-cli/src/scan/stacks.rs +++ b/components/chainhook-cli/src/scan/stacks.rs @@ -224,9 +224,14 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate( } }; + let mut loop_did_trigger = false; while let Some(current_block_height) = block_heights_to_scan.pop_front() { if let Some(ref mut predicates_db_conn) = predicates_db_conn { - if number_of_blocks_scanned % 10 == 0 || number_of_blocks_scanned == 0 { + if number_of_blocks_scanned % 1000 == 0 + || number_of_blocks_scanned == 0 + // if the last loop did trigger a predicate, update the status + || loop_did_trigger + { set_predicate_scanning_status( &predicate_spec.key(), number_of_blocks_to_scan, @@ -238,6 +243,8 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate( ); } } + loop_did_trigger = false; + if current_block_height > chain_tip { let prev_chain_tip = chain_tip; // we've scanned up to the chain tip as of the start of this scan @@ -316,6 +323,7 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate( } Ok(action) => { number_of_times_triggered += 1; + loop_did_trigger = true; let res = match action { StacksChainhookOccurrence::Http(request, _) => { send_request(request, 3, 1, &ctx).await