Skip to content

Commit e2949d2

Browse files
author
Ludo Galabru
committed
fix: disable steam scan when scanning past blocks
1 parent f18bc00 commit e2949d2

File tree

8 files changed

+164
-70
lines changed

8 files changed

+164
-70
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
540540
};
541541

542542
scan_bitcoin_chainstate_via_http_using_predicate(
543-
predicate_spec,
543+
&predicate_spec,
544544
&config,
545545
&ctx,
546546
)
@@ -560,7 +560,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
560560
};
561561

562562
scan_stacks_chainstate_via_csv_using_predicate(
563-
predicate_spec,
563+
&predicate_spec,
564564
&mut config,
565565
&ctx,
566566
)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use chainhook_types::{BitcoinChainEvent, BitcoinChainUpdatedWithBlocksData};
2727
use std::collections::{BTreeMap, HashMap};
2828

2929
pub async fn scan_bitcoin_chainstate_via_http_using_predicate(
30-
predicate_spec: BitcoinChainhookSpecification,
30+
predicate_spec: &BitcoinChainhookSpecification,
3131
config: &Config,
3232
ctx: &Context,
3333
) -> Result<(), String> {
@@ -149,7 +149,7 @@ pub async fn scan_bitcoin_chainstate_via_http_using_predicate(
149149
// Only consider inscriptions in the interval specified
150150
let local_traverals = match inscriptions_cache.remove(&cursor) {
151151
Some(entry) => entry,
152-
None => continue
152+
None => continue,
153153
};
154154
for (transaction_identifier, traversal_result) in local_traverals.into_iter() {
155155
traversals.insert(transaction_identifier, traversal_result);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use chainhook_event_observer::{
2323
use chainhook_types::BlockIdentifier;
2424

2525
pub async fn scan_stacks_chainstate_via_csv_using_predicate(
26-
predicate_spec: StacksChainhookSpecification,
26+
predicate_spec: &StacksChainhookSpecification,
2727
config: &mut Config,
2828
ctx: &Context,
2929
) -> Result<BlockIdentifier, String> {

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::scan::stacks::scan_stacks_chainstate_via_csv_using_predicate;
55
use chainhook_event_observer::chainhooks::types::{ChainhookConfig, ChainhookFullSpecification};
66

77
use chainhook_event_observer::chainhooks::types::ChainhookSpecification;
8-
use chainhook_event_observer::observer::{start_event_observer, ApiKey, ObserverEvent};
8+
use chainhook_event_observer::observer::{
9+
start_event_observer, ApiKey, ObserverCommand, ObserverEvent,
10+
};
911
use chainhook_event_observer::utils::Context;
1012
use chainhook_types::{BitcoinBlockSignaling, StacksBlockData, StacksChainEvent};
1113
use redis::{Commands, Connection};
@@ -115,10 +117,11 @@ impl Service {
115117

116118
let context_cloned = self.ctx.clone();
117119
let event_observer_config_moved = event_observer_config.clone();
120+
let observer_command_tx_moved = observer_command_tx.clone();
118121
let _ = std::thread::spawn(move || {
119122
let future = start_event_observer(
120123
event_observer_config_moved,
121-
observer_command_tx,
124+
observer_command_tx_moved,
122125
observer_command_rx,
123126
Some(observer_event_tx),
124127
context_cloned,
@@ -131,14 +134,16 @@ impl Service {
131134
let stacks_scan_pool = ThreadPool::new(STACKS_SCAN_THREAD_POOL_SIZE);
132135
let ctx = self.ctx.clone();
133136
let config = self.config.clone();
137+
let observer_command_tx_moved = observer_command_tx.clone();
134138
let _ = hiro_system_kit::thread_named("Stacks scan runloop")
135139
.spawn(move || {
136-
while let Ok(predicate_spec) = stacks_scan_op_rx.recv() {
140+
while let Ok((predicate_spec, api_key)) = stacks_scan_op_rx.recv() {
137141
let moved_ctx = ctx.clone();
138142
let mut moved_config = config.clone();
143+
let observer_command_tx = observer_command_tx_moved.clone();
139144
stacks_scan_pool.execute(move || {
140145
let op = scan_stacks_chainstate_via_csv_using_predicate(
141-
predicate_spec,
146+
&predicate_spec,
142147
&mut moved_config,
143148
&moved_ctx,
144149
);
@@ -156,6 +161,10 @@ impl Service {
156161
moved_ctx.expect_logger(),
157162
"Stacks chainstate scan completed up to block: {}", end_block.index
158163
);
164+
let _ = observer_command_tx.send(ObserverCommand::EnablePredicate(
165+
ChainhookSpecification::Stacks(predicate_spec),
166+
api_key,
167+
));
159168
});
160169
}
161170
let res = stacks_scan_pool.join();
@@ -168,14 +177,16 @@ impl Service {
168177
let bitcoin_scan_pool = ThreadPool::new(BITCOIN_SCAN_THREAD_POOL_SIZE);
169178
let ctx = self.ctx.clone();
170179
let config = self.config.clone();
180+
let moved_observer_command_tx = observer_command_tx.clone();
171181
let _ = hiro_system_kit::thread_named("Bitcoin scan runloop")
172182
.spawn(move || {
173-
while let Ok(predicate_spec) = bitcoin_scan_op_rx.recv() {
183+
while let Ok((predicate_spec, api_key)) = bitcoin_scan_op_rx.recv() {
174184
let moved_ctx = ctx.clone();
175185
let moved_config = config.clone();
186+
let observer_command_tx = moved_observer_command_tx.clone();
176187
bitcoin_scan_pool.execute(move || {
177188
let op = scan_bitcoin_chainstate_via_http_using_predicate(
178-
predicate_spec,
189+
&predicate_spec,
179190
&moved_config,
180191
&moved_ctx,
181192
);
@@ -189,6 +200,10 @@ impl Service {
189200
);
190201
}
191202
};
203+
let _ = observer_command_tx.send(ObserverCommand::EnablePredicate(
204+
ChainhookSpecification::Bitcoin(predicate_spec),
205+
api_key,
206+
));
192207
});
193208
}
194209
let res = bitcoin_scan_pool.join();
@@ -219,7 +234,7 @@ impl Service {
219234
}
220235
};
221236
match event {
222-
ObserverEvent::HookRegistered(chainhook) => {
237+
ObserverEvent::HookRegistered(chainhook, api_key) => {
223238
// If start block specified, use it.
224239
// I no start block specified, depending on the nature the hook, we'd like to retrieve:
225240
// - contract-id
@@ -243,10 +258,10 @@ impl Service {
243258
}
244259
match chainhook {
245260
ChainhookSpecification::Stacks(predicate_spec) => {
246-
let _ = stacks_scan_op_tx.send(predicate_spec);
261+
let _ = stacks_scan_op_tx.send((predicate_spec, api_key));
247262
}
248263
ChainhookSpecification::Bitcoin(predicate_spec) => {
249-
let _ = bitcoin_scan_op_tx.send(predicate_spec);
264+
let _ = bitcoin_scan_op_tx.send((predicate_spec, api_key));
250265
}
251266
}
252267
}

components/chainhook-event-observer/src/chainhooks/types.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use schemars::JsonSchema;
1111

1212
use crate::observer::ApiKey;
1313

14-
#[derive(Clone, Debug, JsonSchema)]
14+
#[derive(Clone, Debug)]
1515
pub struct ChainhookConfig {
1616
pub stacks_chainhooks: Vec<StacksChainhookSpecification>,
1717
pub bitcoin_chainhooks: Vec<BitcoinChainhookSpecification>,
@@ -68,6 +68,30 @@ impl ChainhookConfig {
6868
Ok(spec)
6969
}
7070

71+
pub fn enable_specification(
72+
&mut self,
73+
predicate_spec: &ChainhookSpecification,
74+
) {
75+
match predicate_spec {
76+
ChainhookSpecification::Stacks(spec_to_enable) => {
77+
for spec in self.stacks_chainhooks.iter_mut() {
78+
if spec.uuid.eq(&spec_to_enable.uuid) {
79+
spec.enabled = true;
80+
break;
81+
}
82+
}
83+
}
84+
ChainhookSpecification::Bitcoin(spec_to_enable) => {
85+
for spec in self.bitcoin_chainhooks.iter_mut() {
86+
if spec.uuid.eq(&spec_to_enable.uuid) {
87+
spec.enabled = true;
88+
break;
89+
}
90+
}
91+
}
92+
};
93+
}
94+
7195
pub fn register_specification(&mut self, spec: ChainhookSpecification) -> Result<(), String> {
7296
match spec {
7397
ChainhookSpecification::Stacks(spec) => {
@@ -131,7 +155,7 @@ impl Serialize for ChainhookConfig {
131155
}
132156
}
133157

134-
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
158+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
135159
#[serde(rename_all = "snake_case")]
136160
pub enum ChainhookSpecification {
137161
Bitcoin(BitcoinChainhookSpecification),
@@ -201,7 +225,7 @@ impl ChainhookSpecification {
201225
}
202226
}
203227

204-
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
228+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
205229
pub struct BitcoinChainhookSpecification {
206230
pub uuid: String,
207231
#[serde(skip_serializing_if = "Option::is_none")]
@@ -221,6 +245,7 @@ pub struct BitcoinChainhookSpecification {
221245
pub include_inputs: bool,
222246
pub include_outputs: bool,
223247
pub include_witness: bool,
248+
pub enabled: bool,
224249
}
225250

226251
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
@@ -291,6 +316,7 @@ impl BitcoinChainhookFullSpecification {
291316
include_inputs: spec.include_inputs.unwrap_or(false),
292317
include_outputs: spec.include_outputs.unwrap_or(false),
293318
include_witness: spec.include_witness.unwrap_or(false),
319+
enabled: false,
294320
})
295321
}
296322
}
@@ -349,6 +375,7 @@ impl StacksChainhookFullSpecification {
349375
expire_after_occurrence: spec.expire_after_occurrence,
350376
predicate: spec.predicate,
351377
action: spec.action,
378+
enabled: false,
352379
})
353380
}
354381
}
@@ -645,7 +672,7 @@ pub enum BlockIdentifierHashRule {
645672
BuildsOff(String),
646673
}
647674

648-
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
675+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
649676
pub struct StacksChainhookSpecification {
650677
pub uuid: String,
651678
#[serde(skip_serializing_if = "Option::is_none")]
@@ -666,6 +693,7 @@ pub struct StacksChainhookSpecification {
666693
#[serde(rename = "predicate")]
667694
pub predicate: StacksPredicate,
668695
pub action: HookAction,
696+
pub enabled: bool,
669697
}
670698

671699
impl StacksChainhookSpecification {

components/chainhook-event-observer/src/hord/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_reveal_data(
198198
Some(traversal) => traversal,
199199
None => {
200200
ctx.try_log(|logger| {
201-
slog::info!(logger, "Unable to retrieve cached inscription data for inscription {}", new_tx.transaction_identifier.hash);
201+
slog::info!(
202+
logger,
203+
"Unable to retrieve cached inscription data for inscription {}",
204+
new_tx.transaction_identifier.hash
205+
);
202206
});
203207
ordinals_events_indexes_to_discard.push_front(ordinal_event_index);
204208
continue;

0 commit comments

Comments
 (0)