Skip to content

Commit

Permalink
refactor: remove unnecessary inner methods by exposing the newtypes i…
Browse files Browse the repository at this point in the history
…nner value where necessary (#1510)
  • Loading branch information
dinhani-cw authored Jul 22, 2024
1 parent 58be5b5 commit d0cef05
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/eth/consensus/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl AppendEntryService for AppendEntryServiceImpl {
Status::internal("Failed to get pending transactions")
})?;

let transaction_executions: Vec<LocalTransactionExecution> = pending_transactions.iter().filter_map(|tx| tx.inner_local()).collect();
let transaction_executions: Vec<LocalTransactionExecution> = pending_transactions.into_iter().filter_map(|tx| tx.as_local()).collect();

//TODO move this logic into BlockMiner and add safety checks as consensus is or isnt on follower mode there
let block_result = block_from_propagation(block_entry.clone(), transaction_executions);
Expand Down
2 changes: 1 addition & 1 deletion src/eth/primitives/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl From<&Account> for RevmAccountInfo {
Self {
nonce: value.nonce.into(),
balance: value.balance.into(),
code_hash: value.code_hash.inner().0.into(),
code_hash: value.code_hash.0 .0.into(),
code: value.bytecode.as_ref().cloned().map_into(),
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/eth/primitives/code_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::gen_newtype_from;
/// In the case of an externally-owned account (EOA), bytecode is null
/// and the code hash is fixed as the keccak256 hash of an empty string
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CodeHash(H256);
pub struct CodeHash(pub H256);

impl Dummy<Faker> for CodeHash {
fn dummy_with_rng<R: ethers_core::rand::prelude::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
Expand All @@ -31,10 +31,6 @@ impl CodeHash {
None => CodeHash::default(),
}
}

pub fn inner(&self) -> H256 {
self.0
}
}

// -----------------------------------------------------------------------------
Expand Down
6 changes: 1 addition & 5 deletions src/eth/primitives/log_topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ use crate::gen_newtype_from;

/// Topic is part of a [`Log`](super::Log) emitted by the EVM during contract execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default, Hash)]
pub struct LogTopic(H256);
pub struct LogTopic(pub H256);

impl LogTopic {
pub fn new(inner: H256) -> Self {
Self(inner)
}

pub fn inner(&self) -> H256 {
self.0
}
}

impl Display for LogTopic {
Expand Down
29 changes: 15 additions & 14 deletions src/eth/primitives/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ pub enum TransactionExecution {
}

impl TransactionExecution {
/// Creates a new transaction execution from a local transaction.
/// Creates a new local transaction execution.
pub fn new_local(tx: TransactionInput, result: EvmExecutionResult) -> Self {
Self::Local(LocalTransactionExecution { input: tx, result })
}

/// Extracts the inner [`LocalTransactionExecution`] if the execution is a local execution.
pub fn as_local(self) -> Option<LocalTransactionExecution> {
match self {
Self::Local(inner) => Some(inner),
_ => None,
}
}

/// Checks if the current transaction was completed normally.
pub fn is_success(&self) -> bool {
match self {
Expand Down Expand Up @@ -90,8 +98,8 @@ impl TransactionExecution {
Self::Local(LocalTransactionExecution { input, result }) => append_entry::TransactionExecutionEntry {
hash: input.hash.as_fixed_bytes().to_vec(),
nonce: input.nonce.as_u64(),
value: u256_to_bytes(*input.value.inner()),
gas_price: u256_to_bytes(*input.gas_price.inner()),
value: u256_to_bytes(input.value.0),
gas_price: u256_to_bytes(input.gas_price.0),
input: input.input.to_vec(),
v: input.v.as_u64(),
r: u256_to_bytes(input.r),
Expand All @@ -108,10 +116,10 @@ impl TransactionExecution {
.map(|log| append_entry::Log {
address: log.address.as_bytes().to_vec(),
topics: vec![
log.topic0.map_or_else(Vec::new, |t| t.inner().as_bytes().to_vec()),
log.topic1.map_or_else(Vec::new, |t| t.inner().as_bytes().to_vec()),
log.topic2.map_or_else(Vec::new, |t| t.inner().as_bytes().to_vec()),
log.topic3.map_or_else(Vec::new, |t| t.inner().as_bytes().to_vec()),
log.topic0.map_or_else(Vec::new, |t| t.0.as_bytes().to_vec()),
log.topic1.map_or_else(Vec::new, |t| t.0.as_bytes().to_vec()),
log.topic2.map_or_else(Vec::new, |t| t.0.as_bytes().to_vec()),
log.topic3.map_or_else(Vec::new, |t| t.0.as_bytes().to_vec()),
],
data: log.data.to_vec(),
})
Expand Down Expand Up @@ -185,13 +193,6 @@ impl TransactionExecution {

Ok(Self::Local(LocalTransactionExecution { input, result }))
}

pub fn inner_local(&self) -> Option<LocalTransactionExecution> {
match self {
Self::Local(inner) => Some(inner.clone()),
_ => None,
}
}
}

#[derive(DebugAsJson, Clone, derive_new::new, serde::Serialize)]
Expand Down
6 changes: 1 addition & 5 deletions src/eth/primitives/wei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::gen_newtype_from;
#[derive(
Debug, derive_more::Display, Clone, Copy, Default, PartialOrd, Ord, PartialEq, Eq, derive_more::Add, derive_more::Sub, serde::Serialize, serde::Deserialize,
)]
pub struct Wei(U256);
pub struct Wei(pub U256);

impl Wei {
pub const ZERO: Wei = Wei(U256::zero());
Expand All @@ -34,10 +34,6 @@ impl Wei {
pub fn is_zero(&self) -> bool {
self == &Self::ZERO
}

pub fn inner(&self) -> &U256 {
&self.0
}
}

impl Dummy<Faker> for Wei {
Expand Down

0 comments on commit d0cef05

Please sign in to comment.