diff --git a/src/eth/primitives/slot.rs b/src/eth/primitives/slot.rs index 91b585c2e..3f83ab2ef 100644 --- a/src/eth/primitives/slot.rs +++ b/src/eth/primitives/slot.rs @@ -227,6 +227,14 @@ impl SlotValue { pub fn as_u256(&self) -> U256 { self.0 } + + pub fn new(value: U256) -> Self { + SlotValue(value) + } + + pub fn inner_value(&self) -> U256 { + self.0 + } } impl FromStr for SlotValue { diff --git a/src/eth/storage/rocks/rocks_permanent.rs b/src/eth/storage/rocks/rocks_permanent.rs index cf4708ba0..5fd45b99d 100644 --- a/src/eth/storage/rocks/rocks_permanent.rs +++ b/src/eth/storage/rocks/rocks_permanent.rs @@ -27,7 +27,7 @@ use crate::eth::primitives::SlotSample; use crate::eth::primitives::SlotValue; use crate::eth::primitives::StoragePointInTime; use crate::eth::primitives::TransactionMined; -use crate::eth::storage::rocks::rocks_state::AccountInfo; +use crate::eth::storage::rocks::rocks_state::AccountRocksdb; use crate::eth::storage::PermanentStorage; use crate::eth::storage::StorageError; @@ -85,9 +85,9 @@ impl RocksPermanentStorage { for (slot_index, slot_change) in &change.slots { if let Some(value) = state.account_slots.get(&(address.clone(), slot_index.clone())) { if let Some(original_slot) = slot_change.take_original_ref() { - let account_slot_value = value.clone(); - if original_slot.value != account_slot_value { - conflicts.add_slot(address.clone(), slot_index.clone(), account_slot_value, original_slot.value.clone()); + let account_slot_value: SlotValue = value.clone().into(); + if original_slot.value != account_slot_value.clone() { + conflicts.add_slot(address.clone(), slot_index.clone(), account_slot_value.clone(), original_slot.value.clone()); } } } @@ -232,7 +232,7 @@ impl PermanentStorage for RocksPermanentStorage { for account in accounts { self.state.accounts.insert( account.address.clone(), - AccountInfo { + AccountRocksdb { balance: account.balance.clone(), nonce: account.nonce.clone(), bytecode: account.bytecode.clone(), @@ -242,7 +242,7 @@ impl PermanentStorage for RocksPermanentStorage { self.state.accounts_history.insert( (account.address.clone(), 0.into()), - AccountInfo { + AccountRocksdb { balance: account.balance.clone(), nonce: account.nonce.clone(), bytecode: account.bytecode.clone(), diff --git a/src/eth/storage/rocks/rocks_state.rs b/src/eth/storage/rocks/rocks_state.rs index 30f34fa2d..96346b13a 100644 --- a/src/eth/storage/rocks/rocks_state.rs +++ b/src/eth/storage/rocks/rocks_state.rs @@ -3,6 +3,7 @@ use std::sync::atomic::AtomicU64; use std::sync::Arc; use anyhow::anyhow; +use ethereum_types::U256; use futures::future::join_all; use itertools::Itertools; use num_traits::cast::ToPrimitive; @@ -37,14 +38,14 @@ use crate::eth::storage::rocks_db::RocksDb; use crate::log_and_err; #[derive(Debug, serde::Serialize, serde::Deserialize, Clone)] -pub struct AccountInfo { +pub struct AccountRocksdb { pub balance: Wei, pub nonce: Nonce, pub bytecode: Option, pub code_hash: CodeHash, } -impl AccountInfo { +impl AccountRocksdb { pub fn to_account(&self, address: &Address) -> Account { Account { address: address.clone(), @@ -58,11 +59,32 @@ impl AccountInfo { } } +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct SlotValueRocksdb(U256); + +impl SlotValueRocksdb { + pub fn inner_value(&self) -> U256 { + self.0.clone() + } +} + +impl From for SlotValueRocksdb { + fn from(item: SlotValue) -> Self { + SlotValueRocksdb(item.inner_value()) + } +} + +impl From for SlotValue { + fn from(item: SlotValueRocksdb) -> Self { + SlotValue::new(item.inner_value()) + } +} + pub struct RocksStorageState { - pub accounts: Arc>, - pub accounts_history: Arc>, - pub account_slots: Arc>, - pub account_slots_history: Arc>, + pub accounts: Arc>, + pub accounts_history: Arc>, + pub account_slots: Arc>, + pub account_slots_history: Arc>, pub transactions: Arc>, pub blocks_by_number: Arc>, pub blocks_by_hash: Arc>, @@ -98,10 +120,10 @@ impl RocksStorageState { } pub fn listen_for_backup_trigger(&self, rx: mpsc::Receiver<()>) -> anyhow::Result<()> { - let accounts = Arc::>::clone(&self.accounts); - let accounts_history = Arc::>::clone(&self.accounts_history); - let account_slots = Arc::>::clone(&self.account_slots); - let account_slots_history = Arc::>::clone(&self.account_slots_history); + let accounts = Arc::>::clone(&self.accounts); + let accounts_history = Arc::>::clone(&self.accounts_history); + let account_slots = Arc::>::clone(&self.account_slots); + let account_slots_history = Arc::>::clone(&self.account_slots_history); let blocks_by_hash = Arc::>::clone(&self.blocks_by_hash); let blocks_by_number = Arc::>::clone(&self.blocks_by_number); let transactions = Arc::>::clone(&self.transactions); @@ -381,7 +403,7 @@ impl RocksStorageState { let account_changes_future = tokio::task::spawn_blocking(move || { for change in changes_clone_for_accounts { let address = change.address.clone(); - let mut account_info_entry = accounts.entry_or_insert_with(address.clone(), || AccountInfo { + let mut account_info_entry = accounts.entry_or_insert_with(address.clone(), || AccountRocksdb { balance: Wei::ZERO, // Initialize with default values nonce: Nonce::ZERO, bytecode: None, @@ -413,8 +435,8 @@ impl RocksStorageState { let address = change.address.clone(); for (slot_index, slot_change) in change.slots.clone() { if let Some(slot) = slot_change.take_modified() { - slot_changes.push(((address.clone(), slot_index.clone()), slot.value.clone())); - slot_history_changes.push(((address.clone(), slot_index, block_number), slot.value)); + slot_changes.push(((address.clone(), slot_index.clone()), slot.value.clone().into())); + slot_history_changes.push(((address.clone(), slot_index, block_number), slot.value.into())); } } } @@ -473,7 +495,7 @@ impl RocksStorageState { match point_in_time { StoragePointInTime::Present => self.account_slots.get(&(address.clone(), index.clone())).map(|account_slot_value| Slot { index: index.clone(), - value: account_slot_value.clone(), + value: account_slot_value.clone().into(), }), StoragePointInTime::Past(number) => { if let Some(((rocks_address, rocks_index, _), value)) = self @@ -482,7 +504,10 @@ impl RocksStorageState { .next() { if index == &rocks_index && address == &rocks_address { - return Some(Slot { index: rocks_index, value }); + return Some(Slot { + index: rocks_index, + value: value.into(), + }); } } None @@ -546,7 +571,7 @@ impl RocksStorageState { for account in accounts { account_batch.push(( account.address, - AccountInfo { + AccountRocksdb { balance: account.balance, nonce: account.nonce, bytecode: account.bytecode, @@ -563,7 +588,7 @@ impl RocksStorageState { let mut slot_batch = vec![]; for (address, slot) in slots { - slot_batch.push(((address, slot.index), slot.value)); + slot_batch.push(((address, slot.index), slot.value.into())); } self.account_slots.insert_batch(slot_batch, Some(block_number.into())); }