Skip to content

feat: impl tx for transactionrequest #75

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

Merged
merged 4 commits into from
Jan 31, 2025
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trevm"
version = "0.19.0"
version = "0.19.1"
rust-version = "1.83.0"
edition = "2021"
authors = ["init4"]
Expand Down
56 changes: 55 additions & 1 deletion src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
EvmNeedsCfg, EvmNeedsTx, EvmReady, EvmTransacted, HasBlock, HasCfg, HasTx, NeedsCfg, NeedsTx,
TransactedState, Tx,
};
use alloy::primitives::{Address, Bytes, U256};
use alloy::{
primitives::{Address, Bytes, U256},
rpc::types::{state::StateOverride, BlockOverrides},
};
use core::convert::Infallible;
use revm::{
db::{states::bundle_state::BundleRetention, BundleState, State},
Expand Down Expand Up @@ -137,6 +140,42 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
None => Ok(None),
}
}

/// Apply [`StateOverride`]s to the current state.
pub fn apply_state_overrides(
mut self,
overrides: &StateOverride,
) -> Result<Self, EVMError<Db::Error>> {
for (address, account_override) in overrides {
if let Some(balance) = account_override.balance {
self.inner.set_balance(*address, balance).map_err(EVMError::Database)?;
}
if let Some(nonce) = account_override.nonce {
self.inner.set_nonce(*address, nonce).map_err(EVMError::Database)?;
}
if let Some(code) = account_override.code.as_ref() {
self.inner
.set_bytecode(
*address,
Bytecode::new_raw_checked(code.clone())
.map_err(|_| EVMError::Custom("Invalid bytecode".to_string()))?,
)
.map_err(EVMError::Database)?;
}
if let Some(state) = account_override.state.as_ref() {
for (slot, value) in state {
self.inner
.set_storage(
*address,
U256::from_be_bytes((*slot).into()),
U256::from_be_bytes((*value).into()),
)
.map_err(EVMError::Database)?;
}
}
}
Ok(self)
}
}

impl<Ext, Db: Database + DatabaseCommit + DatabaseRef, TrevmState> Trevm<'_, Ext, Db, TrevmState> {
Expand Down Expand Up @@ -981,6 +1020,21 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasTx> Trevm<'a, Ext, D
}
}

// -- HAS TX with State<Db>

impl<Ext, Db: Database> EvmNeedsTx<'_, Ext, State<Db>> {
/// Apply block overrides to the current block.
pub fn apply_block_overrides(mut self, overrides: BlockOverrides) -> Self {
overrides.fill_block(&mut self.inner);

if let Some(hashes) = overrides.block_hash {
self.inner.db_mut().block_hashes.extend(hashes)
}

self
}
}

// --- READY

impl<'a, Ext, Db: Database + DatabaseCommit> EvmReady<'a, Ext, Db> {
Expand Down
90 changes: 90 additions & 0 deletions src/fill/alloy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,96 @@ impl<T: Send + Sync> Block for alloy::rpc::types::eth::Block<T> {
}
}

impl Tx for alloy::rpc::types::TransactionRequest {
fn fill_tx_env(&self, tx_env: &mut TxEnv) {
let TxEnv {
caller,
gas_limit,
gas_price,
transact_to,
value,
data,
nonce,
chain_id,
access_list,
gas_priority_fee,
blob_hashes,
max_fee_per_blob_gas,
authorization_list,
} = tx_env;

*caller = self.from.unwrap_or_default();
*gas_limit = self.gas.unwrap_or_default();
*gas_price = U256::from(self.gas_price.unwrap_or_default());
*transact_to = self.to.unwrap_or_default();
*value = self.value.unwrap_or_default();
*data = self.input.input().cloned().unwrap_or_default();
*nonce = self.nonce;
*chain_id = self.chain_id;
if let Some(al) = &self.access_list {
access_list.clone_from(al);
} else {
access_list.clear();
}
if let Some(gpf) = &self.max_priority_fee_per_gas {
*gas_priority_fee = Some(U256::from(*gpf));
} else {
gas_priority_fee.take();
}
if let Some(bh) = &self.blob_versioned_hashes {
blob_hashes.clone_from(bh);
} else {
blob_hashes.clear();
}
if let Some(mfbg) = &self.max_fee_per_blob_gas {
*max_fee_per_blob_gas = Some(U256::from(*mfbg));
} else {
max_fee_per_blob_gas.take();
}
if let Some(al) = &self.authorization_list {
*authorization_list = Some(al.clone().into());
} else {
authorization_list.take();
}
}
}

impl Block for alloy::rpc::types::BlockOverrides {
fn fill_block_env(&self, block_env: &mut BlockEnv) {
let BlockEnv {
number,
coinbase,
timestamp,
gas_limit,
basefee,
difficulty,
prevrandao,
blob_excess_gas_and_price: _,
} = block_env;
if let Some(n) = &self.number {
*number = U256::from(*n);
}
if let Some(d) = &self.difficulty {
*difficulty = U256::from(*d);
}
if let Some(t) = &self.time {
*timestamp = U256::from(*t);
}
if let Some(g) = &self.gas_limit {
*gas_limit = U256::from(*g);
}
if let Some(c) = &self.coinbase {
*coinbase = *c;
}
if let Some(r) = self.random {
*prevrandao = Some(r);
}
if let Some(b) = &self.base_fee {
*basefee = U256::from(*b);
}
}
}

#[cfg(test)]
mod tests {
use crate::{NoopBlock, NoopCfg, TrevmBuilder};
Expand Down