Skip to content

Commit d03f851

Browse files
author
Ludo Galabru
committed
fix: boot sequence, logs, format
1 parent 90af1de commit d03f851

File tree

4 files changed

+62
-47
lines changed

4 files changed

+62
-47
lines changed

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
726726
let mut hord_config = config.get_hord_config();
727727
hord_config.network_thread_max = cmd.network_threads;
728728

729-
rebuild_rocks_db(
730-
&config,
731-
cmd.start_block,
732-
cmd.end_block,
733-
&ctx,
734-
)
735-
.await?
729+
rebuild_rocks_db(&config, cmd.start_block, cmd.end_block, &ctx).await?
736730
}
737731
RepairCommand::Transfers(cmd) => {
738732
let config = Config::default(false, false, false, &cmd.config_path)?;

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

+41-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::{
77

88
use chainhook_sdk::{
99
indexer::bitcoin::{
10-
build_http_client, download_block_with_retry, retrieve_block_hash_with_retry, try_download_block_bytes_with_retry, parse_fetched_block, download_block, parse_downloaded_block,
10+
build_http_client, download_block, download_block_with_retry, parse_downloaded_block,
11+
parse_fetched_block, retrieve_block_hash_with_retry, try_download_block_bytes_with_retry,
1112
},
1213
types::{
1314
BitcoinBlockData, BlockIdentifier, OrdinalInscriptionRevealData,
@@ -21,18 +22,21 @@ use rand::{thread_rng, Rng};
2122

2223
use rocksdb::DB;
2324
use rusqlite::{Connection, OpenFlags, ToSql, Transaction};
24-
use tokio::task::JoinSet;
2525
use std::io::Cursor;
2626
use std::io::{Read, Write};
2727
use threadpool::ThreadPool;
28+
use tokio::task::JoinSet;
2829

2930
use chainhook_sdk::{
3031
indexer::bitcoin::BitcoinBlockFullBreakdown, observer::BitcoinConfig, utils::Context,
3132
};
3233

33-
use crate::{hord::{self, HordConfig}, config::Config};
3434
use crate::hord::{new_traversals_lazy_cache, update_hord_db_and_augment_bitcoin_block};
3535
use crate::ord::{height::Height, sat::Sat};
36+
use crate::{
37+
config::Config,
38+
hord::{self, HordConfig},
39+
};
3640

3741
fn get_default_hord_db_file_path(base_dir: &PathBuf) -> PathBuf {
3842
let mut destination_path = base_dir.clone();
@@ -973,6 +977,10 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
973977
};
974978
let _ = block_data_tx.send(res);
975979
});
980+
// TODO: remove this join?
981+
if block_height >= ordinal_computing_height {
982+
let _ = retrieve_block_data_pool.join();
983+
}
976984
}
977985
let res = retrieve_block_data_pool.join();
978986
res
@@ -994,6 +1002,9 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
9941002
block_data,
9951003
)));
9961004
});
1005+
if block_height >= ordinal_computing_height {
1006+
let _ = compress_block_data_pool.join();
1007+
}
9971008
}
9981009
let res = compress_block_data_pool.join();
9991010
res
@@ -1018,10 +1029,15 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
10181029

10191030
// Should we start look for inscriptions data in blocks?
10201031
if raw_block.height as u64 >= ordinal_computing_height {
1021-
if cursor == 0 {
1032+
if (cursor as u64) < ordinal_computing_height {
10221033
cursor = raw_block.height;
10231034
}
1024-
ctx.try_log(|logger| slog::info!(logger, "Queueing compacted block #{block_height}",));
1035+
ctx.try_log(|logger| {
1036+
slog::info!(
1037+
logger,
1038+
"Queueing compacted block #{block_height} (#{cursor})",
1039+
)
1040+
});
10251041
// Is the action of processing a block allows us
10261042
// to process more blocks present in the inbox?
10271043
inbox.insert(raw_block.height, raw_block);
@@ -1871,7 +1887,6 @@ pub async fn rebuild_rocks_db(
18711887
let number_of_blocks_to_process = end_block - start_block + 1;
18721888
let (block_req_lim, block_process_lim) = (128, 128);
18731889

1874-
18751890
let (block_data_tx, block_data_rx) = crossbeam_channel::bounded(block_req_lim);
18761891
let compress_block_data_pool = ThreadPool::new(hord_config.ingestion_thread_max);
18771892
let (block_compressed_tx, block_compressed_rx) = crossbeam_channel::bounded(block_process_lim);
@@ -1902,7 +1917,7 @@ pub async fn rebuild_rocks_db(
19021917

19031918
let _ = hiro_system_kit::thread_named("Block data compression")
19041919
.spawn(move || {
1905-
while let Ok(Some(block_bytes)) = block_data_rx.recv() {
1920+
while let Ok(Some(block_bytes)) = block_data_rx.recv() {
19061921
let block_compressed_tx_moved = block_compressed_tx.clone();
19071922
compress_block_data_pool.execute(move || {
19081923
let block_data = parse_downloaded_block(block_bytes).unwrap();
@@ -1919,43 +1934,46 @@ pub async fn rebuild_rocks_db(
19191934
let res = compress_block_data_pool.join();
19201935
res
19211936
})
1922-
.expect("unable to spawn thread");
1937+
.expect("unable to spawn thread");
19231938

1924-
let blocks_db_rw =
1925-
open_readwrite_hord_db_conn_rocks_db(&config.expected_cache_path(), &ctx)?;
1939+
let blocks_db_rw = open_readwrite_hord_db_conn_rocks_db(&config.expected_cache_path(), &ctx)?;
19261940
let cloned_ctx = ctx.clone();
19271941
let ingestion_thread = hiro_system_kit::thread_named("Block data ingestion")
19281942
.spawn(move || {
19291943
let mut blocks_stored = 0;
19301944
let mut num_writes = 0;
1931-
1932-
while let Ok(Some((block_height, compacted_block, _raw_block))) = block_compressed_rx.recv() {
1945+
1946+
while let Ok(Some((block_height, compacted_block, _raw_block))) =
1947+
block_compressed_rx.recv()
1948+
{
19331949
insert_entry_in_blocks(block_height, &compacted_block, &blocks_db_rw, &cloned_ctx);
19341950
blocks_stored += 1;
19351951
num_writes += 1;
1936-
1952+
19371953
// In the context of ordinals, we're constrained to process blocks sequentially
19381954
// Blocks are processed by a threadpool and could be coming out of order.
19391955
// Inbox block for later if the current block is not the one we should be
19401956
// processing.
1941-
1957+
19421958
// Should we start look for inscriptions data in blocks?
1943-
cloned_ctx.try_log(|logger| slog::info!(logger, "Storing compacted block #{block_height}",));
1944-
1959+
cloned_ctx.try_log(|logger| {
1960+
slog::info!(logger, "Storing compacted block #{block_height}",)
1961+
});
1962+
19451963
if blocks_stored == number_of_blocks_to_process {
19461964
cloned_ctx.try_log(|logger| {
19471965
slog::info!(
19481966
logger,
19491967
"Local block storage successfully seeded with #{blocks_stored} blocks"
19501968
)
19511969
});
1952-
1970+
19531971
// match guard.report().build() {
19541972
// Ok(report) => {
19551973
// ctx.try_log(|logger| {
19561974
// slog::info!(logger, "Generating report");
19571975
// });
1958-
1976+
19591977
// let file = std::fs::File::create("hord-perf.svg").unwrap();
19601978
// report.flamegraph(file).unwrap();
19611979
// }
@@ -1966,7 +1984,7 @@ pub async fn rebuild_rocks_db(
19661984
// }
19671985
// }
19681986
}
1969-
1987+
19701988
if num_writes % 128 == 0 {
19711989
cloned_ctx.try_log(|logger| {
19721990
slog::info!(logger, "Flushing DB to disk ({num_writes} inserts)");
@@ -1979,20 +1997,20 @@ pub async fn rebuild_rocks_db(
19791997
num_writes = 0;
19801998
}
19811999
}
1982-
2000+
19832001
if let Err(e) = blocks_db_rw.flush() {
19842002
cloned_ctx.try_log(|logger| {
19852003
slog::error!(logger, "{}", e.to_string());
19862004
});
19872005
}
19882006
()
1989-
}).expect("unable to spawn thread");
2007+
})
2008+
.expect("unable to spawn thread");
19902009

19912010
while let Some(res) = set.join_next().await {
19922011
let block = res.unwrap().unwrap();
19932012

1994-
let _ = block_data_tx
1995-
.send(Some(block));
2013+
let _ = block_data_tx.send(Some(block));
19962014

19972015
if let Some(block_height) = block_heights.pop_front() {
19982016
let config = moved_config.clone();

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ pub fn update_hord_db_and_augment_bitcoin_block(
437437
&inner_ctx,
438438
)?;
439439

440+
if !any_inscription_revealed && !any_inscription_transferred {
441+
return Ok(());
442+
}
443+
440444
if discard_changes {
441445
ctx.try_log(|logger| {
442446
slog::info!(
@@ -462,22 +466,21 @@ pub fn update_hord_db_and_augment_bitcoin_block(
462466
)
463467
});
464468
}
465-
if any_inscription_revealed || any_inscription_transferred {
466-
let inscriptions_revealed = get_inscriptions_revealed_in_block(&new_block)
467-
.iter()
468-
.map(|d| d.inscription_number.to_string())
469-
.collect::<Vec<String>>();
470469

471-
ctx.try_log(|logger| {
472-
slog::info!(
473-
logger,
474-
"Block #{} processed through hord, revealing {} inscriptions [{}]",
475-
new_block.block_identifier.index,
476-
inscriptions_revealed.len(),
477-
inscriptions_revealed.join(", ")
478-
)
479-
});
480-
}
470+
let inscriptions_revealed = get_inscriptions_revealed_in_block(&new_block)
471+
.iter()
472+
.map(|d| d.inscription_number.to_string())
473+
.collect::<Vec<String>>();
474+
475+
ctx.try_log(|logger| {
476+
slog::info!(
477+
logger,
478+
"Block #{} processed through hord, revealing {} inscriptions [{}]",
479+
new_block.block_identifier.index,
480+
inscriptions_revealed.len(),
481+
inscriptions_revealed.join(", ")
482+
)
483+
});
481484
Ok(())
482485
}
483486

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::config::{Config, PredicatesApi, PredicatesApiConfig};
66
use crate::db::{
77
find_all_inscriptions_in_block, format_satpoint_to_watch, insert_entry_in_locations,
88
open_readwrite_hord_db_conn, open_readwrite_hord_dbs, parse_satpoint_to_watch,
9-
remove_entries_from_locations_at_block_height, rebuild_rocks_db,
9+
rebuild_rocks_db, remove_entries_from_locations_at_block_height,
1010
};
1111
use crate::hord::{
1212
new_traversals_lazy_cache, revert_hord_db_with_augmented_bitcoin_block, should_sync_hord_db,
@@ -152,7 +152,7 @@ impl Service {
152152
})
153153
.expect("unable to spawn thread");
154154

155-
rebuild_rocks_db(&self.config, 420000, 767420, &self.ctx).await?;
155+
// rebuild_rocks_db(&self.config, 420000, 767420, &self.ctx).await?;
156156

157157
while let Some((start_block, end_block)) = should_sync_hord_db(&self.config, &self.ctx)? {
158158
if start_block == 0 {

0 commit comments

Comments
 (0)