Skip to content

Commit cdfbf48

Browse files
author
Ludo Galabru
committed
fix: off by one inscriptions number
1 parent 8c8c5c8 commit cdfbf48

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

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

+28-12
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use chainhook_event_observer::chainhooks::bitcoin::{
66
BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
77
};
88
use chainhook_event_observer::chainhooks::types::{
9-
BitcoinChainhookSpecification, BitcoinPredicateType,
10-
Protocols,
9+
BitcoinChainhookSpecification, BitcoinPredicateType, Protocols,
1110
};
1211
use chainhook_event_observer::hord::db::{
1312
fetch_and_cache_blocks_in_hord_db, find_all_inscriptions, find_block_at_block_height,
@@ -27,7 +26,7 @@ use chainhook_event_observer::utils::{file_append, send_request, Context};
2726
use chainhook_types::{BitcoinChainEvent, BitcoinChainUpdatedWithBlocksData};
2827
use std::collections::{BTreeMap, HashMap};
2928

30-
pub async fn scan_bitcoin_chain_with_predicate_via_http(
29+
pub async fn scan_bitcoin_chainstate_via_http_using_predicate(
3130
predicate_spec: BitcoinChainhookSpecification,
3231
config: &Config,
3332
ctx: &Context,
@@ -137,6 +136,7 @@ pub async fn scan_bitcoin_chain_with_predicate_via_http(
137136

138137
let mut blocks_scanned = 0;
139138
let mut actions_triggered = 0;
139+
let mut err_count = 0;
140140

141141
let event_observer_config = config.get_event_observer_config();
142142
let bitcoin_config = event_observer_config.get_bitcoin_config();
@@ -199,8 +199,14 @@ pub async fn scan_bitcoin_chain_with_predicate_via_http(
199199
ctx,
200200
);
201201

202-
actions_triggered +=
203-
execute_predicates_action(hits, &event_observer_config, &ctx).await;
202+
match execute_predicates_action(hits, &event_observer_config, &ctx).await {
203+
Ok(actions) => actions_triggered += actions,
204+
Err(_) => err_count += 1,
205+
}
206+
207+
if err_count >= 3 {
208+
return Err(format!("Scan aborted (consecutive action errors >= 3)"));
209+
}
204210
}
205211
} else {
206212
let use_scan_to_seed_hord_db = true;
@@ -242,8 +248,14 @@ pub async fn scan_bitcoin_chain_with_predicate_via_http(
242248
ctx,
243249
);
244250

245-
actions_triggered +=
246-
execute_predicates_action(hits, &event_observer_config, &ctx).await;
251+
match execute_predicates_action(hits, &event_observer_config, &ctx).await {
252+
Ok(actions) => actions_triggered += actions,
253+
Err(_) => err_count += 1,
254+
}
255+
256+
if err_count >= 3 {
257+
return Err(format!("Scan aborted (consecutive action errors >= 3)"));
258+
}
247259
}
248260
}
249261
info!(
@@ -258,7 +270,7 @@ pub async fn execute_predicates_action<'a>(
258270
hits: Vec<BitcoinTriggerChainhook<'a>>,
259271
config: &EventObserverConfig,
260272
ctx: &Context,
261-
) -> u32 {
273+
) -> Result<u32, ()> {
262274
let mut actions_triggered = 0;
263275
let proofs = gather_proofs(&hits, &config, &ctx);
264276
for hit in hits.into_iter() {
@@ -269,13 +281,17 @@ pub async fn execute_predicates_action<'a>(
269281
Ok(action) => {
270282
actions_triggered += 1;
271283
match action {
272-
BitcoinChainhookOccurrence::Http(request) => send_request(request, &ctx).await,
273-
BitcoinChainhookOccurrence::File(path, bytes) => file_append(path, bytes, &ctx),
284+
BitcoinChainhookOccurrence::Http(request) => {
285+
send_request(request, &ctx).await?
286+
}
287+
BitcoinChainhookOccurrence::File(path, bytes) => {
288+
file_append(path, bytes, &ctx)?
289+
}
274290
BitcoinChainhookOccurrence::Data(_payload) => unreachable!(),
275-
}
291+
};
276292
}
277293
}
278294
}
279295

280-
actions_triggered
296+
Ok(actions_triggered)
281297
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ pub fn find_latest_inscription_block_height(
517517
pub fn find_latest_inscription_number(
518518
inscriptions_db_conn: &Connection,
519519
_ctx: &Context,
520-
) -> Result<u64, String> {
520+
) -> Result<Option<u64>, String> {
521521
let args: &[&dyn ToSql] = &[];
522522
let mut stmt = inscriptions_db_conn
523523
.prepare(
@@ -527,9 +527,9 @@ pub fn find_latest_inscription_number(
527527
let mut rows = stmt.query(args).unwrap();
528528
while let Ok(Some(row)) = rows.next() {
529529
let inscription_number: u64 = row.get(0).unwrap();
530-
return Ok(inscription_number);
530+
return Ok(Some(inscription_number));
531531
}
532-
Ok(0)
532+
Ok(None)
533533
}
534534

535535
pub fn find_inscription_with_ordinal_number(

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_reveal_data(
229229
} else {
230230
inscription.inscription_number =
231231
match find_latest_inscription_number(&inscription_db_conn, &ctx) {
232-
Ok(inscription_number) => inscription_number + 1,
232+
Ok(None) => 0,
233+
Ok(Some(inscription_number)) => inscription_number + 1,
233234
Err(e) => {
234235
ctx.try_log(|logger| {
235236
slog::error!(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ pub async fn start_observer_commands_handler(
10281028
request
10291029
)
10301030
});
1031-
send_request(request, &ctx).await;
1031+
let _ = send_request(request, &ctx).await;
10321032
}
10331033

10341034
if let Some(ref tx) = observer_events_tx {
@@ -1160,7 +1160,7 @@ pub async fn start_observer_commands_handler(
11601160
request
11611161
)
11621162
});
1163-
send_request(request, &ctx).await;
1163+
let _ = send_request(request, &ctx).await;
11641164
}
11651165

11661166
if let Some(ref tx) = observer_events_tx {

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ impl AbstractBlock for BitcoinBlockData {
139139
}
140140
}
141141

142-
pub async fn send_request(request: RequestBuilder, ctx: &Context) {
142+
pub async fn send_request(request: RequestBuilder, ctx: &Context) -> Result<(), ()> {
143143
match request.send().await {
144144
Ok(res) => {
145145
if res.status().is_success() {
146146
ctx.try_log(|logger| slog::info!(logger, "Trigger {} successful", res.url()));
147+
return Ok(());
147148
} else {
148149
ctx.try_log(|logger| {
149150
slog::warn!(
@@ -161,15 +162,16 @@ pub async fn send_request(request: RequestBuilder, ctx: &Context) {
161162
});
162163
}
163164
}
165+
Err(())
164166
}
165167

166-
pub fn file_append(path: String, bytes: Vec<u8>, ctx: &Context) {
168+
pub fn file_append(path: String, bytes: Vec<u8>, ctx: &Context) -> Result<(), ()> {
167169
let mut file_path = match std::env::current_dir() {
168170
Err(e) => {
169171
ctx.try_log(|logger| {
170172
slog::warn!(logger, "unable to retrieve current_dir {}", e.to_string())
171173
});
172-
return;
174+
return Err(());
173175
}
174176
Ok(p) => p,
175177
};
@@ -188,7 +190,7 @@ pub fn file_append(path: String, bytes: Vec<u8>, ctx: &Context) {
188190
e.to_string()
189191
)
190192
});
191-
return;
193+
return Err(());
192194
}
193195
}
194196
}
@@ -201,7 +203,7 @@ pub fn file_append(path: String, bytes: Vec<u8>, ctx: &Context) {
201203
{
202204
Err(e) => {
203205
ctx.try_log(|logger| slog::warn!(logger, "unable to open file {}", e.to_string()));
204-
return;
206+
return Err(());
205207
}
206208
Ok(p) => p,
207209
};
@@ -216,12 +218,15 @@ pub fn file_append(path: String, bytes: Vec<u8>, ctx: &Context) {
216218
e.to_string()
217219
)
218220
});
219-
return;
221+
return Err(());
220222
}
221223
};
222224

223225
if let Err(e) = writeln!(file, "{}", utf8) {
224226
ctx.try_log(|logger| slog::warn!(logger, "unable to open file {}", e.to_string()));
225227
eprintln!("Couldn't write to file: {}", e);
228+
return Err(());
226229
}
230+
231+
Ok(())
227232
}

0 commit comments

Comments
 (0)