Skip to content

Commit

Permalink
Merge pull request #321 from hirosystems/develop
Browse files Browse the repository at this point in the history
release 2.2.2
  • Loading branch information
rafaelcr authored Jun 18, 2024
2 parents 63e0309 + 79da285 commit 8c92f80
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 75 deletions.
29 changes: 27 additions & 2 deletions components/ordhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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::open_readwrite_brc20_db_conn;
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;
Expand All @@ -28,7 +29,7 @@ use ordhook::db::{
find_block_bytes_at_block_height, find_inscription_with_id, find_last_block_inserted,
find_latest_inscription_block_height, find_missing_blocks, get_default_ordhook_db_file_path,
open_ordhook_db_conn_rocks_db_loop, open_readonly_ordhook_db_conn,
open_readonly_ordhook_db_conn_rocks_db, BlockBytesCursor,
open_readonly_ordhook_db_conn_rocks_db, open_readwrite_ordhook_dbs, BlockBytesCursor,
};
use ordhook::download::download_ordinals_dataset_if_required;
use ordhook::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate;
Expand Down Expand Up @@ -712,6 +713,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
let last_known_block =
find_latest_inscription_block_height(&inscriptions_db_conn, ctx)?;
if last_known_block.is_none() {
// Create DB
open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
Expand Down Expand Up @@ -785,6 +787,7 @@ 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)?;
// Create DB
initialize_db(&config, ctx);
open_ordhook_db_conn_rocks_db_loop(
true,
Expand Down Expand Up @@ -920,7 +923,29 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
return Err("Deletion aborted".to_string());
}

delete_data_in_ordhook_db(cmd.start_block, cmd.end_block, &config, ctx)?;
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 brc_20_db_conn_rw = if config.meta_protocols.brc20 {
Some(open_readwrite_brc20_db_conn(
&config.expected_cache_path(),
ctx,
)?)
} else {
None
};

delete_data_in_ordhook_db(
cmd.start_block,
cmd.end_block,
&inscriptions_db_conn_rw,
&blocks_db_rw,
&brc_20_db_conn_rw,
ctx,
)?;
info!(
ctx.expect_logger(),
"Cleaning ordhook_db: {} blocks dropped",
Expand Down
24 changes: 9 additions & 15 deletions components/ordhook-core/src/core/meta_protocols/brc20/db.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{collections::HashMap, path::PathBuf};

use crate::db::{
create_or_open_readwrite_db, format_inscription_id, open_existing_readonly_db,
perform_query_one, perform_query_set,
create_or_open_readwrite_db, open_existing_readonly_db, perform_query_one, perform_query_set,
};
use chainhook_sdk::{
types::{
Expand Down Expand Up @@ -331,7 +330,7 @@ pub fn get_brc20_operations_on_block(
block_identifier: &BlockIdentifier,
db_tx: &Connection,
ctx: &Context,
) -> HashMap<String, Brc20DbLedgerRow> {
) -> HashMap<u64, Brc20DbLedgerRow> {
let args: &[&dyn ToSql] = &[&block_identifier.index.to_sql().unwrap()];
let query = "
SELECT
Expand All @@ -353,20 +352,19 @@ pub fn get_brc20_operations_on_block(
operation: row.get(9).unwrap(),
});
for row in rows.iter() {
map.insert(row.inscription_id.clone(), row.clone());
map.insert(row.tx_index, row.clone());
}
map
}

pub fn augment_transaction_with_brc20_operation_data(
tx: &mut BitcoinTransactionData,
token_map: &mut HashMap<String, Brc20DbTokenRow>,
block_ledger_map: &mut HashMap<String, Brc20DbLedgerRow>,
block_ledger_map: &mut HashMap<u64, Brc20DbLedgerRow>,
db_conn: &Connection,
ctx: &Context,
) {
let inscription_id = format_inscription_id(&tx.transaction_identifier, 0);
let Some(entry) = block_ledger_map.remove(inscription_id.as_str()) else {
let Some(entry) = block_ledger_map.remove(&(tx.metadata.index as u64)) else {
return;
};
if token_map.get(&entry.tick) == None {
Expand Down Expand Up @@ -411,21 +409,17 @@ pub fn augment_transaction_with_brc20_operation_data(
let Some(receiver_address) =
get_transfer_send_receiver_address(entry.ordinal_number, &db_conn, &ctx)
else {
unreachable!("Unable to fetch receiver address for transfer_send operation");
unreachable!("Unable to fetch receiver address for transfer_send operation {:?}", entry);
};
tx.metadata.brc20_operation = Some(Brc20Operation::TransferSend(Brc20TransferData {
tick: entry.tick.clone(),
amt: format!(
"{:.precision$}",
entry.trans_balance * -1.0,
precision = dec
),
amt: format!("{:.precision$}", entry.trans_balance.abs(), precision = dec),
sender_address: entry.address.clone(),
receiver_address,
inscription_id: entry.inscription_id,
}));
}
// `transfer_receive` ops are not reflected in transaction metadata.
// `transfer_receive` ops are not reflected in transaction metadata, they are sent as part of `transfer_send`.
_ => {}
}
}
Expand Down Expand Up @@ -590,7 +584,7 @@ pub fn write_augmented_block_to_brc20_db(
});
continue;
};
let amt = transfer.amt.parse::<f64>().unwrap();
let amt = transfer.amt.parse::<f64>().unwrap().abs();
ledger_rows.push(Brc20DbLedgerRow {
inscription_id: transfer.inscription_id.clone(),
inscription_number,
Expand Down
18 changes: 5 additions & 13 deletions components/ordhook-core/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,25 +1287,18 @@ pub fn remove_entries_from_locations_at_block_height(
pub fn delete_data_in_ordhook_db(
start_block: u64,
end_block: u64,
config: &Config,
inscriptions_db_conn_rw: &Connection,
blocks_db_rw: &DB,
brc_20_db_conn_rw: &Option<Connection>,
ctx: &Context,
) -> Result<(), 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 inscriptions_db_conn_rw =
open_readwrite_ordhook_db_conn(&config.expected_cache_path(), ctx)?;
ctx.try_log(|logger| {
info!(
logger,
"Deleting entries from block #{start_block} to block #{end_block}"
)
});
delete_blocks_in_block_range(start_block as u32, end_block as u32, &blocks_db, &ctx);
delete_blocks_in_block_range(start_block as u32, end_block as u32, &blocks_db_rw, &ctx);
ctx.try_log(|logger| {
info!(
logger,
Expand All @@ -1318,8 +1311,7 @@ pub fn delete_data_in_ordhook_db(
&inscriptions_db_conn_rw,
&ctx,
);
if config.meta_protocols.brc20 {
let conn = open_readwrite_brc20_db_conn(&config.expected_cache_path(), ctx)?;
if let Some(conn) = brc_20_db_conn_rw {
delete_activity_in_block_range(start_block as u32, end_block as u32, &conn, &ctx);
ctx.try_log(|logger| {
info!(
Expand Down
Loading

0 comments on commit 8c92f80

Please sign in to comment.