@@ -5,8 +5,9 @@ use crate::{
5
5
block:: { Record , RecordKind } ,
6
6
config:: Config ,
7
7
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,
10
11
} ,
11
12
} ;
12
13
use chainhook_event_observer:: {
@@ -123,16 +124,19 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
123
124
}
124
125
} ;
125
126
126
- let end_block = match predicate_spec. end_block {
127
+ let mut end_block = match predicate_spec. end_block {
127
128
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) {
129
130
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
+ } ,
136
140
} ,
137
141
} ;
138
142
@@ -146,7 +150,8 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
146
150
) ;
147
151
let mut last_block_scanned = BlockIdentifier :: default ( ) ;
148
152
let mut err_count = 0 ;
149
- for cursor in start_block..=end_block {
153
+ let mut cursor = start_block;
154
+ while cursor <= end_block {
150
155
let block_data = match get_stacks_block_at_block_height ( cursor, true , 3 , stacks_db_conn) {
151
156
Ok ( Some ( block) ) => block,
152
157
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(
163
168
164
169
let hits_per_blocks = evaluate_stacks_chainhook_on_blocks ( blocks, & predicate_spec, ctx) ;
165
170
if hits_per_blocks. is_empty ( ) {
171
+ cursor += 1 ;
166
172
continue ;
167
173
}
168
174
@@ -195,6 +201,26 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
195
201
if err_count >= 3 {
196
202
return Err ( format ! ( "Scan aborted (consecutive action errors >= 3)" ) ) ;
197
203
}
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
+ }
198
224
}
199
225
info ! (
200
226
ctx. expect_logger( ) ,
0 commit comments