From 356bb5a45a71c75065046ece164aca1b331a5381 Mon Sep 17 00:00:00 2001 From: carneiro-cw <156914855+carneiro-cw@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:02:57 -0300 Subject: [PATCH] chore: rocks: refactor types.rs into types module (#1514) --- src/eth/storage/rocks/mod.rs | 1 + src/eth/storage/rocks/types.rs | 682 ------------------ src/eth/storage/rocks/types/account.rs | 53 ++ src/eth/storage/rocks/types/address.rs | 26 + src/eth/storage/rocks/types/block.rs | 75 ++ src/eth/storage/rocks/types/block_header.rs | 34 + src/eth/storage/rocks/types/block_number.rs | 34 + src/eth/storage/rocks/types/bytes.rs | 44 ++ src/eth/storage/rocks/types/chain_id.rs | 26 + src/eth/storage/rocks/types/difficulty.rs | 24 + src/eth/storage/rocks/types/execution.rs | 50 ++ .../storage/rocks/types/execution_result.rs | 27 + src/eth/storage/rocks/types/gas.rs | 24 + src/eth/storage/rocks/types/hash.rs | 26 + src/eth/storage/rocks/types/index.rs | 24 + src/eth/storage/rocks/types/log.rs | 38 + src/eth/storage/rocks/types/log_mined.rs | 43 ++ src/eth/storage/rocks/types/logs_bloom.rs | 18 + src/eth/storage/rocks/types/miner_nonce.rs | 17 + src/eth/storage/rocks/types/mod.rs | 33 + src/eth/storage/rocks/types/nonce.rs | 27 + src/eth/storage/rocks/types/size.rs | 24 + src/eth/storage/rocks/types/slot.rs | 51 ++ .../storage/rocks/types/transaction_input.rs | 72 ++ .../storage/rocks/types/transaction_mined.rs | 46 ++ src/eth/storage/rocks/types/unix_time.rs | 21 + src/eth/storage/rocks/types/wei.rs | 28 + 27 files changed, 886 insertions(+), 682 deletions(-) delete mode 100644 src/eth/storage/rocks/types.rs create mode 100644 src/eth/storage/rocks/types/account.rs create mode 100644 src/eth/storage/rocks/types/address.rs create mode 100644 src/eth/storage/rocks/types/block.rs create mode 100644 src/eth/storage/rocks/types/block_header.rs create mode 100644 src/eth/storage/rocks/types/block_number.rs create mode 100644 src/eth/storage/rocks/types/bytes.rs create mode 100644 src/eth/storage/rocks/types/chain_id.rs create mode 100644 src/eth/storage/rocks/types/difficulty.rs create mode 100644 src/eth/storage/rocks/types/execution.rs create mode 100644 src/eth/storage/rocks/types/execution_result.rs create mode 100644 src/eth/storage/rocks/types/gas.rs create mode 100644 src/eth/storage/rocks/types/hash.rs create mode 100644 src/eth/storage/rocks/types/index.rs create mode 100644 src/eth/storage/rocks/types/log.rs create mode 100644 src/eth/storage/rocks/types/log_mined.rs create mode 100644 src/eth/storage/rocks/types/logs_bloom.rs create mode 100644 src/eth/storage/rocks/types/miner_nonce.rs create mode 100644 src/eth/storage/rocks/types/mod.rs create mode 100644 src/eth/storage/rocks/types/nonce.rs create mode 100644 src/eth/storage/rocks/types/size.rs create mode 100644 src/eth/storage/rocks/types/slot.rs create mode 100644 src/eth/storage/rocks/types/transaction_input.rs create mode 100644 src/eth/storage/rocks/types/transaction_mined.rs create mode 100644 src/eth/storage/rocks/types/unix_time.rs create mode 100644 src/eth/storage/rocks/types/wei.rs diff --git a/src/eth/storage/rocks/mod.rs b/src/eth/storage/rocks/mod.rs index 8246a7b42..1bb2fd901 100644 --- a/src/eth/storage/rocks/mod.rs +++ b/src/eth/storage/rocks/mod.rs @@ -8,4 +8,5 @@ mod rocks_db; pub mod rocks_permanent; /// State handler for DB and column families. mod rocks_state; + mod types; diff --git a/src/eth/storage/rocks/types.rs b/src/eth/storage/rocks/types.rs deleted file mode 100644 index de9f4bb99..000000000 --- a/src/eth/storage/rocks/types.rs +++ /dev/null @@ -1,682 +0,0 @@ -use std::collections::HashMap; -use std::fmt::Debug; -use std::fmt::Display; -use std::ops::Deref; - -use ethereum_types::Bloom; -use ethereum_types::H160; -use ethereum_types::H256; -use ethereum_types::H64; -use ethereum_types::U256; -use ethereum_types::U64; -use revm::primitives::KECCAK_EMPTY; - -use crate::eth::primitives::logs_bloom::LogsBloom; -use crate::eth::primitives::Account; -use crate::eth::primitives::Address; -use crate::eth::primitives::Block; -use crate::eth::primitives::BlockHeader; -use crate::eth::primitives::BlockNumber; -use crate::eth::primitives::Bytes; -use crate::eth::primitives::ChainId; -use crate::eth::primitives::Difficulty; -use crate::eth::primitives::EvmExecution; -use crate::eth::primitives::ExecutionResult; -use crate::eth::primitives::Gas; -use crate::eth::primitives::Hash; -use crate::eth::primitives::Index; -use crate::eth::primitives::Log; -use crate::eth::primitives::LogMined; -use crate::eth::primitives::MinerNonce; -use crate::eth::primitives::Nonce; -use crate::eth::primitives::Size; -use crate::eth::primitives::SlotIndex; -use crate::eth::primitives::SlotValue; -use crate::eth::primitives::TransactionInput; -use crate::eth::primitives::TransactionMined; -use crate::eth::primitives::UnixTime; -use crate::eth::primitives::Wei; -use crate::ext::OptionExt; -use crate::gen_newtype_from; - -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] -pub struct AccountRocksdb { - pub balance: WeiRocksdb, - pub nonce: NonceRocksdb, - pub bytecode: Option, -} - -impl From for (AddressRocksdb, AccountRocksdb) { - fn from(value: Account) -> Self { - ( - value.address.into(), - AccountRocksdb { - balance: value.balance.into(), - nonce: value.nonce.into(), - bytecode: value.bytecode.map_into(), - }, - ) - } -} - -impl Default for AccountRocksdb { - fn default() -> Self { - Self { - balance: WeiRocksdb::ZERO, - nonce: NonceRocksdb::ZERO, - bytecode: None, - } - } -} - -#[derive(Clone, Default, Eq, PartialEq, fake::Dummy, serde::Serialize, serde::Deserialize)] -pub struct BytesRocksdb(pub Vec); - -impl Deref for BytesRocksdb { - type Target = Vec; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Display for BytesRocksdb { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if self.len() <= 256 { - write!(f, "{}", const_hex::encode_prefixed(&self.0)) - } else { - write!(f, "too long") - } - } -} - -impl Debug for BytesRocksdb { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Bytes").field(&self.to_string()).finish() - } -} - -impl From for BytesRocksdb { - fn from(value: Bytes) -> Self { - Self(value.0) - } -} - -impl From for Bytes { - fn from(value: BytesRocksdb) -> Self { - Self(value.0) - } -} - -#[derive(Debug, Clone, Default, Eq, PartialEq, derive_more::Add, derive_more::Sub, serde::Serialize, serde::Deserialize)] -pub struct WeiRocksdb(U256); - -gen_newtype_from!(self = WeiRocksdb, other = U256); - -impl From for Wei { - fn from(value: WeiRocksdb) -> Self { - value.0.into() - } -} - -impl From for WeiRocksdb { - fn from(value: Wei) -> Self { - U256::from(value).into() - } -} - -impl WeiRocksdb { - pub const ZERO: WeiRocksdb = WeiRocksdb(U256::zero()); - pub const ONE: WeiRocksdb = WeiRocksdb(U256::one()); -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct NonceRocksdb(U64); - -gen_newtype_from!(self = NonceRocksdb, other = u64); - -impl From for Nonce { - fn from(value: NonceRocksdb) -> Self { - value.0.as_u64().into() - } -} - -impl From for NonceRocksdb { - fn from(value: Nonce) -> Self { - u64::from(value).into() - } -} - -impl NonceRocksdb { - pub const ZERO: NonceRocksdb = NonceRocksdb(U64::zero()); -} - -impl AccountRocksdb { - pub fn to_account(&self, address: &Address) -> Account { - Account { - address: *address, - nonce: self.nonce.clone().into(), - balance: self.balance.clone().into(), - bytecode: self.bytecode.clone().map_into(), - code_hash: KECCAK_EMPTY.into(), - } - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct SlotValueRocksdb(pub U256); - -impl From for SlotValueRocksdb { - fn from(item: SlotValue) -> Self { - SlotValueRocksdb(item.as_u256()) - } -} - -impl From for SlotValue { - fn from(item: SlotValueRocksdb) -> Self { - SlotValue::from(item.0) - } -} - -#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] -pub struct AddressRocksdb(pub H160); - -impl From
for AddressRocksdb { - fn from(item: Address) -> Self { - AddressRocksdb(item.0) - } -} - -impl From for Address { - fn from(item: AddressRocksdb) -> Self { - Address::new_from_h160(item.0) - } -} - -#[derive(Debug, derive_more::Display, Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] -pub struct BlockNumberRocksdb(pub U64); - -gen_newtype_from!(self = BlockNumberRocksdb, other = u8, u16, u32, u64, U64, usize, i32, i64); - -impl From for BlockNumberRocksdb { - fn from(item: BlockNumber) -> Self { - BlockNumberRocksdb(item.0) - } -} - -impl From for BlockNumber { - fn from(item: BlockNumberRocksdb) -> Self { - BlockNumber::from(item.0) - } -} - -impl From for u64 { - fn from(value: BlockNumberRocksdb) -> Self { - value.0.as_u64() - } -} - -#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)] -pub struct SlotIndexRocksdb(U256); - -gen_newtype_from!(self = SlotIndexRocksdb, other = u64); - -impl From for SlotIndexRocksdb { - fn from(item: SlotIndex) -> Self { - SlotIndexRocksdb(item.as_u256()) - } -} - -impl From for SlotIndex { - fn from(item: SlotIndexRocksdb) -> Self { - SlotIndex::from(item.0) - } -} - -#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] -pub struct HashRocksdb(pub H256); - -impl From for HashRocksdb { - fn from(item: Hash) -> Self { - HashRocksdb(item.0) - } -} - -impl From for Hash { - fn from(item: HashRocksdb) -> Self { - Hash::new_from_h256(item.0) - } -} - -#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize, derive_more::Add, Copy, Hash)] -pub struct IndexRocksdb(pub u64); - -impl From for IndexRocksdb { - fn from(item: Index) -> Self { - IndexRocksdb(item.0) - } -} - -impl From for Index { - fn from(item: IndexRocksdb) -> Self { - Index::new(item.0) - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -#[serde(transparent)] -pub struct GasRocksdb(U64); - -gen_newtype_from!(self = GasRocksdb, other = u64); - -impl From for Gas { - fn from(value: GasRocksdb) -> Self { - value.0.as_u64().into() - } -} - -impl From for GasRocksdb { - fn from(value: Gas) -> Self { - u64::from(value).into() - } -} - -#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] -pub struct MinerNonceRocksdb(H64); - -gen_newtype_from!(self = MinerNonceRocksdb, other = H64, [u8; 8], MinerNonce); - -impl From for MinerNonce { - fn from(value: MinerNonceRocksdb) -> Self { - value.0.into() - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -#[serde(transparent)] -pub struct DifficultyRocksdb(U256); - -gen_newtype_from!(self = DifficultyRocksdb, other = U256); - -impl From for Difficulty { - fn from(value: DifficultyRocksdb) -> Self { - value.0.into() - } -} - -impl From for DifficultyRocksdb { - fn from(value: Difficulty) -> Self { - U256::from(value).into() - } -} - -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct BlockHeaderRocksdb { - pub number: BlockNumberRocksdb, - pub hash: HashRocksdb, - pub transactions_root: HashRocksdb, - pub gas_used: GasRocksdb, - pub gas_limit: GasRocksdb, - pub bloom: LogsBloomRocksdb, - pub timestamp: UnixTimeRocksdb, - pub parent_hash: HashRocksdb, - pub author: AddressRocksdb, - pub extra_data: BytesRocksdb, - pub miner: AddressRocksdb, - pub difficulty: DifficultyRocksdb, - pub receipts_root: HashRocksdb, - pub uncle_hash: HashRocksdb, - pub size: SizeRocksdb, - pub state_root: HashRocksdb, - pub total_difficulty: DifficultyRocksdb, - pub nonce: MinerNonceRocksdb, -} - -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct UnixTimeRocksdb(u64); - -gen_newtype_from!(self = UnixTimeRocksdb, other = u64); - -impl From for UnixTimeRocksdb { - fn from(value: UnixTime) -> Self { - Self(*value) - } -} - -impl From for UnixTime { - fn from(value: UnixTimeRocksdb) -> Self { - value.0.into() - } -} - -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)] -#[serde(transparent)] -pub struct LogsBloomRocksdb(Bloom); - -gen_newtype_from!(self = LogsBloomRocksdb, other = Bloom, LogsBloom); - -impl From for LogsBloom { - fn from(value: LogsBloomRocksdb) -> Self { - value.0.into() - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -#[serde(transparent)] -pub struct SizeRocksdb(U64); - -gen_newtype_from!(self = SizeRocksdb, other = U64, u64); - -impl From for SizeRocksdb { - fn from(value: Size) -> Self { - u64::from(value).into() - } -} - -impl From for Size { - fn from(value: SizeRocksdb) -> Self { - value.0.as_u64().into() - } -} - -#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)] -pub struct ChainIdRocksdb(pub U64); - -impl From for ChainIdRocksdb { - fn from(value: ChainId) -> Self { - ChainIdRocksdb(value.0) - } -} - -impl From for ChainId { - fn from(value: ChainIdRocksdb) -> Self { - ChainId::new(value.0) - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct TransactionInputRocksdb { - pub chain_id: Option, - pub hash: HashRocksdb, - pub nonce: NonceRocksdb, - pub signer: AddressRocksdb, - pub from: AddressRocksdb, - pub to: Option, - pub value: WeiRocksdb, - pub input: BytesRocksdb, - pub gas_limit: GasRocksdb, - pub gas_price: WeiRocksdb, - pub v: U64, - pub r: U256, - pub s: U256, -} - -impl From for TransactionInputRocksdb { - fn from(item: TransactionInput) -> Self { - Self { - chain_id: item.chain_id.map_into(), - hash: HashRocksdb::from(item.hash), - nonce: NonceRocksdb::from(item.nonce), - signer: AddressRocksdb::from(item.signer), - from: AddressRocksdb::from(item.from), - to: item.to.map(AddressRocksdb::from), - value: WeiRocksdb::from(item.value), - input: BytesRocksdb::from(item.input), - gas_limit: GasRocksdb::from(item.gas_limit), - gas_price: WeiRocksdb::from(item.gas_price), - v: item.v, - r: item.r, - s: item.s, - } - } -} - -impl From for TransactionInput { - fn from(item: TransactionInputRocksdb) -> Self { - Self { - chain_id: item.chain_id.map_into(), - hash: item.hash.into(), - nonce: item.nonce.into(), - signer: item.signer.into(), - from: item.from.into(), - to: item.to.map(Into::into), - value: item.value.into(), - input: item.input.into(), - gas_limit: item.gas_limit.into(), - gas_price: item.gas_price.into(), - v: item.v, - r: item.r, - s: item.s, - tx_type: None, - } - } -} - -pub enum ExecutionResultRocksdb { - Success, - Reverted, - Halted { reason: String }, -} - -impl From for ExecutionResultRocksdb { - fn from(item: ExecutionResult) -> Self { - match item { - ExecutionResult::Success => ExecutionResultRocksdb::Success, - ExecutionResult::Reverted => ExecutionResultRocksdb::Reverted, - ExecutionResult::Halted { reason } => ExecutionResultRocksdb::Halted { reason }, - } - } -} - -impl From for ExecutionResult { - fn from(item: ExecutionResultRocksdb) -> Self { - match item { - ExecutionResultRocksdb::Success => ExecutionResult::Success, - ExecutionResultRocksdb::Reverted => ExecutionResult::Reverted, - ExecutionResultRocksdb::Halted { reason } => ExecutionResult::Halted { reason }, - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct LogRocksdb { - pub address: AddressRocksdb, - pub topics: (Option, Option, Option, Option), - pub data: BytesRocksdb, -} - -impl From for LogRocksdb { - fn from(item: Log) -> Self { - Self { - address: AddressRocksdb::from(item.address), - topics: (item.topic0.map_into(), item.topic1.map_into(), item.topic2.map_into(), item.topic3.map_into()), - data: BytesRocksdb::from(item.data), - } - } -} - -impl From for Log { - fn from(item: LogRocksdb) -> Self { - Self { - address: item.address.into(), - topic0: item.topics.0.map_into(), - topic1: item.topics.1.map_into(), - topic2: item.topics.2.map_into(), - topic3: item.topics.3.map_into(), - data: item.data.into(), - } - } -} - -pub struct ExecutionRocksdb { - pub block_timestamp: UnixTimeRocksdb, - pub execution_costs_applied: bool, - pub result: ExecutionResultRocksdb, - pub output: BytesRocksdb, - pub logs: Vec, - pub gas: GasRocksdb, - pub deployed_contract_address: Option, -} - -impl From for ExecutionRocksdb { - fn from(item: EvmExecution) -> Self { - Self { - block_timestamp: UnixTimeRocksdb::from(item.block_timestamp), - execution_costs_applied: item.receipt_applied, - result: item.result.into(), - output: BytesRocksdb::from(item.output), - logs: item.logs.into_iter().map(LogRocksdb::from).collect(), - gas: GasRocksdb::from(item.gas), - deployed_contract_address: item.deployed_contract_address.map_into(), - } - } -} - -impl From for EvmExecution { - fn from(item: ExecutionRocksdb) -> Self { - Self { - block_timestamp: item.block_timestamp.into(), - receipt_applied: item.execution_costs_applied, - result: item.result.into(), - output: item.output.into(), - logs: item.logs.into_iter().map(Log::from).collect(), - gas: item.gas.into(), - changes: HashMap::new(), - deployed_contract_address: item.deployed_contract_address.map_into(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct LogMinedRockdb { - pub log: LogRocksdb, - pub transaction_hash: HashRocksdb, - pub transaction_index: IndexRocksdb, - pub log_index: IndexRocksdb, - pub block_number: BlockNumberRocksdb, - pub block_hash: HashRocksdb, -} - -impl From for LogMinedRockdb { - fn from(item: LogMined) -> Self { - Self { - log: item.log.into(), - transaction_hash: item.transaction_hash.into(), - transaction_index: item.transaction_index.into(), - log_index: item.log_index.into(), - block_number: item.block_number.into(), - block_hash: item.block_hash.into(), - } - } -} - -impl From for LogMined { - fn from(item: LogMinedRockdb) -> Self { - Self { - log: item.log.into(), - transaction_hash: item.transaction_hash.into(), - transaction_index: item.transaction_index.into(), - log_index: item.log_index.into(), - block_number: item.block_number.into(), - block_hash: item.block_hash.into(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct TransactionMinedRocksdb { - pub input: TransactionInputRocksdb, - pub execution: EvmExecution, - pub logs: Vec, - pub transaction_index: IndexRocksdb, - pub block_number: BlockNumberRocksdb, - pub block_hash: HashRocksdb, -} - -impl From for TransactionMinedRocksdb { - fn from(item: TransactionMined) -> Self { - Self { - input: item.input.into(), - execution: item.execution, - logs: item.logs.into_iter().map(LogMinedRockdb::from).collect(), - transaction_index: IndexRocksdb::from(item.transaction_index), - block_number: BlockNumberRocksdb::from(item.block_number), - block_hash: HashRocksdb::from(item.block_hash), - } - } -} - -impl From for TransactionMined { - fn from(item: TransactionMinedRocksdb) -> Self { - Self { - input: item.input.into(), - execution: item.execution, - logs: item.logs.into_iter().map(LogMined::from).collect(), - transaction_index: item.transaction_index.into(), - block_number: item.block_number.into(), - block_hash: item.block_hash.into(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct BlockRocksdb { - pub header: BlockHeaderRocksdb, - pub transactions: Vec, -} - -impl From for BlockRocksdb { - fn from(item: Block) -> Self { - BlockRocksdb { - header: BlockHeaderRocksdb { - number: BlockNumberRocksdb::from(item.header.number), - hash: HashRocksdb::from(item.header.hash), - transactions_root: HashRocksdb::from(item.header.transactions_root), - gas_used: item.header.gas_used.into(), - gas_limit: item.header.gas_limit.into(), - bloom: item.header.bloom.into(), - timestamp: item.header.timestamp.into(), - parent_hash: HashRocksdb::from(item.header.parent_hash), - author: AddressRocksdb::from(item.header.author), - extra_data: item.header.extra_data.into(), - miner: AddressRocksdb::from(item.header.miner), - difficulty: item.header.difficulty.into(), - receipts_root: HashRocksdb::from(item.header.receipts_root), - uncle_hash: HashRocksdb::from(item.header.uncle_hash), - size: item.header.size.into(), - state_root: HashRocksdb::from(item.header.state_root), - total_difficulty: item.header.total_difficulty.into(), - nonce: item.header.nonce.into(), - }, - transactions: item.transactions.into_iter().map(TransactionMinedRocksdb::from).collect(), - } - } -} - -impl From for Block { - fn from(item: BlockRocksdb) -> Self { - Block { - header: BlockHeader { - number: BlockNumber::from(item.header.number), - hash: Hash::from(item.header.hash), - transactions_root: Hash::from(item.header.transactions_root), - gas_used: item.header.gas_used.into(), - gas_limit: item.header.gas_limit.into(), - bloom: item.header.bloom.into(), - timestamp: item.header.timestamp.into(), - parent_hash: Hash::from(item.header.parent_hash), - author: Address::from(item.header.author), - extra_data: item.header.extra_data.into(), - miner: Address::from(item.header.miner), - difficulty: item.header.difficulty.into(), - receipts_root: Hash::from(item.header.receipts_root), - uncle_hash: Hash::from(item.header.uncle_hash), - size: item.header.size.into(), - state_root: Hash::from(item.header.state_root), - total_difficulty: item.header.total_difficulty.into(), - nonce: item.header.nonce.into(), - }, - transactions: item.transactions.into_iter().map(TransactionMined::from).collect(), - } - } -} diff --git a/src/eth/storage/rocks/types/account.rs b/src/eth/storage/rocks/types/account.rs new file mode 100644 index 000000000..24e6d7583 --- /dev/null +++ b/src/eth/storage/rocks/types/account.rs @@ -0,0 +1,53 @@ +use std::fmt::Debug; + +use revm::primitives::KECCAK_EMPTY; + +use super::address::AddressRocksdb; +use super::bytes::BytesRocksdb; +use super::nonce::NonceRocksdb; +use super::wei::WeiRocksdb; +use crate::eth::primitives::Account; +use crate::eth::primitives::Address; +use crate::ext::OptionExt; + +#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] +pub struct AccountRocksdb { + pub balance: WeiRocksdb, + pub nonce: NonceRocksdb, + pub bytecode: Option, +} + +impl AccountRocksdb { + pub fn to_account(&self, address: &Address) -> Account { + Account { + address: *address, + nonce: self.nonce.clone().into(), + balance: self.balance.clone().into(), + bytecode: self.bytecode.clone().map_into(), + code_hash: KECCAK_EMPTY.into(), + } + } +} + +impl From for (AddressRocksdb, AccountRocksdb) { + fn from(value: Account) -> Self { + ( + value.address.into(), + AccountRocksdb { + balance: value.balance.into(), + nonce: value.nonce.into(), + bytecode: value.bytecode.map_into(), + }, + ) + } +} + +impl Default for AccountRocksdb { + fn default() -> Self { + Self { + balance: WeiRocksdb::ZERO, + nonce: NonceRocksdb::ZERO, + bytecode: None, + } + } +} diff --git a/src/eth/storage/rocks/types/address.rs b/src/eth/storage/rocks/types/address.rs new file mode 100644 index 000000000..21b9f85aa --- /dev/null +++ b/src/eth/storage/rocks/types/address.rs @@ -0,0 +1,26 @@ +use std::fmt::Debug; + +use ethereum_types::H160; + +use crate::eth::primitives::Address; + +#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +pub struct AddressRocksdb(H160); + +impl AddressRocksdb { + pub fn inner_value(&self) -> H160 { + self.0 + } +} + +impl From
for AddressRocksdb { + fn from(item: Address) -> Self { + AddressRocksdb(item.0) + } +} + +impl From for Address { + fn from(item: AddressRocksdb) -> Self { + Address::new_from_h160(item.inner_value()) + } +} diff --git a/src/eth/storage/rocks/types/block.rs b/src/eth/storage/rocks/types/block.rs new file mode 100644 index 000000000..6a249e1a8 --- /dev/null +++ b/src/eth/storage/rocks/types/block.rs @@ -0,0 +1,75 @@ +use std::fmt::Debug; + +use super::address::AddressRocksdb; +use super::block_header::BlockHeaderRocksdb; +use super::block_number::BlockNumberRocksdb; +use super::hash::HashRocksdb; +use super::transaction_mined::TransactionMinedRocksdb; +use crate::eth::primitives::Address; +use crate::eth::primitives::Block; +use crate::eth::primitives::BlockHeader; +use crate::eth::primitives::BlockNumber; +use crate::eth::primitives::Hash; +use crate::eth::primitives::TransactionMined; + +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct BlockRocksdb { + pub header: BlockHeaderRocksdb, + pub transactions: Vec, +} + +impl From for BlockRocksdb { + fn from(item: Block) -> Self { + BlockRocksdb { + header: BlockHeaderRocksdb { + number: BlockNumberRocksdb::from(item.header.number), + hash: HashRocksdb::from(item.header.hash), + transactions_root: HashRocksdb::from(item.header.transactions_root), + gas_used: item.header.gas_used.into(), + gas_limit: item.header.gas_limit.into(), + bloom: item.header.bloom.into(), + timestamp: item.header.timestamp.into(), + parent_hash: HashRocksdb::from(item.header.parent_hash), + author: AddressRocksdb::from(item.header.author), + extra_data: item.header.extra_data.into(), + miner: AddressRocksdb::from(item.header.miner), + difficulty: item.header.difficulty.into(), + receipts_root: HashRocksdb::from(item.header.receipts_root), + uncle_hash: HashRocksdb::from(item.header.uncle_hash), + size: item.header.size.into(), + state_root: HashRocksdb::from(item.header.state_root), + total_difficulty: item.header.total_difficulty.into(), + nonce: item.header.nonce.into(), + }, + transactions: item.transactions.into_iter().map(TransactionMinedRocksdb::from).collect(), + } + } +} + +impl From for Block { + fn from(item: BlockRocksdb) -> Self { + Block { + header: BlockHeader { + number: BlockNumber::from(item.header.number), + hash: Hash::from(item.header.hash), + transactions_root: Hash::from(item.header.transactions_root), + gas_used: item.header.gas_used.into(), + gas_limit: item.header.gas_limit.into(), + bloom: item.header.bloom.into(), + timestamp: item.header.timestamp.into(), + parent_hash: Hash::from(item.header.parent_hash), + author: Address::from(item.header.author), + extra_data: item.header.extra_data.into(), + miner: Address::from(item.header.miner), + difficulty: item.header.difficulty.into(), + receipts_root: Hash::from(item.header.receipts_root), + uncle_hash: Hash::from(item.header.uncle_hash), + size: item.header.size.into(), + state_root: Hash::from(item.header.state_root), + total_difficulty: item.header.total_difficulty.into(), + nonce: item.header.nonce.into(), + }, + transactions: item.transactions.into_iter().map(TransactionMined::from).collect(), + } + } +} diff --git a/src/eth/storage/rocks/types/block_header.rs b/src/eth/storage/rocks/types/block_header.rs new file mode 100644 index 000000000..c6eacd682 --- /dev/null +++ b/src/eth/storage/rocks/types/block_header.rs @@ -0,0 +1,34 @@ +use std::fmt::Debug; + +use super::address::AddressRocksdb; +use super::block_number::BlockNumberRocksdb; +use super::bytes::BytesRocksdb; +use super::difficulty::DifficultyRocksdb; +use super::gas::GasRocksdb; +use super::hash::HashRocksdb; +use super::logs_bloom::LogsBloomRocksdb; +use super::miner_nonce::MinerNonceRocksdb; +use super::size::SizeRocksdb; +use super::unix_time::UnixTimeRocksdb; + +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct BlockHeaderRocksdb { + pub number: BlockNumberRocksdb, + pub hash: HashRocksdb, + pub transactions_root: HashRocksdb, + pub gas_used: GasRocksdb, + pub gas_limit: GasRocksdb, + pub bloom: LogsBloomRocksdb, + pub timestamp: UnixTimeRocksdb, + pub parent_hash: HashRocksdb, + pub author: AddressRocksdb, + pub extra_data: BytesRocksdb, + pub miner: AddressRocksdb, + pub difficulty: DifficultyRocksdb, + pub receipts_root: HashRocksdb, + pub uncle_hash: HashRocksdb, + pub size: SizeRocksdb, + pub state_root: HashRocksdb, + pub total_difficulty: DifficultyRocksdb, + pub nonce: MinerNonceRocksdb, +} diff --git a/src/eth/storage/rocks/types/block_number.rs b/src/eth/storage/rocks/types/block_number.rs new file mode 100644 index 000000000..0bedd8c23 --- /dev/null +++ b/src/eth/storage/rocks/types/block_number.rs @@ -0,0 +1,34 @@ +use std::fmt::Debug; + +use ethereum_types::U64; + +use crate::eth::primitives::BlockNumber; +use crate::gen_newtype_from; + +#[derive(Debug, derive_more::Display, Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)] +pub struct BlockNumberRocksdb(U64); + +gen_newtype_from!(self = BlockNumberRocksdb, other = u8, u16, u32, u64, U64, usize, i32, i64); +impl BlockNumberRocksdb { + pub fn inner_value(&self) -> U64 { + self.0 + } +} + +impl From for BlockNumberRocksdb { + fn from(item: BlockNumber) -> Self { + BlockNumberRocksdb(item.0) + } +} + +impl From for BlockNumber { + fn from(item: BlockNumberRocksdb) -> Self { + BlockNumber::from(item.inner_value()) + } +} + +impl From for u64 { + fn from(value: BlockNumberRocksdb) -> Self { + value.0.as_u64() + } +} diff --git a/src/eth/storage/rocks/types/bytes.rs b/src/eth/storage/rocks/types/bytes.rs new file mode 100644 index 000000000..00f0a1c05 --- /dev/null +++ b/src/eth/storage/rocks/types/bytes.rs @@ -0,0 +1,44 @@ +use std::fmt::Debug; +use std::fmt::Display; +use std::ops::Deref; + +use crate::eth::primitives::Bytes; + +#[derive(Clone, Default, Eq, PartialEq, fake::Dummy, serde::Serialize, serde::Deserialize)] +pub struct BytesRocksdb(pub Vec); + +impl Deref for BytesRocksdb { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Display for BytesRocksdb { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.len() <= 256 { + write!(f, "{}", const_hex::encode_prefixed(&self.0)) + } else { + write!(f, "too long") + } + } +} + +impl Debug for BytesRocksdb { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("Bytes").field(&self.to_string()).finish() + } +} + +impl From for BytesRocksdb { + fn from(value: Bytes) -> Self { + Self(value.0) + } +} + +impl From for Bytes { + fn from(value: BytesRocksdb) -> Self { + Self(value.0) + } +} diff --git a/src/eth/storage/rocks/types/chain_id.rs b/src/eth/storage/rocks/types/chain_id.rs new file mode 100644 index 000000000..d81a6b005 --- /dev/null +++ b/src/eth/storage/rocks/types/chain_id.rs @@ -0,0 +1,26 @@ +use std::fmt::Debug; + +use ethereum_types::U64; + +use crate::eth::primitives::ChainId; + +#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct ChainIdRocksdb(U64); + +impl ChainIdRocksdb { + pub fn inner_value(&self) -> U64 { + self.0 + } +} + +impl From for ChainIdRocksdb { + fn from(value: ChainId) -> Self { + ChainIdRocksdb(value.0) + } +} + +impl From for ChainId { + fn from(value: ChainIdRocksdb) -> Self { + ChainId::new(value.inner_value()) + } +} diff --git a/src/eth/storage/rocks/types/difficulty.rs b/src/eth/storage/rocks/types/difficulty.rs new file mode 100644 index 000000000..c559e804e --- /dev/null +++ b/src/eth/storage/rocks/types/difficulty.rs @@ -0,0 +1,24 @@ +use std::fmt::Debug; + +use ethereum_types::U256; + +use crate::eth::primitives::Difficulty; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(transparent)] +pub struct DifficultyRocksdb(U256); + +gen_newtype_from!(self = DifficultyRocksdb, other = U256); + +impl From for Difficulty { + fn from(value: DifficultyRocksdb) -> Self { + value.0.into() + } +} + +impl From for DifficultyRocksdb { + fn from(value: Difficulty) -> Self { + U256::from(value).into() + } +} diff --git a/src/eth/storage/rocks/types/execution.rs b/src/eth/storage/rocks/types/execution.rs new file mode 100644 index 000000000..98593e4a2 --- /dev/null +++ b/src/eth/storage/rocks/types/execution.rs @@ -0,0 +1,50 @@ +use std::collections::HashMap; + +use super::address::AddressRocksdb; +use super::bytes::BytesRocksdb; +use super::execution_result::ExecutionResultRocksdb; +use super::gas::GasRocksdb; +use super::log::LogRocksdb; +use super::unix_time::UnixTimeRocksdb; +use crate::eth::primitives::EvmExecution; +use crate::eth::primitives::Log; +use crate::ext::OptionExt; + +pub struct ExecutionRocksdb { + pub block_timestamp: UnixTimeRocksdb, + pub execution_costs_applied: bool, + pub result: ExecutionResultRocksdb, + pub output: BytesRocksdb, + pub logs: Vec, + pub gas: GasRocksdb, + pub deployed_contract_address: Option, +} + +impl From for ExecutionRocksdb { + fn from(item: EvmExecution) -> Self { + Self { + block_timestamp: UnixTimeRocksdb::from(item.block_timestamp), + execution_costs_applied: item.receipt_applied, + result: item.result.into(), + output: BytesRocksdb::from(item.output), + logs: item.logs.into_iter().map(LogRocksdb::from).collect(), + gas: GasRocksdb::from(item.gas), + deployed_contract_address: item.deployed_contract_address.map_into(), + } + } +} + +impl From for EvmExecution { + fn from(item: ExecutionRocksdb) -> Self { + Self { + block_timestamp: item.block_timestamp.into(), + receipt_applied: item.execution_costs_applied, + result: item.result.into(), + output: item.output.into(), + logs: item.logs.into_iter().map(Log::from).collect(), + gas: item.gas.into(), + changes: HashMap::new(), + deployed_contract_address: item.deployed_contract_address.map_into(), + } + } +} diff --git a/src/eth/storage/rocks/types/execution_result.rs b/src/eth/storage/rocks/types/execution_result.rs new file mode 100644 index 000000000..827222d91 --- /dev/null +++ b/src/eth/storage/rocks/types/execution_result.rs @@ -0,0 +1,27 @@ +use crate::eth::primitives::ExecutionResult; + +pub enum ExecutionResultRocksdb { + Success, + Reverted, + Halted { reason: String }, +} + +impl From for ExecutionResultRocksdb { + fn from(item: ExecutionResult) -> Self { + match item { + ExecutionResult::Success => ExecutionResultRocksdb::Success, + ExecutionResult::Reverted => ExecutionResultRocksdb::Reverted, + ExecutionResult::Halted { reason } => ExecutionResultRocksdb::Halted { reason }, + } + } +} + +impl From for ExecutionResult { + fn from(item: ExecutionResultRocksdb) -> Self { + match item { + ExecutionResultRocksdb::Success => ExecutionResult::Success, + ExecutionResultRocksdb::Reverted => ExecutionResult::Reverted, + ExecutionResultRocksdb::Halted { reason } => ExecutionResult::Halted { reason }, + } + } +} diff --git a/src/eth/storage/rocks/types/gas.rs b/src/eth/storage/rocks/types/gas.rs new file mode 100644 index 000000000..43c0b03ab --- /dev/null +++ b/src/eth/storage/rocks/types/gas.rs @@ -0,0 +1,24 @@ +use std::fmt::Debug; + +use ethereum_types::U64; + +use crate::eth::primitives::Gas; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(transparent)] +pub struct GasRocksdb(U64); + +gen_newtype_from!(self = GasRocksdb, other = u64); + +impl From for Gas { + fn from(value: GasRocksdb) -> Self { + value.0.as_u64().into() + } +} + +impl From for GasRocksdb { + fn from(value: Gas) -> Self { + u64::from(value).into() + } +} diff --git a/src/eth/storage/rocks/types/hash.rs b/src/eth/storage/rocks/types/hash.rs new file mode 100644 index 000000000..c2ba199da --- /dev/null +++ b/src/eth/storage/rocks/types/hash.rs @@ -0,0 +1,26 @@ +use std::fmt::Debug; + +use ethereum_types::H256; + +use crate::eth::primitives::Hash; + +#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +pub struct HashRocksdb(H256); + +impl HashRocksdb { + pub fn inner_value(&self) -> H256 { + self.0 + } +} + +impl From for HashRocksdb { + fn from(item: Hash) -> Self { + HashRocksdb(item.0) + } +} + +impl From for Hash { + fn from(item: HashRocksdb) -> Self { + Hash::new_from_h256(item.inner_value()) + } +} diff --git a/src/eth/storage/rocks/types/index.rs b/src/eth/storage/rocks/types/index.rs new file mode 100644 index 000000000..206bf9aa9 --- /dev/null +++ b/src/eth/storage/rocks/types/index.rs @@ -0,0 +1,24 @@ +use std::fmt::Debug; + +use crate::eth::primitives::Index; + +#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize, derive_more::Add, Copy, Hash)] +pub struct IndexRocksdb(u64); + +impl IndexRocksdb { + pub fn inner_value(&self) -> u64 { + self.0 + } +} + +impl From for IndexRocksdb { + fn from(item: Index) -> Self { + IndexRocksdb(item.0) + } +} + +impl From for Index { + fn from(item: IndexRocksdb) -> Self { + Index::new(item.inner_value()) + } +} diff --git a/src/eth/storage/rocks/types/log.rs b/src/eth/storage/rocks/types/log.rs new file mode 100644 index 000000000..b7d099d4e --- /dev/null +++ b/src/eth/storage/rocks/types/log.rs @@ -0,0 +1,38 @@ +use std::fmt::Debug; + +use ethereum_types::H256; + +use super::address::AddressRocksdb; +use super::bytes::BytesRocksdb; +use crate::eth::primitives::Log; +use crate::ext::OptionExt; + +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct LogRocksdb { + pub address: AddressRocksdb, + pub topics: (Option, Option, Option, Option), + pub data: BytesRocksdb, +} + +impl From for LogRocksdb { + fn from(item: Log) -> Self { + Self { + address: AddressRocksdb::from(item.address), + topics: (item.topic0.map_into(), item.topic1.map_into(), item.topic2.map_into(), item.topic3.map_into()), + data: BytesRocksdb::from(item.data), + } + } +} + +impl From for Log { + fn from(item: LogRocksdb) -> Self { + Self { + address: item.address.into(), + topic0: item.topics.0.map_into(), + topic1: item.topics.1.map_into(), + topic2: item.topics.2.map_into(), + topic3: item.topics.3.map_into(), + data: item.data.into(), + } + } +} diff --git a/src/eth/storage/rocks/types/log_mined.rs b/src/eth/storage/rocks/types/log_mined.rs new file mode 100644 index 000000000..fd0254175 --- /dev/null +++ b/src/eth/storage/rocks/types/log_mined.rs @@ -0,0 +1,43 @@ +use std::fmt::Debug; + +use super::block_number::BlockNumberRocksdb; +use super::hash::HashRocksdb; +use super::index::IndexRocksdb; +use super::log::LogRocksdb; +use crate::eth::primitives::LogMined; + +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct LogMinedRockdb { + pub log: LogRocksdb, + pub transaction_hash: HashRocksdb, + pub transaction_index: IndexRocksdb, + pub log_index: IndexRocksdb, + pub block_number: BlockNumberRocksdb, + pub block_hash: HashRocksdb, +} + +impl From for LogMinedRockdb { + fn from(item: LogMined) -> Self { + Self { + log: item.log.into(), + transaction_hash: item.transaction_hash.into(), + transaction_index: item.transaction_index.into(), + log_index: item.log_index.into(), + block_number: item.block_number.into(), + block_hash: item.block_hash.into(), + } + } +} + +impl From for LogMined { + fn from(item: LogMinedRockdb) -> Self { + Self { + log: item.log.into(), + transaction_hash: item.transaction_hash.into(), + transaction_index: item.transaction_index.into(), + log_index: item.log_index.into(), + block_number: item.block_number.into(), + block_hash: item.block_hash.into(), + } + } +} diff --git a/src/eth/storage/rocks/types/logs_bloom.rs b/src/eth/storage/rocks/types/logs_bloom.rs new file mode 100644 index 000000000..c077e9b09 --- /dev/null +++ b/src/eth/storage/rocks/types/logs_bloom.rs @@ -0,0 +1,18 @@ +use std::fmt::Debug; + +use ethereum_types::Bloom; + +use crate::eth::primitives::logs_bloom::LogsBloom; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)] +#[serde(transparent)] +pub struct LogsBloomRocksdb(Bloom); + +gen_newtype_from!(self = LogsBloomRocksdb, other = Bloom, LogsBloom); + +impl From for LogsBloom { + fn from(value: LogsBloomRocksdb) -> Self { + value.0.into() + } +} diff --git a/src/eth/storage/rocks/types/miner_nonce.rs b/src/eth/storage/rocks/types/miner_nonce.rs new file mode 100644 index 000000000..285aff03d --- /dev/null +++ b/src/eth/storage/rocks/types/miner_nonce.rs @@ -0,0 +1,17 @@ +use std::fmt::Debug; + +use ethereum_types::H64; + +use crate::eth::primitives::MinerNonce; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +pub struct MinerNonceRocksdb(H64); + +gen_newtype_from!(self = MinerNonceRocksdb, other = H64, [u8; 8], MinerNonce); + +impl From for MinerNonce { + fn from(value: MinerNonceRocksdb) -> Self { + value.0.into() + } +} diff --git a/src/eth/storage/rocks/types/mod.rs b/src/eth/storage/rocks/types/mod.rs new file mode 100644 index 000000000..16485c4d1 --- /dev/null +++ b/src/eth/storage/rocks/types/mod.rs @@ -0,0 +1,33 @@ +mod account; +mod address; +mod block; +mod block_header; +mod block_number; +mod bytes; +mod chain_id; +mod difficulty; +mod execution; +mod execution_result; +mod gas; +mod hash; +mod index; +mod log; +mod log_mined; +mod logs_bloom; +mod miner_nonce; +mod nonce; +mod size; +mod slot; +mod transaction_input; +mod transaction_mined; +mod unix_time; +mod wei; + +pub use account::AccountRocksdb; +pub use address::AddressRocksdb; +pub use block::BlockRocksdb; +pub use block_number::BlockNumberRocksdb; +pub use hash::HashRocksdb; +pub use index::IndexRocksdb; +pub use slot::SlotIndexRocksdb; +pub use slot::SlotValueRocksdb; diff --git a/src/eth/storage/rocks/types/nonce.rs b/src/eth/storage/rocks/types/nonce.rs new file mode 100644 index 000000000..58b4949a7 --- /dev/null +++ b/src/eth/storage/rocks/types/nonce.rs @@ -0,0 +1,27 @@ +use std::fmt::Debug; + +use ethereum_types::U64; + +use crate::eth::primitives::Nonce; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct NonceRocksdb(U64); + +gen_newtype_from!(self = NonceRocksdb, other = u64); + +impl From for Nonce { + fn from(value: NonceRocksdb) -> Self { + value.0.as_u64().into() + } +} + +impl From for NonceRocksdb { + fn from(value: Nonce) -> Self { + u64::from(value).into() + } +} + +impl NonceRocksdb { + pub const ZERO: NonceRocksdb = NonceRocksdb(U64::zero()); +} diff --git a/src/eth/storage/rocks/types/size.rs b/src/eth/storage/rocks/types/size.rs new file mode 100644 index 000000000..f84f0e69c --- /dev/null +++ b/src/eth/storage/rocks/types/size.rs @@ -0,0 +1,24 @@ +use std::fmt::Debug; + +use ethereum_types::U64; + +use crate::eth::primitives::Size; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(transparent)] +pub struct SizeRocksdb(U64); + +gen_newtype_from!(self = SizeRocksdb, other = U64, u64); + +impl From for SizeRocksdb { + fn from(value: Size) -> Self { + u64::from(value).into() + } +} + +impl From for Size { + fn from(value: SizeRocksdb) -> Self { + value.0.as_u64().into() + } +} diff --git a/src/eth/storage/rocks/types/slot.rs b/src/eth/storage/rocks/types/slot.rs new file mode 100644 index 000000000..1ff14c628 --- /dev/null +++ b/src/eth/storage/rocks/types/slot.rs @@ -0,0 +1,51 @@ +use std::fmt::Debug; + +use ethereum_types::U256; + +use crate::eth::primitives::SlotIndex; +use crate::eth::primitives::SlotValue; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct SlotValueRocksdb(U256); + +impl SlotValueRocksdb { + pub fn inner_value(&self) -> U256 { + self.0 + } +} + +impl From for SlotValueRocksdb { + fn from(item: SlotValue) -> Self { + SlotValueRocksdb(item.as_u256()) + } +} + +impl From for SlotValue { + fn from(item: SlotValueRocksdb) -> Self { + SlotValue::from(item.inner_value()) + } +} + +#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)] +pub struct SlotIndexRocksdb(U256); + +gen_newtype_from!(self = SlotIndexRocksdb, other = u64); + +impl SlotIndexRocksdb { + pub fn inner_value(&self) -> U256 { + self.0 + } +} + +impl From for SlotIndexRocksdb { + fn from(item: SlotIndex) -> Self { + SlotIndexRocksdb(item.as_u256()) + } +} + +impl From for SlotIndex { + fn from(item: SlotIndexRocksdb) -> Self { + SlotIndex::from(item.inner_value()) + } +} diff --git a/src/eth/storage/rocks/types/transaction_input.rs b/src/eth/storage/rocks/types/transaction_input.rs new file mode 100644 index 000000000..f5230e547 --- /dev/null +++ b/src/eth/storage/rocks/types/transaction_input.rs @@ -0,0 +1,72 @@ +use std::fmt::Debug; + +use ethereum_types::U256; +use ethereum_types::U64; + +use super::address::AddressRocksdb; +use super::bytes::BytesRocksdb; +use super::chain_id::ChainIdRocksdb; +use super::gas::GasRocksdb; +use super::hash::HashRocksdb; +use super::nonce::NonceRocksdb; +use super::wei::WeiRocksdb; +use crate::eth::primitives::TransactionInput; +use crate::ext::OptionExt; + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct TransactionInputRocksdb { + pub chain_id: Option, + pub hash: HashRocksdb, + pub nonce: NonceRocksdb, + pub signer: AddressRocksdb, + pub from: AddressRocksdb, + pub to: Option, + pub value: WeiRocksdb, + pub input: BytesRocksdb, + pub gas_limit: GasRocksdb, + pub gas_price: WeiRocksdb, + pub v: U64, + pub r: U256, + pub s: U256, +} + +impl From for TransactionInputRocksdb { + fn from(item: TransactionInput) -> Self { + Self { + chain_id: item.chain_id.map_into(), + hash: HashRocksdb::from(item.hash), + nonce: NonceRocksdb::from(item.nonce), + signer: AddressRocksdb::from(item.signer), + from: AddressRocksdb::from(item.from), + to: item.to.map(AddressRocksdb::from), + value: WeiRocksdb::from(item.value), + input: BytesRocksdb::from(item.input), + gas_limit: GasRocksdb::from(item.gas_limit), + gas_price: WeiRocksdb::from(item.gas_price), + v: item.v, + r: item.r, + s: item.s, + } + } +} + +impl From for TransactionInput { + fn from(item: TransactionInputRocksdb) -> Self { + Self { + chain_id: item.chain_id.map_into(), + hash: item.hash.into(), + nonce: item.nonce.into(), + signer: item.signer.into(), + from: item.from.into(), + to: item.to.map(Into::into), + value: item.value.into(), + input: item.input.into(), + gas_limit: item.gas_limit.into(), + gas_price: item.gas_price.into(), + v: item.v, + r: item.r, + s: item.s, + tx_type: None, + } + } +} diff --git a/src/eth/storage/rocks/types/transaction_mined.rs b/src/eth/storage/rocks/types/transaction_mined.rs new file mode 100644 index 000000000..fc1ca1f06 --- /dev/null +++ b/src/eth/storage/rocks/types/transaction_mined.rs @@ -0,0 +1,46 @@ +use std::fmt::Debug; + +use super::block_number::BlockNumberRocksdb; +use super::hash::HashRocksdb; +use super::index::IndexRocksdb; +use super::log_mined::LogMinedRockdb; +use super::transaction_input::TransactionInputRocksdb; +use crate::eth::primitives::EvmExecution; +use crate::eth::primitives::LogMined; +use crate::eth::primitives::TransactionMined; + +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct TransactionMinedRocksdb { + pub input: TransactionInputRocksdb, + pub execution: EvmExecution, + pub logs: Vec, + pub transaction_index: IndexRocksdb, + pub block_number: BlockNumberRocksdb, + pub block_hash: HashRocksdb, +} + +impl From for TransactionMinedRocksdb { + fn from(item: TransactionMined) -> Self { + Self { + input: item.input.into(), + execution: item.execution, + logs: item.logs.into_iter().map(LogMinedRockdb::from).collect(), + transaction_index: IndexRocksdb::from(item.transaction_index), + block_number: BlockNumberRocksdb::from(item.block_number), + block_hash: HashRocksdb::from(item.block_hash), + } + } +} + +impl From for TransactionMined { + fn from(item: TransactionMinedRocksdb) -> Self { + Self { + input: item.input.into(), + execution: item.execution, + logs: item.logs.into_iter().map(LogMined::from).collect(), + transaction_index: item.transaction_index.into(), + block_number: item.block_number.into(), + block_hash: item.block_hash.into(), + } + } +} diff --git a/src/eth/storage/rocks/types/unix_time.rs b/src/eth/storage/rocks/types/unix_time.rs new file mode 100644 index 000000000..5ce7deab1 --- /dev/null +++ b/src/eth/storage/rocks/types/unix_time.rs @@ -0,0 +1,21 @@ +use std::fmt::Debug; + +use crate::eth::primitives::UnixTime; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct UnixTimeRocksdb(u64); + +gen_newtype_from!(self = UnixTimeRocksdb, other = u64); + +impl From for UnixTimeRocksdb { + fn from(value: UnixTime) -> Self { + Self(*value) + } +} + +impl From for UnixTime { + fn from(value: UnixTimeRocksdb) -> Self { + value.0.into() + } +} diff --git a/src/eth/storage/rocks/types/wei.rs b/src/eth/storage/rocks/types/wei.rs new file mode 100644 index 000000000..3cfd184cc --- /dev/null +++ b/src/eth/storage/rocks/types/wei.rs @@ -0,0 +1,28 @@ +use std::fmt::Debug; + +use ethereum_types::U256; + +use crate::eth::primitives::Wei; +use crate::gen_newtype_from; + +#[derive(Debug, Clone, Default, Eq, PartialEq, derive_more::Add, derive_more::Sub, serde::Serialize, serde::Deserialize)] +pub struct WeiRocksdb(U256); + +gen_newtype_from!(self = WeiRocksdb, other = U256); + +impl From for Wei { + fn from(value: WeiRocksdb) -> Self { + value.0.into() + } +} + +impl From for WeiRocksdb { + fn from(value: Wei) -> Self { + U256::from(value).into() + } +} + +impl WeiRocksdb { + pub const ZERO: WeiRocksdb = WeiRocksdb(U256::zero()); + pub const ONE: WeiRocksdb = WeiRocksdb(U256::one()); +}