Skip to content

Commit

Permalink
feat: improve performance by not decoding sender when dealing with im…
Browse files Browse the repository at this point in the history
…ported external transactions (#763)
  • Loading branch information
dinhani-cw authored May 3, 2024
1 parent 79fa631 commit 1b3dff6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
11 changes: 3 additions & 8 deletions src/eth/primitives/external_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use ethers_core::types::Transaction as EthersTransaction;
use crate::eth::evm::EvmExecutionResult;
use crate::eth::primitives::ExternalReceipt;
use crate::eth::primitives::Hash;
use crate::eth::primitives::TransactionInput;

#[derive(Debug, derive_new::new)]
pub struct ExternalTransactionExecution {
Expand All @@ -23,13 +22,9 @@ impl ExternalTransaction {
}
}

impl TryFrom<ExternalTransaction> for TransactionInput {
type Error = anyhow::Error;
fn try_from(value: ExternalTransaction) -> Result<Self, Self::Error> {
value.0.try_into()
}
}

// -----------------------------------------------------------------------------
// Conversions: Other -> Self
// -----------------------------------------------------------------------------
impl From<EthersTransaction> for ExternalTransaction {
fn from(value: EthersTransaction) -> Self {
ExternalTransaction(value)
Expand Down
75 changes: 45 additions & 30 deletions src/eth/primitives/transaction_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rlp::Decodable;
use crate::eth::primitives::Address;
use crate::eth::primitives::Bytes;
use crate::eth::primitives::ChainId;
use crate::eth::primitives::ExternalTransaction;
use crate::eth::primitives::Gas;
use crate::eth::primitives::Hash;
use crate::eth::primitives::Nonce;
Expand Down Expand Up @@ -88,47 +89,61 @@ impl Decodable for TransactionInput {
}

// -----------------------------------------------------------------------------
// Conversions: Other -> Self
// Conversion: Other -> Self
// -----------------------------------------------------------------------------
impl TryFrom<ExternalTransaction> for TransactionInput {
type Error = anyhow::Error;

fn try_from(value: ExternalTransaction) -> anyhow::Result<Self> {
try_from_ethers_transaction(value.0, false)
}
}

impl TryFrom<EthersTransaction> for TransactionInput {
type Error = anyhow::Error;

fn try_from(value: EthersTransaction) -> anyhow::Result<Self> {
// extract signer
let signer: Address = match value.recover_from() {
try_from_ethers_transaction(value, true)
}
}

fn try_from_ethers_transaction(value: EthersTransaction, compute_signer: bool) -> anyhow::Result<TransactionInput> {
// extract signer
let signer: Address = match compute_signer {
true => match value.recover_from() {
Ok(signer) => signer.into(),
Err(e) => {
tracing::warn!(reason = ?e, "failed to recover transaction signer");
return Err(anyhow!("Transaction signer cannot be recovered. Check the transaction signature is valid."));
}
};

// extract gas price
let gas_price: Wei = match value.gas_price {
Some(wei) => wei.into(),
None => return log_and_err!("transaction without gas_price id is not allowed"),
};

Ok(Self {
chain_id: match value.chain_id {
Some(chain_id) => Some(chain_id.try_into()?),
None => None,
},
hash: value.hash.into(),
nonce: value.nonce.try_into()?,
signer,
from: Address::new(value.from.into()),
to: value.to.map_into(),
value: value.value.into(),
input: value.input.clone().into(),
gas_limit: value.gas.try_into()?,
gas_price,
v: value.v,
r: value.r,
s: value.s,
})
}
},
false => value.from.into(),
};

// extract gas price
let gas_price: Wei = match value.gas_price {
Some(wei) => wei.into(),
None => return log_and_err!("transaction without gas_price id is not allowed"),
};

Ok(TransactionInput {
chain_id: match value.chain_id {
Some(chain_id) => Some(chain_id.try_into()?),
None => None,
},
hash: value.hash.into(),
nonce: value.nonce.try_into()?,
signer,
from: Address::new(value.from.into()),
to: value.to.map_into(),
value: value.value.into(),
input: value.input.clone().into(),
gas_limit: value.gas.try_into()?,
gas_price,
v: value.v,
r: value.r,
s: value.s,
})
}

// -----------------------------------------------------------------------------
Expand Down

0 comments on commit 1b3dff6

Please sign in to comment.