mirror of
https://github.com/alexgo-io/bitcoin-indexer.git
synced 2026-06-14 08:29:31 +08:00
fix: build errors / merge snafu
This commit is contained in:
@@ -11,9 +11,10 @@ use crate::storage::{
|
||||
};
|
||||
|
||||
use chainhook_sdk::chainhooks::types::{
|
||||
BitcoinChainhookFullSpecification, BitcoinChainhookNetworkSpecification, BitcoinPredicateType, ChainhookFullSpecification, FileHook,
|
||||
HookAction, OrdinalOperations, StacksChainhookFullSpecification,
|
||||
StacksChainhookNetworkSpecification, StacksPredicate, StacksPrintEventBasedPredicate,
|
||||
BitcoinChainhookFullSpecification, BitcoinChainhookNetworkSpecification, BitcoinPredicateType,
|
||||
ChainhookFullSpecification, FileHook, HookAction, OrdinalOperations,
|
||||
StacksChainhookFullSpecification, StacksChainhookNetworkSpecification, StacksPredicate,
|
||||
StacksPrintEventBasedPredicate,
|
||||
};
|
||||
use chainhook_sdk::hord::db::{
|
||||
delete_data_in_hord_db, find_last_block_inserted, find_lazy_block_at_block_height,
|
||||
@@ -480,8 +481,8 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
|
||||
ConfigCommand::New(cmd) => {
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
let _config = Config::default(cmd.devnet, cmd.testnet, cmd.mainnet, &None)?;
|
||||
let config_content = generate_config();
|
||||
let config = Config::default(cmd.devnet, cmd.testnet, cmd.mainnet, &None)?;
|
||||
let config_content = generate_config(&config.network.bitcoin_network);
|
||||
let mut file_path = PathBuf::new();
|
||||
file_path.push("Chainhook.toml");
|
||||
let mut file = File::create(&file_path)
|
||||
|
||||
@@ -4,7 +4,7 @@ pub mod generator;
|
||||
use chainhook_sdk::hord::HordConfig;
|
||||
pub use chainhook_sdk::indexer::IndexerConfig;
|
||||
use chainhook_sdk::observer::EventObserverConfig;
|
||||
use chainhook_types::{BitcoinBlockSignaling, BitcoinNetwork, StacksNetwork};
|
||||
use chainhook_types::{BitcoinBlockSignaling, BitcoinNetwork, StacksNetwork, StacksNodeConfig};
|
||||
pub use file::ConfigFile;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
@@ -409,9 +409,10 @@ impl Config {
|
||||
bitcoind_rpc_url: "http://0.0.0.0:18443".into(),
|
||||
bitcoind_rpc_username: "devnet".into(),
|
||||
bitcoind_rpc_password: "devnet".into(),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(
|
||||
"http://0.0.0.0:20443".into(),
|
||||
),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(StacksNodeConfig {
|
||||
rpc_url: "http://localhost:20443".to_string(),
|
||||
ingestion_port: DEFAULT_INGESTION_PORT,
|
||||
}),
|
||||
stacks_network: StacksNetwork::Devnet,
|
||||
bitcoin_network: BitcoinNetwork::Regtest,
|
||||
},
|
||||
@@ -440,9 +441,10 @@ impl Config {
|
||||
bitcoind_rpc_url: "http://0.0.0.0:18332".into(),
|
||||
bitcoind_rpc_username: "devnet".into(),
|
||||
bitcoind_rpc_password: "devnet".into(),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(
|
||||
"http://0.0.0.0:20443".into(),
|
||||
),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(StacksNodeConfig {
|
||||
rpc_url: "http://localhost:20443".to_string(),
|
||||
ingestion_port: DEFAULT_INGESTION_PORT,
|
||||
}),
|
||||
stacks_network: StacksNetwork::Testnet,
|
||||
bitcoin_network: BitcoinNetwork::Testnet,
|
||||
},
|
||||
@@ -476,9 +478,10 @@ impl Config {
|
||||
bitcoind_rpc_url: "http://0.0.0.0:8332".into(),
|
||||
bitcoind_rpc_username: "devnet".into(),
|
||||
bitcoind_rpc_password: "devnet".into(),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(
|
||||
"http://0.0.0.0:20443".into(),
|
||||
),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(StacksNodeConfig {
|
||||
rpc_url: "http://localhost:20443".to_string(),
|
||||
ingestion_port: DEFAULT_INGESTION_PORT,
|
||||
}),
|
||||
stacks_network: StacksNetwork::Mainnet,
|
||||
bitcoin_network: BitcoinNetwork::Mainnet,
|
||||
},
|
||||
|
||||
@@ -4,9 +4,10 @@ use chainhook_sdk::{
|
||||
bitcoincore_rpc::{Auth, Client, RpcApi},
|
||||
hord::{
|
||||
db::{
|
||||
fetch_and_cache_blocks_in_hord_db, find_last_block_inserted, initialize_hord_db,
|
||||
fetch_and_cache_blocks_in_hord_db, find_last_block_inserted,
|
||||
find_latest_inscription_block_height, initialize_hord_db, open_readonly_hord_db_conn,
|
||||
open_readonly_hord_db_conn_rocks_db, open_readwrite_hord_db_conn,
|
||||
open_readwrite_hord_db_conn_rocks_db, find_latest_inscription_block_height, open_readonly_hord_db_conn,
|
||||
open_readwrite_hord_db_conn_rocks_db,
|
||||
},
|
||||
HordConfig,
|
||||
},
|
||||
@@ -30,14 +31,14 @@ pub fn should_sync_hord_db(config: &Config, ctx: &Context) -> Result<Option<(u64
|
||||
}
|
||||
};
|
||||
|
||||
let mut start_block = match open_readonly_hord_db_conn_rocks_db(&config.expected_cache_path(), &ctx)
|
||||
{
|
||||
Ok(blocks_db) => find_last_block_inserted(&blocks_db) as u64,
|
||||
Err(err) => {
|
||||
warn!(ctx.expect_logger(), "{}", err);
|
||||
0
|
||||
}
|
||||
};
|
||||
let mut start_block =
|
||||
match open_readonly_hord_db_conn_rocks_db(&config.expected_cache_path(), &ctx) {
|
||||
Ok(blocks_db) => find_last_block_inserted(&blocks_db) as u64,
|
||||
Err(err) => {
|
||||
warn!(ctx.expect_logger(), "{}", err);
|
||||
0
|
||||
}
|
||||
};
|
||||
|
||||
if start_block == 0 {
|
||||
let _ = initialize_hord_db(&config.expected_cache_path(), &ctx);
|
||||
@@ -54,7 +55,6 @@ pub fn should_sync_hord_db(config: &Config, ctx: &Context) -> Result<Option<(u64
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let end_block = match bitcoin_rpc.get_blockchain_info() {
|
||||
Ok(result) => result.blocks,
|
||||
Err(e) => {
|
||||
|
||||
@@ -6,7 +6,8 @@ use std::{
|
||||
};
|
||||
|
||||
use chainhook_types::{
|
||||
BitcoinBlockData, BlockIdentifier, OrdinalInscriptionRevealData, TransactionIdentifier, OrdinalInscriptionTransferData,
|
||||
BitcoinBlockData, BlockIdentifier, OrdinalInscriptionRevealData,
|
||||
OrdinalInscriptionTransferData, TransactionIdentifier,
|
||||
};
|
||||
use dashmap::DashMap;
|
||||
use fxhash::FxHasher;
|
||||
@@ -66,7 +67,13 @@ pub fn initialize_hord_db(path: &PathBuf, ctx: &Context) -> Connection {
|
||||
)",
|
||||
[],
|
||||
) {
|
||||
ctx.try_log(|logger| slog::warn!(logger, "Unable to create table inscriptions: {}", e.to_string()));
|
||||
ctx.try_log(|logger| {
|
||||
slog::warn!(
|
||||
logger,
|
||||
"Unable to create table inscriptions: {}",
|
||||
e.to_string()
|
||||
)
|
||||
});
|
||||
} else {
|
||||
if let Err(e) = conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS locations (
|
||||
@@ -78,7 +85,9 @@ pub fn initialize_hord_db(path: &PathBuf, ctx: &Context) -> Connection {
|
||||
)",
|
||||
[],
|
||||
) {
|
||||
ctx.try_log(|logger| slog::warn!(logger, "Unable to create table locations:{}", e.to_string()));
|
||||
ctx.try_log(|logger| {
|
||||
slog::warn!(logger, "Unable to create table locations:{}", e.to_string())
|
||||
});
|
||||
}
|
||||
|
||||
// Legacy table - to be removed
|
||||
@@ -88,7 +97,9 @@ pub fn initialize_hord_db(path: &PathBuf, ctx: &Context) -> Connection {
|
||||
)",
|
||||
[],
|
||||
) {
|
||||
ctx.try_log(|logger| slog::warn!(logger, "Unable to create table locations:{}", e.to_string()));
|
||||
ctx.try_log(|logger| {
|
||||
slog::warn!(logger, "Unable to create table locations:{}", e.to_string())
|
||||
});
|
||||
}
|
||||
|
||||
if let Err(e) = conn.execute(
|
||||
@@ -399,8 +410,7 @@ pub fn insert_transfer_in_locations(
|
||||
inscriptions_db_conn_rw: &Connection,
|
||||
ctx: &Context,
|
||||
) {
|
||||
let (tx, output_index, offset) =
|
||||
parse_satpoint_to_watch(&transfer_data.satpoint_post_transfer);
|
||||
let (tx, output_index, offset) = parse_satpoint_to_watch(&transfer_data.satpoint_post_transfer);
|
||||
let outpoint_to_watch = format_outpoint_to_watch(&tx, output_index);
|
||||
if let Err(e) = inscriptions_db_conn_rw.execute(
|
||||
"INSERT INTO locations (inscription_id, outpoint_to_watch, offset, block_height, tx_index) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
|
||||
@@ -41,8 +41,9 @@ use crate::{
|
||||
use self::db::{
|
||||
find_inscription_with_id, find_latest_cursed_inscription_number_at_block_height,
|
||||
find_latest_inscription_number_at_block_height, format_satpoint_to_watch,
|
||||
parse_satpoint_to_watch, remove_entry_from_blocks, remove_entry_from_inscriptions, LazyBlock,
|
||||
LazyBlockTransaction, TraversalResult, WatchedSatpoint, insert_transfer_in_locations, parse_outpoint_to_watch,
|
||||
insert_transfer_in_locations, parse_outpoint_to_watch, parse_satpoint_to_watch,
|
||||
remove_entry_from_blocks, remove_entry_from_inscriptions, LazyBlock, LazyBlockTransaction,
|
||||
TraversalResult, WatchedSatpoint,
|
||||
};
|
||||
use self::inscription::InscriptionParser;
|
||||
use self::ord::inscription_id::InscriptionId;
|
||||
@@ -651,7 +652,7 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
|
||||
|
||||
// For each satpoint inscribed retrieved, we need to compute the next
|
||||
// outpoint to watch
|
||||
for mut watched_satpoint in entries.into_iter() {
|
||||
for mut watched_satpoint in entries.into_iter() {
|
||||
let satpoint_pre_transfer =
|
||||
format!("{}:{}", outpoint_pre_transfer, watched_satpoint.offset);
|
||||
|
||||
@@ -659,9 +660,24 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
|
||||
// burnt or lost in fees and transfered to the miner?
|
||||
|
||||
let (_, input_index) = parse_outpoint_to_watch(&outpoint_pre_transfer);
|
||||
let inputs = new_tx.metadata.inputs.iter().map(|o| o.previous_output.value).collect::<_>();
|
||||
let outputs = new_tx.metadata.outputs.iter().map(|o| o.value).collect::<_>();
|
||||
let post_transfer_data = compute_next_satpoint_data(input_index, watched_satpoint.offset, &inputs, &outputs);
|
||||
let inputs = new_tx
|
||||
.metadata
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|o| o.previous_output.value)
|
||||
.collect::<_>();
|
||||
let outputs = new_tx
|
||||
.metadata
|
||||
.outputs
|
||||
.iter()
|
||||
.map(|o| o.value)
|
||||
.collect::<_>();
|
||||
let post_transfer_data = compute_next_satpoint_data(
|
||||
input_index,
|
||||
watched_satpoint.offset,
|
||||
&inputs,
|
||||
&outputs,
|
||||
);
|
||||
|
||||
let (
|
||||
outpoint_post_transfer,
|
||||
@@ -707,8 +723,7 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
|
||||
}
|
||||
SatPosition::Fee(offset) => {
|
||||
// Get Coinbase TX
|
||||
let offset =
|
||||
first_sat_post_subsidy + cumulated_fees + offset;
|
||||
let offset = first_sat_post_subsidy + cumulated_fees + offset;
|
||||
let outpoint = format_outpoint_to_watch(&coinbase_txid, 0);
|
||||
(outpoint, offset, None, None)
|
||||
}
|
||||
@@ -726,7 +741,6 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
|
||||
)
|
||||
});
|
||||
|
||||
|
||||
let satpoint_post_transfer =
|
||||
format!("{}:{}", outpoint_post_transfer, offset_post_transfer);
|
||||
|
||||
@@ -776,8 +790,12 @@ pub enum SatPosition {
|
||||
Fee(u64),
|
||||
}
|
||||
|
||||
|
||||
pub fn compute_next_satpoint_data(input_index: usize, offset_intra_input: u64, inputs: &Vec<u64>, outputs: &Vec<u64>) -> SatPosition {
|
||||
pub fn compute_next_satpoint_data(
|
||||
input_index: usize,
|
||||
offset_intra_input: u64,
|
||||
inputs: &Vec<u64>,
|
||||
outputs: &Vec<u64>,
|
||||
) -> SatPosition {
|
||||
let mut offset_cross_inputs = 0;
|
||||
for (index, input_value) in inputs.iter().enumerate() {
|
||||
if index == input_index {
|
||||
@@ -794,7 +812,7 @@ pub fn compute_next_satpoint_data(input_index: usize, offset_intra_input: u64, i
|
||||
for (index, output_value) in outputs.iter().enumerate() {
|
||||
floating_bound += output_value;
|
||||
output_index = index;
|
||||
if floating_bound > offset_cross_inputs {
|
||||
if floating_bound > offset_cross_inputs {
|
||||
break;
|
||||
}
|
||||
offset_intra_outputs += output_value;
|
||||
@@ -802,7 +820,7 @@ pub fn compute_next_satpoint_data(input_index: usize, offset_intra_input: u64, i
|
||||
|
||||
if output_index == (outputs.len() - 1) && offset_cross_inputs >= floating_bound {
|
||||
// Satoshi spent in fees
|
||||
return SatPosition::Fee(offset_cross_inputs-floating_bound);
|
||||
return SatPosition::Fee(offset_cross_inputs - floating_bound);
|
||||
}
|
||||
SatPosition::Output((output_index, (offset_cross_inputs - offset_intra_outputs)))
|
||||
}
|
||||
@@ -810,15 +828,38 @@ pub fn compute_next_satpoint_data(input_index: usize, offset_intra_input: u64, i
|
||||
#[cfg(test)]
|
||||
pub mod tests;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_identify_next_output_index_destination() {
|
||||
assert_eq!(compute_next_satpoint_data(0, 10, &vec![20, 30, 45], &vec![20, 30, 45]), SatPosition::Output((0, 10)));
|
||||
assert_eq!(compute_next_satpoint_data(0, 20, &vec![20, 30, 45], &vec![20, 30, 45]), SatPosition::Output((1, 0)));
|
||||
assert_eq!(compute_next_satpoint_data(1, 5, &vec![20, 30, 45], &vec![20, 30, 45]), SatPosition::Output((1, 5)));
|
||||
assert_eq!(compute_next_satpoint_data(1, 6, &vec![20, 30, 45], &vec![20, 5, 45]), SatPosition::Output((2, 1)));
|
||||
assert_eq!(compute_next_satpoint_data(1, 10, &vec![10, 10, 10], &vec![30]), SatPosition::Output((0, 20)));
|
||||
assert_eq!(compute_next_satpoint_data(0, 30, &vec![10, 10, 10], &vec![30]), SatPosition::Fee(0));
|
||||
assert_eq!(compute_next_satpoint_data(0, 0, &vec![10, 10, 10], &vec![30]), SatPosition::Output((0, 0)));
|
||||
assert_eq!(compute_next_satpoint_data(2, 45, &vec![20, 30, 45], &vec![20, 30, 45]), SatPosition::Fee(0));
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(0, 10, &vec![20, 30, 45], &vec![20, 30, 45]),
|
||||
SatPosition::Output((0, 10))
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(0, 20, &vec![20, 30, 45], &vec![20, 30, 45]),
|
||||
SatPosition::Output((1, 0))
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(1, 5, &vec![20, 30, 45], &vec![20, 30, 45]),
|
||||
SatPosition::Output((1, 5))
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(1, 6, &vec![20, 30, 45], &vec![20, 5, 45]),
|
||||
SatPosition::Output((2, 1))
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(1, 10, &vec![10, 10, 10], &vec![30]),
|
||||
SatPosition::Output((0, 20))
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(0, 30, &vec![10, 10, 10], &vec![30]),
|
||||
SatPosition::Fee(0)
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(0, 0, &vec![10, 10, 10], &vec![30]),
|
||||
SatPosition::Output((0, 0))
|
||||
);
|
||||
assert_eq!(
|
||||
compute_next_satpoint_data(2, 45, &vec![20, 30, 45], &vec![20, 30, 45]),
|
||||
SatPosition::Fee(0)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ use bitcoincore_rpc::{Auth, Client, RpcApi};
|
||||
use chainhook_types::{
|
||||
BitcoinBlockData, BitcoinBlockSignaling, BitcoinChainEvent, BitcoinChainUpdatedWithBlocksData,
|
||||
BitcoinChainUpdatedWithReorgData, BitcoinNetwork, BlockIdentifier, BlockchainEvent,
|
||||
StacksChainEvent, StacksNetwork, TransactionIdentifier,
|
||||
StacksChainEvent, StacksNetwork, StacksNodeConfig, TransactionIdentifier,
|
||||
};
|
||||
use hiro_system_kit;
|
||||
use hiro_system_kit::slog;
|
||||
@@ -216,7 +216,12 @@ impl EventObserverConfig {
|
||||
bitcoin_block_signaling: overrides
|
||||
.and_then(|c| match c.bitcoind_zmq_url.as_ref() {
|
||||
Some(url) => Some(BitcoinBlockSignaling::ZeroMQ(url.clone())),
|
||||
None => Some(BitcoinBlockSignaling::Stacks(stacks_node_rpc_url.clone())),
|
||||
None => Some(BitcoinBlockSignaling::Stacks(StacksNodeConfig {
|
||||
rpc_url: stacks_node_rpc_url.clone(),
|
||||
ingestion_port: overrides
|
||||
.and_then(|c| c.ingestion_port)
|
||||
.unwrap_or(DEFAULT_INGESTION_PORT),
|
||||
})),
|
||||
})
|
||||
.unwrap_or(BitcoinBlockSignaling::Stacks(StacksNodeConfig {
|
||||
rpc_url: stacks_node_rpc_url.clone(),
|
||||
|
||||
@@ -17,13 +17,14 @@ use crate::utils::{AbstractBlock, Context};
|
||||
use chainhook_types::{
|
||||
BitcoinBlockSignaling, BitcoinNetwork, BlockchainEvent, BlockchainUpdatedWithHeaders,
|
||||
StacksBlockUpdate, StacksChainEvent, StacksChainUpdatedWithBlocksData, StacksNetwork,
|
||||
StacksNodeConfig,
|
||||
};
|
||||
use hiro_system_kit;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use super::ObserverEvent;
|
||||
use super::{ObserverEvent, DEFAULT_INGESTION_PORT};
|
||||
|
||||
fn generate_test_config() -> (EventObserverConfig, ChainhookStore) {
|
||||
let config: EventObserverConfig = EventObserverConfig {
|
||||
@@ -35,7 +36,10 @@ fn generate_test_config() -> (EventObserverConfig, ChainhookStore) {
|
||||
bitcoind_rpc_password: "user".into(),
|
||||
bitcoind_rpc_url: "http://localhost:18443".into(),
|
||||
display_logs: false,
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks("http://localhost:20443".into()),
|
||||
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(StacksNodeConfig {
|
||||
rpc_url: "http://localhost:20443".to_string(),
|
||||
ingestion_port: DEFAULT_INGESTION_PORT,
|
||||
}),
|
||||
cache_path: "cache".into(),
|
||||
bitcoin_network: BitcoinNetwork::Regtest,
|
||||
stacks_network: StacksNetwork::Devnet,
|
||||
|
||||
@@ -885,10 +885,16 @@ impl BitcoinNetwork {
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub enum BitcoinBlockSignaling {
|
||||
Stacks(String),
|
||||
Stacks(StacksNodeConfig),
|
||||
ZeroMQ(String),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct StacksNodeConfig {
|
||||
pub rpc_url: String,
|
||||
pub ingestion_port: u16,
|
||||
}
|
||||
|
||||
impl BitcoinBlockSignaling {
|
||||
pub fn should_ignore_bitcoin_block_signaling_through_stacks(&self) -> bool {
|
||||
match &self {
|
||||
|
||||
Reference in New Issue
Block a user