Skip to content

Commit

Permalink
enha: tidying rocksdb - don't store tx index
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19-cw committed Dec 10, 2024
1 parent 3941200 commit bf1ce28
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/bin/historic_events_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ fn transaction_mined_rocks_db_to_events(
tx: TransactionMinedRocksdb,
block_number: BlockNumberRocksdb,
block_hash: HashRocksdb,
index: usize,
) -> Vec<AccountTransfers> {
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))
}

Expand Down Expand Up @@ -82,7 +83,8 @@ fn process_block_events(block: BlockRocksdb) -> Vec<String> {
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()
}
Expand Down
11 changes: 8 additions & 3 deletions src/eth/storage/permanent/rocks/rocks_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion src/eth/storage/permanent/rocks/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ impl From<BlockRocksdb> 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 }
}
Expand Down
8 changes: 3 additions & 5 deletions src/eth/storage/permanent/rocks/types/transaction_mined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,7 +14,6 @@ pub struct TransactionMinedRocksdb {
pub input: TransactionInputRocksdb,
pub execution: ExecutionRocksdb,
pub logs: Vec<LogMinedRocksdb>,
pub transaction_index: IndexRocksdb,
}

impl From<TransactionMined> for TransactionMinedRocksdb {
Expand All @@ -23,13 +22,12 @@ impl From<TransactionMined> 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(),
Expand All @@ -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),
}
}
}

0 comments on commit bf1ce28

Please sign in to comment.