From 91dfc02684c27b3953fd18febee886faad7842b8 Mon Sep 17 00:00:00 2001 From: Ludo Galabru Date: Tue, 28 Nov 2023 18:45:39 -0500 Subject: [PATCH] chore: refactor BlockBytesCursor usage --- components/ordhook-cli/src/cli/mod.rs | 11 +- components/ordhook-core/src/core/mod.rs | 10 +- .../ordhook-core/src/core/pipeline/mod.rs | 10 +- .../pipeline/processors/block_archiving.rs | 4 +- .../processors/inscription_indexing.rs | 6 +- .../core/protocol/inscription_sequencing.rs | 4 +- .../src/core/protocol/satoshi_numbering.rs | 58 +++----- components/ordhook-core/src/db/mod.rs | 129 ++++++++++-------- components/ordhook-core/src/service/mod.rs | 16 +-- 9 files changed, 125 insertions(+), 123 deletions(-) diff --git a/components/ordhook-cli/src/cli/mod.rs b/components/ordhook-cli/src/cli/mod.rs index 5d8df7ef..05ff2bee 100644 --- a/components/ordhook-cli/src/cli/mod.rs +++ b/components/ordhook-cli/src/cli/mod.rs @@ -24,11 +24,11 @@ use ordhook::core::protocol::inscription_parsing::parse_inscriptions_and_standar use ordhook::core::protocol::satoshi_numbering::compute_satoshi_number; use ordhook::db::{ delete_data_in_ordhook_db, find_all_inscription_transfers, find_all_inscriptions_in_block, - find_all_transfers_in_block, find_inscription_with_id, find_last_block_inserted, - find_latest_inscription_block_height, find_lazy_block_at_block_height, find_missing_blocks, + find_all_transfers_in_block, 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, initialize_ordhook_db, open_ordhook_db_conn_rocks_db_loop, open_readonly_ordhook_db_conn, open_readonly_ordhook_db_conn_rocks_db, - open_readwrite_ordhook_db_conn, + open_readwrite_ordhook_db_conn, BlockBytesCursor, }; use ordhook::download::download_ordinals_dataset_if_required; use ordhook::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate; @@ -796,9 +796,10 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> { ctx, ); for i in cmd.get_blocks().into_iter() { - let block = - find_lazy_block_at_block_height(i as u32, 10, false, &blocks_db, ctx) + let block_bytes = + find_block_bytes_at_block_height(i as u32, 10, &blocks_db, ctx) .expect("unable to retrieve block {i}"); + let block = BlockBytesCursor::new(&block_bytes); info!(ctx.expect_logger(), "--------------------"); info!(ctx.expect_logger(), "Block: {i}"); for tx in block.iter_tx() { diff --git a/components/ordhook-core/src/core/mod.rs b/components/ordhook-core/src/core/mod.rs index ae935d53..8c510f43 100644 --- a/components/ordhook-core/src/core/mod.rs +++ b/components/ordhook-core/src/core/mod.rs @@ -14,7 +14,7 @@ use chainhook_sdk::{ use crate::{ config::{Config, LogConfig}, - db::{find_lazy_block_at_block_height, open_ordhook_db_conn_rocks_db_loop}, + db::{find_pinned_block_bytes_at_block_height, open_ordhook_db_conn_rocks_db_loop}, }; use crate::db::{ @@ -22,7 +22,7 @@ use crate::db::{ open_readonly_ordhook_db_conn, }; -use crate::db::LazyBlockTransaction; +use crate::db::TransactionBytesCursor; #[derive(Clone, Debug)] pub struct OrdhookConfig { @@ -44,11 +44,11 @@ pub fn new_traversals_cache( pub fn new_traversals_lazy_cache( cache_size: usize, -) -> DashMap<(u32, [u8; 8]), LazyBlockTransaction, BuildHasherDefault> { +) -> DashMap<(u32, [u8; 8]), TransactionBytesCursor, BuildHasherDefault> { let hasher = FxBuildHasher::default(); DashMap::with_capacity_and_hasher( ((cache_size.saturating_sub(500)) * 1000 * 1000) - .div(LazyBlockTransaction::get_average_bytes_size()), + .div(TransactionBytesCursor::get_average_bytes_size()), hasher, ) } @@ -139,7 +139,7 @@ pub fn should_sync_ordhook_db( match find_latest_inscription_block_height(&inscriptions_db_conn, ctx)? { Some(height) => { - if find_lazy_block_at_block_height(height as u32, 3, false, &blocks_db, &ctx).is_none() + if find_pinned_block_bytes_at_block_height(height as u32, 3, &blocks_db, &ctx).is_none() { start_block = start_block.min(height); } else { diff --git a/components/ordhook-core/src/core/pipeline/mod.rs b/components/ordhook-core/src/core/pipeline/mod.rs index f9d4ae6a..cdf92c99 100644 --- a/components/ordhook-core/src/core/pipeline/mod.rs +++ b/components/ordhook-core/src/core/pipeline/mod.rs @@ -10,7 +10,7 @@ use std::time::Duration; use tokio::task::JoinSet; use crate::config::Config; -use crate::db::LazyBlock; +use crate::db::BlockBytesCursor; use chainhook_sdk::indexer::bitcoin::{ build_http_client, parse_downloaded_block, try_download_block_bytes_with_retry, @@ -19,7 +19,7 @@ use chainhook_sdk::indexer::bitcoin::{ use super::protocol::inscription_parsing::parse_inscriptions_and_standardize_block; pub enum PostProcessorCommand { - ProcessBlocks(Vec<(u64, LazyBlock)>, Vec), + ProcessBlocks(Vec<(u64, Vec)>, Vec), Terminate, } @@ -111,7 +111,7 @@ pub async fn download_and_pipeline_blocks( while let Ok(Some(block_bytes)) = rx.recv() { let raw_block_data = parse_downloaded_block(block_bytes).expect("unable to parse block"); - let compressed_block = LazyBlock::from_full_block(&raw_block_data) + let compressed_block = BlockBytesCursor::from_full_block(&raw_block_data) .expect("unable to compress block"); let block_height = raw_block_data.height as u64; let block_data = if block_height >= start_sequencing_blocks_at_height { @@ -195,9 +195,9 @@ pub async fn download_and_pipeline_blocks( let mut ooo_compacted_blocks = vec![]; for (block_height, block_opt, compacted_block) in new_blocks.into_iter() { if let Some(block) = block_opt { - inbox.insert(block_height, (block, compacted_block)); + inbox.insert(block_height, (block, compacted_block.to_vec())); } else { - ooo_compacted_blocks.push((block_height, compacted_block)); + ooo_compacted_blocks.push((block_height, compacted_block.to_vec())); } } diff --git a/components/ordhook-core/src/core/pipeline/processors/block_archiving.rs b/components/ordhook-core/src/core/pipeline/processors/block_archiving.rs index 1561f939..38453e52 100644 --- a/components/ordhook-core/src/core/pipeline/processors/block_archiving.rs +++ b/components/ordhook-core/src/core/pipeline/processors/block_archiving.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ config::Config, core::pipeline::{PostProcessorCommand, PostProcessorController, PostProcessorEvent}, - db::{insert_entry_in_blocks, open_ordhook_db_conn_rocks_db_loop, LazyBlock}, + db::{insert_entry_in_blocks, open_ordhook_db_conn_rocks_db_loop}, }; pub fn start_block_archiving_processor( @@ -72,7 +72,7 @@ pub fn start_block_archiving_processor( } pub fn store_compacted_blocks( - mut compacted_blocks: Vec<(u64, LazyBlock)>, + mut compacted_blocks: Vec<(u64, Vec)>, update_tip: bool, blocks_db_rw: &DB, ctx: &Context, 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 8cdc4cff..fd4481ab 100644 --- a/components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs +++ b/components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs @@ -38,7 +38,7 @@ use crate::{ }, }; -use crate::db::{LazyBlockTransaction, TraversalResult}; +use crate::db::{TransactionBytesCursor, TraversalResult}; use crate::{ config::Config, @@ -162,7 +162,7 @@ pub fn start_inscription_indexing_processor( pub fn process_blocks( next_blocks: &mut Vec, sequence_cursor: &mut SequenceCursor, - cache_l2: &Arc>>, + cache_l2: &Arc>>, inscriptions_db_conn_rw: &mut Connection, ordhook_config: &OrdhookConfig, post_processor: &Option>, @@ -259,7 +259,7 @@ pub fn process_block( next_blocks: &Vec, sequence_cursor: &mut SequenceCursor, cache_l1: &mut BTreeMap<(TransactionIdentifier, usize), TraversalResult>, - cache_l2: &Arc>>, + cache_l2: &Arc>>, inscriptions_db_tx: &Transaction, ordhook_config: &OrdhookConfig, ctx: &Context, diff --git a/components/ordhook-core/src/core/protocol/inscription_sequencing.rs b/components/ordhook-core/src/core/protocol/inscription_sequencing.rs index 8eca0ed6..09d0aa5d 100644 --- a/components/ordhook-core/src/core/protocol/inscription_sequencing.rs +++ b/components/ordhook-core/src/core/protocol/inscription_sequencing.rs @@ -23,7 +23,7 @@ use crate::{ find_blessed_inscription_with_ordinal_number, find_latest_cursed_inscription_number_at_block_height, find_latest_inscription_number_at_block_height, format_satpoint_to_watch, - update_inscriptions_with_block, LazyBlockTransaction, TraversalResult, + update_inscriptions_with_block, TransactionBytesCursor, TraversalResult, }, ord::height::Height, }; @@ -67,7 +67,7 @@ pub fn parallelize_inscription_data_computations( block: &BitcoinBlockData, next_blocks: &Vec, cache_l1: &mut BTreeMap<(TransactionIdentifier, usize), TraversalResult>, - cache_l2: &Arc>>, + cache_l2: &Arc>>, inscriptions_db_tx: &Transaction, ordhook_config: &OrdhookConfig, ctx: &Context, diff --git a/components/ordhook-core/src/core/protocol/satoshi_numbering.rs b/components/ordhook-core/src/core/protocol/satoshi_numbering.rs index 97f3dd9a..1c0ef537 100644 --- a/components/ordhook-core/src/core/protocol/satoshi_numbering.rs +++ b/components/ordhook-core/src/core/protocol/satoshi_numbering.rs @@ -7,10 +7,11 @@ use std::path::PathBuf; use std::sync::Arc; use crate::db::{ - find_lazy_block_at_block_height, open_ordhook_db_conn_rocks_db_loop, TransferData, + find_pinned_block_bytes_at_block_height, open_ordhook_db_conn_rocks_db_loop, BlockBytesCursor, + TransferData, }; -use crate::db::{LazyBlockTransaction, TraversalResult}; +use crate::db::{TransactionBytesCursor, TraversalResult}; use crate::ord::height::Height; pub fn compute_satoshi_number( @@ -20,7 +21,7 @@ pub fn compute_satoshi_number( inscription_input_index: usize, inscription_number: i64, traversals_cache: &Arc< - DashMap<(u32, [u8; 8]), LazyBlockTransaction, BuildHasherDefault>, + DashMap<(u32, [u8; 8]), TransactionBytesCursor, BuildHasherDefault>, >, ctx: &Context, ) -> Result { @@ -30,7 +31,7 @@ pub fn compute_satoshi_number( let mut ordinal_block_number = block_identifier.index as u32; let txid = transaction_identifier.get_8_hash_bytes(); - let mut blocks_db = open_ordhook_db_conn_rocks_db_loop(false, &blocks_db_dir, &ctx); + let blocks_db = open_ordhook_db_conn_rocks_db_loop(false, &blocks_db_dir, &ctx); let (sats_ranges, inscription_offset_cross_outputs) = match traversals_cache .get(&(block_identifier.index as u32, txid.clone())) @@ -42,26 +43,15 @@ pub fn compute_satoshi_number( tx.get_cumulated_sats_in_until_input_index(inscription_input_index), ) } - None => { - let mut attempt = 0; - loop { - match find_lazy_block_at_block_height( - ordinal_block_number, - 3, - false, - &blocks_db, - &ctx, - ) { - None => { - if attempt < 3 { - attempt += 1; - blocks_db = - open_ordhook_db_conn_rocks_db_loop(false, &blocks_db_dir, &ctx); - } else { - return Err(format!("block #{ordinal_block_number} not in database")); - } - } - Some(block) => match block.find_and_serialize_transaction_with_txid(&txid) { + None => loop { + match find_pinned_block_bytes_at_block_height(ordinal_block_number, 3, &blocks_db, &ctx) + { + None => { + return Err(format!("block #{ordinal_block_number} not in database")); + } + Some(block_bytes) => { + let cursor = BlockBytesCursor::new(&block_bytes.as_ref()); + match cursor.find_and_serialize_transaction_with_txid(&txid) { Some(tx) => { let sats_ranges = tx.get_sat_ranges(); let inscription_offset_cross_outputs = @@ -70,10 +60,10 @@ pub fn compute_satoshi_number( break (sats_ranges, inscription_offset_cross_outputs); } None => return Err(format!("txid not in block #{ordinal_block_number}")), - }, + } } } - } + }, }; for (i, (min, max)) in sats_ranges.into_iter().enumerate() { @@ -158,30 +148,22 @@ pub fn compute_satoshi_number( } } - let lazy_block = { - let mut attempt = 0; + let pinned_block_bytes = { loop { - match find_lazy_block_at_block_height( + match find_pinned_block_bytes_at_block_height( ordinal_block_number, 3, - false, &blocks_db, &ctx, ) { Some(block) => break block, None => { - if attempt < 3 { - attempt += 1; - blocks_db = - open_ordhook_db_conn_rocks_db_loop(false, &blocks_db_dir, &ctx); - } else { - return Err(format!("block #{ordinal_block_number} not in database")); - } + return Err(format!("block #{ordinal_block_number} not in database")); } } } }; - + let lazy_block = BlockBytesCursor::new(pinned_block_bytes.as_ref()); let coinbase_txid = lazy_block.get_coinbase_txid(); let txid = tx_cursor.0; diff --git a/components/ordhook-core/src/db/mod.rs b/components/ordhook-core/src/db/mod.rs index 386a4bb3..09bc8796 100644 --- a/components/ordhook-core/src/db/mod.rs +++ b/components/ordhook-core/src/db/mod.rs @@ -8,7 +8,7 @@ use std::{ use rand::{thread_rng, Rng}; -use rocksdb::{DBCompactionStyle, DB}; +use rocksdb::{DBCompactionStyle, DBPinnableSlice, DB}; use rusqlite::{Connection, OpenFlags, ToSql, Transaction}; use std::io::Cursor; @@ -332,7 +332,7 @@ fn open_readwrite_ordhook_db_conn_rocks_db( pub fn insert_entry_in_blocks( block_height: u32, - lazy_block: &LazyBlock, + block_bytes: &[u8], update_tip: bool, blocks_db_rw: &DB, ctx: &Context, @@ -340,7 +340,7 @@ pub fn insert_entry_in_blocks( let block_height_bytes = block_height.to_be_bytes(); let mut retries = 0; loop { - let res = blocks_db_rw.put(&block_height_bytes, &lazy_block.bytes); + let res = blocks_db_rw.put(&block_height_bytes, block_bytes); match res { Ok(_) => break, Err(e) => { @@ -373,13 +373,47 @@ pub fn find_last_block_inserted(blocks_db: &DB) -> u32 { } } -pub fn find_lazy_block_at_block_height( +pub fn find_pinned_block_bytes_at_block_height<'a>( + block_height: u32, + retry: u8, + blocks_db: &'a DB, + ctx: &Context, +) -> Option> { + let mut attempt = 1; + // let mut read_options = rocksdb::ReadOptions::default(); + // read_options.fill_cache(true); + // read_options.set_verify_checksums(false); + let mut backoff: f64 = 1.0; + let mut rng = thread_rng(); + + loop { + match blocks_db.get_pinned(block_height.to_be_bytes()) { + Ok(Some(res)) => return Some(res), + _ => { + attempt += 1; + backoff = 2.0 * backoff + (backoff * rng.gen_range(0.0..1.0)); + let duration = std::time::Duration::from_millis((backoff * 1_000.0) as u64); + ctx.try_log(|logger| { + warn!( + logger, + "Unable to find block #{}, will retry in {:?}", block_height, duration + ) + }); + std::thread::sleep(duration); + if attempt > retry { + return None; + } + } + } + } +} + +pub fn find_block_bytes_at_block_height<'a>( block_height: u32, retry: u8, - try_iterator: bool, blocks_db: &DB, ctx: &Context, -) -> Option { +) -> Option> { let mut attempt = 1; // let mut read_options = rocksdb::ReadOptions::default(); // read_options.fill_cache(true); @@ -389,23 +423,8 @@ pub fn find_lazy_block_at_block_height( loop { match blocks_db.get(block_height.to_be_bytes()) { - Ok(Some(res)) => return Some(LazyBlock::new(res)), + Ok(Some(res)) => return Some(res), _ => { - if attempt == 1 && try_iterator { - ctx.try_log(|logger| { - warn!( - logger, - "Attempt to retrieve block #{} through iterator", block_height, - ) - }); - let mut iter = blocks_db.iterator(rocksdb::IteratorMode::End); - let block_height_bytes = block_height.to_be_bytes(); - while let Some(Ok((k, res))) = iter.next() { - if (*k).eq(&block_height_bytes) { - return Some(LazyBlock::new(res.to_vec())); - } - } - } attempt += 1; backoff = 2.0 * backoff + (backoff * rng.gen_range(0.0..1.0)); let duration = std::time::Duration::from_millis((backoff * 1_000.0) as u64); @@ -432,7 +451,7 @@ pub fn run_compaction(blocks_db_rw: &DB, lim: u32) { pub fn find_missing_blocks(blocks_db: &DB, start: u32, end: u32, ctx: &Context) -> Vec { let mut missing_blocks = vec![]; for i in start..=end { - if find_lazy_block_at_block_height(i as u32, 0, false, &blocks_db, ctx).is_none() { + if find_pinned_block_bytes_at_block_height(i as u32, 0, &blocks_db, ctx).is_none() { missing_blocks.push(i); } } @@ -1352,21 +1371,21 @@ pub fn parse_outpoint_to_watch(outpoint_to_watch: &str) -> (TransactionIdentifie } #[derive(Debug)] -pub struct LazyBlock { - pub bytes: Vec, +pub struct BlockBytesCursor<'a> { + pub bytes: &'a [u8], pub tx_len: u16, } #[derive(Debug, Clone)] -pub struct LazyBlockTransaction { +pub struct TransactionBytesCursor { pub txid: [u8; 8], - pub inputs: Vec, + pub inputs: Vec, pub outputs: Vec, } -impl LazyBlockTransaction { +impl TransactionBytesCursor { pub fn get_average_bytes_size() -> usize { - TXID_LEN + 3 * LazyBlockTransactionInput::get_average_bytes_size() + 3 * SATS_LEN + TXID_LEN + 3 * TransactionInputBytesCursor::get_average_bytes_size() + 3 * SATS_LEN } pub fn get_sat_ranges(&self) -> Vec<(u64, u64)> { @@ -1392,14 +1411,14 @@ impl LazyBlockTransaction { } #[derive(Debug, Clone)] -pub struct LazyBlockTransactionInput { +pub struct TransactionInputBytesCursor { pub txin: [u8; 8], pub block_height: u32, pub vout: u16, pub txin_value: u64, } -impl LazyBlockTransactionInput { +impl TransactionInputBytesCursor { pub fn get_average_bytes_size() -> usize { TXID_LEN + SATS_LEN + 4 + 2 } @@ -1410,10 +1429,10 @@ const SATS_LEN: usize = 8; const INPUT_SIZE: usize = TXID_LEN + 4 + 2 + SATS_LEN; const OUTPUT_SIZE: usize = 8; -impl LazyBlock { - pub fn new(bytes: Vec) -> LazyBlock { +impl<'a> BlockBytesCursor<'a> { + pub fn new(bytes: &[u8]) -> BlockBytesCursor { let tx_len = u16::from_be_bytes([bytes[0], bytes[1]]); - LazyBlock { bytes, tx_len } + BlockBytesCursor { bytes, tx_len } } pub fn get_coinbase_data_pos(&self) -> usize { @@ -1461,11 +1480,11 @@ impl LazyBlock { pub fn get_lazy_transaction_at_pos( &self, - cursor: &mut Cursor<&Vec>, + cursor: &mut Cursor<&[u8]>, txid: [u8; 8], inputs_len: u16, outputs_len: u16, - ) -> LazyBlockTransaction { + ) -> TransactionBytesCursor { let mut inputs = Vec::with_capacity(inputs_len as usize); for _ in 0..inputs_len { let mut txin = [0u8; 8]; @@ -1478,7 +1497,7 @@ impl LazyBlock { cursor.read_exact(&mut vout).expect("data corrupted"); let mut txin_value = [0u8; 8]; cursor.read_exact(&mut txin_value).expect("data corrupted"); - inputs.push(LazyBlockTransactionInput { + inputs.push(TransactionInputBytesCursor { txin: txin, block_height: u32::from_be_bytes(block_height), vout: u16::from_be_bytes(vout), @@ -1491,7 +1510,7 @@ impl LazyBlock { cursor.read_exact(&mut value).expect("data corrupted"); outputs.push(u64::from_be_bytes(value)) } - LazyBlockTransaction { + TransactionBytesCursor { txid, inputs, outputs, @@ -1501,10 +1520,10 @@ impl LazyBlock { pub fn find_and_serialize_transaction_with_txid( &self, searched_txid: &[u8], - ) -> Option { + ) -> Option { // println!("{:?}", hex::encode(searched_txid)); let mut entry = None; - let mut cursor = Cursor::new(&self.bytes); + let mut cursor = Cursor::new(self.bytes); let mut cumulated_offset = 0; let mut i = 0; while entry.is_none() { @@ -1533,11 +1552,11 @@ impl LazyBlock { entry } - pub fn iter_tx(&self) -> LazyBlockTransactionIterator { - LazyBlockTransactionIterator::new(&self) + pub fn iter_tx(&self) -> TransactionBytesCursorIterator { + TransactionBytesCursorIterator::new(&self) } - pub fn from_full_block(block: &BitcoinBlockFullBreakdown) -> std::io::Result { + pub fn from_full_block<'b>(block: &BitcoinBlockFullBreakdown) -> std::io::Result> { let mut buffer = vec![]; // Number of transactions in the block (not including coinbase) let tx_len = block.tx.len() as u16 - 1; @@ -1624,10 +1643,10 @@ impl LazyBlock { buffer.write(&sats.to_be_bytes())?; } } - Ok(Self::new(buffer)) + Ok(buffer) } - pub fn from_standardized_block(block: &BitcoinBlockData) -> std::io::Result { + pub fn from_standardized_block<'b>(block: &BitcoinBlockData) -> std::io::Result> { let mut buffer = vec![]; // Number of transactions in the block (not including coinbase) let tx_len = block.transactions.len() as u16 - 1; @@ -1678,19 +1697,19 @@ impl LazyBlock { buffer.write(&sats.to_be_bytes())?; } } - Ok(Self::new(buffer)) + Ok(buffer) } } -pub struct LazyBlockTransactionIterator<'a> { - lazy_block: &'a LazyBlock, +pub struct TransactionBytesCursorIterator<'a> { + lazy_block: &'a BlockBytesCursor<'a>, tx_index: u16, cumulated_offset: usize, } -impl<'a> LazyBlockTransactionIterator<'a> { - pub fn new(lazy_block: &'a LazyBlock) -> LazyBlockTransactionIterator<'a> { - LazyBlockTransactionIterator { +impl<'a> TransactionBytesCursorIterator<'a> { + pub fn new(lazy_block: &'a BlockBytesCursor) -> TransactionBytesCursorIterator<'a> { + TransactionBytesCursorIterator { lazy_block, tx_index: 0, cumulated_offset: 0, @@ -1698,17 +1717,17 @@ impl<'a> LazyBlockTransactionIterator<'a> { } } -impl<'a> Iterator for LazyBlockTransactionIterator<'a> { - type Item = LazyBlockTransaction; +impl<'a> Iterator for TransactionBytesCursorIterator<'a> { + type Item = TransactionBytesCursor; - fn next(&mut self) -> Option { + fn next(&mut self) -> Option { if self.tx_index >= self.lazy_block.tx_len { return None; } let pos = self.lazy_block.get_transactions_data_pos() + self.cumulated_offset; let (inputs_len, outputs_len, size) = self.lazy_block.get_transaction_format(self.tx_index); // println!("{inputs_len} / {outputs_len} / {size}"); - let mut cursor = Cursor::new(&self.lazy_block.bytes); + let mut cursor = Cursor::new(self.lazy_block.bytes); cursor.set_position(pos as u64); let mut txid = [0u8; 8]; let _ = cursor.read_exact(&mut txid); diff --git a/components/ordhook-core/src/service/mod.rs b/components/ordhook-core/src/service/mod.rs index c30ccdba..772f2a02 100644 --- a/components/ordhook-core/src/service/mod.rs +++ b/components/ordhook-core/src/service/mod.rs @@ -17,7 +17,7 @@ use crate::core::{new_traversals_lazy_cache, should_sync_ordhook_db, should_sync use crate::db::{ delete_data_in_ordhook_db, insert_entry_in_blocks, open_ordhook_db_conn_rocks_db_loop, open_readwrite_ordhook_db_conn, open_readwrite_ordhook_dbs, update_inscriptions_with_block, - update_locations_with_block, LazyBlock, LazyBlockTransaction, + update_locations_with_block, BlockBytesCursor, TransactionBytesCursor, }; use crate::db::{ find_last_block_inserted, find_missing_blocks, run_compaction, @@ -648,8 +648,8 @@ fn chainhook_sidecar_mutate_ordhook_db(command: HandleBlock, config: &Config, ct } } HandleBlock::ApplyBlock(block) => { - let compressed_block: LazyBlock = match LazyBlock::from_standardized_block(&block) { - Ok(block) => block, + let block_bytes = match BlockBytesCursor::from_standardized_block(&block) { + Ok(block_bytes) => block_bytes, Err(e) => { ctx.try_log(|logger| { error!( @@ -664,7 +664,7 @@ fn chainhook_sidecar_mutate_ordhook_db(command: HandleBlock, config: &Config, ct }; insert_entry_in_blocks( block.block_identifier.index as u32, - &compressed_block, + &block_bytes, true, &blocks_db_rw, &ctx, @@ -718,7 +718,7 @@ pub fn start_observer_forwarding( pub fn chainhook_sidecar_mutate_blocks( blocks_to_mutate: &mut Vec, blocks_ids_to_rollback: &Vec, - cache_l2: &Arc>>, + cache_l2: &Arc>>, config: &Config, ctx: &Context, ) { @@ -755,8 +755,8 @@ pub fn chainhook_sidecar_mutate_blocks( let ordhook_config = config.get_ordhook_config(); for cache in blocks_to_mutate.iter_mut() { - let compressed_block: LazyBlock = match LazyBlock::from_standardized_block(&cache.block) { - Ok(block) => block, + let block_bytes = match BlockBytesCursor::from_standardized_block(&cache.block) { + Ok(block_bytes) => block_bytes, Err(e) => { ctx.try_log(|logger| { error!( @@ -772,7 +772,7 @@ pub fn chainhook_sidecar_mutate_blocks( insert_entry_in_blocks( cache.block.block_identifier.index as u32, - &compressed_block, + &block_bytes, true, &blocks_db_rw, &ctx,