Skip to content

Commit

Permalink
chore: update predicate scanning status on every trigger (#603)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
MicaiahReid authored Jun 18, 2024
1 parent 376a2c3 commit a161034
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion components/chainhook-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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(())
Expand Down
10 changes: 9 additions & 1 deletion components/chainhook-cli/src/scan/stacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a161034

Please sign in to comment.