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

Generic transaction and block Environment. #916

Open
rakita opened this issue Dec 18, 2023 · 4 comments
Open

Generic transaction and block Environment. #916

rakita opened this issue Dec 18, 2023 · 4 comments
Labels
feature New feature or lib ability

Comments

@rakita
Copy link
Member

rakita commented Dec 18, 2023

After Evm builder is finished we should generalize the Block and Transaction to the trait, this would allow adding new fields to both of those structs and elevate usage of the External Contexts as it would allow multiple generics and easier integration with other types.

Evn should stay as the default type that we use, and this would be overridable in EvmBuilder.

@rakita rakita added the feature New feature or lib ability label Dec 25, 2023
@rakita
Copy link
Member Author

rakita commented Jan 10, 2024

Make Transaction generic contains tx type so we have a clean difference between transactions, currently we have done it ad hoc if field is Some.

@Rjected
Copy link
Collaborator

Rjected commented Jan 22, 2024

Yeah, at least for the concrete types we would want something like this:

/// Ethereum tx env
pub struct EthTxEnv {
    // ... the tx env fields for mainnet ethereum
}

/// Data used to calculate the l1 cost of a transaction
pub struct RollupCostData {
    /// The number of ones in the rlp encoding of the signed transaction being executed
    pub ones: u64,
    /// The number of zeroes in the rlp encoding of the signed transaction being executed
    pub zeroes: u64,
}

/// Optimism tx env
pub struct OptimismTxEnv {
    /// The underlying ethereum tx environment
    pub eth_env: EthTxEnv,
    // ... then the optimism specific fields
    // ideally including this:
    pub rollup_cost_data: RollupCostData,
}

And each of these would implement an environment trait with methods to expose what we currently use in mainnet eth execution.

We'd also want some flexibility, for example to let optimism actually use its custom tx env fields.
For example here are some things that optimism currently does with custom fields:

// Do not allow for a system transaction to be processed if Regolith is enabled.
if self.tx.optimism.is_system_transaction.unwrap_or(false)
&& SPEC::enabled(SpecId::REGOLITH)
{
return Err(InvalidTransaction::DepositSystemTxPostRegolith);
}
// Do not perform any extra validation for deposit transactions, they are pre-verified on L1.
if self.tx.optimism.source_hash.is_some() {
return Ok(());
}

// On Optimism, deposit transactions do not have verification on the nonce
// nor the balance of the account.
#[cfg(feature = "optimism")]
if self.cfg.optimism && self.tx.optimism.source_hash.is_some() {
return Ok(());
}

// If the transaction is not a deposit transaction, subtract the L1 data fee from the
// caller's balance directly after minting the requested amount of ETH.
if context.evm.env.tx.optimism.source_hash.is_none() {
// get envelope
let Some(enveloped_tx) = context.evm.env.tx.optimism.enveloped_tx.clone() else {
return Err(EVMError::Custom(
"[OPTIMISM] Failed to load enveloped transaction.".to_string(),
));
};
let tx_l1_cost = context
.evm
.l1_block_info
.as_ref()
.expect("L1BlockInfo should be loaded")
.calculate_tx_l1_cost(&enveloped_tx, SPEC::SPEC_ID);
if tx_l1_cost.gt(&caller_account.info.balance) {
return Err(EVMError::Transaction(
InvalidTransaction::LackOfFundForMaxFee {
fee: tx_l1_cost.into(),
balance: caller_account.info.balance.into(),
},
));
}
caller_account.info.balance = caller_account.info.balance.saturating_sub(tx_l1_cost);
}

Not sure how yet we could provide this flexibility, maybe adding some hooks that are run in the beginning of tx validation, or elsewhere

@rakita
Copy link
Member Author

rakita commented Jan 23, 2024

This is imagined to be a TxTrait or TxEnvTrait that you can override if needed. It is up to lib user to specify different generics of the trait and add handlers that use that trait. In reth, this would be just implementing TxTrait over &Transaction and you could use &Transaction directly inside revm without need to populate Env

@Autoparallel
Copy link
Contributor

I'll give this a shot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or lib ability
Projects
None yet
Development

No branches or pull requests

3 participants