EIP-4762 Statelessness gas cost changes
No call decision · Fees & gas · EIP status Draft · created 2022-02-03
Ahead of introducing the Verkle tree, this repricing aligns the gas costs of reading and writing state with the cost of generating witnesses, and requires clients to restructure their databases to match the new scheme.
Why it is needed
Ethereum is preparing a major shift of its state storage structure to the Verkle tree. In this structure, block validation requires generating a witness, the bundle of data needed for validation, but current gas costs do not reflect the cost of producing that witness. This EIP therefore reprices gas costs in the fork immediately preceding the Verkle tree fork. The goal is to give dapp developers enough time to adapt their contracts to the new storage model and to push client developers to migrate their database formats before the Verkle fork. At the same time, this change makes reading and writing multiple values within the same branch cheaper, and if clients leave their current database structure unchanged, this could open a denial-of-service (DoS) attack vector. The proposal therefore also requires restructuring the database alongside the gas changes.
What changes
Today
- Reading any storage slot for the first time costs 2100 gas, regardless of location
- There is no per-chunk access cost for executing or reading contract code
- SSTORE follows the multi-tier cost rules from EIP-2200
- A CALL that sends value incurs extra gas
After EIP-4762
- Access cost for adjacent slots and slots 0 through 63 drops from 2100 gas to 200 gas
- Code also incurs a 200 gas access cost per 31-byte chunk
- SSTORE cost is replaced by witness-based write costs, keeping only SLOAD_GAS
- The extra gas for value-sending CALL is removed, and CREATE and CREATE2 drop to 1000 gas
Who is affected
- Dapp developers: Contracts with large code may see gas increase due to chunk access costs, while designs using low-numbered or adjacent slots become cheaper. Adjustments are needed to fit the new storage model.
- Client teams: Database structure must change to colocate values belonging to the same stem. Leaving it unchanged could turn the newly cheaper access into a DoS vector.
- Users: There are estimates that code chunk costs could raise average gas usage by roughly 6 to 12 percent, but the source expects the storage access reduction to offset much of that.
- Compiler developers: The source notes that adapting code generation to the new cost rules could further improve efficiency.
In depth
Account header access events
An access event of (address, 0, BASIC_DATA_LEAF_KEY) occurs when an address that is neither a precompile nor a system contract is the target of *CALL, CALLCODE, SELFDESTRUCT, EXTCODESIZE, or EXTCODECOPY; when initcode execution begins for contract creation; when any address is the target of BALANCE; or when a deployed contract calls CODECOPY. A *CALL, CALLCODE, or SELFDESTRUCT that sends nonzero wei additionally generates a (caller, 0, BASIC_DATA_LEAF_KEY) event, even if the counterparty is a precompile or system contract. EXTCODEHASH generates (address, 0, CODEHASH_LEAF_KEY), except for precompiles and system contracts, whose hashes are already known. When a contract is created, both the BASIC_DATA_LEAF_KEY and CODEHASH_LEAF_KEY events occur. Checking whether a callee exists is done by checking whether the corresponding stem has an extension and suffix tree, not by relying on CODEHASH_LEAF_KEY.
Access rules for storage and code chunks
Storage locations are determined by get_storage_slot_tree_keys. If the slot number is less than CODE_OFFSET - HEADER_STORAGE_OFFSET, HEADER_STORAGE_OFFSET is added; otherwise MAIN_STORAGE_OFFSET is added; the quotient and remainder when divided by 256 become tree_key and sub_key. For code, a chunk PC // CHUNK_SIZE is considered accessed only when PC < len(code) during execution. A JUMP destination counts as accessed even if it is not a jumpdest or falls inside pushdata, but the destination of a JUMPI whose condition is false, a JUMP that runs out of gas, a destination outside the code's range, or execution halting past the end of the code do not count as accessed. PUSH{n} accesses every chunk from PC // CHUNK_SIZE through (PC + n) // CHUNK_SIZE. CODECOPY and EXTCODECOPY with a nonzero read size access every chunk spanning the bytes read, while CODESIZE, EXTCODESIZE, and EXTCODEHASH access no chunks at all.
Write events
A write event occurs whenever a write happens, and write events are a subset of access events. CALL, CALLCODE, and SELFDESTRUCT that send a nonzero balance create (address, 0, BASIC_DATA_LEAF_KEY) write events for both the sender and the recipient, and if the recipient address has no account, a (callee, 0, CODEHASH_LEAF_KEY) write event is also added. When contract creation begins, write events occur for BASIC_DATA_LEAF_KEY and CODEHASH_LEAF_KEY. SSTORE creates write events by computing tree_key and sub_key the same way as storage access. For contract creation code, write events occur for (address, (CODE_OFFSET + i) // VERKLE_NODE_WIDTH, (CODE_OFFSET + i) % VERKLE_NODE_WIDTH) for i ranging from 0 to (len(code)+30)//31. Before this EIP, code had no access list, so no warm cost was charged for code access.
Witness gas constants and the four sets
Among existing costs, the extra cost of a value-sending CALL, the SSTORE cost from EIP-2200 excluding SLOAD_GAS, the 200-gas-per-byte contract code cost, and the value-transfer-related CALLCODE cost are removed, and CREATE and CREATE2 are lowered to 1000 gas. The new constants are WITNESS_BRANCH_COST at 1900, WITNESS_CHUNK_COST at 200, SUBTREE_EDIT_COST at 3000, CHUNK_EDIT_COST at 500, and CHUNK_FILL_COST at 6200. During transaction execution, four sets are maintained: accessed_subtrees, accessed_leaves, edited_subtrees, and edited_leaves, and the corresponding cost is only charged the first time a subtree or leaf not already in the set is touched. When writing to a slot that previously held no value (None), CHUNK_FILL_COST is additionally charged. Tree keys can no longer be emptied, so 0 and None are distinguished, and once a key becomes non-None it never reverts to None. If there is not enough gas to pay an event's cost, all remaining gas is consumed, and only the value that could be covered is added to the witness.
Transaction-level events and the 1/64 gas reservation
The transaction itself creates access events for the BASIC_DATA_LEAF_KEY and CODEHASH_LEAF_KEY of tx.origin and tx.target, a write event for tx.origin's BASIC_DATA_LEAF_KEY, and if value is nonzero, a write event for tx.target as well. However, the subtree and leaf charging steps are not applied to these transaction-level events. CREATE*, and *CALL reserve 1/64 of the gas before nested execution; to align with the behavior from the access-list era, CALL, CALLCODE, DELEGATECALL, and STATICCALL check the 1/64 reservation after first charging the witness cost, while CREATE and CREATE2 subtract the 1/64 first and then charge the witness cost.
Block-level processing and system contracts
At the start of a transaction, none of the precompile accounts, system contract accounts, slots of system contracts accessed during system calls, the coinbase account, or the withdrawal account are in a warm state. When a system contract is called via a system call, or called to process a precompile or an opcode, the code chunk and account header accesses for it are not included in the witness, since the client is assumed to already know or cache them. All other accesses, and all writes, must appear in the witness. In terms of cost, witness costs are charged for calls made to process a precompile or an opcode, but not for system calls.
Basis for the cost figures and database redesign
WITNESS_CHUNK_COST was set at 6.25 gas per byte of chunk, and WITNESS_BRANCH_COST was set so that, assuming a 144-byte branch, the average cost is about 13.2 gas per byte, while the worst case, where an attacker fills keys to maximize proof length, comes to about 2.5 gas. Compared to Berlin, the newly added 200 gas per 31-byte code chunk is estimated to increase average gas usage by roughly 6 to 12 percent, and this loss is expected to be largely offset since access to adjacent slots and slots 0 through 63 drops from 2100 to 200. This EIP requires a hard fork, and the reduced economic viability of some apps due to code chunk costs can be mitigated by raising the gas limit at the same time. On the security side, the commitment scheme and data storage need to be logically separated, and the database needs to be restructured so that the 256 leaves of the same stem (32 bytes each) are stored together like an 8kB BLOB; otherwise, access to the same stem, which has become cheap, could become a DoS vector.
What remains undecided
This EIP's status is Draft, and the figures and rules in the specification may still change. The account abstraction section of the source text also leaves a TODO pending a final decision between EIP-7702 and EIP-3074. The Motivation section only states that the target is the fork immediately preceding the Verkle tree fork, and the provided call records contain no decision on whether this EIP will be included in that fork.