Skip to content

Commit 0633182

Browse files
author
Ludo Galabru
committed
fix: myriad of improvements
1 parent aed7d5d commit 0633182

File tree

11 files changed

+73
-59
lines changed

11 files changed

+73
-59
lines changed

Cargo.lock

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/chainhook-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ hex = "0.4.3"
1717
rand = "0.8.5"
1818
# tikv-client = { git = "https://github.com/tikv/client-rust.git", rev = "8f54e6114227718e256027df2577bbacdf425f86" }
1919
# raft-proto = { git = "https://github.com/tikv/raft-rs", rev="f73766712a538c2f6eb135b455297ad6c03fc58d", version = "0.7.0"}
20-
chainhook-sdk = { version = "0.2.0", default-features = false, features = ["ordinals", "zeromq"], path = "../chainhook-sdk" }
20+
chainhook-sdk = { version = "0.4.0", default-features = false, features = ["ordinals", "zeromq"], path = "../chainhook-sdk" }
2121
chainhook-types = { version = "1.0.6", path = "../chainhook-types-rs" }
2222
clarinet-files = "1"
2323
hiro-system-kit = "0.1.0"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
859859

860860
let mut missing_blocks = vec![];
861861
for i in 1..=790000 {
862-
if find_lazy_block_at_block_height(i, 3, &blocks_db_rw).is_none() {
862+
if find_lazy_block_at_block_height(i, 10, &blocks_db_rw, &ctx).is_none() {
863863
println!("Missing block {i}");
864864
missing_blocks.push(i);
865865
}

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
8080
let blocks_db_rw =
8181
open_readwrite_hord_db_conn_rocks_db(&config.expected_cache_path(), ctx)?;
8282

83-
if find_lazy_block_at_block_height(end_block as u32, 3, &blocks_db_rw).is_none() {
83+
if find_lazy_block_at_block_height(end_block as u32, 10, &blocks_db_rw, &ctx).is_none() {
8484
// Count how many entries in the table
8585
// Compute the right interval
8686
// Start the build local storage routine
@@ -162,16 +162,13 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
162162
}
163163

164164
// Evaluating every single block is required for also keeping track of transfers.
165-
let local_traverals = match find_all_inscriptions_in_block(
166-
&cursor,
167-
&inscriptions_db_conn,
168-
&ctx,
169-
)
170-
.remove(&cursor)
171-
{
172-
Some(entry) => entry,
173-
None => vec![],
174-
};
165+
let local_traverals =
166+
match find_all_inscriptions_in_block(&cursor, &inscriptions_db_conn, &ctx)
167+
.remove(&cursor)
168+
{
169+
Some(entry) => entry,
170+
None => vec![],
171+
};
175172
for (transaction_identifier, traversal_result) in local_traverals.into_iter() {
176173
traversals.insert(
177174
(transaction_identifier, traversal_result.input_index),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Service {
119119
let context_cloned = self.ctx.clone();
120120
let event_observer_config_moved = event_observer_config.clone();
121121
let observer_command_tx_moved = observer_command_tx.clone();
122-
let _ = std::thread::spawn(move || {
122+
let _ = hiro_system_kit::thread_named("Chainhook event observer").spawn(move || {
123123
let future = start_event_observer(
124124
event_observer_config_moved,
125125
observer_command_tx_moved,

components/chainhook-sdk/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ serde = "1"
1212
serde_json = { version = "1", features = ["arbitrary_precision"] }
1313
serde-hex = "0.1.0"
1414
serde_derive = "1"
15-
stacks-rpc-client = "=1.0.6"
15+
stacks-rpc-client = "=1.0.7"
1616
clarinet-utils = "1"
1717
hiro-system-kit = "0.1.0"
1818
# stacks-rpc-client = { version = "1", path = "../../../clarinet/components/stacks-rpc-client" }

components/chainhook-sdk/src/chainhooks/stacks/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use chainhook_types::{
88
BlockIdentifier, StacksChainEvent, StacksTransactionData, StacksTransactionEvent,
99
StacksTransactionKind, TransactionIdentifier,
1010
};
11-
use stacks_rpc_client::clarity::stacks_common::codec::StacksMessageCodec;
12-
use stacks_rpc_client::clarity::vm::types::{CharType, SequenceData, Value as ClarityValue};
1311
use hiro_system_kit::slog;
1412
use reqwest::{Client, Method};
1513
use serde_json::Value as JsonValue;
14+
use stacks_rpc_client::clarity::stacks_common::codec::StacksMessageCodec;
15+
use stacks_rpc_client::clarity::vm::types::{CharType, SequenceData, Value as ClarityValue};
1616
use std::collections::{BTreeMap, HashMap};
1717
use std::io::Cursor;
1818

components/chainhook-sdk/src/hord/db/mod.rs

+42-33
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ pub fn find_lazy_block_at_block_height(
250250
block_height: u32,
251251
retry: u8,
252252
blocks_db: &DB,
253+
ctx: &Context,
253254
) -> Option<LazyBlock> {
254255
let mut attempt = 0;
255256
// let mut read_options = rocksdb::ReadOptions::default();
@@ -265,6 +266,14 @@ pub fn find_lazy_block_at_block_height(
265266
attempt += 1;
266267
backoff = 2.0 * backoff + (backoff * rng.gen_range(0.0..1.0));
267268
let duration = std::time::Duration::from_millis((backoff * 1_000.0) as u64);
269+
ctx.try_log(|logger| {
270+
slog::warn!(
271+
logger,
272+
"Unable to find block {}, will retry in {:?}",
273+
block_height,
274+
duration
275+
)
276+
});
268277
std::thread::sleep(duration);
269278
if attempt > retry {
270279
return None;
@@ -496,9 +505,7 @@ pub fn find_all_inscriptions_in_block(
496505
let ordinal_number: u64 = row.get(1).unwrap();
497506
let block_height: u64 = row.get(2).unwrap();
498507
let inscription_id: String = row.get(3).unwrap();
499-
let (transaction_identifier, input_index) = {
500-
parse_inscription_id(&inscription_id)
501-
};
508+
let (transaction_identifier, input_index) = { parse_inscription_id(&inscription_id) };
502509
let inscription_offset_intra_output: u64 = row.get(4).unwrap();
503510
let outpoint_to_watch: String = row.get(5).unwrap();
504511
let (latest_transaction_id, mut output_index) = parse_outpoint_to_watch(&outpoint_to_watch);
@@ -508,7 +515,8 @@ pub fn find_all_inscriptions_in_block(
508515
slog::warn!(
509516
logger,
510517
"Inscription {} ({}) most likely to end up lost in transfers",
511-
inscription_number, inscription_id
518+
inscription_number,
519+
inscription_id
512520
)
513521
});
514522
output_index = 0;
@@ -961,31 +969,32 @@ pub fn retrieve_satoshi_point_using_lazy_storage(
961969
let mut ordinal_block_number = block_identifier.index as u32;
962970
let txid = transaction_identifier.get_8_hash_bytes();
963971

964-
let (sats_ranges, inscription_offset_cross_outputs) =
965-
match traversals_cache.get(&(block_identifier.index as u32, txid.clone())) {
966-
Some(entry) => {
967-
let tx = entry.value();
968-
(
969-
tx.get_sat_ranges(),
970-
tx.get_cumulated_sats_in_until_input_index(input_index),
971-
)
972+
let (sats_ranges, inscription_offset_cross_outputs) = match traversals_cache
973+
.get(&(block_identifier.index as u32, txid.clone()))
974+
{
975+
Some(entry) => {
976+
let tx = entry.value();
977+
(
978+
tx.get_sat_ranges(),
979+
tx.get_cumulated_sats_in_until_input_index(input_index),
980+
)
981+
}
982+
None => match find_lazy_block_at_block_height(ordinal_block_number, 10, &blocks_db, &ctx) {
983+
None => {
984+
return Err(format!("block #{ordinal_block_number} not in database"));
972985
}
973-
None => match find_lazy_block_at_block_height(ordinal_block_number, 10, &blocks_db) {
974-
None => {
975-
return Err(format!("block #{ordinal_block_number} not in database"));
986+
Some(block) => match block.find_and_serialize_transaction_with_txid(&txid) {
987+
Some(tx) => {
988+
let sats_ranges = tx.get_sat_ranges();
989+
let inscription_offset_cross_outputs =
990+
tx.get_cumulated_sats_in_until_input_index(input_index);
991+
traversals_cache.insert((ordinal_block_number, txid.clone()), tx);
992+
(sats_ranges, inscription_offset_cross_outputs)
976993
}
977-
Some(block) => match block.find_and_serialize_transaction_with_txid(&txid) {
978-
Some(tx) => {
979-
let sats_ranges = tx.get_sat_ranges();
980-
let inscription_offset_cross_outputs =
981-
tx.get_cumulated_sats_in_until_input_index(input_index);
982-
traversals_cache.insert((ordinal_block_number, txid.clone()), tx);
983-
(sats_ranges, inscription_offset_cross_outputs)
984-
}
985-
None => return Err(format!("txid not in block #{ordinal_block_number}")),
986-
},
994+
None => return Err(format!("txid not in block #{ordinal_block_number}")),
987995
},
988-
};
996+
},
997+
};
989998

990999
for (i, (min, max)) in sats_ranges.into_iter().enumerate() {
9911000
if inscription_offset_cross_outputs >= min && inscription_offset_cross_outputs < max {
@@ -1068,13 +1077,13 @@ pub fn retrieve_satoshi_point_using_lazy_storage(
10681077
}
10691078
}
10701079

1071-
let lazy_block = match find_lazy_block_at_block_height(ordinal_block_number, 10, &blocks_db)
1072-
{
1073-
Some(block) => block,
1074-
None => {
1075-
return Err(format!("block #{ordinal_block_number} not in database"));
1076-
}
1077-
};
1080+
let lazy_block =
1081+
match find_lazy_block_at_block_height(ordinal_block_number, 10, &blocks_db, &ctx) {
1082+
Some(block) => block,
1083+
None => {
1084+
return Err(format!("block #{ordinal_block_number} not in database"));
1085+
}
1086+
};
10781087

10791088
let coinbase_txid = lazy_block.get_coinbase_txid();
10801089
let txid = tx_cursor.0;

components/chainhook-sdk/src/indexer/bitcoin/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ fn test_bitcoin_vector_040() {
211211

212212
#[test]
213213
fn test_ordinal_inscription_parsing() {
214-
215214
let bytes = hex::decode("208737bc46923c3e64c7e6768c0346879468bf3aba795a5f5f56efca288f50ed2aac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d38004c9948656c6c6f2030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030300a68").unwrap();
216215

217216
let script = Script::from(bytes);

components/chainhook-sdk/src/indexer/stacks/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use crate::indexer::AssetClassCache;
77
use crate::indexer::{IndexerConfig, StacksChainContext};
88
use crate::utils::Context;
99
use chainhook_types::*;
10-
use stacks_rpc_client::clarity::stacks_common::codec::StacksMessageCodec;
11-
use stacks_rpc_client::clarity::vm::types::{SequenceData, Value as ClarityValue};
12-
use stacks_rpc_client::clarity::codec::{StacksTransaction, TransactionAuth, TransactionPayload};
1310
use hiro_system_kit::slog;
1411
use rocket::serde::json::Value as JsonValue;
1512
use rocket::serde::Deserialize;
13+
use stacks_rpc_client::clarity::codec::{StacksTransaction, TransactionAuth, TransactionPayload};
14+
use stacks_rpc_client::clarity::stacks_common::codec::StacksMessageCodec;
15+
use stacks_rpc_client::clarity::vm::types::{SequenceData, Value as ClarityValue};
1616
use std::collections::{BTreeMap, HashMap, HashSet};
1717
use std::convert::TryInto;
1818
use std::io::Cursor;

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,9 @@ pub async fn start_observer_commands_handler(
10491049
});
10501050

10511051
if let Some(ref tx) = observer_events_tx {
1052-
let _ = tx.send(ObserverEvent::PredicatesTriggered(chainhooks_to_trigger.len()));
1052+
let _ = tx.send(ObserverEvent::PredicatesTriggered(
1053+
chainhooks_to_trigger.len(),
1054+
));
10531055
}
10541056
for chainhook_to_trigger in chainhooks_to_trigger.into_iter() {
10551057
match handle_bitcoin_hook_action(chainhook_to_trigger, &proofs) {
@@ -1186,7 +1188,9 @@ pub async fn start_observer_commands_handler(
11861188
}
11871189

11881190
if let Some(ref tx) = observer_events_tx {
1189-
let _ = tx.send(ObserverEvent::PredicatesTriggered(chainhooks_to_trigger.len()));
1191+
let _ = tx.send(ObserverEvent::PredicatesTriggered(
1192+
chainhooks_to_trigger.len(),
1193+
));
11901194
}
11911195
let proofs = HashMap::new();
11921196
for chainhook_to_trigger in chainhooks_to_trigger.into_iter() {

0 commit comments

Comments
 (0)