EIP-2200 - Structured Definitions for Net Gas Metering

Created 2019-07-18
Status Final
Category Core
Type Standards Track
Authors

Simple Summary

This is an EIP that implements net gas metering. It's a combined version of EIP-1283 and EIP-1706, with a structured definition so as to make it interoperable with other gas changes such as EIP-1884.

Abstract

This EIP provides a structured definition of net gas metering changes for SSTORE opcode, enabling new usages for contract storage, and reducing excessive gas costs where it doesn’t match how most implementation works.

This is a combination of EIP-1283 and EIP-1706.

Motivation

This EIP proposes a way for gas metering on SSTORE, using information that is more universally available to most implementations, and require as little change in implementation structures as possible.

Usages that benefits from this EIP’s gas reduction scheme includes:

The original definition of EIP-1283 created a danger of a new kind of reentrancy attacks on existing contracts as Solidity by default grants a "stipend" of 2300 gas to simple transfer calls. This danger is easily mitigated if SSTORE is not allowed in low gasleft state, without breaking the backward compatibility and the original intention of EIP-1283.

This EIP also replaces the original EIP-1283 value definitions of gas by parameters, so that it's more structured, and easier to define changes in the future.

Specification

Define variables SLOAD_GAS, SSTORE_SET_GAS, SSTORE_RESET_GAS and SSTORE_CLEARS_SCHEDULE. The old and new values for those variables are:

Change the definition of EIP-1283 using those variables. The new specification, combining EIP-1283 and EIP-1706, will look like below. The terms original value, current value and new value are defined in EIP-1283.

Replace SSTORE opcode gas cost calculation (including refunds) with the following logic:

An implementation should also note that with the above definition, if the implementation uses call-frame refund counter, the counter can go negative. If the implementation uses transaction-wise refund counter, the counter always stays positive.

Rationale

This EIP mostly achieves what a transient storage tries to do (EIP-1087 and EIP-1153), but without the complexity of introducing the concept of "dirty maps", or an extra storage struct.

Regarding SSTORE gas cost and refunds, see Appendix for proofs of properties that this EIP satisfies.

Examine examples provided in EIP-1087's Motivation (with SLOAD_GAS being 200):

In order to keep in place the implicit reentrancy protection of existing contracts, transactions should not be allowed to modify state if the remaining gas is lower then the gas stipend given to "transfer"/"send" in Solidity. These are other proposed remediations and objections to implementing them:

Backwards Compatibility

This EIP requires a hard fork to implement. No gas cost increase is anticipated, and many contracts will see gas reduction.

Performing SSTORE has never been possible with less than 5000 gas, so it does not introduce incompatibility to the Ethereum Mainnet. Gas estimation should account for this requirement.

Test Cases

Code Used Gas Refund Original 1st 2nd 3rd
0x60006000556000600055 1612 0 0 0 0
0x60006000556001600055 20812 0 0 0 1
0x60016000556000600055 20812 19200 0 1 0
0x60016000556002600055 20812 0 0 1 2
0x60016000556001600055 20812 0 0 1 1
0x60006000556000600055 5812 15000 1 0 0
0x60006000556001600055 5812 4200 1 0 1
0x60006000556002600055 5812 0 1 0 2
0x60026000556000600055 5812 15000 1 2 0
0x60026000556003600055 5812 0 1 2 3
0x60026000556001600055 5812 4200 1 2 1
0x60026000556002600055 5812 0 1 2 2
0x60016000556000600055 5812 15000 1 1 0
0x60016000556002600055 5812 0 1 1 2
0x60016000556001600055 1612 0 1 1 1
0x600160005560006000556001600055 40818 19200 0 1 0 1
0x600060005560016000556000600055 10818 19200 1 0 1 0

Implementation

To be added.

Appendix: Proof

Because the storage slot's original value is defined as the value when a reversion happens on the current transaction, it's easy to see that call frames won't interfere SSTORE gas calculation. So although the below proof is discussed without call frames, it applies to all situations with call frames. We will discuss the case separately for original value being zero and not zero, and use induction to prove some properties of SSTORE gas cost.

Final value is the value of a particular storage slot at the end of a transaction. Absolute gas used is the absolute value of gas used minus refund. We use N to represent the total number of SSTORE operations on a storage slot. For states discussed below, refer to State Transition in Explanation section.

Below we do the proof under the assumption that all parameters are unchanged, meaning SLOAD_GAS is 200. However, note that the proof still applies no matter how SLOAD_GAS is changed.

Original Value Being Zero

When original value is 0, we want to prove that:

Base Case

We always start at state A. The first SSTORE can:

Inductive Step

Original Value Not Being Zero

When original value is not 0, we want to prove that:

Base Case

We always start at state X. The first SSTORE can:

Inductive Step

Copyright

Copyright and related rights waived via CC0.