Skip to content

Commit 62fd929

Browse files
author
Ludo Galabru
committed
fix: inject l1 cache hit in results (+ clearing)
1 parent b38bfd7 commit 62fd929

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ struct CheckDbCommand {
416416
/// Starting block
417417
pub start_block: u64,
418418
/// Ending block
419-
pub end_block: u64,
419+
pub end_block: u64,
420420
/// Load config file path
421421
#[clap(long = "config-path")]
422422
pub config_path: Option<String>,
@@ -891,7 +891,9 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
891891

892892
let mut missing_blocks = vec![];
893893
for i in cmd.start_block..=cmd.end_block {
894-
if find_lazy_block_at_block_height(i as u32, 0, false, &blocks_db, &ctx).is_none() {
894+
if find_lazy_block_at_block_height(i as u32, 0, false, &blocks_db, &ctx)
895+
.is_none()
896+
{
895897
println!("Missing block {i}");
896898
missing_blocks.push(i);
897899
}

components/hord-cli/src/config/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl Config {
121121
BitcoinNetwork::Testnet => 2413343,
122122
// BitcoinNetwork::Signet => 112402,
123123
},
124+
logs: self.logs.clone(),
124125
}
125126
}
126127

components/hord-cli/src/db/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2375,12 +2375,12 @@ pub async fn rebuild_rocks_db(
23752375
// Early return / wait for next block
23762376
cloned_ctx.try_log(|logger| {
23772377
slog::info!(logger, "Inboxing compacted block #{block_index}")
2378-
});
2379-
continue;
2378+
});
2379+
continue;
23802380
} else {
23812381
if let Some(ref tx) = blocks_post_processor {
23822382
let _ = tx.send(chunk);
2383-
}
2383+
}
23842384
}
23852385
}
23862386

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

+43-16
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::db::{
3939
};
4040
use crate::ord::height::Height;
4141

42-
use crate::config::Config;
42+
use crate::config::{Config, LogConfig};
4343

4444
use crate::db::format_outpoint_to_watch;
4545
use chainhook_sdk::indexer::bitcoin::{
@@ -70,6 +70,7 @@ pub struct HordConfig {
7070
pub cache_size: usize,
7171
pub db_path: PathBuf,
7272
pub first_inscription_height: u64,
73+
pub logs: LogConfig,
7374
}
7475

7576
pub fn parse_ordinals_and_standardize_block(
@@ -1109,8 +1110,12 @@ pub fn get_transactions_to_process(
11091110
cache_l1: &mut HashMap<(TransactionIdentifier, usize), TraversalResult>,
11101111
inscriptions_db_conn: &mut Connection,
11111112
ctx: &Context,
1112-
) -> Vec<(TransactionIdentifier, usize)> {
1113+
) -> (
1114+
Vec<(TransactionIdentifier, usize)>,
1115+
Vec<(TransactionIdentifier, usize)>,
1116+
) {
11131117
let mut transactions_ids: Vec<(TransactionIdentifier, usize)> = vec![];
1118+
let mut l1_cache_hits = vec![];
11141119

11151120
for tx in block.transactions.iter().skip(1) {
11161121
// Have a new inscription been revealed, if so, are looking at a re-inscription
@@ -1126,11 +1131,12 @@ pub fn get_transactions_to_process(
11261131
continue;
11271132
}
11281133
};
1129-
1130-
if cache_l1.contains_key(&(
1134+
let key = (
11311135
tx.transaction_identifier.clone(),
11321136
inscription_data.inscription_input_index,
1133-
)) {
1137+
);
1138+
if cache_l1.contains_key(&key) {
1139+
l1_cache_hits.push(key);
11341140
continue;
11351141
}
11361142

@@ -1155,7 +1161,7 @@ pub fn get_transactions_to_process(
11551161
}
11561162
}
11571163
}
1158-
transactions_ids
1164+
(transactions_ids, l1_cache_hits)
11591165
}
11601166

11611167
pub fn retrieve_inscribed_satoshi_points_from_block_v3(
@@ -1167,23 +1173,30 @@ pub fn retrieve_inscribed_satoshi_points_from_block_v3(
11671173
hord_config: &HordConfig,
11681174
ctx: &Context,
11691175
) -> Result<bool, String> {
1170-
let mut transactions_ids =
1176+
let (mut transactions_ids, l1_cache_hits) =
11711177
get_transactions_to_process(block, cache_l1, inscriptions_db_conn, ctx);
11721178

1173-
let has_transactions_to_process = !transactions_ids.is_empty();
1179+
let inner_ctx = if hord_config.logs.ordinals_computation {
1180+
ctx.clone()
1181+
} else {
1182+
Context::empty()
1183+
};
1184+
1185+
let has_transactions_to_process = !transactions_ids.is_empty() || !l1_cache_hits.is_empty();
11741186

11751187
if has_transactions_to_process {
1176-
let expected_traversals = transactions_ids.len();
1188+
let expected_traversals = transactions_ids.len() + l1_cache_hits.len();
11771189
let (traversal_tx, traversal_rx) = channel();
1178-
let traversal_data_pool = ThreadPool::new(hord_config.ingestion_thread_max);
11791190

1191+
let traversal_data_pool = ThreadPool::new(hord_config.ingestion_thread_max);
11801192
let mut tx_thread_pool = vec![];
1193+
11811194
for thread_index in 0..hord_config.ingestion_thread_max {
11821195
let (tx, rx) = channel();
11831196
tx_thread_pool.push(tx);
11841197

11851198
let moved_traversal_tx = traversal_tx.clone();
1186-
let moved_ctx = ctx.clone();
1199+
let moved_ctx = inner_ctx.clone();
11871200
let moved_hord_db_path = hord_config.db_path.clone();
11881201
let local_cache = cache_l2.clone();
11891202

@@ -1206,12 +1219,22 @@ pub fn retrieve_inscribed_satoshi_points_from_block_v3(
12061219
});
12071220
}
12081221

1222+
// Empty cache
1223+
let mut thread_index = 0;
1224+
for key in l1_cache_hits.iter() {
1225+
if let Some(entry) = cache_l1.remove(key) {
1226+
let _ = traversal_tx.send((Ok(entry), true, thread_index));
1227+
thread_index = (thread_index + 1) % hord_config.ingestion_thread_max;
1228+
}
1229+
}
1230+
12091231
ctx.try_log(|logger| {
12101232
info!(
12111233
logger,
1212-
"Number of inscriptions in block #{} to process: {}",
1234+
"Number of inscriptions in block #{} to process: {} (L1 cache hits: {})",
12131235
block.block_identifier.index,
1214-
transactions_ids.len()
1236+
transactions_ids.len(),
1237+
l1_cache_hits.len(),
12151238
)
12161239
});
12171240

@@ -1245,7 +1268,7 @@ pub fn retrieve_inscribed_satoshi_points_from_block_v3(
12451268
}
12461269
match traversal_result {
12471270
Ok(traversal) => {
1248-
ctx.try_log(|logger| {
1271+
inner_ctx.try_log(|logger| {
12491272
info!(
12501273
logger,
12511274
"Satoshi #{} was minted in block #{} at offset {} and was transferred {} times (progress: {traversals_received}/{expected_traversals}) (priority queue: {prioritary}, thread: {thread_index}).",
@@ -1277,7 +1300,7 @@ pub fn retrieve_inscribed_satoshi_points_from_block_v3(
12771300
let _ = tx_thread_pool[thread_index].send(Some(w));
12781301
} else {
12791302
if let Some(next_block) = next_block_iter.next() {
1280-
let mut transactions_ids = get_transactions_to_process(
1303+
let (mut transactions_ids, _) = get_transactions_to_process(
12811304
next_block,
12821305
cache_l1,
12831306
inscriptions_db_conn,
@@ -1358,7 +1381,11 @@ pub fn update_hord_db_and_augment_bitcoin_block_v3(
13581381
let inner_ctx = if discard_changes {
13591382
Context::empty()
13601383
} else {
1361-
ctx.clone()
1384+
if hord_config.logs.ordinals_computation {
1385+
ctx.clone()
1386+
} else {
1387+
Context::empty()
1388+
}
13621389
};
13631390

13641391
let transaction = inscriptions_db_conn_rw.transaction().unwrap();

components/hord-cli/src/hord/ordinals.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn start_ordinals_number_processor(
6161
&ctx,
6262
);
6363
num_writes += 1;
64-
64+
6565
if block.block_identifier.index >= hord_config.first_inscription_height {
6666
blocks.push(block);
6767
}
@@ -91,19 +91,13 @@ pub fn start_ordinals_number_processor(
9191
}
9292
garbage_collect_nth_block += blocks.len();
9393

94-
let detailed_ctx = if config.logs.ordinals_computation {
95-
ctx.clone()
96-
} else {
97-
Context::empty()
98-
};
99-
10094
process_blocks(
10195
blocks,
10296
&cache_l2,
10397
&mut inscription_height_hint,
10498
&mut inscriptions_db_conn_rw,
10599
&hord_config,
106-
&detailed_ctx,
100+
&ctx,
107101
);
108102

109103
// Clear L2 cache on a regular basis

0 commit comments

Comments
 (0)