Skip to content

Commit 1eabce2

Browse files
author
Ludo Galabru
committed
fix: include blocks discovered during scan, if any
1 parent f6d050f commit 1eabce2

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

components/chainhook-cli/src/scan/stacks.rs

+37-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use crate::{
55
block::{Record, RecordKind},
66
config::Config,
77
storage::{
8-
get_last_block_height_inserted, get_stacks_block_at_block_height,
9-
insert_entry_in_stacks_blocks, is_stacks_block_present, open_readwrite_stacks_db_conn,
8+
get_last_block_height_inserted, get_last_unconfirmed_block_height_inserted,
9+
get_stacks_block_at_block_height, insert_entry_in_stacks_blocks, is_stacks_block_present,
10+
open_readwrite_stacks_db_conn,
1011
},
1112
};
1213
use chainhook_event_observer::{
@@ -123,16 +124,19 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
123124
}
124125
};
125126

126-
let end_block = match predicate_spec.end_block {
127+
let mut end_block = match predicate_spec.end_block {
127128
Some(end_block) => end_block,
128-
None => match get_last_block_height_inserted(stacks_db_conn, ctx) {
129+
None => match get_last_unconfirmed_block_height_inserted(stacks_db_conn, ctx) {
129130
Some(end_block) => end_block,
130-
None => {
131-
return Err(
132-
"Chainhook specification must include fields 'start_block' when using the scan command"
133-
.into(),
134-
);
135-
}
131+
None => match get_last_block_height_inserted(stacks_db_conn, ctx) {
132+
Some(end_block) => end_block,
133+
None => {
134+
return Err(
135+
"Chainhook specification must include fields 'end_block' when using the scan command"
136+
.into(),
137+
);
138+
}
139+
},
136140
},
137141
};
138142

@@ -146,7 +150,8 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
146150
);
147151
let mut last_block_scanned = BlockIdentifier::default();
148152
let mut err_count = 0;
149-
for cursor in start_block..=end_block {
153+
let mut cursor = start_block;
154+
while cursor <= end_block {
150155
let block_data = match get_stacks_block_at_block_height(cursor, true, 3, stacks_db_conn) {
151156
Ok(Some(block)) => block,
152157
Ok(None) => match get_stacks_block_at_block_height(cursor, false, 3, stacks_db_conn) {
@@ -163,6 +168,7 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
163168

164169
let hits_per_blocks = evaluate_stacks_chainhook_on_blocks(blocks, &predicate_spec, ctx);
165170
if hits_per_blocks.is_empty() {
171+
cursor += 1;
166172
continue;
167173
}
168174

@@ -195,6 +201,26 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
195201
if err_count >= 3 {
196202
return Err(format!("Scan aborted (consecutive action errors >= 3)"));
197203
}
204+
205+
cursor += 1;
206+
// Update end_block, in case a new block was discovered during the scan
207+
if cursor == end_block {
208+
end_block = match predicate_spec.end_block {
209+
Some(end_block) => end_block,
210+
None => match get_last_unconfirmed_block_height_inserted(stacks_db_conn, ctx) {
211+
Some(end_block) => end_block,
212+
None => match get_last_block_height_inserted(stacks_db_conn, ctx) {
213+
Some(end_block) => end_block,
214+
None => {
215+
return Err(
216+
"Chainhook specification must include fields 'end_block' when using the scan command"
217+
.into(),
218+
);
219+
}
220+
},
221+
},
222+
};
223+
}
198224
}
199225
info!(
200226
ctx.expect_logger(),

0 commit comments

Comments
 (0)