Skip to content

Commit

Permalink
refactor: delete ordhook config, unify use of config
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Sep 10, 2024
1 parent b991c56 commit 5f42235
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 274 deletions.
69 changes: 18 additions & 51 deletions components/ordhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ use ordhook::config::Config;
use ordhook::core::meta_protocols::brc20::db::{
brc20_new_rw_db_conn, get_brc20_operations_on_block,
};
use ordhook::core::new_traversals_lazy_cache;
use ordhook::core::pipeline::download_and_pipeline_blocks;
use ordhook::core::pipeline::processors::block_archiving::start_block_archiving_processor;
use ordhook::core::pipeline::processors::start_inscription_indexing_processor;
use ordhook::core::protocol::inscription_parsing::parse_inscriptions_and_standardize_block;
use ordhook::core::protocol::satoshi_numbering::compute_satoshi_number;
use ordhook::core::{first_inscription_height, new_traversals_lazy_cache};
use ordhook::db::blocks::{
find_block_bytes_at_block_height, find_last_block_inserted, find_missing_blocks,
open_ordhook_db_conn_rocks_db_loop, open_readonly_ordhook_db_conn_rocks_db,
open_blocks_db_with_retry, open_readonly_blocks_db,
};
use ordhook::db::cursor::BlockBytesCursor;
use ordhook::db::ordinals::{
Expand Down Expand Up @@ -712,15 +712,12 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
let transaction_identifier = TransactionIdentifier::new(&cmd.transaction_id);
let cache = new_traversals_lazy_cache(100);
let (res, _, mut back_trace) = compute_satoshi_number(
&config.get_ordhook_config().db_path,
&block.block_identifier,
&transaction_identifier,
cmd.input_index,
0,
&Arc::new(cache),
config.resources.ulimit,
config.resources.memory_available,
true,
&config,

Check warning on line 720 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L720

Added line #L720 was not covered by tests
ctx,
)?;
back_trace.reverse();
Expand Down Expand Up @@ -750,27 +747,20 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
let last_known_block =
find_latest_inscription_block_height(&db_connections.ordhook, ctx)?;
if last_known_block.is_none() {
open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
ctx,
);
open_blocks_db_with_retry(true, &config, ctx);

Check warning on line 750 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L750

Added line #L750 was not covered by tests
}

let ordhook_config = config.get_ordhook_config();
let start_block = match cmd.start_at_block {
Some(entry) => entry,
None => match last_known_block {
Some(entry) => entry,
None => {
let first_height = first_inscription_height(&config);

Check warning on line 758 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L758

Added line #L758 was not covered by tests
warn!(
ctx.expect_logger(),
"Inscription ingestion will start at block #{}",
ordhook_config.first_inscription_height
"Inscription ingestion will start at block #{}", first_height
);
ordhook_config.first_inscription_height
first_height

Check warning on line 763 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L763

Added line #L763 was not covered by tests
}
},
};
Expand Down Expand Up @@ -819,13 +809,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
// Create DB
initialize_databases(&config, ctx);
open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
ctx,
);
open_blocks_db_with_retry(true, &config, ctx);

Check warning on line 812 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L812

Added line #L812 was not covered by tests
}
Command::Db(OrdhookDbCommand::Sync(cmd)) => {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
Expand All @@ -835,31 +819,24 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
}
Command::Db(OrdhookDbCommand::Repair(subcmd)) => match subcmd {
RepairCommand::Blocks(cmd) => {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
let mut ordhook_config = config.get_ordhook_config();
let mut config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;

Check warning on line 822 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L822

Added line #L822 was not covered by tests
if let Some(network_threads) = cmd.network_threads {
ordhook_config.resources.bitcoind_rpc_threads = network_threads;
config.resources.bitcoind_rpc_threads = network_threads;

Check warning on line 824 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L824

Added line #L824 was not covered by tests
}
let blocks = cmd.get_blocks();
let block_ingestion_processor =
start_block_archiving_processor(&config, ctx, false, None);
download_and_pipeline_blocks(
&config,
blocks,
ordhook_config.first_inscription_height,
first_inscription_height(&config),

Check warning on line 832 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L832

Added line #L832 was not covered by tests
Some(&block_ingestion_processor),
10_000,
ctx,
)
.await?;
if let Some(true) = cmd.debug {
let blocks_db = open_ordhook_db_conn_rocks_db_loop(
false,
&config.get_ordhook_config().db_path,
config.resources.ulimit,
config.resources.memory_available,
ctx,
);
let blocks_db = open_blocks_db_with_retry(false, &config, ctx);

Check warning on line 839 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L839

Added line #L839 was not covered by tests
for i in cmd.get_blocks().into_iter() {
let block_bytes =
find_block_bytes_at_block_height(i as u32, 10, &blocks_db, ctx)
Expand All @@ -874,10 +851,9 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
}
}
RepairCommand::Inscriptions(cmd) => {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
let mut ordhook_config = config.get_ordhook_config();
let mut config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;

Check warning on line 854 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L854

Added line #L854 was not covered by tests
if let Some(network_threads) = cmd.network_threads {
ordhook_config.resources.bitcoind_rpc_threads = network_threads;
config.resources.bitcoind_rpc_threads = network_threads;

Check warning on line 856 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L856

Added line #L856 was not covered by tests
}
let block_post_processor = match cmd.repair_observers {
Some(true) => {
Expand All @@ -898,7 +874,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
download_and_pipeline_blocks(
&config,
blocks,
ordhook_config.first_inscription_height,
first_inscription_height(&config),

Check warning on line 877 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L877

Added line #L877 was not covered by tests
Some(&inscription_indexing_processor),
10_000,
ctx,
Expand Down Expand Up @@ -932,12 +908,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
Command::Db(OrdhookDbCommand::Check(cmd)) => {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
{
let blocks_db = open_readonly_ordhook_db_conn_rocks_db(
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
ctx,
)?;
let blocks_db = open_readonly_blocks_db(&config, ctx)?;

Check warning on line 911 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L911

Added line #L911 was not covered by tests
let tip = find_last_block_inserted(&blocks_db);
println!("Tip: {}", tip);
let missing_blocks = find_missing_blocks(&blocks_db, 1, tip, ctx);
Expand All @@ -957,12 +928,8 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
return Err("Deletion aborted".to_string());
}

let (blocks_db_rw, inscriptions_db_conn_rw) = open_readwrite_ordhook_dbs(
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
&ctx,
)?;
let (blocks_db_rw, inscriptions_db_conn_rw) =

Check warning on line 931 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L931

Added line #L931 was not covered by tests
open_readwrite_ordhook_dbs(&config, &ctx)?;
let brc_20_db_conn_rw = brc20_new_rw_db_conn(&config, ctx);

delete_data_in_ordhook_db(
Expand Down
16 changes: 0 additions & 16 deletions components/ordhook-core/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::core::OrdhookConfig;
use chainhook_sdk::observer::EventObserverConfig;
use chainhook_sdk::types::{
BitcoinBlockSignaling, BitcoinNetwork, StacksNetwork, StacksNodeConfig,
Expand Down Expand Up @@ -118,21 +117,6 @@ impl Config {
}
}

pub fn get_ordhook_config(&self) -> OrdhookConfig {
OrdhookConfig {
resources: self.resources.clone(),
db_path: self.expected_cache_path(),
first_inscription_height: match self.network.bitcoin_network {
BitcoinNetwork::Mainnet => 767430,
BitcoinNetwork::Regtest => 1,
BitcoinNetwork::Testnet => 2413343,
BitcoinNetwork::Signet => 112402,
},
logs: self.logs.clone(),
meta_protocols: self.meta_protocols.clone(),
}
}

pub fn get_event_observer_config(&self) -> EventObserverConfig {
EventObserverConfig {
bitcoin_rpc_proxy_enabled: true,
Expand Down
39 changes: 13 additions & 26 deletions components/ordhook-core/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use dashmap::DashMap;
use fxhash::{FxBuildHasher, FxHasher};
use std::hash::BuildHasherDefault;
use std::ops::Div;
use std::path::PathBuf;

use chainhook_sdk::utils::Context;
use chainhook_sdk::{types::BitcoinNetwork, utils::Context};

use crate::{
config::{Config, LogConfig, MetaProtocolsConfig, ResourcesConfig},
config::Config,
db::{
blocks::{
find_last_block_inserted, find_pinned_block_bytes_at_block_height,
open_ordhook_db_conn_rocks_db_loop,
open_blocks_db_with_retry,
},
cursor::TransactionBytesCursor,
ordinals::{find_latest_inscription_block_height, open_readonly_ordhook_db_conn},
Expand All @@ -24,13 +23,13 @@ use crate::{
utils::bitcoind::bitcoind_get_block_height,
};

#[derive(Clone, Debug)]
pub struct OrdhookConfig {
pub resources: ResourcesConfig,
pub db_path: PathBuf,
pub first_inscription_height: u64,
pub logs: LogConfig,
pub meta_protocols: MetaProtocolsConfig,
pub fn first_inscription_height(config: &Config) -> u64 {
match config.network.bitcoin_network {
BitcoinNetwork::Mainnet => 767430,
BitcoinNetwork::Regtest => 1,
BitcoinNetwork::Testnet => 2413343,
BitcoinNetwork::Signet => 112402,

Check warning on line 31 in components/ordhook-core/src/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/core/mod.rs#L26-L31

Added lines #L26 - L31 were not covered by tests
}
}

pub fn new_traversals_cache(
Expand Down Expand Up @@ -116,13 +115,7 @@ pub fn compute_next_satpoint_data(
}

pub fn should_sync_rocks_db(config: &Config, ctx: &Context) -> Result<Option<(u64, u64)>, String> {
let blocks_db = open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
&ctx,
);
let blocks_db = open_blocks_db_with_retry(true, &config, &ctx);

Check warning on line 118 in components/ordhook-core/src/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/core/mod.rs#L118

Added line #L118 was not covered by tests
let inscriptions_db_conn = open_readonly_ordhook_db_conn(&config.expected_cache_path(), &ctx)?;
let last_compressed_block = find_last_block_inserted(&blocks_db) as u64;
let last_indexed_block = match find_latest_inscription_block_height(&inscriptions_db_conn, ctx)?
Expand All @@ -143,13 +136,7 @@ pub fn should_sync_ordhook_db(
config: &Config,
ctx: &Context,
) -> Result<Option<(u64, u64, usize)>, String> {
let blocks_db = open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
&ctx,
);
let blocks_db = open_blocks_db_with_retry(true, &config, &ctx);

Check warning on line 139 in components/ordhook-core/src/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/core/mod.rs#L139

Added line #L139 was not covered by tests
let mut start_block = find_last_block_inserted(&blocks_db) as u64;

if start_block == 0 {
Expand All @@ -169,7 +156,7 @@ pub fn should_sync_ordhook_db(
start_block += 1;
}
None => {
start_block = start_block.min(config.get_ordhook_config().first_inscription_height);
start_block = start_block.min(first_inscription_height(config));

Check warning on line 159 in components/ordhook-core/src/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/core/mod.rs#L159

Added line #L159 was not covered by tests
}
};

Expand Down
6 changes: 2 additions & 4 deletions components/ordhook-core/src/core/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub async fn download_and_pipeline_blocks(
bitcoin_block_signaling: config.network.bitcoin_block_signaling.clone(),
};

let ordhook_config = config.get_ordhook_config();

let number_of_blocks_to_process = blocks.len() as u64;

let (block_compressed_tx, block_compressed_rx) = crossbeam_channel::bounded(speed);
Expand All @@ -83,13 +81,13 @@ pub async fn download_and_pipeline_blocks(
// - 1 thread for the thread handling networking
// - 1 thread for the thread handling disk serialization
let thread_pool_network_response_processing_capacity =
ordhook_config.resources.get_optimal_thread_pool_capacity();
config.resources.get_optimal_thread_pool_capacity();
// For each worker in that pool, we want to bound the size of the queue to avoid OOM
// Blocks size can range from 1 to 4Mb (when packed with witness data).
// Start blocking networking when each worker has a backlog of 8 blocks seems reasonable.
let worker_queue_size = 2;

for _ in 0..ordhook_config.resources.bitcoind_rpc_threads {
for _ in 0..config.resources.bitcoind_rpc_threads {

Check warning on line 90 in components/ordhook-core/src/core/pipeline/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/core/pipeline/mod.rs#L90

Added line #L90 was not covered by tests
if let Some(block_height) = block_heights.pop_front() {
let config = moved_config.clone();
let ctx = moved_ctx.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use crate::{
config::Config,
core::pipeline::{PostProcessorCommand, PostProcessorController, PostProcessorEvent},
db::blocks::{insert_entry_in_blocks, open_ordhook_db_conn_rocks_db_loop},
db::blocks::{insert_entry_in_blocks, open_blocks_db_with_retry},
try_error, try_info,
};

Expand All @@ -26,13 +26,7 @@ pub fn start_block_archiving_processor(
let ctx = ctx.clone();
let handle: JoinHandle<()> = hiro_system_kit::thread_named("Processor Runloop")
.spawn(move || {
let blocks_db_rw = open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
config.resources.ulimit,
config.resources.memory_available,
&ctx,
);
let blocks_db_rw = open_blocks_db_with_retry(true, &config, &ctx);

Check warning on line 29 in components/ordhook-core/src/core/pipeline/processors/block_archiving.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/core/pipeline/processors/block_archiving.rs#L29

Added line #L29 was not covered by tests
let mut processed_blocks = 0;

loop {
Expand Down
Loading

0 comments on commit 5f42235

Please sign in to comment.