diff --git a/.changeset/eip8038-state-access-gas.md b/.changeset/eip8038-state-access-gas.md new file mode 100644 index 0000000000..161bfb2e36 --- /dev/null +++ b/.changeset/eip8038-state-access-gas.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/edr": patch +--- + +Added experimental EIP-8038 support: from the Amsterdam hardfork, state-access operations are repriced. diff --git a/crates/edr_provider/tests/common/provider.rs b/crates/edr_provider/tests/common/provider.rs index b5174e0a0a..9eb4170ab9 100644 --- a/crates/edr_provider/tests/common/provider.rs +++ b/crates/edr_provider/tests/common/provider.rs @@ -70,6 +70,17 @@ pub fn gas_used(provider: &Provider, 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, + request: TransactionRequest, +) -> anyhow::Result { + 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, request: L1CallRequest) -> u64 { let response = provider diff --git a/crates/edr_provider/tests/integration/eip2780.rs b/crates/edr_provider/tests/integration/eip2780.rs index 9aa5065590..324d0e42c5 100644 --- a/crates/edr_provider/tests/integration/eip2780.rs +++ b/crates/edr_provider/tests/integration/eip2780.rs @@ -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, }, }; @@ -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, - request: TransactionRequest, -) -> anyhow::Result { - 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)); diff --git a/crates/edr_provider/tests/integration/eip7981.rs b/crates/edr_provider/tests/integration/eip7981.rs index c0db11c08d..40e4646023 100644 --- a/crates/edr_provider/tests/integration/eip7981.rs +++ b/crates/edr_provider/tests/integration/eip7981.rs @@ -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}; @@ -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; diff --git a/crates/edr_provider/tests/integration/eip8038.rs b/crates/edr_provider/tests/integration/eip8038.rs new file mode 100644 index 0000000000..bbe557a7d7 --- /dev/null +++ b/crates/edr_provider/tests/integration/eip8038.rs @@ -0,0 +1,160 @@ +#![cfg(feature = "test-utils")] + +//! EIP-8038: State-Access Gas Cost Update. +//! see +//! +//! 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, +} + +impl StateAccess { + fn deploy(hardfork: edr_chain_l1::Hardfork) -> anyhow::Result { + 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 { + let mut data: Vec = 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 { + 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 { + 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 { + 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(()) +} diff --git a/crates/edr_provider/tests/integration/mod.rs b/crates/edr_provider/tests/integration/mod.rs index 40c50e5c89..dca7aa9b0f 100644 --- a/crates/edr_provider/tests/integration/mod.rs +++ b/crates/edr_provider/tests/integration/mod.rs @@ -20,6 +20,7 @@ mod eip7981; mod eip7997; mod eip8024; mod eip8037; +mod eip8038; mod eip8246; mod estimate_gas; mod eth_get_proof; diff --git a/data/contracts/test/StateAccess.sol b/data/contracts/test/StateAccess.sol new file mode 100644 index 0000000000..d42f2d8c47 --- /dev/null +++ b/data/contracts/test/StateAccess.sol @@ -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) + } + } +} diff --git a/data/deployment_bytecode/StateAccess.bin b/data/deployment_bytecode/StateAccess.bin new file mode 100644 index 0000000000..4a85813be2 --- /dev/null +++ b/data/deployment_bytecode/StateAccess.bin @@ -0,0 +1 @@ +0x6080604052348015600e575f5ffd5b506101918061001c5f395ff3fe608060405234801561000f575f5ffd5b506004361061003f575f3560e01c80632f048afa146100435780635dfc2e4a1461005f57806369a2800614610069575b5f5ffd5b61005d60048036038101906100589190610108565b610087565b005b610067610098565b005b6100716100a2565b60405161007e9190610142565b60405180910390f35b61008f6100c9565b805f8190555050565b6100a06100c9565b565b5f6100ab6100c9565b60aa73ffffffffffffffffffffffffffffffffffffffff1631905090565b5f6201ffe052565b5f5ffd5b5f819050919050565b6100e7816100d5565b81146100f1575f5ffd5b50565b5f81359050610102816100de565b92915050565b5f6020828403121561011d5761011c6100d1565b5b5f61012a848285016100f4565b91505092915050565b61013c816100d5565b82525050565b5f6020820190506101555f830184610133565b9291505056fea2646970667358221220f5aa47886db54d0d67463281b46847473ce612d40364dae628c32355a0a10b9464736f6c634300081e0033 \ No newline at end of file