Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eip8038-state-access-gas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": patch
---

Added experimental EIP-8038 support: from the Amsterdam hardfork, state-access operations are repriced.
11 changes: 11 additions & 0 deletions crates/edr_provider/tests/common/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ pub fn gas_used(provider: &Provider<L1ChainSpec>, transaction_hash: B256) -> u64
.gas_used
}

/// Sends the transaction via `eth_sendTransaction` and returns the `gasUsed`
/// of its receipt.
pub fn gas_used_by_sending(
provider: &Provider<L1ChainSpec>,
request: TransactionRequest,
) -> anyhow::Result<u64> {
let transaction_hash = send_transaction(provider, request)?;

Ok(gas_used(provider, transaction_hash))
}

/// Estimates the request's gas usage via `eth_estimateGas`.
pub fn estimate_gas(provider: &Provider<L1ChainSpec>, request: L1CallRequest) -> u64 {
let response = provider
Expand Down
14 changes: 2 additions & 12 deletions crates/edr_provider/tests/integration/eip2780.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use revm_primitives::{
use crate::common::{
bytecode::{opcode, BytecodeBuilder},
provider::{
estimate_gas, gas_used, new_provider, new_provider_with_config, send_transaction,
transaction_receipt,
estimate_gas, gas_used_by_sending, new_provider, new_provider_with_config,
send_transaction, transaction_receipt,
},
};

Expand Down Expand Up @@ -87,16 +87,6 @@ fn as_call_request(request: &TransactionRequest) -> L1CallRequest {
}
}

/// Sends the transaction and returns the `gasUsed` of its receipt.
fn gas_used_by_sending(
provider: &Provider<L1ChainSpec>,
request: TransactionRequest,
) -> anyhow::Result<u64> {
let transaction_hash = send_transaction(provider, request)?;

Ok(gas_used(provider, transaction_hash))
}

#[tokio::test(flavor = "multi_thread")]
async fn self_transfer_pays_only_the_base_cost_from_amsterdam() -> anyhow::Result<()> {
let request = transfer_request(SENDER, U256::from(1));
Expand Down
16 changes: 7 additions & 9 deletions crates/edr_provider/tests/integration/eip7981.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ use edr_chain_l1::{rpc::TransactionRequest, L1ChainSpec};
use edr_eip2930::AccessListItem;
use edr_primitives::{address, Address, Bytes, B256};
use edr_provider::Provider;
use revm_primitives::{
eip2780::TX_BASE_COST as EIP2780_TX_BASE_COST,
eip8038::{
ACCESS_LIST_ADDRESS_COST as EIP8038_ACCESS_LIST_ADDRESS_COST,
ACCESS_LIST_STORAGE_KEY_COST as EIP8038_ACCESS_LIST_STORAGE_KEY_COST,
},
};

use crate::common::provider::{gas_used, new_provider, send_transaction};

Expand All @@ -33,19 +40,10 @@ fn assert_transaction_gas_usage(

const TX_BASE_COST: u64 = 21_000;

/// EIP-2780 base cost from Amsterdam onward. The fixtures are self-transfers,
/// which pay neither the `to`- nor the value-based charge on top of it.
const EIP2780_TX_BASE_COST: u64 = 12_000;

/// EIP-2930 per-item access list costs, unchanged by EIP-7981.
const ACCESS_LIST_ADDRESS_COST: u64 = 2_400;
const ACCESS_LIST_STORAGE_KEY_COST: u64 = 1_900;

/// EIP-8038 per-item access list costs from Amsterdam onward (cold account
/// and cold storage access less the warm access still charged at first use).
const EIP8038_ACCESS_LIST_ADDRESS_COST: u64 = 2_900;
const EIP8038_ACCESS_LIST_STORAGE_KEY_COST: u64 = 2_000;

/// Intrinsic cost of a nonzero calldata byte (EIP-2028).
const NONZERO_CALLDATA_BYTE_COST: u64 = 16;

Expand Down
160 changes: 160 additions & 0 deletions crates/edr_provider/tests/integration/eip8038.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#![cfg(feature = "test-utils")]

//! EIP-8038: State-Access Gas Cost Update.
//! see <https://eips.ethereum.org/EIPS/eip-8038>
//!
//! From Amsterdam onward, state-access operations are repriced and the
//! storage-clear refund grows. revm prices them. These tests only check that
//! the repricing activates with the hardfork, by comparing costs on Osaka and
//! on Amsterdam.
//!
//! An operation's cost is the `gasUsed` of calling the `StateAccess` function
//! performing it, less the `gasUsed` of calling its `noop`, so the intrinsic
//! gas (also repriced in Amsterdam, by EIP-2780) and the contract's overhead
//! cancel out. The cost's change from Osaka to Amsterdam then follows from
//! the EIP's parameters alone.

use std::str::FromStr as _;

use edr_chain_l1::{rpc::TransactionRequest, L1ChainSpec};
use edr_primitives::{address, keccak256, Address, Bytes, U256};
use edr_provider::{test_utils::deploy_contract, Provider};
use revm_primitives::eip8038::{
COLD_ACCOUNT_ACCESS, COLD_STORAGE_ACCESS, STORAGE_CLEAR_REFUND, STORAGE_WRITE, WARM_ACCESS,
};

use crate::common::provider::{gas_used_by_sending, new_provider};

const STATE_ACCESS_BYTECODE: &str =
include_str!("../../../../data/deployment_bytecode/StateAccess.bin");

const SENDER: Address = address!("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

// The EIP's table values before Amsterdam.
const PRE_AMSTERDAM_COLD_ACCOUNT_ACCESS: u64 = 2_600;
const PRE_AMSTERDAM_STORAGE_WRITE: u64 = 2_800;
const PRE_AMSTERDAM_STORAGE_CLEAR_REFUND: u64 = 4_800;

/// A cold `SSTORE` changing a nonzero slot. Before Amsterdam the warm base is
/// charged on top of the cold access; from Amsterdam it is folded into it.
const PRE_AMSTERDAM_COLD_SLOT_CHANGE: u64 =
COLD_STORAGE_ACCESS + WARM_ACCESS + PRE_AMSTERDAM_STORAGE_WRITE;
const COLD_SLOT_CHANGE: u64 = COLD_STORAGE_ACCESS + STORAGE_WRITE;

/// The deployed `StateAccess` contract on a provider.
struct StateAccess {
address: Address,
provider: Provider<L1ChainSpec>,
}

impl StateAccess {
fn deploy(hardfork: edr_chain_l1::Hardfork) -> anyhow::Result<Self> {
let provider = new_provider(hardfork)?;
let address = deploy_contract(&provider, SENDER, Bytes::from_str(STATE_ACCESS_BYTECODE)?)?;

Ok(Self { address, provider })
}

/// Calls the function with the given signature and arguments and returns
/// the `gasUsed` of the receipt.
fn gas_used_by_calling(&self, signature: &str, arguments: &[U256]) -> anyhow::Result<u64> {
let mut data: Vec<u8> = keccak256(signature).into_iter().take(4).collect();
for argument in arguments {
data.extend_from_slice(&argument.to_be_bytes::<32>());
}

gas_used_by_sending(
&self.provider,
TransactionRequest {
from: SENDER,
to: Some(self.address),
data: Some(data.into()),
..TransactionRequest::default()
},
)
}

/// The gas the function's operation costs on top of a `noop` call.
fn operation_cost(&self, signature: &str, arguments: &[U256]) -> anyhow::Result<u64> {
let gas_used = self.gas_used_by_calling(signature, arguments)?;
let baseline = self.gas_used_by_calling("noop()", &[])?;

Ok(gas_used - baseline)
}

/// The cost of writing `value` to the contract's slot.
fn write_cost(&self, value: u64) -> anyhow::Result<u64> {
self.operation_cost("write(uint256)", &[U256::from(value)])
}

/// Sets the slot to a nonzero value (1).
fn seed_slot(&self) -> anyhow::Result<()> {
self.gas_used_by_calling("write(uint256)", &[U256::from(1)])?;

Ok(())
}

/// The refund earned by clearing the slot, as the cost of changing it minus
/// the cost of clearing it.
fn storage_clear_refund(&self) -> anyhow::Result<u64> {
let change_cost = self.write_cost(2)?;
let clear_cost = self.write_cost(0)?;

Ok(change_cost - clear_cost)
}
}

#[tokio::test(flavor = "multi_thread")]
async fn cold_account_access_costs_more_from_amsterdam() -> anyhow::Result<()> {
let osaka = StateAccess::deploy(edr_chain_l1::Hardfork::Osaka)?;
let osaka_cost = osaka.operation_cost("readBalance()", &[])?;

let amsterdam = StateAccess::deploy(edr_chain_l1::Hardfork::Amsterdam)?;
let amsterdam_cost = amsterdam.operation_cost("readBalance()", &[])?;

assert_eq!(
amsterdam_cost,
osaka_cost + (COLD_ACCOUNT_ACCESS - PRE_AMSTERDAM_COLD_ACCOUNT_ACCESS),
"a cold account access should cost more from Amsterdam"
);

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn changing_a_slot_costs_more_from_amsterdam() -> anyhow::Result<()> {
let osaka = StateAccess::deploy(edr_chain_l1::Hardfork::Osaka)?;
osaka.seed_slot()?;
let osaka_cost = osaka.write_cost(2)?;

let amsterdam = StateAccess::deploy(edr_chain_l1::Hardfork::Amsterdam)?;
amsterdam.seed_slot()?;
let amsterdam_cost = amsterdam.write_cost(2)?;

assert_eq!(
amsterdam_cost,
osaka_cost + (COLD_SLOT_CHANGE - PRE_AMSTERDAM_COLD_SLOT_CHANGE),
"changing a slot should cost more from Amsterdam"
);

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn clearing_a_slot_refunds_more_from_amsterdam() -> anyhow::Result<()> {
let osaka = StateAccess::deploy(edr_chain_l1::Hardfork::Osaka)?;
osaka.seed_slot()?;
let osaka_refund = osaka.storage_clear_refund()?;

let amsterdam = StateAccess::deploy(edr_chain_l1::Hardfork::Amsterdam)?;
amsterdam.seed_slot()?;
let amsterdam_refund = amsterdam.storage_clear_refund()?;

assert_eq!(
amsterdam_refund,
osaka_refund + (STORAGE_CLEAR_REFUND - PRE_AMSTERDAM_STORAGE_CLEAR_REFUND),
"clearing a slot should refund more from Amsterdam"
);

Ok(())
}
1 change: 1 addition & 0 deletions crates/edr_provider/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod eip7981;
mod eip7997;
mod eip8024;
mod eip8037;
mod eip8038;
mod eip8246;
mod estimate_gas;
mod eth_get_proof;
Expand Down
33 changes: 33 additions & 0 deletions data/contracts/test/StateAccess.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

// State-access operations repriced by EIP-8038, each behind its own function.
// `noop` is the baseline their cost is measured against.
contract StateAccess {
uint256 private slot;

function noop() external {
burnGas();
}

function readBalance() external view returns (uint256) {
burnGas();
// An account outside the genesis state, so the access is cold.
return address(0xaa).balance;
}

function write(uint256 value) external {
burnGas();
slot = value;
}

// Expands memory to 4,096 words, burning execution gas that EIP-8038
// leaves unchanged. Every call pays it, so neither the calldata floor
// (EIP-7976) nor EIP-3529's cap on refunds of a fifth of the gas used
// distorts a measurement.
function burnGas() private pure {
assembly ("memory-safe") {
mstore(0x1ffe0, 0)
}
}
}
1 change: 1 addition & 0 deletions data/deployment_bytecode/StateAccess.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x6080604052348015600e575f5ffd5b506101918061001c5f395ff3fe608060405234801561000f575f5ffd5b506004361061003f575f3560e01c80632f048afa146100435780635dfc2e4a1461005f57806369a2800614610069575b5f5ffd5b61005d60048036038101906100589190610108565b610087565b005b610067610098565b005b6100716100a2565b60405161007e9190610142565b60405180910390f35b61008f6100c9565b805f8190555050565b6100a06100c9565b565b5f6100ab6100c9565b60aa73ffffffffffffffffffffffffffffffffffffffff1631905090565b5f6201ffe052565b5f5ffd5b5f819050919050565b6100e7816100d5565b81146100f1575f5ffd5b50565b5f81359050610102816100de565b92915050565b5f6020828403121561011d5761011c6100d1565b5b5f61012a848285016100f4565b91505092915050565b61013c816100d5565b82525050565b5f6020820190506101555f830184610133565b9291505056fea2646970667358221220f5aa47886db54d0d67463281b46847473ce612d40364dae628c32355a0a10b9464736f6c634300081e0033
Loading