Skip to content

Commit

Permalink
refactor: centralize the zero-amount check for burning
Browse files Browse the repository at this point in the history
refactor: Balances -> TokenBalances
  • Loading branch information
IaroslavMazur committed Aug 27, 2024
1 parent 60afbd2 commit 55fc3de
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bins/revm-test/src/bin/burntpix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use regex::bytes::Regex;
use revm::{
db::{CacheDB, EmptyDB},
primitives::{
address, hex, keccak256, AccountInfo, Address, Balances, Bytecode, Bytes, ExecutionResult,
address, hex, keccak256, AccountInfo, Address, TokenBalances, Bytecode, Bytes, ExecutionResult,
Output, TransactTo, B256, BASE_TOKEN_ID, U256,
},
Evm,
Expand Down Expand Up @@ -102,7 +102,7 @@ fn try_from_hex_to_u32(hex: &str) -> eyre::Result<u32> {
fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: Bytes) {
let code_hash = hex::encode(keccak256(code.clone()));
let account_info = AccountInfo::new(
Balances::from([(BASE_TOKEN_ID, U256::ZERO)]),
TokenBalances::from([(BASE_TOKEN_ID, U256::ZERO)]),
0,
B256::from_str(&code_hash).unwrap(),
Bytecode::new_raw(code),
Expand Down
15 changes: 6 additions & 9 deletions crates/primitives/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub type EvmStorage = HashMap<U256, EvmStorageSlot>;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Account {
/// Balances, nonce, and code.
/// TokenBalances, nonce, and code.
pub info: AccountInfo,
/// Storage cache
pub storage: EvmStorage,
Expand Down Expand Up @@ -184,17 +184,14 @@ impl EvmStorageSlot {
}
}

// TODO: create a custom type with a suggestive name for the (U256, U256) tuple. This will make
/// the code more readable - especially in places where the tuple members are currently accessed
/// via `.0` and `.1`.
pub type Balances = HashMap<U256, U256>;
pub type TokenBalances = HashMap<U256, U256>;

/// The account information.
#[derive(Clone, Debug, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccountInfo {
/// Token balances.
pub balances: Balances,
pub balances: TokenBalances,
/// Account nonce.
pub nonce: u64,
/// code hash,
Expand Down Expand Up @@ -248,7 +245,7 @@ impl Hash for AccountInfo {
}

impl AccountInfo {
pub fn new(balances: Balances, nonce: u64, code_hash: B256, code: Bytecode) -> Self {
pub fn new(balances: TokenBalances, nonce: u64, code_hash: B256, code: Bytecode) -> Self {
Self {
balances,
nonce,
Expand Down Expand Up @@ -379,8 +376,8 @@ impl AccountInfo {
}
}

impl From<Balances> for AccountInfo {
fn from(balances: Balances) -> Self {
impl From<TokenBalances> for AccountInfo {
fn from(balances: TokenBalances) -> Self {
AccountInfo {
balances,
..Default::default()
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/utilities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
b256, Balances, B256, BASE_TOKEN_ID, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE,
b256, TokenBalances, B256, BASE_TOKEN_ID, BLOB_GASPRICE_UPDATE_FRACTION, MIN_BLOB_GASPRICE,
TARGET_BLOB_GAS_PER_BLOCK,
};
pub use alloy_primitives::keccak256;
Expand Down Expand Up @@ -32,8 +32,8 @@ pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 {
}

/// Creates a simple balances map with the given balance for the base token.
pub fn init_balances(base_balance: U256) -> Balances {
let mut balances = Balances::new();
pub fn init_balances(base_balance: U256) -> TokenBalances {
let mut balances = TokenBalances::new();
balances.insert(BASE_TOKEN_ID, base_balance);
balances
}
Expand Down
6 changes: 1 addition & 5 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,6 @@ impl<EXT, DB: Database> Host for Context<EXT, DB> {
}

fn burn(&mut self, burner: Address, sub_id: U256, token_holder: Address, amount: U256) -> bool {
if amount == U256::ZERO {
return false;
}

self.evm.inner.journaled_state.burn(
burner,
sub_id,
Expand All @@ -214,7 +210,7 @@ impl<EXT, DB: Database> Host for Context<EXT, DB> {
)
}
fn mint(&mut self, minter: Address, recipient: Address, sub_id: U256, amount: U256) -> bool {
// TODO: also return the generated Token Id from this function
// TODO: also return the generated Token Id from this function?

if amount == U256::ZERO {
return false;
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/context/evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub(crate) mod test_utils {
use crate::{
db::{CacheDB, EmptyDB},
journaled_state::JournaledState,
primitives::{address, Address, Balances, SpecId, B256},
primitives::{address, Address, SpecId, TokenBalances, B256},
};

/// Mock caller address.
Expand Down Expand Up @@ -324,7 +324,7 @@ pub(crate) mod test_utils {
pub fn create_cache_db_evm_context_with_balances(
env: Box<Env>,
mut db: CacheDB<EmptyDB>,
balances: Balances,
balances: TokenBalances,
) -> EvmContext<CacheDB<EmptyDB>> {
db.insert_account_info(
test_utils::MOCK_CALLER,
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/db/alloydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<T: Transport + Clone, N: Network, P: Provider<T, N>> DatabaseRef for AlloyD
let nonce = nonce?;

Ok(Some(AccountInfo::new(
Balances::from([(BASE_TOKEN_ID, balance)]),
TokenBalances::from([(BASE_TOKEN_ID, balance)]),
nonce,
code_hash,
code,
Expand Down
5 changes: 4 additions & 1 deletion crates/revm/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ impl JournaledState {
amount: U256,
db: &mut DB,
) -> bool {
if amount == U256::ZERO {
return false;
}

if self.load_native_token_ids(db).is_err() {
return false;
}
Expand All @@ -838,7 +842,6 @@ impl JournaledState {

let token_id = token_id_address(burner, sub_id);

// TODO: shouldn't this be verified before this function is called?
let result = db.is_token_id_valid(token_id);
if result.is_err() || result.is_ok_and(|r| !r) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions crates/revm/src/sablier/test_native_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
mod test {
use crate::{
primitives::{
address, bytes, keccak256, token_id_address, AccountInfo, Address, Balances, Bytecode,
Bytes, SpecId, TokenTransfer, TransactTo, B256, BASE_TOKEN_ID, U256,
address, bytes, keccak256, token_id_address, AccountInfo, Address, Bytecode, Bytes,
SpecId, TokenBalances, TokenTransfer, TransactTo, B256, BASE_TOKEN_ID, U256,
},
sablier::native_tokens::{ADDRESS as NATIVE_TOKENS_PRECOMPILE_ADDRESS, BALANCEOF_SELECTOR},
Evm, InMemoryDB,
Expand Down Expand Up @@ -705,7 +705,7 @@ mod test {
db.insert_account_info(caller_eoa, caller_info);

let token_transferrer_mock_bytecode = &NAIVE_TOKEN_TRANSFERRER_MOCK_BYTECODE;
let mut balances: Balances = HashMap::new();
let mut balances: TokenBalances = HashMap::new();
for (token_id, balance) in token_ids
.iter()
.zip(token_transferrer_balances.iter())
Expand Down

0 comments on commit 55fc3de

Please sign in to comment.