Skip to content

Commit

Permalink
refac: don't use references with Copy types (#1875)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19-cw authored Nov 19, 2024
1 parent 5e14bea commit 70bb3c1
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 144 deletions.
2 changes: 1 addition & 1 deletion src/bin/importer_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rpc_downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn download_balances(rpc_storage: Arc<dyn ExternalRpcStorage>, 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?;
}

Expand Down
6 changes: 3 additions & 3 deletions src/eth/executor/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) {
Expand Down
6 changes: 3 additions & 3 deletions src/eth/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 });
};
Expand Down
2 changes: 1 addition & 1 deletion src/eth/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub fn block_from_local(pending_header: PendingBlockHeader, txs: Vec<LocalTransa

// calculate transactions hash
if not(block.transactions.is_empty()) {
let transactions_hashes: Vec<&Hash> = block.transactions.iter().map(|x| &x.input.hash).collect();
let transactions_hashes: Vec<Hash> = block.transactions.iter().map(|x| x.input.hash).collect();
block.header.transactions_root = triehash::ordered_trie_root::<KeccakHasher, _>(transactions_hashes).into();
}

Expand Down
12 changes: 6 additions & 6 deletions src/eth/primitives/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/eth/primitives/external_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct ExternalReceipts(HashMap<Hash, ExternalReceipt>);

impl ExternalReceipts {
/// Tries to remove a receipt by its hash.
pub fn try_remove(&mut self, tx_hash: &Hash) -> anyhow::Result<ExternalReceipt> {
match self.0.remove(tx_hash) {
pub fn try_remove(&mut self, tx_hash: Hash) -> anyhow::Result<ExternalReceipt> {
match self.0.remove(&tx_hash) {
Some(receipt) => Ok(receipt),
None => {
tracing::error!(%tx_hash, "receipt is missing for hash");
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/log_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/eth/primitives/log_filter_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/eth/primitives/log_mined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/eth/primitives/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn evm_set_next_block_timestamp(params: Params<'_>, ctx: Arc<RpcContext>, _: Ext
use crate::log_and_err;

let (_, timestamp) = next_rpc_param::<UnixTime>(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")?,
Expand Down Expand Up @@ -679,7 +679,7 @@ fn eth_get_block_by_selector<const KIND: char>(params: Params<'_>, ctx: Arc<RpcC
tracing::info!(%filter, %full_transactions, "reading block");

// execute
let block = ctx.storage.read_block(&filter)?;
let block = ctx.storage.read_block(filter)?;
Span::with(|s| {
s.record("found", block.is_some());
if let Some(ref block) = block {
Expand Down Expand Up @@ -723,7 +723,7 @@ fn eth_get_transaction_by_hash(params: Params<'_>, ctx: Arc<RpcContext>, 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());
});
Expand Down Expand Up @@ -753,7 +753,7 @@ fn eth_get_transaction_receipt(params: Params<'_>, ctx: Arc<RpcContext>, 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());
});
Expand Down Expand Up @@ -828,7 +828,7 @@ fn eth_call(params: Params<'_>, ctx: Arc<RpcContext>, 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() => {
Expand Down Expand Up @@ -937,7 +937,7 @@ fn eth_get_logs(params: Params<'_>, ctx: Arc<RpcContext>, 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| {
Expand Down Expand Up @@ -985,8 +985,8 @@ fn eth_get_transaction_count(params: Params<'_>, ctx: Arc<RpcContext>, 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))
}

Expand All @@ -1007,8 +1007,8 @@ fn eth_get_balance(params: Params<'_>, ctx: Arc<RpcContext>, 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))
}

Expand All @@ -1028,8 +1028,8 @@ fn eth_get_code(params: Params<'_>, ctx: Arc<RpcContext>, 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))
}
Expand Down Expand Up @@ -1115,8 +1115,8 @@ fn eth_get_storage_at(params: Params<'_>, ctx: Arc<RpcContext>, 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()))
Expand Down
6 changes: 3 additions & 3 deletions src/eth/storage/inmemory/inmemory_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ where
}

/// Returns the value at the given point in time.
pub fn get_at_point(&self, point_in_time: &StoragePointInTime) -> Option<T> {
pub fn get_at_point(&self, point_in_time: StoragePointInTime) -> Option<T> {
match point_in_time {
StoragePointInTime::Mined | StoragePointInTime::Pending => Some(self.get_current()),
StoragePointInTime::MinedPast(block_number) => self.get_at_block(block_number),
}
}

/// Returns the most recent value before or at the given block number.
pub fn get_at_block(&self, block_number: &BlockNumber) -> Option<T> {
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<T> {
self.0.iter().take_while(|x| x.block_number <= block_number).map(|x| &x.value).last().cloned()
}

/// Returns the most recent value.
Expand Down
24 changes: 12 additions & 12 deletions src/eth/storage/inmemory/inmemory_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ impl PermanentStorage for InMemoryPermanentStorage {
// State operations
// -------------------------------------------------------------------------

fn read_account(&self, address: &Address, point_in_time: &StoragePointInTime) -> anyhow::Result<Option<Account>> {
fn read_account(&self, address: Address, point_in_time: StoragePointInTime) -> anyhow::Result<Option<Account>> {
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))
Expand All @@ -113,14 +113,14 @@ impl PermanentStorage for InMemoryPermanentStorage {
}
}

fn read_slot(&self, address: &Address, index: &SlotIndex, point_in_time: &StoragePointInTime) -> anyhow::Result<Option<Slot>> {
fn read_slot(&self, address: Address, index: SlotIndex, point_in_time: StoragePointInTime) -> anyhow::Result<Option<Slot>> {
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))
Expand All @@ -129,24 +129,24 @@ impl PermanentStorage for InMemoryPermanentStorage {
}
}

fn read_block(&self, selection: &BlockFilter) -> anyhow::Result<Option<Block>> {
fn read_block(&self, selection: BlockFilter) -> anyhow::Result<Option<Block>> {
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())),
None => Ok(None),
}
}

fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>> {
fn read_transaction(&self, hash: Hash) -> anyhow::Result<Option<TransactionMined>> {
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<Vec<LogMined>> {
Expand Down Expand Up @@ -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(),
Expand Down
Loading

0 comments on commit 70bb3c1

Please sign in to comment.