Merge pull request #443 from hirosystems/chore/consolidate-bitcoin-rpc-into-chainhook-sdk

consolidate bitcoin rpc tools into chainhook-sdk
This commit is contained in:
ASuciuX
2025-02-19 18:59:53 +02:00
committed by GitHub
9 changed files with 26 additions and 32 deletions

View File

@@ -9,8 +9,9 @@ use bitcoincore_rpc::jsonrpc::error::RpcError;
use bitcoincore_rpc_json::GetRawTransactionResultVoutScriptPubKey;
use chainhook_types::bitcoin::{OutPoint, TxIn, TxOut};
use chainhook_types::{
BitcoinBlockData, BitcoinBlockMetadata, BitcoinNetwork, BitcoinTransactionData,
BitcoinTransactionMetadata, BlockHeader, BlockIdentifier, TransactionIdentifier,
BitcoinBlockData, BitcoinBlockMetadata, BitcoinBlockSignaling, BitcoinNetwork,
BitcoinTransactionData,BitcoinTransactionMetadata, BlockHeader, BlockIdentifier,
TransactionIdentifier,
};
use hiro_system_kit::slog;
use reqwest::Client as HttpClient;

View File

@@ -39,6 +39,7 @@ pub struct IndexerConfig {
pub bitcoind_rpc_username: String,
pub bitcoind_rpc_password: String,
pub bitcoin_block_signaling: BitcoinBlockSignaling,
pub prometheus_monitoring_port: Option<u16>,
}
pub struct Indexer {

View File

@@ -1,19 +1,19 @@
use std::{thread::sleep, time::Duration};
use chainhook_sdk::{
bitcoincore_rpc::{Auth, Client, RpcApi},
utils::Context,
};
use bitcoincore_rpc::{Auth, Client, RpcApi};
use hiro_system_kit::slog;
use crate::utils::Context;
use crate::indexer::IndexerConfig;
use crate::{config::Config, try_error, try_info};
use crate::{try_error, try_info};
fn bitcoind_get_client(config: &Config, ctx: &Context) -> Client {
fn bitcoind_get_client(config: &IndexerConfig, ctx: &Context) -> Client {
loop {
let auth = Auth::UserPass(
config.network.bitcoind_rpc_username.clone(),
config.network.bitcoind_rpc_password.clone(),
config.bitcoind_rpc_username.clone(),
config.bitcoind_rpc_password.clone(),
);
match Client::new(&config.network.bitcoind_rpc_url, auth) {
match Client::new(&config.bitcoind_rpc_url, auth) {
Ok(con) => {
return con;
}
@@ -26,7 +26,7 @@ fn bitcoind_get_client(config: &Config, ctx: &Context) -> Client {
}
/// Retrieves the block height from bitcoind.
pub fn bitcoind_get_block_height(config: &Config, ctx: &Context) -> u64 {
pub fn bitcoind_get_block_height(config: &IndexerConfig, ctx: &Context) -> u64 {
let bitcoin_rpc = bitcoind_get_client(config, ctx);
loop {
match bitcoin_rpc.get_blockchain_info() {
@@ -46,7 +46,7 @@ pub fn bitcoind_get_block_height(config: &Config, ctx: &Context) -> u64 {
}
/// Checks if bitcoind is still synchronizing blocks and waits until it's finished if that is the case.
pub fn bitcoind_wait_for_chain_tip(config: &Config, ctx: &Context) {
pub fn bitcoind_wait_for_chain_tip(config: &IndexerConfig, ctx: &Context) {
let bitcoin_rpc = bitcoind_get_client(config, ctx);
let mut confirmations = 0;
loop {

View File

@@ -1,3 +1,5 @@
pub mod bitcoind;
use std::{
collections::{BTreeSet, VecDeque},
fs::{self, OpenOptions},

View File

@@ -1,6 +1,7 @@
use chainhook_types::{BitcoinBlockSignaling, BitcoinNetwork};
use chainhook_sdk::indexer::IndexerConfig;
use ordhook::config::{
Config, IndexerConfig, LogConfig, MetaProtocolsConfig, ResourcesConfig, SnapshotConfig,
Config, LogConfig, MetaProtocolsConfig, ResourcesConfig, SnapshotConfig,
SnapshotConfigDownloadUrls, StorageConfig, DEFAULT_BITCOIND_RPC_THREADS,
DEFAULT_BITCOIND_RPC_TIMEOUT, DEFAULT_BRC20_LRU_CACHE_SIZE, DEFAULT_MEMORY_AVAILABLE,
DEFAULT_ULIMIT,

View File

@@ -1,5 +1,5 @@
pub use chainhook_postgres::PgConnectionConfig;
use chainhook_sdk::observer::EventObserverConfig;
use chainhook_sdk::{indexer::IndexerConfig, observer::EventObserverConfig};
use chainhook_types::{BitcoinBlockSignaling, BitcoinNetwork};
use std::path::PathBuf;
@@ -66,16 +66,6 @@ pub struct UrlConfig {
pub file_url: String,
}
#[derive(Debug, Clone)]
pub struct IndexerConfig {
pub bitcoin_network: BitcoinNetwork,
pub bitcoind_rpc_url: String,
pub bitcoind_rpc_username: String,
pub bitcoind_rpc_password: String,
pub bitcoin_block_signaling: BitcoinBlockSignaling,
pub prometheus_monitoring_port: Option<u16>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct ResourcesConfig {
pub ulimit: usize,

View File

@@ -23,9 +23,9 @@ use crate::{
cursor::TransactionBytesCursor,
ordinals_pg,
},
service::PgConnectionPools,
utils::bitcoind::bitcoind_get_block_height,
service::PgConnectionPools
};
use chainhook_sdk::utils::bitcoind::bitcoind_get_block_height;
pub fn first_inscription_height(config: &Config) -> u64 {
match config.network.bitcoin_network {
@@ -164,7 +164,7 @@ pub async fn should_sync_ordinals_db(
};
// TODO: Gracefully handle Regtest, Testnet and Signet
let end_block = bitcoind_get_block_height(config, ctx);
let end_block = bitcoind_get_block_height(&config.network, ctx);
let (mut end_block, speed) = if start_block < 200_000 {
(end_block.min(200_000), 10_000)
} else if start_block < 550_000 {

View File

@@ -15,13 +15,13 @@ use crate::db::blocks::{
};
use crate::db::cursor::{BlockBytesCursor, TransactionBytesCursor};
use crate::db::ordinals_pg;
use crate::utils::bitcoind::bitcoind_wait_for_chain_tip;
use crate::utils::monitoring::{start_serving_prometheus_metrics, PrometheusMonitoring};
use crate::{try_crit, try_error, try_info};
use chainhook_postgres::{pg_begin, pg_pool, pg_pool_client};
use chainhook_sdk::observer::{
start_event_observer, BitcoinBlockDataCached, ObserverEvent, ObserverSidecar,
};
use chainhook_sdk::utils::bitcoind::bitcoind_wait_for_chain_tip;
use chainhook_sdk::utils::{BlockHeights, Context};
use chainhook_types::BlockIdentifier;
use crossbeam_channel::select;
@@ -224,7 +224,7 @@ impl Service {
}
pub async fn check_blocks_db_integrity(&mut self) -> Result<(), String> {
bitcoind_wait_for_chain_tip(&self.config, &self.ctx);
bitcoind_wait_for_chain_tip(&self.config.network, &self.ctx);
let (tip, missing_blocks) = {
let blocks_db = open_blocks_db_with_retry(false, &self.config, &self.ctx);
let ord_client = pg_pool_client(&self.pg_pools.ordinals).await?;
@@ -263,7 +263,7 @@ impl Service {
/// Synchronizes and indexes all databases until their block height matches bitcoind's block height.
pub async fn catch_up_to_bitcoin_chain_tip(&self) -> Result<(), String> {
// 0: Make sure bitcoind is synchronized.
bitcoind_wait_for_chain_tip(&self.config, &self.ctx);
bitcoind_wait_for_chain_tip(&self.config.network, &self.ctx);
// 1: Catch up blocks DB so it is at least at the same height as the ordinals DB.
if let Some((start_block, end_block)) =

View File

@@ -1,4 +1,3 @@
pub mod bitcoind;
pub mod logger;
pub mod monitoring;