Skip to content

Commit

Permalink
fix: include ordinals operations in standardized blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Jul 7, 2023
1 parent a8988ba commit a13351d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
16 changes: 7 additions & 9 deletions components/hord-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use crate::db::{
open_readwrite_hord_db_conn_rocks_db, retrieve_satoshi_point_using_lazy_storage,
};
use crate::hord::{
new_traversals_lazy_cache, retrieve_inscribed_satoshi_points_from_block,
self, new_traversals_lazy_cache, retrieve_inscribed_satoshi_points_from_block,
update_storage_and_augment_bitcoin_block_with_inscription_transfer_data, Storage,
};
use chainhook_sdk::chainhooks::types::ChainhookFullSpecification;
use chainhook_sdk::indexer;
use chainhook_sdk::indexer::bitcoin::{
download_and_parse_block_with_retry, retrieve_block_hash_with_retry,
};
Expand Down Expand Up @@ -355,7 +354,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
find_last_block_inserted(&hord_db_conn) as u64
};
if cmd.block_height > tip_height {
crate::hord::perform_hord_db_update(
hord::perform_hord_db_update(
tip_height,
cmd.block_height,
&config.get_hord_config(),
Expand Down Expand Up @@ -427,7 +426,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
let tip_height = find_last_block_inserted(&blocks_db_conn) as u64;
let _end_at = match cmd.block_height {
Some(block_height) if block_height > tip_height => {
crate::hord::perform_hord_db_update(
hord::perform_hord_db_update(
tip_height,
block_height,
&config.get_hord_config(),
Expand Down Expand Up @@ -481,8 +480,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
}
Command::Db(HordDbCommand::Sync(cmd)) => {
let config = Config::default(false, false, false, &cmd.config_path)?;
if let Some((start_block, end_block)) = crate::hord::should_sync_hord_db(&config, &ctx)?
{
if let Some((start_block, end_block)) = hord::should_sync_hord_db(&config, &ctx)? {
if start_block == 0 {
info!(
ctx.expect_logger(),
Expand All @@ -494,7 +492,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
"Resuming hord indexing from block #{}", start_block
);
}
crate::hord::perform_hord_db_update(
hord::perform_hord_db_update(
start_block,
end_block,
&config.get_hord_config(),
Expand Down Expand Up @@ -525,7 +523,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
)?;
}
// Update data
crate::hord::perform_hord_db_update(
hord::perform_hord_db_update(
cmd.start_block,
cmd.end_block,
&config.get_hord_config(),
Expand Down Expand Up @@ -605,6 +603,6 @@ pub async fn fetch_and_standardize_block(
let block_breakdown =
download_and_parse_block_with_retry(&block_hash, &bitcoin_config, &ctx).await?;

indexer::bitcoin::standardize_bitcoin_block(block_breakdown, &bitcoin_config.network, &ctx)
hord::parse_ordinals_and_standardize_block(block_breakdown, &bitcoin_config.network, &ctx)
.map_err(|(e, _)| e)
}
29 changes: 15 additions & 14 deletions components/hord-cli/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ use threadpool::ThreadPool;

use chainhook_sdk::{
indexer::bitcoin::{
download_block_with_retry, retrieve_block_hash_with_retry, standardize_bitcoin_block,
BitcoinBlockFullBreakdown,
download_block_with_retry, retrieve_block_hash_with_retry, BitcoinBlockFullBreakdown,
},
observer::BitcoinConfig,
utils::Context,
};

use crate::hord::HordConfig;
use crate::hord::{self, HordConfig};
use crate::hord::{new_traversals_lazy_cache, update_hord_db_and_augment_bitcoin_block};
use crate::ord::{height::Height, sat::Sat};

Expand Down Expand Up @@ -979,19 +978,21 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
inbox.len()
)
});
let mut new_block =
match standardize_bitcoin_block(next_block, &bitcoin_network, &ctx) {
Ok(block) => block,
Err((e, _)) => {
ctx.try_log(|logger| {
slog::error!(logger, "Unable to standardize bitcoin block: {e}",)
});
return Err(e);
}
};
let mut new_block = match hord::parse_ordinals_and_standardize_block(
next_block,
&bitcoin_network,
&ctx,
) {
Ok(block) => block,
Err((e, _)) => {
ctx.try_log(|logger| {
slog::error!(logger, "Unable to standardize bitcoin block: {e}",)
});
return Err(e);
}
};

let _ = blocks_db_rw.flush();

if let Err(e) = update_hord_db_and_augment_bitcoin_block(
&mut new_block,
blocks_db_rw,
Expand Down
33 changes: 29 additions & 4 deletions components/hord-cli/src/hord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ use crate::ord::height::Height;
use crate::config::Config;

use crate::db::format_outpoint_to_watch;
use chainhook_sdk::indexer::bitcoin::BitcoinTransactionFullBreakdown;
use chainhook_sdk::indexer::bitcoin::{
standardize_bitcoin_block, BitcoinBlockFullBreakdown, BitcoinTransactionFullBreakdown,
};

use crate::db::{
fetch_and_cache_blocks_in_hord_db, find_last_block_inserted,
Expand All @@ -54,9 +56,9 @@ use self::inscription::InscriptionParser;
use crate::db::{
find_inscription_with_id, find_latest_cursed_inscription_number_at_block_height,
find_latest_inscription_number_at_block_height, format_satpoint_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,
insert_transfer_in_locations, parse_satpoint_to_watch, remove_entry_from_blocks,
remove_entry_from_inscriptions, LazyBlock, LazyBlockTransaction, TraversalResult,
WatchedSatpoint,
};
use crate::ord::inscription_id::InscriptionId;

Expand All @@ -69,6 +71,29 @@ pub struct HordConfig {
pub first_inscription_height: u64,
}

pub fn parse_ordinals_and_standardize_block(
raw_block: BitcoinBlockFullBreakdown,
network: &BitcoinNetwork,
ctx: &Context,
) -> Result<BitcoinBlockData, (String, bool)> {
let mut ordinal_operations = BTreeMap::new();

for tx in raw_block.tx.iter() {
ordinal_operations.insert(tx.txid.to_string(), parse_ordinal_operations(&tx, ctx));
}

let mut block = standardize_bitcoin_block(raw_block, network, ctx)?;

for tx in block.transactions.iter_mut() {
if let Some(ordinal_operations) =
ordinal_operations.remove(tx.transaction_identifier.get_hash_bytes_str())
{
tx.metadata.ordinal_operations = ordinal_operations;
}
}
Ok(block)
}

pub fn parse_ordinal_operations(
tx: &BitcoinTransactionFullBreakdown,
_ctx: &Context,
Expand Down
5 changes: 2 additions & 3 deletions components/hord-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::db::{
find_all_inscriptions_in_block, get_any_entry_in_ordinal_activities, open_readonly_hord_db_conn,
};
use crate::hord::{
get_inscriptions_revealed_in_block,
self, get_inscriptions_revealed_in_block,
update_storage_and_augment_bitcoin_block_with_inscription_reveal_data,
update_storage_and_augment_bitcoin_block_with_inscription_transfer_data, Storage,
};
Expand All @@ -19,7 +19,6 @@ use chainhook_sdk::chainhooks::bitcoin::{
BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
};
use chainhook_sdk::chainhooks::types::{BitcoinChainhookSpecification, BitcoinPredicateType};
use chainhook_sdk::indexer;
use chainhook_sdk::indexer::bitcoin::{
download_and_parse_block_with_retry, retrieve_block_hash_with_retry,
};
Expand Down Expand Up @@ -111,7 +110,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
let block_hash = retrieve_block_hash_with_retry(&cursor, &bitcoin_config, ctx).await?;
let block_breakdown =
download_and_parse_block_with_retry(&block_hash, &bitcoin_config, ctx).await?;
let mut block = match indexer::bitcoin::standardize_bitcoin_block(
let mut block = match hord::parse_ordinals_and_standardize_block(
block_breakdown,
&event_observer_config.bitcoin_network,
ctx,
Expand Down

0 comments on commit a13351d

Please sign in to comment.