From 70bb3c16da9c936e53f706cf0b6be8aa14f78d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos?= <164224824+marcospb19-cw@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:16:46 -0300 Subject: [PATCH] refac: don't use references with `Copy` types (#1875) --- src/bin/importer_offline.rs | 2 +- src/bin/rpc_downloader.rs | 2 +- src/eth/executor/evm.rs | 6 +-- src/eth/executor/executor.rs | 6 +-- src/eth/miner/miner.rs | 2 +- src/eth/primitives/block.rs | 12 ++--- src/eth/primitives/block_number.rs | 2 +- src/eth/primitives/external_receipts.rs | 4 +- src/eth/primitives/log_filter.rs | 2 +- src/eth/primitives/log_filter_input.rs | 6 +-- src/eth/primitives/log_mined.rs | 4 +- src/eth/primitives/transaction_execution.rs | 6 +-- src/eth/rpc/rpc_server.rs | 28 +++++------ src/eth/storage/inmemory/inmemory_history.rs | 6 +-- .../storage/inmemory/inmemory_permanent.rs | 24 +++++----- .../storage/inmemory/inmemory_temporary.rs | 46 +++++++++---------- src/eth/storage/permanent_storage.rs | 8 ++-- src/eth/storage/redis/redis_permanent.rs | 36 +++++++-------- src/eth/storage/rocks/rocks_permanent.rs | 8 ++-- src/eth/storage/rocks/rocks_state.rs | 34 +++++++------- src/eth/storage/rocks/types/account.rs | 4 +- src/eth/storage/storage_point_in_time.rs | 4 +- src/eth/storage/stratus_storage.rs | 28 +++++------ src/eth/storage/temporary_storage.rs | 6 +-- .../blockchain_client/blockchain_client.rs | 2 +- 25 files changed, 144 insertions(+), 144 deletions(-) diff --git a/src/bin/importer_offline.rs b/src/bin/importer_offline.rs index c10b4153c..9398deadd 100644 --- a/src/bin/importer_offline.rs +++ b/src/bin/importer_offline.rs @@ -135,7 +135,7 @@ fn execute_block_importer( tracing::info!(parent: None, %block_start, %block_end, %receipts_len, "reexecuting blocks"); // ensure block range have no gaps - if block_start.count_to(&block_end) != batch_blocks_len as u64 { + if block_start.count_to(block_end) != batch_blocks_len as u64 { let message = GlobalState::shutdown_from(TASK_NAME, "received block range with gaps to reexecute"); return log_and_err!(message); } diff --git a/src/bin/rpc_downloader.rs b/src/bin/rpc_downloader.rs index 4821eb774..412e24f1f 100644 --- a/src/bin/rpc_downloader.rs +++ b/src/bin/rpc_downloader.rs @@ -82,7 +82,7 @@ async fn download_balances(rpc_storage: Arc, chain: &Blo // download missing balances for address in address_to_download { - let balance = chain.fetch_balance(&address, Some(BlockNumber::ZERO)).await?; + let balance = chain.fetch_balance(address, Some(BlockNumber::ZERO)).await?; rpc_storage.save_initial_account(address, balance).await?; } diff --git a/src/eth/executor/evm.rs b/src/eth/executor/evm.rs index 081d9660d..771d8c7b6 100644 --- a/src/eth/executor/evm.rs +++ b/src/eth/executor/evm.rs @@ -174,7 +174,7 @@ impl Evm { // track metrics #[cfg(feature = "metrics")] { - metrics::inc_evm_execution(start.elapsed(), &session_point_in_time, execution.is_ok()); + metrics::inc_evm_execution(start.elapsed(), session_point_in_time, execution.is_ok()); metrics::inc_evm_execution_account_reads(session_metrics.account_reads); } @@ -235,7 +235,7 @@ impl Database for RevmSession { // retrieve account let address: Address = revm_address.into(); - let account = self.storage.read_account(&address, &self.input.point_in_time)?; + let account = self.storage.read_account(address, self.input.point_in_time)?; // warn if the loaded account is the `to` account and it does not have a bytecode if let Some(ref to_address) = self.input.to { @@ -272,7 +272,7 @@ impl Database for RevmSession { let index: SlotIndex = revm_index.into(); // load slot from storage - let slot = self.storage.read_slot(&address, &index, &self.input.point_in_time)?; + let slot = self.storage.read_slot(address, index, self.input.point_in_time)?; // track original value, except if ignored address if not(address.is_ignored()) { diff --git a/src/eth/executor/executor.rs b/src/eth/executor/executor.rs index 1432c39a7..e7d9f94f7 100644 --- a/src/eth/executor/executor.rs +++ b/src/eth/executor/executor.rs @@ -247,7 +247,7 @@ impl Executor { // determine how to execute each transaction for tx in block_transactions { - let receipt = receipts.try_remove(&tx.hash())?; + let receipt = receipts.try_remove(tx.hash())?; self.execute_external_transaction( tx, receipt, @@ -329,7 +329,7 @@ impl Executor { // // failed external transaction, re-create from receipt without re-executing false => { - let sender = self.storage.read_account(&receipt.from.into(), &StoragePointInTime::Pending)?; + let sender = self.storage.read_account(receipt.from.into(), StoragePointInTime::Pending)?; let execution = EvmExecution::from_failed_external_transaction(sender, &receipt, block_timestamp)?; let evm_result = EvmExecutionResult { execution, @@ -548,7 +548,7 @@ impl Executor { let pending_header = self.storage.read_pending_block_header()?.unwrap_or_default(); let mined_block = match point_in_time { StoragePointInTime::MinedPast(number) => { - let Some(block) = self.storage.read_block(&BlockFilter::Number(number))? else { + let Some(block) = self.storage.read_block(BlockFilter::Number(number))? else { let filter = BlockFilter::Number(number); return Err(StratusError::RpcBlockFilterInvalid { filter }); }; diff --git a/src/eth/miner/miner.rs b/src/eth/miner/miner.rs index 3aa877487..a091b5913 100644 --- a/src/eth/miner/miner.rs +++ b/src/eth/miner/miner.rs @@ -421,7 +421,7 @@ pub fn block_from_local(pending_header: PendingBlockHeader, txs: Vec = block.transactions.iter().map(|x| &x.input.hash).collect(); + let transactions_hashes: Vec = block.transactions.iter().map(|x| x.input.hash).collect(); block.header.transactions_root = triehash::ordered_trie_root::(transactions_hashes).into(); } diff --git a/src/eth/primitives/block.rs b/src/eth/primitives/block.rs index e64d071d6..f26f2ec10 100644 --- a/src/eth/primitives/block.rs +++ b/src/eth/primitives/block.rs @@ -130,8 +130,8 @@ impl Block { .entry(transaction_changes.address) .or_insert(transaction_changes.clone()); - if let Some(nonce) = transaction_changes.nonce.take_modified_ref() { - account_compacted_changes.nonce.set_modified(*nonce); + if let Some(&nonce) = transaction_changes.nonce.take_modified_ref() { + account_compacted_changes.nonce.set_modified(nonce); } if let Some(balance) = transaction_changes.balance.take_modified_ref() { @@ -142,10 +142,10 @@ impl Block { account_compacted_changes.bytecode.set_modified(bytecode.clone()); } - for (slot_index, slot) in &transaction_changes.slots { - let slot_compacted_changes = account_compacted_changes.slots.entry(*slot_index).or_insert(slot.clone()); - if let Some(slot_value) = slot.take_modified_ref() { - slot_compacted_changes.set_modified(*slot_value); + for (&slot_index, slot) in &transaction_changes.slots { + let slot_compacted_changes = account_compacted_changes.slots.entry(slot_index).or_insert(slot.clone()); + if let Some(&slot_value) = slot.take_modified_ref() { + slot_compacted_changes.set_modified(slot_value); } } } diff --git a/src/eth/primitives/block_number.rs b/src/eth/primitives/block_number.rs index 9b5c8e7a1..5002b169c 100644 --- a/src/eth/primitives/block_number.rs +++ b/src/eth/primitives/block_number.rs @@ -69,7 +69,7 @@ impl BlockNumber { /// Count how many blocks there is between itself and the othe block. /// /// Assumes that self is the lower-end of the range. - pub fn count_to(&self, higher_end: &BlockNumber) -> u64 { + pub fn count_to(self, higher_end: BlockNumber) -> u64 { if higher_end >= self { higher_end.as_u64() - self.as_u64() + 1 } else { diff --git a/src/eth/primitives/external_receipts.rs b/src/eth/primitives/external_receipts.rs index d2a1a084d..7e33cdfc3 100644 --- a/src/eth/primitives/external_receipts.rs +++ b/src/eth/primitives/external_receipts.rs @@ -11,8 +11,8 @@ pub struct ExternalReceipts(HashMap); impl ExternalReceipts { /// Tries to remove a receipt by its hash. - pub fn try_remove(&mut self, tx_hash: &Hash) -> anyhow::Result { - match self.0.remove(tx_hash) { + pub fn try_remove(&mut self, tx_hash: Hash) -> anyhow::Result { + match self.0.remove(&tx_hash) { Some(receipt) => Ok(receipt), None => { tracing::error!(%tx_hash, "receipt is missing for hash"); diff --git a/src/eth/primitives/log_filter.rs b/src/eth/primitives/log_filter.rs index 58d78554b..56999851c 100644 --- a/src/eth/primitives/log_filter.rs +++ b/src/eth/primitives/log_filter.rs @@ -32,7 +32,7 @@ impl LogFilter { // filter address let has_addresses = not(self.addresses.is_empty()); - if has_addresses && not(self.addresses.contains(log.address())) { + if has_addresses && not(self.addresses.contains(&log.address())) { return false; } diff --git a/src/eth/primitives/log_filter_input.rs b/src/eth/primitives/log_filter_input.rs index 66af1c77f..199327501 100644 --- a/src/eth/primitives/log_filter_input.rs +++ b/src/eth/primitives/log_filter_input.rs @@ -48,12 +48,12 @@ impl LogFilterInput { // parse point-in-time let (from, to) = match self.block_hash { Some(hash) => { - let from_to = storage.translate_to_point_in_time(&BlockFilter::Hash(hash))?; + let from_to = storage.translate_to_point_in_time(BlockFilter::Hash(hash))?; (from_to, from_to) } None => { - let from = storage.translate_to_point_in_time(&self.from_block.unwrap_or(BlockFilter::Latest))?; - let to = storage.translate_to_point_in_time(&self.to_block.unwrap_or(BlockFilter::Latest))?; + let from = storage.translate_to_point_in_time(self.from_block.unwrap_or(BlockFilter::Latest))?; + let to = storage.translate_to_point_in_time(self.to_block.unwrap_or(BlockFilter::Latest))?; (from, to) } }; diff --git a/src/eth/primitives/log_mined.rs b/src/eth/primitives/log_mined.rs index 2e4e16ca4..1c136b3a3 100644 --- a/src/eth/primitives/log_mined.rs +++ b/src/eth/primitives/log_mined.rs @@ -36,8 +36,8 @@ pub struct LogMined { impl LogMined { /// Returns the address that emitted the log. - pub fn address(&self) -> &Address { - &self.log.address + pub fn address(&self) -> Address { + self.log.address } /// Returns all non-empty topics in the log. diff --git a/src/eth/primitives/transaction_execution.rs b/src/eth/primitives/transaction_execution.rs index 3c26824ca..352696224 100644 --- a/src/eth/primitives/transaction_execution.rs +++ b/src/eth/primitives/transaction_execution.rs @@ -73,10 +73,10 @@ impl TransactionExecution { } /// Returns the EVM execution metrics. - pub fn metrics(&self) -> &EvmExecutionMetrics { + pub fn metrics(&self) -> EvmExecutionMetrics { match self { - Self::Local(LocalTransactionExecution { result, .. }) => &result.metrics, - Self::External(ExternalTransactionExecution { evm_execution: result, .. }) => &result.metrics, + Self::Local(LocalTransactionExecution { result, .. }) => result.metrics, + Self::External(ExternalTransactionExecution { evm_execution: result, .. }) => result.metrics, } } } diff --git a/src/eth/rpc/rpc_server.rs b/src/eth/rpc/rpc_server.rs index 7fd9d9604..926d103ad 100644 --- a/src/eth/rpc/rpc_server.rs +++ b/src/eth/rpc/rpc_server.rs @@ -267,7 +267,7 @@ fn evm_set_next_block_timestamp(params: Params<'_>, ctx: Arc, _: Ext use crate::log_and_err; let (_, timestamp) = next_rpc_param::(params.sequence())?; - let latest = ctx.storage.read_block(&BlockFilter::Latest)?; + let latest = ctx.storage.read_block(BlockFilter::Latest)?; match latest { Some(block) => UnixTime::set_offset(block.header.timestamp, timestamp)?, None => return log_and_err!("reading latest block returned None")?, @@ -679,7 +679,7 @@ fn eth_get_block_by_selector(params: Params<'_>, ctx: Arc, ctx: Arc, ext: &E tracing::info!(%tx_hash, "reading transaction"); // execute - let tx = ctx.storage.read_transaction(&tx_hash)?; + let tx = ctx.storage.read_transaction(tx_hash)?; Span::with(|s| { s.record("found", tx.is_some()); }); @@ -753,7 +753,7 @@ fn eth_get_transaction_receipt(params: Params<'_>, ctx: Arc, ext: &E tracing::info!(%tx_hash, "reading transaction receipt"); // execute - let tx = ctx.storage.read_transaction(&tx_hash)?; + let tx = ctx.storage.read_transaction(tx_hash)?; Span::with(|s| { s.record("found", tx.is_some()); }); @@ -828,7 +828,7 @@ fn eth_call(params: Params<'_>, ctx: Arc, ext: &Extensions) -> Resul tracing::info!(%filter, "executing eth_call"); // execute - let point_in_time = ctx.storage.translate_to_point_in_time(&filter)?; + let point_in_time = ctx.storage.translate_to_point_in_time(filter)?; match ctx.executor.execute_local_call(call, point_in_time) { // result is success Ok(result) if result.is_success() => { @@ -937,7 +937,7 @@ fn eth_get_logs(params: Params<'_>, ctx: Arc, ext: &Extensions) -> R block } }; - let blocks_in_range = filter.from_block.count_to(&to_block); + let blocks_in_range = filter.from_block.count_to(to_block); // track Span::with(|s| { @@ -985,8 +985,8 @@ fn eth_get_transaction_count(params: Params<'_>, ctx: Arc, ext: &Ext }); tracing::info!(%address, %filter, "reading account nonce"); - let point_in_time = ctx.storage.translate_to_point_in_time(&filter)?; - let account = ctx.storage.read_account(&address, &point_in_time)?; + let point_in_time = ctx.storage.translate_to_point_in_time(filter)?; + let account = ctx.storage.read_account(address, point_in_time)?; Ok(hex_num(account.nonce)) } @@ -1007,8 +1007,8 @@ fn eth_get_balance(params: Params<'_>, ctx: Arc, ext: &Extensions) - tracing::info!(%address, %filter, "reading account native balance"); // execute - let point_in_time = ctx.storage.translate_to_point_in_time(&filter)?; - let account = ctx.storage.read_account(&address, &point_in_time)?; + let point_in_time = ctx.storage.translate_to_point_in_time(filter)?; + let account = ctx.storage.read_account(address, point_in_time)?; Ok(hex_num(account.balance)) } @@ -1028,8 +1028,8 @@ fn eth_get_code(params: Params<'_>, ctx: Arc, ext: &Extensions) -> R }); // execute - let point_in_time = ctx.storage.translate_to_point_in_time(&filter)?; - let account = ctx.storage.read_account(&address, &point_in_time)?; + let point_in_time = ctx.storage.translate_to_point_in_time(filter)?; + let account = ctx.storage.read_account(address, point_in_time)?; Ok(account.bytecode.map(hex_data).unwrap_or_else(hex_null)) } @@ -1115,8 +1115,8 @@ fn eth_get_storage_at(params: Params<'_>, ctx: Arc, ext: &Extensions }); // execute - let point_in_time = ctx.storage.translate_to_point_in_time(&block_filter)?; - let slot = ctx.storage.read_slot(&address, &index, &point_in_time)?; + let point_in_time = ctx.storage.translate_to_point_in_time(block_filter)?; + let slot = ctx.storage.read_slot(address, index, point_in_time)?; // It must be padded, even if it is zero. Ok(hex_num_zero_padded(slot.value.as_u256())) diff --git a/src/eth/storage/inmemory/inmemory_history.rs b/src/eth/storage/inmemory/inmemory_history.rs index dc3d4c14c..1621cd0e9 100644 --- a/src/eth/storage/inmemory/inmemory_history.rs +++ b/src/eth/storage/inmemory/inmemory_history.rs @@ -45,7 +45,7 @@ where } /// Returns the value at the given point in time. - pub fn get_at_point(&self, point_in_time: &StoragePointInTime) -> Option { + pub fn get_at_point(&self, point_in_time: StoragePointInTime) -> Option { match point_in_time { StoragePointInTime::Mined | StoragePointInTime::Pending => Some(self.get_current()), StoragePointInTime::MinedPast(block_number) => self.get_at_block(block_number), @@ -53,8 +53,8 @@ where } /// Returns the most recent value before or at the given block number. - pub fn get_at_block(&self, block_number: &BlockNumber) -> Option { - self.0.iter().take_while(|x| x.block_number <= *block_number).map(|x| &x.value).last().cloned() + pub fn get_at_block(&self, block_number: BlockNumber) -> Option { + self.0.iter().take_while(|x| x.block_number <= block_number).map(|x| &x.value).last().cloned() } /// Returns the most recent value. diff --git a/src/eth/storage/inmemory/inmemory_permanent.rs b/src/eth/storage/inmemory/inmemory_permanent.rs index fac95449b..d2690c2e9 100644 --- a/src/eth/storage/inmemory/inmemory_permanent.rs +++ b/src/eth/storage/inmemory/inmemory_permanent.rs @@ -101,10 +101,10 @@ impl PermanentStorage for InMemoryPermanentStorage { // State operations // ------------------------------------------------------------------------- - fn read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> anyhow::Result> { + fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> anyhow::Result> { let state = self.lock_read(); - match state.accounts.get(address) { + match state.accounts.get(&address) { Some(inmemory_account) => { let account = inmemory_account.to_account(point_in_time); Ok(Some(account)) @@ -113,14 +113,14 @@ impl PermanentStorage for InMemoryPermanentStorage { } } - fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result> { + fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> anyhow::Result> { let state = self.lock_read(); - let Some(account) = state.accounts.get(address) else { + let Some(account) = state.accounts.get(&address) else { return Ok(None); }; - match account.slots.get(index) { + match account.slots.get(&index) { Some(slot_history) => { let slot = slot_history.get_at_point(point_in_time).unwrap_or_default(); Ok(Some(slot)) @@ -129,13 +129,13 @@ impl PermanentStorage for InMemoryPermanentStorage { } } - fn read_block(&self, selection: &BlockFilter) -> anyhow::Result> { + fn read_block(&self, selection: BlockFilter) -> anyhow::Result> { let state_lock = self.lock_read(); let block = match selection { BlockFilter::Latest | BlockFilter::Pending => state_lock.blocks_by_number.values().last().cloned(), BlockFilter::Earliest => state_lock.blocks_by_number.values().next().cloned(), - BlockFilter::Number(block_number) => state_lock.blocks_by_number.get(block_number).cloned(), - BlockFilter::Hash(block_hash) => state_lock.blocks_by_hash.get(block_hash).cloned(), + BlockFilter::Number(block_number) => state_lock.blocks_by_number.get(&block_number).cloned(), + BlockFilter::Hash(block_hash) => state_lock.blocks_by_hash.get(&block_hash).cloned(), }; match block { Some(block) => Ok(Some((*block).clone())), @@ -143,10 +143,10 @@ impl PermanentStorage for InMemoryPermanentStorage { } } - fn read_transaction(&self, hash: &Hash) -> anyhow::Result> { + fn read_transaction(&self, hash: Hash) -> anyhow::Result> { let state_lock = self.lock_read(); - let Some(block) = state_lock.transactions.get(hash) else { return Ok(None) }; - Ok(block.transactions.iter().find(|tx| &tx.input.hash == hash).cloned()) + let Some(block) = state_lock.transactions.get(&hash) else { return Ok(None) }; + Ok(block.transactions.iter().find(|tx| tx.input.hash == hash).cloned()) } fn read_logs(&self, filter: &LogFilter) -> anyhow::Result> { @@ -278,7 +278,7 @@ impl InMemoryPermanentAccount { } /// Converts itself to an account at a point-in-time. - pub fn to_account(&self, point_in_time: &StoragePointInTime) -> Account { + pub fn to_account(&self, point_in_time: StoragePointInTime) -> Account { Account { address: self.address, balance: self.balance.get_at_point(point_in_time).unwrap_or_default(), diff --git a/src/eth/storage/inmemory/inmemory_temporary.rs b/src/eth/storage/inmemory/inmemory_temporary.rs index 7a7da2fb2..8c826a045 100644 --- a/src/eth/storage/inmemory/inmemory_temporary.rs +++ b/src/eth/storage/inmemory/inmemory_temporary.rs @@ -158,8 +158,8 @@ impl TemporaryStorage for InMemoryTemporaryStorage { .or_insert_with(|| InMemoryTemporaryAccount::new(change.address)); // account basic info - if let Some(nonce) = change.nonce.take_ref() { - account.info.nonce = *nonce; + if let Some(&nonce) = change.nonce.take_ref() { + account.info.nonce = nonce; } if let Some(balance) = change.balance.take_ref() { account.info.balance = *balance; @@ -172,8 +172,8 @@ impl TemporaryStorage for InMemoryTemporaryStorage { // slots for slot in change.slots.values() { - if let Some(slot) = slot.take_ref() { - account.slots.insert(slot.index, *slot); + if let Some(&slot) = slot.take_ref() { + account.slots.insert(slot.index, slot); } } } @@ -223,10 +223,10 @@ impl TemporaryStorage for InMemoryTemporaryStorage { Ok(finished_block) } - fn read_pending_execution(&self, hash: &Hash) -> anyhow::Result> { + fn read_pending_execution(&self, hash: Hash) -> anyhow::Result> { let states = self.lock_read(); let Some(ref pending_block) = states.head.block else { return Ok(None) }; - match pending_block.transactions.get(hash) { + match pending_block.transactions.get(&hash) { Some(tx) => Ok(Some(tx.clone())), None => Ok(None), } @@ -236,12 +236,12 @@ impl TemporaryStorage for InMemoryTemporaryStorage { // Accounts and Slots // ------------------------------------------------------------------------- - fn read_account(&self, address: &Address) -> anyhow::Result> { + fn read_account(&self, address: Address) -> anyhow::Result> { let states = self.lock_read(); Ok(do_read_account(&states, address)) } - fn read_slot(&self, address: &Address, index: &SlotIndex) -> anyhow::Result> { + fn read_slot(&self, address: Address, index: SlotIndex) -> anyhow::Result> { let states = self.lock_read(); Ok(do_read_slot(&states, address, index)) } @@ -260,10 +260,10 @@ impl TemporaryStorage for InMemoryTemporaryStorage { // ----------------------------------------------------------------------------- // Implementations without lock // ----------------------------------------------------------------------------- -fn do_read_account(states: &NonEmpty, address: &Address) -> Option { +fn do_read_account(states: &NonEmpty, address: Address) -> Option { // search all for state in states.iter() { - let Some(account) = state.accounts.get(address) else { continue }; + let Some(account) = state.accounts.get(&address) else { continue }; let info = account.info.clone(); let account = Account { @@ -283,14 +283,14 @@ fn do_read_account(states: &NonEmpty, address: &A None } -fn do_read_slot(states: &NonEmpty, address: &Address, index: &SlotIndex) -> Option { +fn do_read_slot(states: &NonEmpty, address: Address, index: SlotIndex) -> Option { // search all for state in states.iter() { - let Some(account) = state.accounts.get(address) else { continue }; - let Some(slot) = account.slots.get(index) else { continue }; + let Some(account) = state.accounts.get(&address) else { continue }; + let Some(&slot) = account.slots.get(&index) else { continue }; tracing::trace!(%address, %index, %slot, "slot found in temporary"); - return Some(*slot); + return Some(slot); } // not found @@ -301,31 +301,31 @@ fn do_read_slot(states: &NonEmpty, address: &Addr fn do_check_conflicts(states: &NonEmpty, execution: &EvmExecution) -> Option { let mut conflicts = ExecutionConflictsBuilder::default(); - for (address, change) in &execution.changes { + for (&address, change) in &execution.changes { // check account info conflicts if let Some(account) = do_read_account(states, address) { - if let Some(expected) = change.nonce.take_original_ref() { - let original = &account.nonce; + if let Some(&expected) = change.nonce.take_original_ref() { + let original = account.nonce; if expected != original { - conflicts.add_nonce(*address, *original, *expected); + conflicts.add_nonce(address, original, expected); } } - if let Some(expected) = change.balance.take_original_ref() { - let original = &account.balance; + if let Some(&expected) = change.balance.take_original_ref() { + let original = account.balance; if expected != original { - conflicts.add_balance(*address, *original, *expected); + conflicts.add_balance(address, original, expected); } } } // check slots conflicts - for (slot_index, slot_change) in &change.slots { + for (&slot_index, slot_change) in &change.slots { if let Some(expected) = slot_change.take_original_ref() { let Some(original) = do_read_slot(states, address, slot_index) else { continue; }; if expected.value != original.value { - conflicts.add_slot(*address, *slot_index, original.value, expected.value); + conflicts.add_slot(address, slot_index, original.value, expected.value); } } } diff --git a/src/eth/storage/permanent_storage.rs b/src/eth/storage/permanent_storage.rs index 1e15e0584..b810f9d90 100644 --- a/src/eth/storage/permanent_storage.rs +++ b/src/eth/storage/permanent_storage.rs @@ -43,10 +43,10 @@ pub trait PermanentStorage: Send + Sync + 'static { fn save_block(&self, block: Block) -> anyhow::Result<()>; /// Retrieves a block from the storage. - fn read_block(&self, block_filter: &BlockFilter) -> anyhow::Result>; + fn read_block(&self, block_filter: BlockFilter) -> anyhow::Result>; /// Retrieves a transaction from the storage. - fn read_transaction(&self, hash: &Hash) -> anyhow::Result>; + fn read_transaction(&self, hash: Hash) -> anyhow::Result>; /// Retrieves logs from the storage. fn read_logs(&self, filter: &LogFilter) -> anyhow::Result>; @@ -59,10 +59,10 @@ pub trait PermanentStorage: Send + Sync + 'static { fn save_accounts(&self, accounts: Vec) -> anyhow::Result<()>; /// Retrieves an account from the storage. Returns Option when not found. - fn read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> anyhow::Result>; + fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> anyhow::Result>; /// Retrieves an slot from the storage. Returns Option when not found. - fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result>; + fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> anyhow::Result>; // ------------------------------------------------------------------------- // Global state diff --git a/src/eth/storage/redis/redis_permanent.rs b/src/eth/storage/redis/redis_permanent.rs index 2b6085b93..9f3dd6096 100644 --- a/src/eth/storage/redis/redis_permanent.rs +++ b/src/eth/storage/redis/redis_permanent.rs @@ -79,7 +79,7 @@ impl PermanentStorage for RedisPermanentStorage { fn save_block(&self, block: Block) -> anyhow::Result<()> { // generate block keys let key_block_number = key_block_by_number(block.number()); - let key_block_hash = key_block_by_hash(&block.hash()); + let key_block_hash = key_block_by_hash(block.hash()); // generate values let block_json = to_json_string(&block); @@ -94,7 +94,7 @@ impl PermanentStorage for RedisPermanentStorage { // transactions for tx in &block.transactions { - let tx_key = key_tx(&tx.input.hash); + let tx_key = key_tx(tx.input.hash); let tx_value = to_json_string(&tx); mset_values.push((tx_key, tx_value)); } @@ -122,8 +122,8 @@ impl PermanentStorage for RedisPermanentStorage { account_value.insert("block".to_owned(), to_json_value(block.number())); let account_value = to_json_string(&account_value); - mset_values.push((key_account(&account.address), account_value.clone())); - zadd_values.push((key_account_history(&account.address), account_value, block.number().as_u64())); + mset_values.push((key_account(account.address), account_value.clone())); + zadd_values.push((key_account_history(account.address), account_value, block.number().as_u64())); } // slots @@ -134,8 +134,8 @@ impl PermanentStorage for RedisPermanentStorage { slot_value.as_object_mut().unwrap().insert("block".to_owned(), to_json_value(block.number())); let slot_value = to_json_string(&slot_value); - mset_values.push((key_slot(&changes.address, &slot.index), slot_value.clone())); - zadd_values.push((key_slot_history(&changes.address, &slot.index), slot_value, block.number().as_u64())); + mset_values.push((key_slot(changes.address, slot.index), slot_value.clone())); + zadd_values.push((key_slot_history(changes.address, slot.index), slot_value, block.number().as_u64())); } } } @@ -161,13 +161,13 @@ impl PermanentStorage for RedisPermanentStorage { Ok(()) } - fn read_block(&self, block_filter: &BlockFilter) -> anyhow::Result> { + fn read_block(&self, block_filter: BlockFilter) -> anyhow::Result> { // prepare keys let block_key = match block_filter { BlockFilter::Latest | BlockFilter::Pending => "block::latest".to_owned(), BlockFilter::Earliest => "block::earliest".to_owned(), BlockFilter::Hash(hash) => key_block_by_hash(hash), - BlockFilter::Number(number) => key_block_by_number(*number), + BlockFilter::Number(number) => key_block_by_number(number), }; // execute command @@ -182,7 +182,7 @@ impl PermanentStorage for RedisPermanentStorage { } } - fn read_transaction(&self, hash: &Hash) -> anyhow::Result> { + fn read_transaction(&self, hash: Hash) -> anyhow::Result> { // prepare keys let tx_key = key_tx(hash); @@ -243,7 +243,7 @@ impl PermanentStorage for RedisPermanentStorage { let redis_accounts = accounts .into_iter() .map(|acc| { - let account_key = key_account(&acc.address); + let account_key = key_account(acc.address); let account_value = to_json_string(&acc); (account_key, account_value) }) @@ -260,7 +260,7 @@ impl PermanentStorage for RedisPermanentStorage { } } - fn read_account(&self, address: &Address, point_in_time: &crate::eth::storage::StoragePointInTime) -> anyhow::Result> { + fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> anyhow::Result> { let mut conn = self.conn()?; match point_in_time { StoragePointInTime::Mined | StoragePointInTime::Pending => { @@ -305,7 +305,7 @@ impl PermanentStorage for RedisPermanentStorage { } } - fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result> { + fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> anyhow::Result> { // execute command and parse let mut conn = self.conn()?; match point_in_time { @@ -372,31 +372,31 @@ fn key_block_by_number(number: impl Into) -> String { } /// Generates a key for accessing a block by hash. -fn key_block_by_hash(hash: &Hash) -> String { +fn key_block_by_hash(hash: Hash) -> String { format!("block::hash::{}", hash) } /// Generates a key for accessing an account. -fn key_account(address: &Address) -> String { +fn key_account(address: Address) -> String { format!("account::{}", address) } /// Generates a key for accessing an account history. -fn key_account_history(address: &Address) -> String { +fn key_account_history(address: Address) -> String { format!("account_history::{}", address) } /// Generates a key for accessing a slot. -fn key_slot(address: &Address, index: &SlotIndex) -> String { +fn key_slot(address: Address, index: SlotIndex) -> String { format!("slot::{}::{}", address, index) } /// Generates a key for accessing a slot history. -fn key_slot_history(address: &Address, index: &SlotIndex) -> String { +fn key_slot_history(address: Address, index: SlotIndex) -> String { format!("slot_history::{}::{}", address, index) } /// Generates a key for accessing a transaction. -fn key_tx(hash: &Hash) -> String { +fn key_tx(hash: Hash) -> String { format!("tx::{}", hash) } diff --git a/src/eth/storage/rocks/rocks_permanent.rs b/src/eth/storage/rocks/rocks_permanent.rs index 33b7e7d5f..11c7aa87d 100644 --- a/src/eth/storage/rocks/rocks_permanent.rs +++ b/src/eth/storage/rocks/rocks_permanent.rs @@ -85,19 +85,19 @@ impl PermanentStorage for RocksPermanentStorage { // State operations // ------------------------------------------------------------------------- - fn read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> anyhow::Result> { + fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> anyhow::Result> { self.state.read_account(address, point_in_time).inspect_err(|e| { tracing::error!(reason = ?e, "failed to read account in RocksPermanent"); }) } - fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result> { + fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> anyhow::Result> { self.state.read_slot(address, index, point_in_time).inspect_err(|e| { tracing::error!(reason = ?e, "failed to read slot in RocksPermanent"); }) } - fn read_block(&self, selection: &BlockFilter) -> anyhow::Result> { + fn read_block(&self, selection: BlockFilter) -> anyhow::Result> { let block = self.state.read_block(selection).inspect_err(|e| { tracing::error!(reason = ?e, "failed to read block in RocksPermanent"); }); @@ -107,7 +107,7 @@ impl PermanentStorage for RocksPermanentStorage { block } - fn read_transaction(&self, hash: &Hash) -> anyhow::Result> { + fn read_transaction(&self, hash: Hash) -> anyhow::Result> { self.state.read_transaction(hash).inspect_err(|e| { tracing::error!(reason = ?e, "failed to read transaction in RocksPermanent"); }) diff --git a/src/eth/storage/rocks/rocks_state.rs b/src/eth/storage/rocks/rocks_state.rs index 98696b3ca..2be891fef 100644 --- a/src/eth/storage/rocks/rocks_state.rs +++ b/src/eth/storage/rocks/rocks_state.rs @@ -232,9 +232,9 @@ impl RocksStorageState { .prepare_batch_insertion([((address, block_number), account_info_entry.into_inner().into())], batch)?; } - for (slot_index, slot_change) in &change.slots { + for (&slot_index, slot_change) in &change.slots { if let Some(slot) = slot_change.take_modified_ref() { - let slot_index = (*slot_index).into(); + let slot_index = slot_index.into(); let slot_value: SlotValueRocksdb = slot.value.into(); self.account_slots @@ -248,8 +248,8 @@ impl RocksStorageState { Ok(()) } - pub fn read_transaction(&self, tx_hash: &Hash) -> Result> { - let Some(block_number) = self.transactions.get(&(*tx_hash).into())? else { + pub fn read_transaction(&self, tx_hash: Hash) -> Result> { + let Some(block_number) = self.transactions.get(&tx_hash.into())? else { return Ok(None); }; @@ -258,7 +258,7 @@ impl RocksStorageState { .with_context(|| format!("block_number = {:?} tx_hash = {}", block_number, tx_hash)); }; - let transaction = block.into_inner().transactions.into_iter().find(|tx| &Hash::from(tx.input.hash) == tx_hash); + let transaction = block.into_inner().transactions.into_iter().find(|tx| Hash::from(tx.input.hash) == tx_hash); match transaction { Some(tx) => { @@ -302,26 +302,26 @@ impl RocksStorageState { Ok(logs_result) } - pub fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> Result> { + pub fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> Result> { if address.is_coinbase() { return Ok(None); } match point_in_time { StoragePointInTime::Mined | StoragePointInTime::Pending => { - let query_params = ((*address).into(), (*index).into()); + let query_params = (address.into(), index.into()); let Some(account_slot_value) = self.account_slots.get(&query_params)? else { return Ok(None); }; Ok(Some(Slot { - index: *index, + index, value: account_slot_value.into_inner().into(), })) } StoragePointInTime::MinedPast(number) => { - let iterator_start = ((*address).into(), (*index).into(), (*number).into()); + let iterator_start = (address.into(), (index).into(), number.into()); if let Some(((rocks_address, rocks_index, _), value)) = self .account_slots_history @@ -329,7 +329,7 @@ impl RocksStorageState { .next() .transpose()? { - if rocks_index == (*index).into() && rocks_address == (*address).into() { + if rocks_index == (index).into() && rocks_address == address.into() { return Ok(Some(Slot { index: rocks_index.into(), value: value.into_inner().into(), @@ -341,14 +341,14 @@ impl RocksStorageState { } } - pub fn read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> Result> { + pub fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> Result> { if address.is_coinbase() || address.is_zero() { return Ok(None); } match point_in_time { StoragePointInTime::Mined | StoragePointInTime::Pending => { - let Some(inner_account) = self.accounts.get(&((*address).into()))? else { + let Some(inner_account) = self.accounts.get(&address.into())? else { tracing::trace!(%address, "account not found"); return Ok(None); }; @@ -358,11 +358,11 @@ impl RocksStorageState { Ok(Some(account)) } StoragePointInTime::MinedPast(block_number) => { - let iterator_start = ((*address).into(), (*block_number).into()); + let iterator_start = (address.into(), block_number.into()); if let Some(next) = self.accounts_history.iter_from(iterator_start, rocksdb::Direction::Reverse)?.next() { let ((addr, _), account_info) = next?; - if addr == (*address).into() { + if addr == address.into() { return Ok(Some(account_info.to_account(address))); } } @@ -371,15 +371,15 @@ impl RocksStorageState { } } - pub fn read_block(&self, selection: &BlockFilter) -> Result> { + pub fn read_block(&self, selection: BlockFilter) -> Result> { tracing::debug!(?selection, "reading block"); let block = match selection { BlockFilter::Latest | BlockFilter::Pending => self.blocks_by_number.last_value(), BlockFilter::Earliest => self.blocks_by_number.first_value(), - BlockFilter::Number(block_number) => self.blocks_by_number.get(&(*block_number).into()), + BlockFilter::Number(block_number) => self.blocks_by_number.get(&block_number.into()), BlockFilter::Hash(block_hash) => - if let Some(block_number) = self.blocks_by_hash.get(&(*block_hash).into())? { + if let Some(block_number) = self.blocks_by_hash.get(&block_hash.into())? { self.blocks_by_number.get(&block_number) } else { Ok(None) diff --git a/src/eth/storage/rocks/types/account.rs b/src/eth/storage/rocks/types/account.rs index 8d4c940fa..f33fccac8 100644 --- a/src/eth/storage/rocks/types/account.rs +++ b/src/eth/storage/rocks/types/account.rs @@ -18,9 +18,9 @@ pub struct AccountRocksdb { } impl AccountRocksdb { - pub fn to_account(&self, address: &Address) -> Account { + pub fn to_account(&self, address: Address) -> Account { Account { - address: *address, + address, nonce: self.nonce.clone().into(), balance: self.balance.clone().into(), bytecode: self.bytecode.clone().map_into(), diff --git a/src/eth/storage/storage_point_in_time.rs b/src/eth/storage/storage_point_in_time.rs index d8e102409..5689ce0b2 100644 --- a/src/eth/storage/storage_point_in_time.rs +++ b/src/eth/storage/storage_point_in_time.rs @@ -23,8 +23,8 @@ pub enum StoragePointInTime { // ----------------------------------------------------------------------------- // Conversions: Self -> Other // ----------------------------------------------------------------------------- -impl From<&StoragePointInTime> for MetricLabelValue { - fn from(value: &StoragePointInTime) -> Self { +impl From for MetricLabelValue { + fn from(value: StoragePointInTime) -> Self { Self::Some(value.to_string()) } } diff --git a/src/eth/storage/stratus_storage.rs b/src/eth/storage/stratus_storage.rs index a98fbcc40..113cf0fea 100644 --- a/src/eth/storage/stratus_storage.rs +++ b/src/eth/storage/stratus_storage.rs @@ -54,7 +54,7 @@ impl StratusStorage { // create genesis block and accounts if necessary #[cfg(feature = "dev")] { - let genesis = this.read_block(&crate::eth::primitives::BlockFilter::Number(crate::eth::primitives::BlockNumber::ZERO))?; + let genesis = this.read_block(BlockFilter::Number(BlockNumber::ZERO))?; if genesis.is_none() { this.reset_to_genesis()?; } @@ -74,7 +74,7 @@ impl StratusStorage { let _span = tracing::info_span!("storage::read_block_number_to_resume_import").entered(); // if does not have the zero block present, should resume from zero - let zero = self.read_block(&BlockFilter::Number(BlockNumber::ZERO))?; + let zero = self.read_block(BlockFilter::Number(BlockNumber::ZERO))?; if zero.is_none() { tracing::info!(block_number = %0, reason = %"block ZERO does not exist", "resume from ZERO"); return Ok(BlockNumber::ZERO); @@ -89,7 +89,7 @@ impl StratusStorage { // fallback to last mined block number let mined_number = self.read_mined_block_number()?; - let mined_block = self.read_block(&BlockFilter::Number(mined_number))?; + let mined_block = self.read_block(BlockFilter::Number(mined_number))?; match mined_block { Some(_) => { tracing::info!(block_number = %mined_number, reason = %"set in storage and block exist", "resume from MINED + 1"); @@ -190,7 +190,7 @@ impl StratusStorage { // keep only accounts that does not exist in permanent storage let mut missing_accounts = Vec::new(); for account in accounts { - let perm_account = self.perm.read_account(&account.address, &StoragePointInTime::Mined)?; + let perm_account = self.perm.read_account(account.address, StoragePointInTime::Mined)?; if perm_account.is_none() { missing_accounts.push(account); } @@ -207,7 +207,7 @@ impl StratusStorage { .map_err(Into::into) } - pub fn read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> Result { + pub fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> Result { #[cfg(feature = "tracing")] let _span = tracing::debug_span!("storage::read_account", %address, %point_in_time).entered(); @@ -241,12 +241,12 @@ impl StratusStorage { } None => { tracing::debug!(storage = %label::PERM, %address, "account not found, assuming default value"); - Ok(Account::new_empty(*address)) + Ok(Account::new_empty(address)) } } } - pub fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> Result { + pub fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> Result { #[cfg(feature = "tracing")] let _span = tracing::debug_span!("storage::read_slot", %address, %index, %point_in_time).entered(); @@ -280,7 +280,7 @@ impl StratusStorage { } None => { tracing::debug!(storage = %label::PERM, %address, %index, "slot not found, assuming default value"); - Ok(Slot::new_empty(*index)) + Ok(Slot::new_empty(index)) } } } @@ -359,7 +359,7 @@ impl StratusStorage { } // check mined block - let existing_block = self.read_block(&BlockFilter::Number(block_number))?; + let existing_block = self.read_block(BlockFilter::Number(block_number))?; if existing_block.is_some() { tracing::error!(%block_number, %mined_number, "failed to save block because block with the same number already exists in the permanent storage"); return Err(StratusError::StorageBlockConflict { number: block_number }); @@ -377,7 +377,7 @@ impl StratusStorage { .map_err(Into::into) } - pub fn read_block(&self, filter: &BlockFilter) -> Result, StratusError> { + pub fn read_block(&self, filter: BlockFilter) -> Result, StratusError> { #[cfg(feature = "tracing")] let _span = tracing::info_span!("storage::read_block", %filter).entered(); tracing::debug!(storage = %label::PERM, ?filter, "reading block"); @@ -392,7 +392,7 @@ impl StratusStorage { .map_err(Into::into) } - pub fn read_transaction(&self, tx_hash: &Hash) -> Result, StratusError> { + pub fn read_transaction(&self, tx_hash: Hash) -> Result, StratusError> { #[cfg(feature = "tracing")] let _span = tracing::info_span!("storage::read_transaction", %tx_hash).entered(); @@ -489,15 +489,15 @@ impl StratusStorage { // ------------------------------------------------------------------------- /// Translates a block filter to a specific storage point-in-time indicator. - pub fn translate_to_point_in_time(&self, block_filter: &BlockFilter) -> Result { + pub fn translate_to_point_in_time(&self, block_filter: BlockFilter) -> Result { match block_filter { BlockFilter::Pending => Ok(StoragePointInTime::Pending), BlockFilter::Latest => Ok(StoragePointInTime::Mined), BlockFilter::Earliest => Ok(StoragePointInTime::MinedPast(BlockNumber::ZERO)), - BlockFilter::Number(number) => Ok(StoragePointInTime::MinedPast(*number)), + BlockFilter::Number(number) => Ok(StoragePointInTime::MinedPast(number)), BlockFilter::Hash(_) => match self.read_block(block_filter)? { Some(block) => Ok(StoragePointInTime::MinedPast(block.header.number)), - None => Err(StratusError::RpcBlockFilterInvalid { filter: *block_filter }), + None => Err(StratusError::RpcBlockFilterInvalid { filter: block_filter }), }, } } diff --git a/src/eth/storage/temporary_storage.rs b/src/eth/storage/temporary_storage.rs index 3494dc795..8f16bfa7a 100644 --- a/src/eth/storage/temporary_storage.rs +++ b/src/eth/storage/temporary_storage.rs @@ -42,17 +42,17 @@ pub trait TemporaryStorage: Send + Sync + 'static { fn read_pending_executions(&self) -> Vec; /// Retrieves a single transaction execution from the pending block. - fn read_pending_execution(&self, hash: &Hash) -> anyhow::Result>; + fn read_pending_execution(&self, hash: Hash) -> anyhow::Result>; // ------------------------------------------------------------------------- // Accounts and slots // ------------------------------------------------------------------------- /// Retrieves an account from the storage. Returns Option when not found. - fn read_account(&self, address: &Address) -> anyhow::Result>; + fn read_account(&self, address: Address) -> anyhow::Result>; /// Retrieves an slot from the storage. Returns Option when not found. - fn read_slot(&self, address: &Address, index: &SlotIndex) -> anyhow::Result>; + fn read_slot(&self, address: Address, index: SlotIndex) -> anyhow::Result>; // ------------------------------------------------------------------------- // Global state diff --git a/src/infra/blockchain_client/blockchain_client.rs b/src/infra/blockchain_client/blockchain_client.rs index ac433a593..909d2d2cf 100644 --- a/src/infra/blockchain_client/blockchain_client.rs +++ b/src/infra/blockchain_client/blockchain_client.rs @@ -185,7 +185,7 @@ impl BlockchainClient { } /// Fetches account balance by address and block number. - pub async fn fetch_balance(&self, address: &Address, block_number: Option) -> anyhow::Result { + pub async fn fetch_balance(&self, address: Address, block_number: Option) -> anyhow::Result { tracing::debug!(%address, block_number = %block_number.or_empty(), "fetching account balance"); let address = to_json_value(address);