Skip to content

Instantly share code, notes, and snippets.

@holiman
Last active July 7, 2021 18:55
Show Gist options
  • Save holiman/aa7d2d28a9747548a0a14321a1572b22 to your computer and use it in GitHub Desktop.
Save holiman/aa7d2d28a9747548a0a14321a1572b22 to your computer and use it in GitHub Desktop.

Current (Istanbul) costs

With Istanbul rules, EIP-2200: https://eips.ethereum.org/EIPS/eip-2200#test-cases, the following gas usages apply for the various scenarions below:

Code Used Gas Refund Original 1st 2nd 3rd Effective gas (after refund)
0x60006000556000600055 1612 0 0 0 0 1612
0x60006000556001600055 20812 0 0 0 1 20812
0x60016000556000600055 20812 19200 0 1 0 1612
0x60016000556002600055 20812 0 0 1 2 20812
0x60016000556001600055 20812 0 0 1 1 20812
0x60006000556000600055 5812 15000 1 0 0 -9188
0x60006000556001600055 5812 4200 1 0 1 1612
0x60006000556002600055 5812 0 1 0 2 5812
0x60026000556000600055 5812 15000 1 2 0 -9188
0x60026000556003600055 5812 0 1 2 3 5812
0x60026000556001600055 5812 4200 1 2 1 1612
0x60026000556002600055 5812 0 1 2 2 5812
0x60016000556000600055 5812 15000 1 1 0 -9188
0x60016000556002600055 5812 0 1 1 2 5812
0x60016000556001600055 1612 0 1 1 1 1612
0x600160005560006000556001600055 40818 19200 0 1 0 1 21618
0x600060005560016000556000600055 10818 19200 1 0 1 0 -8382

Berlin costs

With Berlin, and EIP-2929, the gas costs changes. Note, there is a difference between 'hot' and 'cold' slots. This table show the 'hot' values (the difference being a one-time cost of 2000 gas).

Code Used Gas Refund Original 1st 2nd 3rd Effective gas (after refund)
0x60006000556000600055 212 0 0 0 0 212
0x60006000556001600055 20112 0 0 0 1 20112
0x60016000556000600055 20112 19900 0 1 0 212
0x60016000556002600055 20112 0 0 1 2 20112
0x60016000556001600055 20112 0 0 1 1 20112
0x60006000556000600055 3012 15000 1 0 0 -11988
0x60006000556001600055 3012 2800 1 0 1 212
0x60006000556002600055 3012 0 1 0 2 3012
0x60026000556000600055 3012 15000 1 2 0 -11988
0x60026000556003600055 3012 0 1 2 3 3012
0x60026000556001600055 3012 2800 1 2 1 212
0x60026000556002600055 3012 0 1 2 2 3012
0x60016000556000600055 3012 15000 1 1 0 -11988
0x60016000556002600055 3012 0 1 1 2 3012
0x60016000556001600055 212 0 1 1 1 212
0x600160005560006000556001600055 40118 19900 0 1 0 1 20218
0x600060005560016000556000600055 5918 17800 1 0 1 0 -11882

Without refunds

If refunds were to be removed, this would be the comparative table

Code Original 1st 2nd 3rd Istanbul Berlin (cold) Berlin (hot) Berlin (hot)+norefund
0x60006000556000600055 0 0 0 1612 2312 212 212
0x60006000556001600055 0 0 1 20812 22212 20112 20112
0x60016000556000600055 0 1 0 1612 2312 212 20112
0x60016000556002600055 0 1 2 20812 22212 20112 20112
0x60016000556001600055 0 1 1 20812 22212 20112 20112
0x60006000556000600055 1 0 0 -9188 -9888 -11988 3012
0x60006000556001600055 1 0 1 1612 2312 212 3012
0x60006000556002600055 1 0 2 5812 5112 3012 3012
0x60026000556000600055 1 2 0 -9188 -9888 -11988 3012
0x60026000556003600055 1 2 3 5812 5112 3012 3012
0x60026000556001600055 1 2 1 1612 2312 212 3012
0x60026000556002600055 1 2 2 5812 5112 3012 3012
0x60016000556000600055 1 1 0 -9188 -9888 -11988 3012
0x60016000556002600055 1 1 2 5812 5112 3012 3012
0x60016000556001600055 1 1 1 1612 2312 212 212
0x600160005560006000556001600055 0 1 0 1 21618 22318 20218 40118
0x600060005560016000556000600055 1 0 1 0 -8382 -9782 -11882 5918

With EIP-3403 partial refunds

If refunds were to be partially removed, as specified here, this would be the comparative table

Code Used Gas Refund Original 1st 2nd 3rd Effective gas (after refund)
0x60006000556000600055 212 0 0 0 0 212
0x60006000556001600055 20112 0 0 0 1 20112
0x60016000556000600055 20112 19000 0 1 0 1112
0x60016000556002600055 20112 0 0 1 2 20112
0x60016000556001600055 20112 0 0 1 1 20112
0x60006000556000600055 3012 0 1 0 0 3012
0x60006000556001600055 3012 0 1 0 1 3012
0x60006000556002600055 3012 0 1 0 2 3012
0x60026000556000600055 3012 0 1 2 0 3012
0x60026000556003600055 3012 0 1 2 3 3012
0x60026000556001600055 3012 0 1 2 1 3012
0x60026000556002600055 3012 0 1 2 2 3012
0x60016000556000600055 3012 0 1 1 0 3012
0x60016000556002600055 3012 0 1 1 2 3012
0x60016000556001600055 212 0 1 1 1 212
0x600160005560006000556001600055 40118 19000 0 1 0 1 21118
0x600060005560016000556000600055 5918 0 1 0 1 0 5918

The mutex usecase

There are two typical ways to implement mutexes: '0-1-0' and '1-2-1. Let's see how they differ

  • '0-1-0':
    • Istanbul: 1612
    • Berlin: 212
    • NoRefund: 20112
    • EIP-3403: 1112
  • '1-2-1':
    • Istanbul: 1612
    • Berlin: 212
    • NoRefund: 3012
    • EIP-3403: 3012

Note: In reality, there are never a negative gas cost, since the refund is capped at 0.5 * gasUsed. However, these tables show the negative values, since in a more real-world scenario would likely spend the extra gas on other operations.'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment