Skip to content

Commit f23be24

Browse files
author
Ludo Galabru
committed
fix: enable specs on reboot
1 parent d524771 commit f23be24

File tree

6 files changed

+51
-35
lines changed

6 files changed

+51
-35
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
170170
Ok(action) => {
171171
actions_triggered += 1;
172172
let res = match action {
173-
StacksChainhookOccurrence::Http(request) => send_request(request, 3, 1, &ctx).await,
173+
StacksChainhookOccurrence::Http(request) => {
174+
send_request(request, 3, 1, &ctx).await
175+
}
174176
StacksChainhookOccurrence::File(path, bytes) => file_append(path, bytes, &ctx),
175177
StacksChainhookOccurrence::Data(_payload) => unreachable!(),
176178
};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Service {
3737
let registered_predicates = load_predicates_from_redis(&self.config, &self.ctx)?;
3838
for predicate in registered_predicates.into_iter() {
3939
let predicate_uuid = predicate.uuid().to_string();
40-
match chainhook_config.register_specification(predicate) {
40+
match chainhook_config.register_specification(predicate, true) {
4141
Ok(_) => {
4242
info!(
4343
self.ctx.expect_logger(),
@@ -379,7 +379,7 @@ fn load_predicates_from_redis(
379379

380380
let mut predicates = vec![];
381381
for key in chainhooks_to_load.iter() {
382-
let chainhook = match redis_con.hget::<_, _, String>(key, "specification") {
382+
let mut chainhook = match redis_con.hget::<_, _, String>(key, "specification") {
383383
Ok(spec) => match ChainhookSpecification::deserialize_specification(&spec, key) {
384384
Ok(spec) => spec,
385385
Err(e) => {

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,17 @@ impl ChainhookConfig {
8989
};
9090
}
9191

92-
pub fn register_specification(&mut self, spec: ChainhookSpecification) -> Result<(), String> {
92+
pub fn register_specification(&mut self, spec: ChainhookSpecification, enabled: bool) -> Result<(), String> {
9393
match spec {
9494
ChainhookSpecification::Stacks(spec) => {
95-
self.stacks_chainhooks.push(spec.clone());
95+
let mut spec = spec.clone();
96+
spec.enabled = enabled;
97+
self.stacks_chainhooks.push(spec);
9698
}
9799
ChainhookSpecification::Bitcoin(spec) => {
98-
self.bitcoin_chainhooks.push(spec.clone());
100+
let mut spec = spec.clone();
101+
spec.enabled = enabled;
102+
self.bitcoin_chainhooks.push(spec);
99103
}
100104
};
101105
Ok(())

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub fn initialize_hord_db(path: &PathBuf, ctx: &Context) -> Connection {
8181
ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string()));
8282
}
8383

84-
8584
conn
8685
}
8786

@@ -827,7 +826,11 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
827826
&ctx,
828827
) {
829828
ctx.try_log(|logger| {
830-
slog::error!(logger, "Unable to augment bitcoin block {} with hord_db: {e}", new_block.block_identifier.index)
829+
slog::error!(
830+
logger,
831+
"Unable to augment bitcoin block {} with hord_db: {e}",
832+
new_block.block_identifier.index
833+
)
831834
});
832835
return Err(e);
833836
}

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

+32-26
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ use crate::{
2020
hord::{
2121
db::{
2222
find_inscription_with_ordinal_number, find_inscriptions_at_wached_outpoint,
23-
insert_entry_in_blocks,
24-
retrieve_satoshi_point_using_local_storage, store_new_inscription,
25-
update_transfered_inscription, CompactedBlock,
23+
insert_entry_in_blocks, retrieve_satoshi_point_using_local_storage,
24+
store_new_inscription, update_transfered_inscription, CompactedBlock,
2625
},
2726
ord::height::Height,
2827
},
2928
utils::Context,
3029
};
3130

3231
use self::db::{
33-
find_inscription_with_id, open_readonly_hord_db_conn_rocks_db, remove_entry_from_blocks,
34-
remove_entry_from_inscriptions, TraversalResult, WatchedSatpoint, find_latest_inscription_number_at_block_height,
32+
find_inscription_with_id, find_latest_inscription_number_at_block_height,
33+
open_readonly_hord_db_conn_rocks_db, remove_entry_from_blocks, remove_entry_from_inscriptions,
34+
TraversalResult, WatchedSatpoint,
3535
};
3636

3737
pub fn get_inscriptions_revealed_in_block(
@@ -202,13 +202,33 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_reveal_data(
202202
inscription_db_conn: &Connection,
203203
ctx: &Context,
204204
) {
205+
let mut latest_inscription_number = match find_latest_inscription_number_at_block_height(
206+
&block.block_identifier.index,
207+
&inscription_db_conn,
208+
&ctx,
209+
) {
210+
Ok(None) => 0,
211+
Ok(Some(inscription_number)) => inscription_number + 1,
212+
Err(e) => {
213+
ctx.try_log(|logger| {
214+
slog::error!(
215+
logger,
216+
"unable to retrieve inscription number: {}",
217+
e.to_string()
218+
);
219+
});
220+
return;
221+
}
222+
};
205223
for new_tx in block.transactions.iter_mut().skip(1) {
206224
let mut ordinals_events_indexes_to_discard = VecDeque::new();
207225
// Have a new inscription been revealed, if so, are looking at a re-inscription
208226
for (ordinal_event_index, ordinal_event) in
209227
new_tx.metadata.ordinal_operations.iter_mut().enumerate()
210228
{
211229
if let OrdinalOperation::InscriptionRevealed(inscription) = ordinal_event {
230+
let inscription_number = latest_inscription_number;
231+
latest_inscription_number += 1;
212232
let traversal = match traversals.get(&new_tx.transaction_identifier) {
213233
Some(traversal) => traversal,
214234
None => {
@@ -248,21 +268,7 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_reveal_data(
248268
});
249269
ordinals_events_indexes_to_discard.push_front(ordinal_event_index);
250270
} else {
251-
inscription.inscription_number =
252-
match find_latest_inscription_number_at_block_height(&block.block_identifier.index, &inscription_db_conn, &ctx) {
253-
Ok(None) => 0,
254-
Ok(Some(inscription_number)) => inscription_number + 1,
255-
Err(e) => {
256-
ctx.try_log(|logger| {
257-
slog::error!(
258-
logger,
259-
"unable to retrieve satoshi number: {}",
260-
e.to_string()
261-
);
262-
});
263-
continue;
264-
}
265-
};
271+
inscription.inscription_number = inscription_number;
266272
ctx.try_log(|logger| {
267273
slog::info!(
268274
logger,
@@ -273,13 +279,13 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_reveal_data(
273279
traversal.ordinal_number
274280
);
275281
});
282+
store_new_inscription(
283+
&inscription,
284+
&block.block_identifier,
285+
&rw_hord_db_conn,
286+
&ctx,
287+
);
276288
}
277-
store_new_inscription(
278-
&inscription,
279-
&block.block_identifier,
280-
&rw_hord_db_conn,
281-
&ctx,
282-
);
283289
}
284290
Storage::Memory(map) => {
285291
let outpoint = inscription.satpoint_post_inscription

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ pub async fn start_observer_commands_handler(
699699
ctx.try_log(|logger| {
700700
slog::error!(
701701
logger,
702-
"Unable to insert bitcoin block {} in hord_db: {e}", block.block_identifier.index
702+
"Unable to insert bitcoin block {} in hord_db: {e}",
703+
block.block_identifier.index
703704
)
704705
});
705706
}

0 commit comments

Comments
 (0)