Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve performance by not decoding sender when dealing with imported external transactions #763

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading