Skip to content

Commit 156c463

Browse files
author
Ludo Galabru
committed
feat: expose scanning status in GET endpoint
1 parent 1eabce2 commit 156c463

File tree

7 files changed

+227
-122
lines changed

7 files changed

+227
-122
lines changed

components/chainhook-cli/src/config/generator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ working_dir = "cache"
77
# dynamically predicates.
88
# Disable by default.
99
#
10-
# [http-api]
10+
# [http_api]
1111
# http_port = 20456
1212
# database_uri = "redis://localhost:6379/"
1313

components/chainhook-cli/src/config/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,12 @@ impl Config {
263263
}
264264

265265
pub fn expected_api_database_uri(&self) -> &str {
266+
&self.expected_api_config().database_uri
267+
}
268+
269+
pub fn expected_api_config(&self) -> &PredicatesApiConfig {
266270
match self.http_api {
267-
PredicatesApi::On(ref config) => config.database_uri.as_str(),
271+
PredicatesApi::On(ref config) => config,
268272
_ => unreachable!(),
269273
}
270274
}

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

+40-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ use std::collections::{HashMap, VecDeque};
33
use crate::{
44
archive::download_stacks_dataset_if_required,
55
block::{Record, RecordKind},
6-
config::Config,
6+
config::{Config, PredicatesApi, PredicatesApiConfig},
7+
service::{
8+
open_readwrite_predicates_db_conn_or_panic, update_predicate_status, PredicateStatus,
9+
ScanningData,
10+
},
711
storage::{
812
get_last_block_height_inserted, get_last_unconfirmed_block_height_inserted,
913
get_stacks_block_at_block_height, insert_entry_in_stacks_blocks, is_stacks_block_present,
1014
open_readwrite_stacks_db_conn,
1115
},
1216
};
1317
use chainhook_event_observer::{
14-
chainhooks::stacks::evaluate_stacks_chainhook_on_blocks,
18+
chainhooks::{stacks::evaluate_stacks_chainhook_on_blocks, types::ChainhookSpecification},
1519
indexer::{self, stacks::standardize_stacks_serialized_block_header, Indexer},
1620
rocksdb::DB,
1721
utils::Context,
@@ -140,9 +144,15 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
140144
},
141145
};
142146

143-
let proofs = HashMap::new();
147+
let mut predicates_db_conn = match config.http_api {
148+
PredicatesApi::On(ref api_config) => {
149+
Some(open_readwrite_predicates_db_conn_or_panic(api_config, ctx))
150+
}
151+
PredicatesApi::Off => None,
152+
};
144153

145-
let mut actions_triggered = 0;
154+
let proofs = HashMap::new();
155+
let mut occurrences_found = 0;
146156
let mut blocks_scanned = 0;
147157
info!(
148158
ctx.expect_logger(),
@@ -182,7 +192,7 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
182192
error!(ctx.expect_logger(), "unable to handle action {}", e);
183193
}
184194
Ok(action) => {
185-
actions_triggered += 1;
195+
occurrences_found += 1;
186196
let res = match action {
187197
StacksChainhookOccurrence::Http(request) => {
188198
send_request(request, 3, 1, &ctx).await
@@ -202,6 +212,18 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
202212
return Err(format!("Scan aborted (consecutive action errors >= 3)"));
203213
}
204214

215+
if let Some(ref mut predicates_db_conn) = predicates_db_conn {
216+
if blocks_scanned % 5000 == 0 {
217+
let status = PredicateStatus::Scanning(ScanningData {
218+
start_block,
219+
end_block,
220+
cursor,
221+
occurrences_found,
222+
});
223+
update_predicate_status(&predicate_spec.key(), status, predicates_db_conn)
224+
}
225+
}
226+
205227
cursor += 1;
206228
// Update end_block, in case a new block was discovered during the scan
207229
if cursor == end_block {
@@ -224,9 +246,17 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
224246
}
225247
info!(
226248
ctx.expect_logger(),
227-
"{blocks_scanned} blocks scanned, {actions_triggered} actions triggered"
249+
"{blocks_scanned} blocks scanned, {occurrences_found} occurrences found"
228250
);
229-
251+
let status = PredicateStatus::Scanning(ScanningData {
252+
start_block,
253+
end_block,
254+
cursor,
255+
occurrences_found,
256+
});
257+
if let Some(ref mut predicates_db_conn) = predicates_db_conn {
258+
update_predicate_status(&predicate_spec.key(), status, predicates_db_conn)
259+
}
230260
Ok(last_block_scanned)
231261
}
232262

@@ -253,7 +283,7 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
253283

254284
let proofs = HashMap::new();
255285

256-
let mut actions_triggered = 0;
286+
let mut occurrences_found = 0;
257287
let mut blocks_scanned = 0;
258288
info!(
259289
ctx.expect_logger(),
@@ -294,7 +324,7 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
294324
error!(ctx.expect_logger(), "unable to handle action {}", e);
295325
}
296326
Ok(action) => {
297-
actions_triggered += 1;
327+
occurrences_found += 1;
298328
let res = match action {
299329
StacksChainhookOccurrence::Http(request) => {
300330
send_request(request, 3, 1, &ctx).await
@@ -316,7 +346,7 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
316346
}
317347
info!(
318348
ctx.expect_logger(),
319-
"{blocks_scanned} blocks scanned, {actions_triggered} actions triggered"
349+
"{blocks_scanned} blocks scanned, {occurrences_found} occurrences found"
320350
);
321351

322352
Ok(last_block_scanned)

0 commit comments

Comments
 (0)