Skip to content

Commit

Permalink
refactor: switch to U256 for asset ids
Browse files Browse the repository at this point in the history
  • Loading branch information
IaroslavMazur committed Apr 19, 2024
1 parent 1575f43 commit 1a9cf1c
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 93 deletions.
4 changes: 2 additions & 2 deletions bins/revm-test/src/bin/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use revm::{
db::BenchmarkDB,
primitives::{Asset, Bytecode, TransactTo, B256, BASE_ASSET_ID, U256},
primitives::{Asset, Bytecode, TransactTo, BASE_ASSET_ID, U256},
Evm,
};

Expand All @@ -16,7 +16,7 @@ fn main() {
.parse()
.unwrap();
let asset = Asset {
id: B256::from(BASE_ASSET_ID),
id: BASE_ASSET_ID,
amount: U256::from(10),
};
tx.transferred_assets = vec![asset];
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 @@ -311,7 +311,7 @@ pub fn execute_test_suite(
.unwrap()
.clone();
let asset = Asset {
id: B256::from(BASE_ASSET_ID),
id: BASE_ASSET_ID,
amount: U256::from(unit.transaction.value[test.indexes.value]),
};
env.tx.transferred_assets = vec![asset];
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait Host {
fn log(&mut self, log: Log);

/// Get asset balance of address and if account is cold loaded.
fn balance(&mut self, asset_id: B256, address: Address) -> Option<(U256, bool)>;
fn balance(&mut self, asset_id: U256, address: Address) -> Option<(U256, bool)>;

/// Mint a native asset.
fn mint(&mut self, minter: Address, sub_id: U256, amount: U256) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Host for DummyHost {
}

#[inline]
fn balance(&mut self, _asset_id: B256, _address: Address) -> Option<(U256, bool)> {
fn balance(&mut self, _asset_id: U256, _address: Address) -> Option<(U256, bool)> {
Some((U256::ZERO, false))
}

Expand Down
8 changes: 3 additions & 5 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn self_mna_balances<H: Host + ?Sized, SPEC: Spec>(

// Push balance and asset_id to the stack
push!(interpreter, balance);
push_b256!(interpreter, *asset_id);
push!(interpreter, *asset_id);
}

// Push the number of assets to the stack
Expand Down Expand Up @@ -241,7 +241,7 @@ fn pop_transferred_assets(interpreter: &mut Interpreter, transferred_assets: &mu
for _ in 0..nr_of_transferred_assets {
pop!(interpreter, asset_id, value);
transferred_assets.push(Asset {
id: B256::from(asset_id),
id: asset_id,
amount: value,
});
}
Expand Down Expand Up @@ -607,7 +607,7 @@ pub fn static_call<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,

pub fn balance<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
push_b256!(interpreter, BASE_ASSET_ID);
push!(interpreter, BASE_ASSET_ID);
push_b256!(interpreter, address.into_word());

mna_balance::<H, SPEC>(interpreter, host);
Expand All @@ -617,8 +617,6 @@ pub fn mna_balance<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
pop_address!(interpreter, address);
pop!(interpreter, asset_id);

let asset_id = B256::from(asset_id);

let Some((balance, is_cold)) = host.balance(asset_id, address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn mna_callvalues<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &m

for asset in &interpreter.contract.call_assets {
push!(interpreter, asset.amount);
push!(interpreter, asset.id.into());
push!(interpreter, asset.id);
}

push!(
Expand Down
6 changes: 3 additions & 3 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
CreateInputs, CreateOutcome, Gas, Host, InstructionResult,
};
use core::cmp::min;
use revm_primitives::{B256, U256};
use revm_primitives::U256;

/// EVM bytecode interpreter.
#[derive(Debug)]
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct Interpreter {
pub next_action: InterpreterAction,

// The ids of the assets recognized by the VM
pub asset_ids: Vec<B256>,
pub asset_ids: Vec<U256>,
}

/// The result of an interpreter operation.
Expand Down Expand Up @@ -116,7 +116,7 @@ impl InterpreterAction {

impl Interpreter {
/// Create new interpreter
pub fn new(contract: Contract, gas_limit: u64, is_static: bool, asset_ids: Vec<B256>) -> Self {
pub fn new(contract: Contract, gas_limit: u64, is_static: bool, asset_ids: Vec<U256>) -> Self {
Self {
instruction_pointer: contract.bytecode.as_ptr(),
contract,
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Address, B256};
use crate::{Address, U256};

/// EIP-170: Contract code size limit
/// By default limit is 0x6000 (~25kb)
Expand Down Expand Up @@ -34,4 +34,4 @@ pub const BLOB_GASPRICE_UPDATE_FRACTION: u64 = 3338477;
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;

// ID of base asset
pub const BASE_ASSET_ID: B256 = B256::ZERO;
pub const BASE_ASSET_ID: U256 = U256::ZERO;
16 changes: 8 additions & 8 deletions crates/primitives/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pub trait Database {
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error>;

/// Get the supported asset ids
fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error>;
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error>;

/// Check if asset id is valid
fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error>;
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error>;
}

/// EVM database commit interface.
Expand Down Expand Up @@ -64,10 +64,10 @@ pub trait DatabaseRef {
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error>;

/// Check if asset id is valid
fn is_asset_id_valid_ref(&self, asset_id: B256) -> Result<bool, Self::Error>;
fn is_asset_id_valid_ref(&self, asset_id: U256) -> Result<bool, Self::Error>;

/// Get the supported asset ids
fn get_asset_ids_ref(&self) -> Result<Vec<B256>, Self::Error>;
fn get_asset_ids_ref(&self) -> Result<Vec<U256>, Self::Error>;
}

/// Wraps a [`DatabaseRef`] to provide a [`Database`] implementation.
Expand Down Expand Up @@ -105,12 +105,12 @@ impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
}

#[inline]
fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error> {
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error> {
self.0.is_asset_id_valid_ref(asset_id)
}

#[inline]
fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error> {
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error> {
self.0.get_asset_ids_ref()
}
}
Expand Down Expand Up @@ -162,12 +162,12 @@ impl<'a, E> Database for RefDBWrapper<'a, E> {
}

#[inline]
fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error> {
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error> {
self.db.is_asset_id_valid_ref(asset_id)
}

#[inline]
fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error> {
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error> {
self.db.get_asset_ids_ref()
}
}
8 changes: 4 additions & 4 deletions crates/primitives/src/db/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ impl<S: State, BH: BlockHash> Database for DatabaseComponents<S, BH> {
.map_err(Self::Error::BlockHash)
}

fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error> {
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error> {
self.state
.is_asset_id_valid(asset_id)
.map_err(Self::Error::State)
}

fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error> {
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error> {
self.state.get_asset_ids().map_err(Self::Error::State)
}
}
Expand Down Expand Up @@ -87,13 +87,13 @@ impl<S: StateRef, BH: BlockHashRef> DatabaseRef for DatabaseComponents<S, BH> {
.map_err(Self::Error::BlockHash)
}

fn is_asset_id_valid_ref(&self, asset_id: B256) -> Result<bool, Self::Error> {
fn is_asset_id_valid_ref(&self, asset_id: U256) -> Result<bool, Self::Error> {
self.state
.is_asset_id_valid(asset_id)
.map_err(Self::Error::State)
}

fn get_asset_ids_ref(&self) -> Result<Vec<B256>, Self::Error> {
fn get_asset_ids_ref(&self) -> Result<Vec<U256>, Self::Error> {
self.state.get_asset_ids().map_err(Self::Error::State)
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/primitives/src/db/components/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub trait State {
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;

/// Check if asset id is valid
fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error>;
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error>;

/// Get the supported asset ids
fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error>;
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error>;
}

#[auto_impl(&, &mut, Box, Rc, Arc)]
Expand All @@ -40,10 +40,10 @@ pub trait StateRef {
fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error>;

/// Check if asset id is valid
fn is_asset_id_valid(&self, asset_id: B256) -> Result<bool, Self::Error>;
fn is_asset_id_valid(&self, asset_id: U256) -> Result<bool, Self::Error>;

/// Get the supported asset ids
fn get_asset_ids(&self) -> Result<Vec<B256>, Self::Error>;
fn get_asset_ids(&self) -> Result<Vec<U256>, Self::Error>;
}

impl<T> State for &T
Expand All @@ -65,12 +65,12 @@ where
}

/// Check if asset id is valid
fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error> {
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error> {
StateRef::is_asset_id_valid(*self, asset_id)
}

/// Get the supported asset ids
fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error> {
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error> {
StateRef::get_asset_ids(*self)
}
}
Expand All @@ -93,11 +93,11 @@ where
self.deref().storage(address, index)
}

fn is_asset_id_valid(&mut self, asset_id: B256) -> Result<bool, Self::Error> {
fn is_asset_id_valid(&mut self, asset_id: U256) -> Result<bool, Self::Error> {
self.deref().is_asset_id_valid(asset_id)
}

fn get_asset_ids(&mut self) -> Result<Vec<B256>, Self::Error> {
fn get_asset_ids(&mut self) -> Result<Vec<U256>, Self::Error> {
self.deref().get_asset_ids()
}
}
11 changes: 4 additions & 7 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use crate::{
Spec, SpecId, B256, BASE_ASSET_ID, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK,
MAX_INITCODE_SIZE, U256, VERSIONED_HASH_VERSION_KZG,
};
use core::{
cmp::{min, Ordering},
ops::Deref,
};
use core::cmp::{min, Ordering};

/// EVM environment configuration.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -192,7 +189,7 @@ impl Env {
let slice: &[Asset] = &self.tx.transferred_assets;

// Check that the submitted asset IDs are unique
let unique_ids: HashSet<&B256> = slice.iter().map(|asset| &asset.id).collect();
let unique_ids: HashSet<&U256> = slice.iter().map(|asset| &asset.id).collect();

if unique_ids.len() != slice.len() {
return Err(InvalidTransactionReason::AssetIdsNotUnique);
Expand Down Expand Up @@ -273,7 +270,7 @@ impl Env {
.tx
.transferred_assets
.iter()
.filter(|asset| asset.id.deref() != BASE_ASSET_ID)
.filter(|asset| asset.id != BASE_ASSET_ID)
{
let (asset_id, transfer_amount) = (transferred_asset.id, transferred_asset.amount);
let asset_balance = account.info.get_balance(asset_id);
Expand Down Expand Up @@ -597,7 +594,7 @@ pub struct TxEnv {
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Asset {
pub id: B256,
pub id: U256,
pub amount: U256,
}

Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Address, Bytes, Log, State, B256, U256};
use crate::{Address, Bytes, Log, State, U256};
use core::fmt;

/// Result of EVM execution.
Expand Down Expand Up @@ -211,7 +211,7 @@ pub enum InvalidTransactionReason {
},
/// Transaction account doesn't have enough asset balance to cover the transferred value.
NotEnoughAssetBalanceForTransfer {
asset_id: B256,
asset_id: U256,
required_balance: U256,
actual_balance: U256,
},
Expand All @@ -235,7 +235,7 @@ pub enum InvalidTransactionReason {
InvalidChainId,
/// One of the asset ids in the transaction is invalid.
InvalidAssetId {
asset_id: B256,
asset_id: U256,
},
/// Access list is not supported for blocks before the Berlin hardfork.
AccessListNotSupported,
Expand Down
Loading

0 comments on commit 1a9cf1c

Please sign in to comment.