diff --git a/bins/revm-test/src/bin/transfer.rs b/bins/revm-test/src/bin/transfer.rs index 230943dc..4a38a398 100644 --- a/bins/revm-test/src/bin/transfer.rs +++ b/bins/revm-test/src/bin/transfer.rs @@ -13,7 +13,7 @@ fn main() { evm.env.tx.caller = "0x0000000000000000000000000000000000000001" .parse() .unwrap(); - evm.env.tx.asset_values = Some(vec![(B256::from(BASE_ASSET_ID), U256::from(10))]); + evm.env.tx.transferred_assets = Some(vec![(B256::from(BASE_ASSET_ID), U256::from(10))]); evm.env.tx.transact_to = TransactTo::Call( "0x0000000000000000000000000000000000000000" .parse() diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index 99eb506d..69a2baa6 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -217,7 +217,7 @@ pub fn execute_test_suite( .unwrap() .clone(); let base_transfer_value = unit.transaction.value[test.indexes.value]; - env.tx.asset_values = Some(vec![( + env.tx.transferred_assets = Some(vec![( B256::from(BASE_ASSET_ID), U256::from(base_transfer_value), )]); diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index 6d305ef9..877bc5d4 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -178,12 +178,16 @@ impl Env { } } - match &self.tx.asset_values { + match &self.tx.transferred_assets { Some(vector) => { //TODO: check that the submitted asset IDs are valid/exist // Check that the submitted asset IDs are unique - let unique_ids: HashSet<&B256> = vector.iter().map(|(id, _)| id).collect(); + let unique_ids: HashSet<&B256> = vector + .iter() + .map(|transferred_asset| &transferred_asset.id) + .collect(); + if unique_ids.len() != vector.len() { return Err(InvalidTransactionReason::AssetIdsNotUnique); } @@ -267,16 +271,19 @@ impl Env { // If other native assets are being transferred in the tx, then, for each of the assets, // check that the account has a balance big enough to cover the transfer amount - match &self.tx.asset_values { + match &self.tx.transferred_assets { Some(vector) => { - for (asset_id, transfer_amount) in - vector.iter().filter(|(id, _)| id.deref() != BASE_ASSET_ID) + for transferred_asset in vector + .iter() + .filter(|asset| asset.id.deref() != BASE_ASSET_ID) { - let asset_balance = account.info.get_balance(*asset_id); - if asset_balance < *transfer_amount { + let (asset_id, transfer_amount) = + (transferred_asset.id, transferred_asset.amount); + let asset_balance = account.info.get_balance(asset_id); + if asset_balance < transfer_amount { return Err(InvalidTransactionReason::NotEnoughAssetBalanceForTransfer { - asset_id: (*asset_id), - required_balance: (*transfer_amount), + asset_id: (asset_id), + required_balance: (transfer_amount), actual_balance: (asset_balance), }); } @@ -595,14 +602,21 @@ pub struct TxEnv { /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844 pub max_fee_per_blob_gas: Option, - /// A list of asset IDs and values to transfer in the transaction. - pub asset_values: Option>, + /// The list of assets transferred in the transaction. + pub transferred_assets: Option>, #[cfg(feature = "optimism")] #[cfg_attr(feature = "serde", serde(flatten))] pub optimism: OptimismFields, } +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] +pub struct TransferredAsset { + pub id: B256, + pub amount: U256, +} + impl TxEnv { /// See [EIP-4844] and [`Env::calc_data_fee`]. /// @@ -613,11 +627,12 @@ impl TxEnv { } pub fn get_base_transfer_value(&self) -> U256 { - match &self.asset_values { - Some(vector) => { - if let Some((_, asset_value)) = vector.iter().find(|&&(id, _)| id == BASE_ASSET_ID) + match &self.transferred_assets { + Some(assets) => { + if let Some(transferred_asset) = + assets.iter().find(|asset| asset.id == BASE_ASSET_ID) { - *asset_value + transferred_asset.amount } else { Default::default() } @@ -641,7 +656,7 @@ impl Default for TxEnv { access_list: Vec::new(), blob_hashes: Vec::new(), max_fee_per_blob_gas: None, - asset_values: None, + transferred_assets: None, #[cfg(feature = "optimism")] optimism: OptimismFields::default(), } diff --git a/crates/revm/benches/bench.rs b/crates/revm/benches/bench.rs index 29c2d88f..50bdf83b 100644 --- a/crates/revm/benches/bench.rs +++ b/crates/revm/benches/bench.rs @@ -69,7 +69,7 @@ fn transfer(c: &mut Criterion) { evm.env.tx.caller = address!("0000000000000000000000000000000000000001"); evm.env.tx.transact_to = TransactTo::Call(address!("0000000000000000000000000000000000000000")); - evm.env.tx.asset_values = Some(vec![(B256::from(BASE_ASSET_ID), U256::from(10))]); + evm.env.tx.transferred_assets = Some(vec![(B256::from(BASE_ASSET_ID), U256::from(10))]); let mut g = c.benchmark_group("transfer"); g.noise_threshold(0.03).warm_up_time(Duration::from_secs(1)); diff --git a/crates/revm/src/inspector/customprinter.rs b/crates/revm/src/inspector/customprinter.rs index e324dbea..9da3d495 100644 --- a/crates/revm/src/inspector/customprinter.rs +++ b/crates/revm/src/inspector/customprinter.rs @@ -126,7 +126,7 @@ mod test { evm.env.tx.caller = address!("5fdcca53617f4d2b9134b29090c87d01058e27e0"); evm.env.tx.transact_to = crate::primitives::TransactTo::Call(callee); evm.env.tx.data = crate::primitives::Bytes::new(); - evm.env.tx.asset_values = Some(vec![(B256::from(BASE_ASSET_ID), U256::ZERO)]); + evm.env.tx.transferred_assets = Some(vec![(B256::from(BASE_ASSET_ID), U256::ZERO)]); let _ = evm.inspect_commit(super::CustomPrintTracer::default()); } }