Skip to content

Commit e438db7

Browse files
author
Ludo Galabru
committed
fix: cache's ambitions
1 parent 607ac95 commit e438db7

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
591591
hash: "".into(),
592592
};
593593
// let global_block_cache = HashMap::new();
594-
let (traversal, _) = retrieve_satoshi_point_using_local_storage(
594+
let traversal = retrieve_satoshi_point_using_local_storage(
595595
&hord_db_conn,
596596
&block_identifier,
597597
&transaction_identifier,
@@ -873,7 +873,7 @@ pub async fn perform_hord_db_update(
873873

874874
let blocks_db = open_readwrite_hord_db_conn_rocks_db(&config.expected_cache_path(), &ctx)?;
875875
let inscriptions_db_conn_rw = open_readwrite_hord_db_conn(&config.expected_cache_path(), &ctx)?;
876-
876+
877877
let _ = fetch_and_cache_blocks_in_hord_db(
878878
&bitcoin_config,
879879
&blocks_db,

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
876876
};
877877

878878
let _ = blocks_db_rw.flush();
879-
879+
880880
if let Err(e) = update_hord_db_and_augment_bitcoin_block(
881881
&mut new_block,
882882
blocks_db_rw,
@@ -961,7 +961,7 @@ pub fn retrieve_satoshi_point_using_local_storage(
961961
transaction_identifier: &TransactionIdentifier,
962962
inscription_number: u64,
963963
ctx: &Context,
964-
) -> Result<(TraversalResult, HashMap<u32, CompactedBlock>), String> {
964+
) -> Result<TraversalResult, String> {
965965
ctx.try_log(|logger| {
966966
slog::info!(
967967
logger,
@@ -983,6 +983,8 @@ pub fn retrieve_satoshi_point_using_local_storage(
983983
let mut hops: u32 = 0;
984984
let mut local_block_cache = HashMap::new();
985985
loop {
986+
local_block_cache.clear();
987+
986988
hops += 1;
987989
let block = match local_block_cache.get(&ordinal_block_number) {
988990
Some(block) => block,
@@ -1107,12 +1109,9 @@ pub fn retrieve_satoshi_point_using_local_storage(
11071109
let height = Height(ordinal_block_number.into());
11081110
let ordinal_number = height.starting_sat().0 + ordinal_offset;
11091111

1110-
Ok((
1111-
TraversalResult {
1112-
inscription_number,
1113-
ordinal_number,
1114-
transfers: hops,
1115-
},
1116-
local_block_cache,
1117-
))
1112+
Ok(TraversalResult {
1113+
inscription_number,
1114+
ordinal_number,
1115+
transfers: hops,
1116+
})
11181117
}

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

+22-11
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,40 @@ pub fn update_hord_db_and_augment_bitcoin_block(
146146
let (traversal_tx, traversal_rx) = channel::<(TransactionIdentifier, _)>();
147147
let traversal_data_pool = ThreadPool::new(10);
148148

149+
let mut rng = thread_rng();
150+
transactions_ids.shuffle(&mut rng);
151+
149152
for transaction_id in transactions_ids.into_iter() {
150153
let moved_traversal_tx = traversal_tx.clone();
151154
let moved_ctx = ctx.clone();
152155
let block_identifier = new_block.block_identifier.clone();
153156
let moved_hord_db_path = hord_db_path.clone();
154-
traversal_data_pool.execute(move || {
155-
if let Ok(blocks_db) = open_readonly_hord_db_conn_rocks_db(&moved_hord_db_path, &moved_ctx) {
156-
let traversal = retrieve_satoshi_point_using_local_storage(
157-
&blocks_db,
158-
&block_identifier,
159-
&transaction_id,
160-
0,
161-
&moved_ctx,
162-
);
163-
let _ = moved_traversal_tx.send((transaction_id, traversal));
157+
traversal_data_pool.execute(move || loop {
158+
match open_readonly_hord_db_conn_rocks_db(&moved_hord_db_path, &moved_ctx) {
159+
Ok(blocks_db) => {
160+
let traversal = retrieve_satoshi_point_using_local_storage(
161+
&blocks_db,
162+
&block_identifier,
163+
&transaction_id,
164+
0,
165+
&moved_ctx,
166+
);
167+
let _ = moved_traversal_tx.send((transaction_id, traversal));
168+
break;
169+
}
170+
Err(e) => {
171+
moved_ctx.try_log(|logger| {
172+
slog::error!(logger, "unable to open rocksdb: {e}",);
173+
});
174+
}
164175
}
165176
});
166177
}
167178

168179
let mut traversals_received = 0;
169180
while let Ok((transaction_identifier, traversal_result)) = traversal_rx.recv() {
170181
traversals_received += 1;
171-
let (traversal, _) = traversal_result?;
182+
let traversal = traversal_result?;
172183
traversals.insert(transaction_identifier, traversal);
173184
if traversals_received == expected_traversals {
174185
break;

0 commit comments

Comments
 (0)