diff --git a/components/ordhook-cli/src/cli/mod.rs b/components/ordhook-cli/src/cli/mod.rs index 4df6c9be..2868ad5e 100644 --- a/components/ordhook-cli/src/cli/mod.rs +++ b/components/ordhook-cli/src/cli/mod.rs @@ -16,9 +16,7 @@ use ordhook::chainhook_sdk::types::{BitcoinBlockData, TransactionIdentifier}; use ordhook::chainhook_sdk::utils::BlockHeights; use ordhook::chainhook_sdk::utils::Context; use ordhook::config::Config; -use ordhook::core::meta_protocols::brc20::db::{ - brc20_new_rw_db_conn, get_brc20_operations_on_block, -}; +use ordhook::core::meta_protocols::brc20::db::get_brc20_operations_on_block; 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; @@ -32,17 +30,16 @@ use ordhook::db::blocks::{ use ordhook::db::cursor::BlockBytesCursor; use ordhook::db::ordinals::{ find_all_inscriptions_in_block, find_all_transfers_in_block, find_inscription_with_id, - find_latest_inscription_block_height, get_default_ordhook_db_file_path, - open_readonly_ordhook_db_conn, + find_latest_inscription_block_height, get_default_ordinals_db_file_path, open_ordinals_db, }; -use ordhook::db::{delete_data_in_ordhook_db, open_readwrite_ordhook_dbs}; +use ordhook::db::{drop_block_data_from_all_dbs, initialize_sqlite_dbs, open_all_dbs_rw}; use ordhook::download::download_archive_datasets_if_required; use ordhook::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate; use ordhook::service::observers::initialize_observers_db; use ordhook::service::{start_observer_forwarding, Service}; use ordhook::utils::bitcoind::bitcoind_get_block_height; use ordhook::utils::monitoring::PrometheusMonitoring; -use ordhook::{hex, initialize_databases, try_error, try_info, try_warn}; +use ordhook::{hex, try_error, try_info, try_warn}; use reqwest::Client as HttpClient; use std::collections::HashSet; use std::io::{BufReader, Read}; @@ -598,12 +595,15 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> { let mut total_inscriptions = 0; let mut total_transfers = 0; - let db_connections = initialize_databases(&config, ctx); + let db_connections = initialize_sqlite_dbs(&config, ctx); while let Some(block_height) = block_range.pop_front() { - let inscriptions = - find_all_inscriptions_in_block(&block_height, &db_connections.ordhook, ctx); + let inscriptions = find_all_inscriptions_in_block( + &block_height, + &db_connections.ordinals, + ctx, + ); let locations = - find_all_transfers_in_block(&block_height, &db_connections.ordhook, ctx); + find_all_transfers_in_block(&block_height, &db_connections.ordinals, ctx); let mut total_transfers_in_block = 0; @@ -657,7 +657,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> { } if total_transfers == 0 && total_inscriptions == 0 { let db_file_path = - get_default_ordhook_db_file_path(&config.expected_cache_path()); + get_default_ordinals_db_file_path(&config.expected_cache_path()); try_warn!(ctx, "No data available. Check the validity of the range being scanned and the validity of your local database {}", db_file_path.display()); } } @@ -673,8 +673,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> { let _ = download_archive_datasets_if_required(&config, ctx).await; - let inscriptions_db_conn = - open_readonly_ordhook_db_conn(&config.expected_cache_path(), ctx)?; + let inscriptions_db_conn = open_ordinals_db(&config.expected_cache_path(), ctx)?; let (inscription, block_height) = match find_inscription_with_id(&cmd.inscription_id, &inscriptions_db_conn, ctx)? { Some(entry) => entry, @@ -742,10 +741,10 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> { &cmd.config_path, &None, )?; - let db_connections = initialize_databases(&config, ctx); + let db_connections = initialize_sqlite_dbs(&config, ctx); let last_known_block = - find_latest_inscription_block_height(&db_connections.ordhook, ctx)?; + find_latest_inscription_block_height(&db_connections.ordinals, ctx)?; if last_known_block.is_none() { open_blocks_db_with_retry(true, &config, ctx); } @@ -808,12 +807,12 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> { Command::Db(OrdhookDbCommand::New(cmd)) => { let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?; // Create DB - initialize_databases(&config, ctx); + initialize_sqlite_dbs(&config, ctx); open_blocks_db_with_retry(true, &config, ctx); } Command::Db(OrdhookDbCommand::Sync(cmd)) => { let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?; - initialize_databases(&config, ctx); + initialize_sqlite_dbs(&config, ctx); let service = Service::new(config, ctx.clone()); service.update_state(None).await?; } @@ -928,16 +927,13 @@ 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, &ctx)?; - let brc_20_db_conn_rw = brc20_new_rw_db_conn(&config, ctx); + let (blocks_db_rw, sqlite_dbs_rw) = open_all_dbs_rw(&config, &ctx)?; - delete_data_in_ordhook_db( + drop_block_data_from_all_dbs( cmd.start_block, cmd.end_block, - &inscriptions_db_conn_rw, &blocks_db_rw, - &brc_20_db_conn_rw, + &sqlite_dbs_rw, ctx, )?; info!( diff --git a/components/ordhook-core/src/core/mod.rs b/components/ordhook-core/src/core/mod.rs index 5963bc57..4b80ac07 100644 --- a/components/ordhook-core/src/core/mod.rs +++ b/components/ordhook-core/src/core/mod.rs @@ -17,9 +17,9 @@ use crate::{ open_blocks_db_with_retry, }, cursor::TransactionBytesCursor, - ordinals::{find_latest_inscription_block_height, open_readonly_ordhook_db_conn}, + initialize_sqlite_dbs, + ordinals::{find_latest_inscription_block_height, open_ordinals_db}, }, - initialize_databases, utils::bitcoind::bitcoind_get_block_height, }; @@ -116,7 +116,7 @@ pub fn compute_next_satpoint_data( pub fn should_sync_rocks_db(config: &Config, ctx: &Context) -> Result, String> { let blocks_db = open_blocks_db_with_retry(true, &config, &ctx); - let inscriptions_db_conn = open_readonly_ordhook_db_conn(&config.expected_cache_path(), &ctx)?; + let inscriptions_db_conn = open_ordinals_db(&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)? { @@ -140,10 +140,10 @@ pub fn should_sync_ordhook_db( let mut start_block = find_last_block_inserted(&blocks_db) as u64; if start_block == 0 { - let _ = initialize_databases(config, ctx); + let _ = initialize_sqlite_dbs(config, ctx); } - let inscriptions_db_conn = open_readonly_ordhook_db_conn(&config.expected_cache_path(), &ctx)?; + let inscriptions_db_conn = open_ordinals_db(&config.expected_cache_path(), &ctx)?; match find_latest_inscription_block_height(&inscriptions_db_conn, ctx)? { Some(height) => { diff --git a/components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs b/components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs index 16be0b4d..e17a4bdb 100644 --- a/components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs +++ b/components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs @@ -43,7 +43,7 @@ use crate::{ cursor::TransactionBytesCursor, ordinals::{ get_any_entry_in_ordinal_activities, get_latest_indexed_inscription_number, - open_readonly_ordhook_db_conn, open_readwrite_ordhook_db_conn, + open_ordinals_db, open_ordinals_db_rw, }, }, service::write_brc20_block_operations, @@ -78,11 +78,11 @@ pub fn start_inscription_indexing_processor( let mut garbage_collect_nth_block = 0; let mut inscriptions_db_conn_rw = - open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx).unwrap(); + open_ordinals_db_rw(&config.expected_cache_path(), &ctx).unwrap(); let mut empty_cycles = 0; let inscriptions_db_conn = - open_readonly_ordhook_db_conn(&config.expected_cache_path(), &ctx).unwrap(); + open_ordinals_db(&config.expected_cache_path(), &ctx).unwrap(); let mut sequence_cursor = SequenceCursor::new(&inscriptions_db_conn); let mut brc20_cache = brc20_new_cache(&config); @@ -154,8 +154,7 @@ pub fn start_inscription_indexing_processor( // Recreate sqlite db connection on a regular basis inscriptions_db_conn_rw = - open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx) - .unwrap(); + open_ordinals_db_rw(&config.expected_cache_path(), &ctx).unwrap(); inscriptions_db_conn_rw.flush_prepared_statement_cache(); garbage_collect_nth_block = 0; } @@ -353,8 +352,10 @@ mod test { meta_protocols::brc20::cache::brc20_new_cache, new_traversals_lazy_cache, protocol::inscription_sequencing::SequenceCursor, }, - db::{blocks::open_blocks_db_with_retry, ordinals::open_readwrite_ordhook_db_conn}, - drop_databases, initialize_databases, + db::{ + blocks::open_blocks_db_with_retry, drop_sqlite_dbs, initialize_sqlite_dbs, + ordinals::open_ordinals_db_rw, + }, utils::{ monitoring::PrometheusMonitoring, test_helpers::{new_test_block, new_test_reveal_tx}, @@ -369,15 +370,17 @@ mod test { let config = Config::test_default(); // Create DBs - drop_databases(&config); - let db_conns = initialize_databases(&config, &ctx); + drop_sqlite_dbs(&config); + let db_conns = initialize_sqlite_dbs(&config, &ctx); let _ = open_blocks_db_with_retry(true, &config, &ctx); + // Insert block into rocksdb + let mut next_blocks = vec![new_test_block(vec![new_test_reveal_tx()])]; - let mut sequence_cursor = SequenceCursor::new(&db_conns.ordhook); + let mut sequence_cursor = SequenceCursor::new(&db_conns.ordinals); let cache_l2 = Arc::new(new_traversals_lazy_cache(2048)); let mut inscriptions_db_conn_rw = - open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx).expect(""); + open_ordinals_db_rw(&config.expected_cache_path(), &ctx).expect(""); let results = process_blocks( &mut next_blocks, @@ -416,13 +419,13 @@ mod test { let mut config = Config::mainnet_default(); config.storage.working_dir = "tmp".to_string(); config.meta_protocols.brc20 = true; - drop_databases(&config); - let mut db_conns = initialize_databases(&config, &ctx); + drop_sqlite_dbs(&config); + let mut db_conns = initialize_sqlite_dbs(&config, &ctx); let mut next_blocks = vec![new_test_block(vec![new_test_reveal_tx()])]; - let mut sequence_cursor = SequenceCursor::new(&db_conns.ordhook); + let mut sequence_cursor = SequenceCursor::new(&db_conns.ordinals); let cache_l2 = Arc::new(new_traversals_lazy_cache(2048)); let mut inscriptions_db_conn_rw = - open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx).expect(""); + open_ordinals_db_rw(&config.expected_cache_path(), &ctx).expect(""); let mut brc20_cache = brc20_new_cache(&config); let _ = process_blocks( diff --git a/components/ordhook-core/src/core/pipeline/processors/transfers_recomputing.rs b/components/ordhook-core/src/core/pipeline/processors/transfers_recomputing.rs index c89dbc6d..06a1aab2 100644 --- a/components/ordhook-core/src/core/pipeline/processors/transfers_recomputing.rs +++ b/components/ordhook-core/src/core/pipeline/processors/transfers_recomputing.rs @@ -16,7 +16,7 @@ use crate::{ }, }, db::ordinals::{ - insert_entries_from_block_in_inscriptions, open_readwrite_ordhook_db_conn, + insert_entries_from_block_in_inscriptions, open_ordinals_db_rw, remove_entries_from_locations_at_block_height, }, try_info, try_warn, @@ -35,7 +35,7 @@ pub fn start_transfers_recomputing_processor( let handle: JoinHandle<()> = hiro_system_kit::thread_named("Inscription indexing runloop") .spawn(move || { let mut inscriptions_db_conn_rw = - open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx).unwrap(); + open_ordinals_db_rw(&config.expected_cache_path(), &ctx).unwrap(); let mut empty_cycles = 0; loop { diff --git a/components/ordhook-core/src/core/protocol/inscription_sequencing.rs b/components/ordhook-core/src/core/protocol/inscription_sequencing.rs index 840ccfe5..b51dd028 100644 --- a/components/ordhook-core/src/core/protocol/inscription_sequencing.rs +++ b/components/ordhook-core/src/core/protocol/inscription_sequencing.rs @@ -980,8 +980,10 @@ mod test { use crate::{ config::Config, core::protocol::inscription_sequencing::SequenceCursor, - db::ordinals::update_sequence_metadata_with_block, - drop_databases, initialize_databases, + db::{ + drop_sqlite_dbs, initialize_sqlite_dbs, + ordinals::update_sequence_metadata_with_block, + }, utils::test_helpers::{new_test_block, new_test_reveal_tx_with_operation}, }; @@ -992,14 +994,14 @@ mod test { fn picks_next((block_height, cursed): (u64, bool)) -> (i64, i64) { let ctx = Context::empty(); let config = Config::test_default(); - drop_databases(&config); - let db_conns = initialize_databases(&config, &ctx); + drop_sqlite_dbs(&config); + let db_conns = initialize_sqlite_dbs(&config, &ctx); let mut block = new_test_block(vec![new_test_reveal_tx_with_operation()]); block.block_identifier.index = block_height; // Pick next twice so we can test all cases. - update_sequence_metadata_with_block(&block, &db_conns.ordhook, &ctx); - let mut cursor = SequenceCursor::new(&db_conns.ordhook); + update_sequence_metadata_with_block(&block, &db_conns.ordinals, &ctx); + let mut cursor = SequenceCursor::new(&db_conns.ordinals); let _ = cursor.pick_next( cursed, block.block_identifier.index + 1, @@ -1009,7 +1011,7 @@ mod test { cursor.increment(cursed, &ctx); block.block_identifier.index = block.block_identifier.index + 1; - update_sequence_metadata_with_block(&block, &db_conns.ordhook, &ctx); + update_sequence_metadata_with_block(&block, &db_conns.ordinals, &ctx); let next = cursor.pick_next( cursed, block.block_identifier.index + 1, @@ -1024,11 +1026,11 @@ mod test { fn resets_on_previous_block() { let ctx = Context::empty(); let config = Config::test_default(); - drop_databases(&config); - let db_conns = initialize_databases(&config, &ctx); + drop_sqlite_dbs(&config); + let db_conns = initialize_sqlite_dbs(&config, &ctx); let block = new_test_block(vec![new_test_reveal_tx_with_operation()]); - update_sequence_metadata_with_block(&block, &db_conns.ordhook, &ctx); - let mut cursor = SequenceCursor::new(&db_conns.ordhook); + update_sequence_metadata_with_block(&block, &db_conns.ordinals, &ctx); + let mut cursor = SequenceCursor::new(&db_conns.ordinals); let _ = cursor.pick_next( false, block.block_identifier.index + 1, diff --git a/components/ordhook-core/src/db/mod.rs b/components/ordhook-core/src/db/mod.rs index df6b7cf0..ec988d61 100644 --- a/components/ordhook-core/src/db/mod.rs +++ b/components/ordhook-core/src/db/mod.rs @@ -4,31 +4,63 @@ pub mod ordinals; use blocks::{delete_blocks_in_block_range, open_blocks_db_with_retry}; -use ordinals::{delete_inscriptions_in_block_range, open_readwrite_ordhook_db_conn}; +use ordinals::{delete_inscriptions_in_block_range, initialize_ordinals_db, open_ordinals_db_rw}; use rocksdb::DB; use rusqlite::Connection; use chainhook_sdk::utils::Context; use crate::{ - config::Config, core::meta_protocols::brc20::db::delete_activity_in_block_range, try_info, + config::Config, + core::meta_protocols::brc20::db::{ + brc20_new_rw_db_conn, delete_activity_in_block_range, initialize_brc20_db, + }, + try_info, }; -pub fn open_readwrite_ordhook_dbs( +pub struct SqliteDbConnections { + pub ordinals: Connection, + pub brc20: Option, +} + +/// Opens and initializes all SQLite databases required for Ordhook operation, depending if they are requested by the current +/// `Config`. Returns a struct with all the open connections. +pub fn initialize_sqlite_dbs(config: &Config, ctx: &Context) -> SqliteDbConnections { + SqliteDbConnections { + ordinals: initialize_ordinals_db(&config.expected_cache_path(), ctx), + brc20: match config.meta_protocols.brc20 { + true => Some(initialize_brc20_db( + Some(&config.expected_cache_path()), + ctx, + )), + false => None, + }, + } +} + +/// Opens all DBs required for Ordhook operation (read/write), including blocks DB. +pub fn open_all_dbs_rw( config: &Config, ctx: &Context, -) -> Result<(DB, Connection), String> { - let blocks_db = open_blocks_db_with_retry(true, &config, &ctx); - let inscriptions_db = open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx)?; - Ok((blocks_db, inscriptions_db)) +) -> Result<(DB, SqliteDbConnections), String> { + let blocks_db = open_blocks_db_with_retry(true, &config, ctx); + let inscriptions_db = open_ordinals_db_rw(&config.expected_cache_path(), ctx)?; + let brc20_db = brc20_new_rw_db_conn(config, ctx); + Ok(( + blocks_db, + SqliteDbConnections { + ordinals: inscriptions_db, + brc20: brc20_db, + }, + )) } -pub fn delete_data_in_ordhook_db( +/// Deletes all block data from all databases within the specified block range. +pub fn drop_block_data_from_all_dbs( start_block: u64, end_block: u64, - inscriptions_db_conn_rw: &Connection, blocks_db_rw: &DB, - brc_20_db_conn_rw: &Option, + sqlite_dbs_rw: &SqliteDbConnections, ctx: &Context, ) -> Result<(), String> { try_info!( @@ -43,10 +75,10 @@ pub fn delete_data_in_ordhook_db( delete_inscriptions_in_block_range( start_block as u32, end_block as u32, - &inscriptions_db_conn_rw, + &sqlite_dbs_rw.ordinals, &ctx, ); - if let Some(conn) = brc_20_db_conn_rw { + if let Some(conn) = &sqlite_dbs_rw.brc20 { delete_activity_in_block_range(start_block as u32, end_block as u32, &conn, &ctx); try_info!( ctx, @@ -55,3 +87,22 @@ pub fn delete_data_in_ordhook_db( } Ok(()) } + +#[cfg(test)] +/// Drops SQLite DB files in a test environment. +pub fn drop_sqlite_dbs(config: &Config) { + let Ok(dir) = std::fs::read_dir(&config.expected_cache_path()) else { + return; + }; + for entry in dir { + let entry = entry.unwrap(); + let path = entry.path(); + if path.is_file() { + if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) { + if extension.starts_with("sqlite") { + let _ = std::fs::remove_file(&path); + } + } + } + } +} diff --git a/components/ordhook-core/src/db/ordinals.rs b/components/ordhook-core/src/db/ordinals.rs index 6a3b29a3..d604ecb5 100644 --- a/components/ordhook-core/src/db/ordinals.rs +++ b/components/ordhook-core/src/db/ordinals.rs @@ -27,32 +27,26 @@ use crate::{ }, }; -pub fn get_default_ordhook_db_file_path(base_dir: &PathBuf) -> PathBuf { +pub fn get_default_ordinals_db_file_path(base_dir: &PathBuf) -> PathBuf { let mut destination_path = base_dir.clone(); destination_path.push("hord.sqlite"); destination_path } -pub fn open_readonly_ordhook_db_conn( - base_dir: &PathBuf, - ctx: &Context, -) -> Result { - let path = get_default_ordhook_db_file_path(&base_dir); +pub fn open_ordinals_db(base_dir: &PathBuf, ctx: &Context) -> Result { + let path = get_default_ordinals_db_file_path(&base_dir); let conn = open_existing_readonly_db(&path, ctx); Ok(conn) } -pub fn open_readwrite_ordhook_db_conn( - base_dir: &PathBuf, - ctx: &Context, -) -> Result { - let db_path = get_default_ordhook_db_file_path(&base_dir); +pub fn open_ordinals_db_rw(base_dir: &PathBuf, ctx: &Context) -> Result { + let db_path = get_default_ordinals_db_file_path(&base_dir); let conn = create_or_open_readwrite_db(Some(&db_path), ctx); Ok(conn) } -pub fn initialize_ordhook_db(base_dir: &PathBuf, ctx: &Context) -> Connection { - let db_path = get_default_ordhook_db_file_path(&base_dir); +pub fn initialize_ordinals_db(base_dir: &PathBuf, ctx: &Context) -> Connection { + let db_path = get_default_ordinals_db_file_path(&base_dir); let conn = create_or_open_readwrite_db(Some(&db_path), ctx); // TODO: introduce initial output if let Err(e) = conn.execute( diff --git a/components/ordhook-core/src/lib.rs b/components/ordhook-core/src/lib.rs index 5b2a6f2c..18211f3f 100644 --- a/components/ordhook-core/src/lib.rs +++ b/components/ordhook-core/src/lib.rs @@ -23,49 +23,3 @@ pub mod ord; pub mod scan; pub mod service; pub mod utils; - -use core::meta_protocols::brc20::db::initialize_brc20_db; - -use chainhook_sdk::utils::Context; -use config::Config; -use db::ordinals::initialize_ordhook_db; -use rusqlite::Connection; - -pub struct DbConnections { - pub ordhook: Connection, - pub brc20: Option, -} - -#[cfg(test)] -/// Drops DB files in a test environment. -pub fn drop_databases(config: &Config) { - let Ok(dir) = std::fs::read_dir(&config.expected_cache_path()) else { - return; - }; - for entry in dir { - let entry = entry.unwrap(); - let path = entry.path(); - if path.is_file() { - if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) { - if extension.starts_with("sqlite") { - let _ = std::fs::remove_file(&path); - } - } - } - } -} - -/// Initializes all SQLite databases required for Ordhook operation, depending if they are requested by the current `Config`. -/// Returns a struct with all the open connections. -pub fn initialize_databases(config: &Config, ctx: &Context) -> DbConnections { - DbConnections { - ordhook: initialize_ordhook_db(&config.expected_cache_path(), ctx), - brc20: match config.meta_protocols.brc20 { - true => Some(initialize_brc20_db( - Some(&config.expected_cache_path()), - ctx, - )), - false => None, - }, - } -} diff --git a/components/ordhook-core/src/scan/bitcoin.rs b/components/ordhook-core/src/scan/bitcoin.rs index 5c9c901a..25566f99 100644 --- a/components/ordhook-core/src/scan/bitcoin.rs +++ b/components/ordhook-core/src/scan/bitcoin.rs @@ -4,9 +4,9 @@ use crate::core::protocol::inscription_parsing::{ parse_inscriptions_and_standardize_block, }; use crate::core::protocol::inscription_sequencing::consolidate_block_with_pre_computed_ordinals_data; +use crate::db::initialize_sqlite_dbs; use crate::db::ordinals::get_any_entry_in_ordinal_activities; use crate::download::download_archive_datasets_if_required; -use crate::initialize_databases; use crate::service::observers::{ open_readwrite_observers_db_conn_or_panic, update_observer_progress, }; @@ -77,8 +77,8 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate( while let Some(current_block_height) = block_heights_to_scan.pop_front() { // Open DB connections - let db_connections = initialize_databases(&config, ctx); - let mut inscriptions_db_conn = db_connections.ordhook; + let db_connections = initialize_sqlite_dbs(&config, ctx); + let mut inscriptions_db_conn = db_connections.ordinals; let brc20_db_conn = match predicate_spec.predicate { // Even if we have a valid BRC-20 DB connection, check if the predicate we're evaluating requires us to do the work. BitcoinPredicateType::OrdinalsProtocol(OrdinalOperations::InscriptionFeed( diff --git a/components/ordhook-core/src/service/http_api.rs b/components/ordhook-core/src/service/http_api.rs index 44427a55..a83e5013 100644 --- a/components/ordhook-core/src/service/http_api.rs +++ b/components/ordhook-core/src/service/http_api.rs @@ -11,7 +11,7 @@ use chainhook_sdk::{ utils::Context, }; use rocket::{ - config::{self, Config, LogLevel}, + config::{self, Config as RocketConfig, LogLevel}, Ignite, Rocket, Shutdown, }; use rocket::{ @@ -22,7 +22,7 @@ use rocket::{ use rocket::{response::status::Custom, State}; use crate::{ - config::PredicatesApi, + config::{Config, PredicatesApi}, service::observers::{ insert_entry_in_observers, open_readwrite_observers_db_conn, remove_entry_from_observers, update_observer_progress, update_observer_streaming_enabled, @@ -36,7 +36,7 @@ use super::observers::{ }; pub async fn start_observers_http_server( - config: &crate::Config, + config: &Config, observer_commands_tx: &std::sync::mpsc::Sender, observer_event_rx: crossbeam_channel::Receiver, bitcoin_scan_op_tx: crossbeam_channel::Sender, @@ -147,7 +147,7 @@ pub async fn start_observers_http_server( } async fn build_server( - config: &crate::Config, + config: &Config, observer_command_tx: &std::sync::mpsc::Sender, ctx: &Context, ) -> Rocket { @@ -166,7 +166,7 @@ async fn build_server( shutdown_config.ctrlc = false; shutdown_config.grace = 1; shutdown_config.mercy = 1; - let control_config = Config { + let control_config = RocketConfig { port: api_config.http_port, workers: 1, address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), @@ -175,7 +175,7 @@ async fn build_server( log_level: LogLevel::Off, cli_colors: false, shutdown: shutdown_config, - ..Config::default() + ..RocketConfig::default() }; let routes = routes![ handle_ping, @@ -208,7 +208,7 @@ fn handle_ping(ctx: &State) -> Json { #[get("/v1/observers", format = "application/json")] fn handle_get_predicates( - config: &State, + config: &State, ctx: &State, ) -> Result, Custom>> { try_info!(ctx, "Handling HTTP GET /v1/observers"); @@ -237,7 +237,7 @@ fn handle_get_predicates( #[post("/v1/observers", format = "application/json", data = "")] fn handle_create_predicate( predicate: Json, - config: &State, + config: &State, background_job_tx: &State>>>, ctx: &State, ) -> Result, Custom>> { @@ -309,7 +309,7 @@ fn handle_create_predicate( #[get("/v1/observers/", format = "application/json")] fn handle_get_predicate( predicate_uuid: String, - config: &State, + config: &State, ctx: &State, ) -> Result, Custom>> { try_info!(ctx, "Handling HTTP GET /v1/observers/{}", predicate_uuid); @@ -352,7 +352,7 @@ fn handle_get_predicate( #[delete("/v1/observers/", format = "application/json")] fn handle_delete_bitcoin_predicate( predicate_uuid: String, - config: &State, + config: &State, background_job_tx: &State>>>, ctx: &State, ) -> Result, Custom>> { diff --git a/components/ordhook-core/src/service/mod.rs b/components/ordhook-core/src/service/mod.rs index fee45b8b..57604e3f 100644 --- a/components/ordhook-core/src/service/mod.rs +++ b/components/ordhook-core/src/service/mod.rs @@ -5,9 +5,7 @@ mod runloops; use crate::config::{Config, PredicatesApi}; use crate::core::meta_protocols::brc20::brc20_activation_height; use crate::core::meta_protocols::brc20::cache::{brc20_new_cache, Brc20MemoryCache}; -use crate::core::meta_protocols::brc20::db::{ - brc20_new_rw_db_conn, write_augmented_block_to_brc20_db, -}; +use crate::core::meta_protocols::brc20::db::write_augmented_block_to_brc20_db; use crate::core::meta_protocols::brc20::parser::ParsedBrc20Operation; use crate::core::meta_protocols::brc20::verifier::{ verify_brc20_operation, verify_brc20_transfer, VerifiedBrc20Operation, @@ -30,11 +28,10 @@ use crate::db::blocks::{ }; use crate::db::cursor::{BlockBytesCursor, TransactionBytesCursor}; use crate::db::ordinals::{ - find_latest_inscription_block_height, get_latest_indexed_inscription_number, - open_readonly_ordhook_db_conn, update_ordinals_db_with_block, - update_sequence_metadata_with_block, + find_latest_inscription_block_height, get_latest_indexed_inscription_number, open_ordinals_db, + update_ordinals_db_with_block, update_sequence_metadata_with_block, }; -use crate::db::{delete_data_in_ordhook_db, open_readwrite_ordhook_dbs}; +use crate::db::{drop_block_data_from_all_dbs, open_all_dbs_rw}; use crate::scan::bitcoin::process_block_with_predicates; use crate::service::observers::create_and_consolidate_chainhook_config_with_predicates; use crate::service::runloops::start_bitcoin_scan_runloop; @@ -102,9 +99,8 @@ impl Service { )); }); } - let ordhook_db = - open_readonly_ordhook_db_conn(&self.config.expected_cache_path(), &self.ctx) - .expect("unable to retrieve ordhook db"); + let ordhook_db = open_ordinals_db(&self.config.expected_cache_path(), &self.ctx) + .expect("unable to retrieve ordhook db"); self.prometheus.initialize( 0, get_latest_indexed_inscription_number(&ordhook_db, &self.ctx).unwrap_or(0), @@ -418,11 +414,9 @@ impl Service { let (tip, missing_blocks) = { let blocks_db = open_blocks_db_with_retry(false, &self.config, &self.ctx); - let ordhook_db = open_readonly_ordhook_db_conn( - &self.config.expected_cache_path(), - &self.ctx, - ) - .expect("unable to retrieve ordhook db"); + let ordhook_db = + open_ordinals_db(&self.config.expected_cache_path(), &self.ctx) + .expect("unable to retrieve ordhook db"); let tip = find_latest_inscription_block_height(&ordhook_db, &self.ctx)?.unwrap() as u32; info!( @@ -555,14 +549,13 @@ impl Service { } fn chainhook_sidecar_mutate_ordhook_db(command: HandleBlock, config: &Config, ctx: &Context) { - let (blocks_db_rw, inscriptions_db_conn_rw) = match open_readwrite_ordhook_dbs(&config, &ctx) { + let (blocks_db_rw, sqlite_dbs_rw) = match open_all_dbs_rw(&config, &ctx) { Ok(dbs) => dbs, Err(e) => { try_error!(ctx, "Unable to open readwrite connection: {e}"); return; } }; - let brc20_db_conn_rw = brc20_new_rw_db_conn(config, ctx); match command { HandleBlock::UndoBlock(block) => { @@ -571,15 +564,13 @@ fn chainhook_sidecar_mutate_ordhook_db(command: HandleBlock, config: &Config, ct "Re-org handling: reverting changes in block #{}", block.block_identifier.index ); - let res = delete_data_in_ordhook_db( + let res = drop_block_data_from_all_dbs( block.block_identifier.index, block.block_identifier.index, - &inscriptions_db_conn_rw, &blocks_db_rw, - &brc20_db_conn_rw, + &sqlite_dbs_rw, ctx, ); - if let Err(e) = res { try_error!( ctx, @@ -612,11 +603,11 @@ fn chainhook_sidecar_mutate_ordhook_db(command: HandleBlock, config: &Config, ct try_error!(ctx, "{}", e.to_string()); } - update_ordinals_db_with_block(&block, &inscriptions_db_conn_rw, ctx); - update_sequence_metadata_with_block(&block, &inscriptions_db_conn_rw, &ctx); + update_ordinals_db_with_block(&block, &sqlite_dbs_rw.ordinals, ctx); + update_sequence_metadata_with_block(&block, &sqlite_dbs_rw.ordinals, &ctx); - if let Some(brc20_conn_rw) = brc20_db_conn_rw { - write_augmented_block_to_brc20_db(&block, &brc20_conn_rw, ctx); + if let Some(brc20_conn_rw) = &sqlite_dbs_rw.brc20 { + write_augmented_block_to_brc20_db(&block, brc20_conn_rw, ctx); } } } @@ -668,23 +659,20 @@ pub fn chainhook_sidecar_mutate_blocks( ) { let mut updated_blocks_ids = vec![]; - let (blocks_db_rw, mut inscriptions_db_conn_rw) = - match open_readwrite_ordhook_dbs(&config, &ctx) { - Ok(dbs) => dbs, - Err(e) => { - try_error!(ctx, "Unable to open readwrite connection: {e}"); - return; - } - }; - let mut brc20_db_conn_rw = brc20_new_rw_db_conn(config, ctx); + let (blocks_db_rw, mut sqlite_dbs_rw) = match open_all_dbs_rw(&config, &ctx) { + Ok(dbs) => dbs, + Err(e) => { + try_error!(ctx, "Unable to open readwrite connection: {e}"); + return; + } + }; for block_id_to_rollback in blocks_ids_to_rollback.iter() { - if let Err(e) = delete_data_in_ordhook_db( + if let Err(e) = drop_block_data_from_all_dbs( block_id_to_rollback.index, block_id_to_rollback.index, - &inscriptions_db_conn_rw, &blocks_db_rw, - &brc20_db_conn_rw, + &sqlite_dbs_rw, &ctx, ) { try_error!( @@ -695,8 +683,11 @@ pub fn chainhook_sidecar_mutate_blocks( } } - let brc20_db_tx = brc20_db_conn_rw.as_mut().map(|c| c.transaction().unwrap()); - let inscriptions_db_tx = inscriptions_db_conn_rw.transaction().unwrap(); + let brc20_db_tx = sqlite_dbs_rw + .brc20 + .as_mut() + .map(|c| c.transaction().unwrap()); + let inscriptions_db_tx = sqlite_dbs_rw.ordinals.transaction().unwrap(); for cache in blocks_to_mutate.iter_mut() { let block_bytes = match BlockBytesCursor::from_standardized_block(&cache.block) {