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: add helper trait for deriving TxEnv from WithEncoded #42

Merged
merged 9 commits into from
Mar 27, 2025

Conversation

lean-apple
Copy link
Contributor

@lean-apple lean-apple commented Mar 16, 2025

Motivation

Closes #40.

Solution

  • Added FromEncodedTx<Tx> helper trait to build transaction environments from encoded bytes
  • Implemented IntoTxEnv<TxEnv> for WithEncoded<T> types
  • Added AsRecoveredTx<T> trait to access recovered transactions
  • Relaxed BlockExecutor::execute_transaction and execute_transaction_with_result_closure to accept any type implementing both IntoTxEnv<TxEnv> and AsRecoveredTx<Self::Transaction>

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@lean-apple lean-apple marked this pull request as ready for review March 17, 2025 09:16
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already looking good, left some comments

Comment on lines 68 to 71
pub trait FromEncodedTx<Tx> {
/// Builds a `TxEnv` from encoded transaction bytes.
fn from_encoded_tx(encoded: &[u8]) -> Self;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also need this to have access to transaction itself and its sender, so I believe signature would need to be something like from_encoded_tx(tx, sender, encoded)

@@ -104,12 +106,14 @@ where

fn execute_transaction_with_result_closure(
&mut self,
tx: Recovered<&R::Transaction>,
tx: impl IntoTxEnv<TxEnv> + AsRecoveredTx<Self::Transaction>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should now use IntoTxEnv when converting to tx_env

the assumption here is that IntoTxEnv will either be the same as .as_recovered_tx().into_tx_env() or more optimized (like in case with WithEncoded)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use directly ().into_tx_env() on tx.as_recovered_tx() after the gas checks,

yet I'm not sure it's possible to use it directly without having a new impl like this

impl<R> FromRecoveredTx<R::Transaction> for TxEnv 
where 
    R: eth::receipt_builder::ReceiptBuilder,
    R::Transaction: Transaction,
{
    fn from_recovered_tx(tx: &R::Transaction, sender: Address) -> Self {
   }
}

Should I add to enable the conversion ?

Copy link
Member

@klkvr klkvr Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want to use the into_tx_env on tx._recovered_tx() as in case of WithEncoded we'll lose optimization that WithEncoded gives us

instead it should be something like

let recovered = tx.as_recovered();
let tx_env = tx.into_tx_env();

or maybe we should even add a separate trait to avoid having to drop value before into_tx_env call

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@mattsse mattsse merged commit 0730612 into alloy-rs:main Mar 27, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add helper trait for deriving optxenv from WithEncoded
3 participants