Skip to content

Commit

Permalink
refactor: implement TransferredAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
IaroslavMazur committed Dec 30, 2023
1 parent bb1c30f commit b8de2eb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bins/revm-test/src/bin/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)]);
Expand Down
47 changes: 31 additions & 16 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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),
});
}
Expand Down Expand Up @@ -595,14 +602,21 @@ pub struct TxEnv {
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
pub max_fee_per_blob_gas: Option<U256>,

/// A list of asset IDs and values to transfer in the transaction.
pub asset_values: Option<Vec<(B256, U256)>>,
/// The list of assets transferred in the transaction.
pub transferred_assets: Option<Vec<TransferredAsset>>,

#[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`].
///
Expand All @@ -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()
}
Expand All @@ -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(),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/inspector/customprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit b8de2eb

Please sign in to comment.