Skip to content

Commit

Permalink
refactor: replace AccessList with alloy version (#1552)
Browse files Browse the repository at this point in the history
* refactor: replace AccessList with alloy version

* misc: apply review suggestion

* revert: type specification

* Revert "revert: type specification"

This reverts commit bb0a1214a19f1e86ec9b166230a7102e2a2c011d.

* Update crates/interpreter/src/gas/calc.rs
  • Loading branch information
Wodann authored Jun 22, 2024
1 parent 784a065 commit 58ab5e3
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 51 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions bins/revme/src/cmd/statetest/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use revm::primitives::{Address, Bytes, HashMap, B256, U256};
use revm::primitives::{AccessList, Address, Bytes, HashMap, B256, U256};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -110,15 +110,6 @@ pub struct TransactionParts {
pub max_fee_per_blob_gas: Option<U256>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AccessListItem {
pub address: Address,
pub storage_keys: Vec<B256>,
}

pub type AccessList = Vec<AccessListItem>;

#[cfg(test)]
mod tests {

Expand Down
16 changes: 3 additions & 13 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use revm::{
inspectors::TracerEip3155,
primitives::{
calc_excess_blob_gas, keccak256, Bytecode, Bytes, EVMResultGeneric, Env, Eof,
ExecutionResult, SpecId, TxKind, B256, EOF_MAGIC_BYTES, U256,
ExecutionResult, SpecId, TxKind, B256, EOF_MAGIC_BYTES,
},
Evm, State,
};
Expand Down Expand Up @@ -350,18 +350,8 @@ pub fn execute_test_suite(
.access_lists
.get(test.indexes.data)
.and_then(Option::as_deref)
.unwrap_or_default()
.iter()
.map(|item| {
(
item.address,
item.storage_keys
.iter()
.map(|key| U256::from_be_bytes(key.0))
.collect::<Vec<_>>(),
)
})
.collect();
.cloned()
.unwrap_or_default();

let to = match unit.transaction.to {
Some(add) => TxKind::Call(add),
Expand Down
14 changes: 8 additions & 6 deletions crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use revm_primitives::AccessListItem;

use super::constants::*;
use crate::{
num_words,
primitives::{Address, SpecId, U256},
primitives::{SpecId, U256},
SelfDestructResult,
};
use std::vec::Vec;

/// `const` Option `?`.
macro_rules! tri {
Expand Down Expand Up @@ -357,7 +358,7 @@ pub fn validate_initial_tx_gas(
spec_id: SpecId,
input: &[u8],
is_create: bool,
access_list: &[(Address, Vec<U256>)],
access_list: &[AccessListItem],
) -> u64 {
let mut initial_gas = 0;
let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
Expand All @@ -375,11 +376,12 @@ pub fn validate_initial_tx_gas(

// get number of access list account and storages.
if spec_id.is_enabled_in(SpecId::BERLIN) {
let accessed_slots = access_list
let accessed_slots: usize = access_list
.iter()
.fold(0, |slot_count, (_, slots)| slot_count + slots.len() as u64);
.map(|item| item.storage_keys.len())
.sum();
initial_gas += access_list.len() as u64 * ACCESS_LIST_ADDRESS;
initial_gas += accessed_slots * ACCESS_LIST_STORAGE_KEY;
initial_gas += accessed_slots as u64 * ACCESS_LIST_STORAGE_KEY;
}

// base stipend
Expand Down
10 changes: 9 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rust_2018_idioms = "deny"
all = "warn"

[dependencies]
alloy-eips = { version = "0.1", default-features = false }
alloy-primitives = { version = "0.7.2", default-features = false, features = [
"rlp",
] }
Expand Down Expand Up @@ -53,6 +54,7 @@ hex = { version = "0.4", default-features = false }
default = ["std", "c-kzg", "portable"]
std = [
"serde?/std",
"alloy-eips/std",
"alloy-primitives/std",
"hex/std",
"bitvec/std",
Expand All @@ -61,14 +63,20 @@ std = [
hashbrown = []
serde = [
"dep:serde",
"alloy-eips/serde",
"alloy-primitives/serde",
"hex/serde",
"hashbrown/serde",
"bitvec/serde",
"bitflags/serde",
"c-kzg?/serde",
]
arbitrary = ["std", "alloy-primitives/arbitrary", "bitflags/arbitrary"]
arbitrary = [
"std",
"alloy-eips/arbitrary",
"alloy-primitives/arbitrary",
"bitflags/arbitrary"
]
asm-keccak = ["alloy-primitives/asm-keccak"]
portable = ["c-kzg?/portable"]

Expand Down
8 changes: 5 additions & 3 deletions crates/primitives/src/bytecode/eof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ mod test {
assert_eq!(eof.data_slice(0, 4), &[0x01, 0x02, 0x03, 0x04]);
assert_eq!(eof.data_slice(0, 5), &[0x01, 0x02, 0x03, 0x04]);
assert_eq!(eof.data_slice(1, 2), &[0x02, 0x03]);
assert_eq!(eof.data_slice(10, 2), &[]);
assert_eq!(eof.data_slice(1, 0), &[]);
assert_eq!(eof.data_slice(10, 0), &[]);

const EMPTY: &[u8] = &[];
assert_eq!(eof.data_slice(10, 2), EMPTY);
assert_eq!(eof.data_slice(1, 0), EMPTY);
assert_eq!(eof.data_slice(10, 0), EMPTY);
}
}
2 changes: 1 addition & 1 deletion crates/primitives/src/bytecode/eof/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ mod tests {
let (header, _) = EofHeader::decode(&input).unwrap();
assert_eq!(header.types_size, 4);
assert_eq!(header.code_sizes, vec![1]);
assert_eq!(header.container_sizes, vec![]);
assert_eq!(header.container_sizes, Vec::<u16>::new());
assert_eq!(header.data_size, 0);
}

Expand Down
13 changes: 8 additions & 5 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use alloy_primitives::TxKind;
pub use handler_cfg::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};

use crate::{
calc_blob_gasprice, Account, Address, Bytes, InvalidHeader, InvalidTransaction, Spec, SpecId,
B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE, U256,
VERSIONED_HASH_VERSION_KZG,
calc_blob_gasprice, AccessListItem, Account, Address, Bytes, InvalidHeader, InvalidTransaction,
Spec, SpecId, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK, MAX_INITCODE_SIZE,
U256, VERSIONED_HASH_VERSION_KZG,
};
use core::cmp::{min, Ordering};
use core::hash::Hash;
Expand Down Expand Up @@ -526,7 +526,7 @@ pub struct TxEnv {
/// Added in [EIP-2930].
///
/// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
pub access_list: Vec<(Address, Vec<U256>)>,
pub access_list: Vec<AccessListItem>,

/// The priority fee per gas.
///
Expand Down Expand Up @@ -704,7 +704,10 @@ mod tests {
#[test]
fn test_validate_tx_access_list() {
let mut env = Env::default();
env.tx.access_list = vec![(Address::ZERO, vec![])];
env.tx.access_list = vec![AccessListItem {
address: Address::ZERO,
storage_keys: vec![],
}];
assert_eq!(
env.validate_tx::<crate::FrontierSpec>(),
Err(InvalidTransaction::AccessListNotSupported)
Expand Down
1 change: 1 addition & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod result;
pub mod specification;
pub mod state;
pub mod utilities;
pub use alloy_eips::eip2930::{AccessList, AccessListItem};
pub use alloy_primitives::{
self, address, b256, bytes, fixed_bytes, hex, hex_literal, ruint, uint, Address, Bytes,
FixedBytes, Log, LogData, TxKind, B256, I256, U256,
Expand Down
12 changes: 8 additions & 4 deletions crates/revm/src/context/inner_evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
},
journaled_state::JournaledState,
primitives::{
keccak256, Account, Address, AnalysisKind, Bytecode, Bytes, CreateScheme, EVMError, Env,
Eof, HashSet, Spec,
keccak256, AccessListItem, Account, Address, AnalysisKind, Bytecode, Bytes, CreateScheme,
EVMError, Env, Eof, HashSet, Spec,
SpecId::{self, *},
B256, EOF_MAGIC_BYTES, EOF_MAGIC_HASH, U256,
},
Expand Down Expand Up @@ -101,9 +101,13 @@ impl<DB: Database> InnerEvmContext<DB> {
/// Loading of accounts/storages is needed to make them warm.
#[inline]
pub fn load_access_list(&mut self) -> Result<(), EVMError<DB::Error>> {
for (address, slots) in self.env.tx.access_list.iter() {
for AccessListItem {
address,
storage_keys,
} in self.env.tx.access_list.iter()
{
self.journaled_state
.initial_account_load(*address, slots, &mut self.db)?;
.initial_account_load(*address, storage_keys, &mut self.db)?;
}
Ok(())
}
Expand Down
10 changes: 6 additions & 4 deletions crates/revm/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::primitives::{
use core::mem;
use revm_interpreter::primitives::SpecId;
use revm_interpreter::{LoadAccountResult, SStoreResult};
use revm_precompile::B256;
use std::vec::Vec;

/// JournalState is internal EVM state that is used to contain state and track changes to that state.
Expand Down Expand Up @@ -530,7 +531,7 @@ impl JournaledState {
pub fn initial_account_load<DB: Database>(
&mut self,
address: Address,
slots: &[U256],
storage_keys: &[B256],
db: &mut DB,
) -> Result<&mut Account, EVMError<DB::Error>> {
// load or get account.
Expand All @@ -544,9 +545,10 @@ impl JournaledState {
),
};
// preload storages.
for slot in slots {
if let Entry::Vacant(entry) = account.storage.entry(*slot) {
let storage = db.storage(address, *slot).map_err(EVMError::Database)?;
for storage_key in storage_keys {
let slot = U256::from_be_bytes(storage_key.0);
if let Entry::Vacant(entry) = account.storage.entry(slot) {
let storage = db.storage(address, slot).map_err(EVMError::Database)?;
entry.insert(EvmStorageSlot::new(storage));
}
}
Expand Down
12 changes: 8 additions & 4 deletions examples/generate_block_traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethers_providers::{Http, Provider};
use indicatif::ProgressBar;
use revm::db::{CacheDB, EthersDB, StateBuilder};
use revm::inspectors::TracerEip3155;
use revm::primitives::{Address, TxKind, U256};
use revm::primitives::{AccessListItem, Address, TxKind, B256, U256};
use revm::{inspector_handle_register, Evm};
use std::fs::OpenOptions;
use std::io::BufWriter;
Expand Down Expand Up @@ -130,12 +130,16 @@ async fn main() -> anyhow::Result<()> {
.0
.into_iter()
.map(|item| {
let new_keys: Vec<U256> = item
let storage_keys: Vec<B256> = item
.storage_keys
.into_iter()
.map(|h256| U256::from_le_bytes(h256.0))
.map(|h256| B256::new(h256.0))
.collect();
(Address::from(item.address.as_fixed_bytes()), new_keys)

AccessListItem {
address: Address::new(item.address.0),
storage_keys,
}
})
.collect();
} else {
Expand Down

0 comments on commit 58ab5e3

Please sign in to comment.