From bf1ce2813bbe938d3ab339fe74c92ea4257f04da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Bezerra?= Date: Thu, 5 Dec 2024 23:22:33 -0300 Subject: [PATCH] enha: tidying rocksdb - don't store tx index --- src/bin/historic_events_processor.rs | 6 ++++-- src/eth/storage/permanent/rocks/rocks_state.rs | 11 ++++++++--- src/eth/storage/permanent/rocks/types/block.rs | 3 ++- .../permanent/rocks/types/transaction_mined.rs | 8 +++----- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/bin/historic_events_processor.rs b/src/bin/historic_events_processor.rs index 319d976d2..615790984 100644 --- a/src/bin/historic_events_processor.rs +++ b/src/bin/historic_events_processor.rs @@ -27,8 +27,9 @@ fn transaction_mined_rocks_db_to_events( tx: TransactionMinedRocksdb, block_number: BlockNumberRocksdb, block_hash: HashRocksdb, + index: usize, ) -> Vec { - let tx = TransactionMined::from_rocks_primitives(tx, block_number, block_hash); + let tx = TransactionMined::from_rocks_primitives(tx, block_number, block_hash, index); transaction_to_events(block_timestamp.into(), Cow::Owned(tx)) } @@ -82,7 +83,8 @@ fn process_block_events(block: BlockRocksdb) -> Vec { block .transactions .into_iter() - .flat_map(|tx| transaction_mined_rocks_db_to_events(timestamp, tx, block.header.number, block.header.hash)) + .enumerate() + .flat_map(|(index, tx)| transaction_mined_rocks_db_to_events(timestamp, tx, block.header.number, block.header.hash, index)) .map(|event| event.event_payload().unwrap()) .collect() } diff --git a/src/eth/storage/permanent/rocks/rocks_state.rs b/src/eth/storage/permanent/rocks/rocks_state.rs index 2c2c0bcf0..4d65f73a4 100644 --- a/src/eth/storage/permanent/rocks/rocks_state.rs +++ b/src/eth/storage/permanent/rocks/rocks_state.rs @@ -261,12 +261,17 @@ impl RocksStorageState { }; let block = block.into_inner(); - let transaction = block.transactions.into_iter().find(|tx| Hash::from(tx.input.hash) == tx_hash); + let transaction = block.transactions.into_iter().enumerate().find(|(_, tx)| Hash::from(tx.input.hash) == tx_hash); match transaction { - Some(tx) => { + Some((index, tx)) => { tracing::trace!(%tx_hash, "transaction found"); - Ok(Some(TransactionMined::from_rocks_primitives(tx, block_number.into_inner(), block.header.hash))) + Ok(Some(TransactionMined::from_rocks_primitives( + tx, + block_number.into_inner(), + block.header.hash, + index, + ))) } None => log_and_err!("rocks error, transaction wasn't found in block where the index pointed at") .with_context(|| format!("block_number = {:?} tx_hash = {}", block_number, tx_hash)), diff --git a/src/eth/storage/permanent/rocks/types/block.rs b/src/eth/storage/permanent/rocks/types/block.rs index 6aea8cfa7..64c837799 100644 --- a/src/eth/storage/permanent/rocks/types/block.rs +++ b/src/eth/storage/permanent/rocks/types/block.rs @@ -71,7 +71,8 @@ impl From for Block { let transactions = item .transactions .into_iter() - .map(|tx| TransactionMined::from_rocks_primitives(tx, header.number.into(), header.hash.into())) + .enumerate() + .map(|(index, tx)| TransactionMined::from_rocks_primitives(tx, header.number.into(), header.hash.into(), index)) .collect(); Block { header, transactions } } diff --git a/src/eth/storage/permanent/rocks/types/transaction_mined.rs b/src/eth/storage/permanent/rocks/types/transaction_mined.rs index 99acfc9c0..554e568b1 100644 --- a/src/eth/storage/permanent/rocks/types/transaction_mined.rs +++ b/src/eth/storage/permanent/rocks/types/transaction_mined.rs @@ -3,9 +3,9 @@ use std::fmt::Debug; use super::block_number::BlockNumberRocksdb; use super::execution::ExecutionRocksdb; use super::hash::HashRocksdb; -use super::index::IndexRocksdb; use super::log_mined::LogMinedRocksdb; use super::transaction_input::TransactionInputRocksdb; +use crate::eth::primitives::Index; use crate::eth::primitives::LogMined; use crate::eth::primitives::TransactionMined; @@ -14,7 +14,6 @@ pub struct TransactionMinedRocksdb { pub input: TransactionInputRocksdb, pub execution: ExecutionRocksdb, pub logs: Vec, - pub transaction_index: IndexRocksdb, } impl From for TransactionMinedRocksdb { @@ -23,13 +22,12 @@ impl From for TransactionMinedRocksdb { input: item.input.into(), execution: item.execution.into(), logs: item.logs.into_iter().map(LogMinedRocksdb::from).collect(), - transaction_index: IndexRocksdb::from(item.transaction_index), } } } impl TransactionMined { - pub fn from_rocks_primitives(other: TransactionMinedRocksdb, block_number: BlockNumberRocksdb, block_hash: HashRocksdb) -> Self { + pub fn from_rocks_primitives(other: TransactionMinedRocksdb, block_number: BlockNumberRocksdb, block_hash: HashRocksdb, index: usize) -> Self { Self { block_number: block_number.into(), block_hash: block_hash.into(), @@ -40,7 +38,7 @@ impl TransactionMined { .into_iter() .map(|log| LogMined::from_rocks_primitives(log, block_number, block_hash)) .collect(), - transaction_index: other.transaction_index.into(), + transaction_index: Index::from(index as u64), } } }