eips.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "fmt"
  19. "sort"
  20. "github.com/ethereum/go-ethereum/params"
  21. "github.com/holiman/uint256"
  22. )
  23. var activators = map[int]func(*JumpTable){
  24. 2929: enable2929,
  25. 2200: enable2200,
  26. 1884: enable1884,
  27. 1344: enable1344,
  28. }
  29. // EnableEIP enables the given EIP on the config.
  30. // This operation writes in-place, and callers need to ensure that the globally
  31. // defined jump tables are not polluted.
  32. func EnableEIP(eipNum int, jt *JumpTable) error {
  33. enablerFn, ok := activators[eipNum]
  34. if !ok {
  35. return fmt.Errorf("undefined eip %d", eipNum)
  36. }
  37. enablerFn(jt)
  38. return nil
  39. }
  40. func ValidEip(eipNum int) bool {
  41. _, ok := activators[eipNum]
  42. return ok
  43. }
  44. func ActivateableEips() []string {
  45. var nums []string
  46. for k := range activators {
  47. nums = append(nums, fmt.Sprintf("%d", k))
  48. }
  49. sort.Strings(nums)
  50. return nums
  51. }
  52. // enable1884 applies EIP-1884 to the given jump table:
  53. // - Increase cost of BALANCE to 700
  54. // - Increase cost of EXTCODEHASH to 700
  55. // - Increase cost of SLOAD to 800
  56. // - Define SELFBALANCE, with cost GasFastStep (5)
  57. func enable1884(jt *JumpTable) {
  58. // Gas cost changes
  59. jt[SLOAD].constantGas = params.SloadGasEIP1884
  60. jt[BALANCE].constantGas = params.BalanceGasEIP1884
  61. jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
  62. // New opcode
  63. jt[SELFBALANCE] = &operation{
  64. execute: opSelfBalance,
  65. constantGas: GasFastStep,
  66. minStack: minStack(0, 1),
  67. maxStack: maxStack(0, 1),
  68. }
  69. }
  70. func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  71. balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(scope.Contract.Address()))
  72. scope.Stack.push(balance)
  73. return nil, nil
  74. }
  75. // enable1344 applies EIP-1344 (ChainID Opcode)
  76. // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
  77. func enable1344(jt *JumpTable) {
  78. // New opcode
  79. jt[CHAINID] = &operation{
  80. execute: opChainID,
  81. constantGas: GasQuickStep,
  82. minStack: minStack(0, 1),
  83. maxStack: maxStack(0, 1),
  84. }
  85. }
  86. // opChainID implements CHAINID opcode
  87. func opChainID(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  88. chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID)
  89. scope.Stack.push(chainId)
  90. return nil, nil
  91. }
  92. // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
  93. func enable2200(jt *JumpTable) {
  94. jt[SLOAD].constantGas = params.SloadGasEIP2200
  95. jt[SSTORE].dynamicGas = gasSStoreEIP2200
  96. }
  97. // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
  98. // https://eips.ethereum.org/EIPS/eip-2929
  99. func enable2929(jt *JumpTable) {
  100. jt[SSTORE].dynamicGas = gasSStoreEIP2929
  101. jt[SLOAD].constantGas = 0
  102. jt[SLOAD].dynamicGas = gasSLoadEIP2929
  103. jt[EXTCODECOPY].constantGas = WarmStorageReadCostEIP2929
  104. jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
  105. jt[EXTCODESIZE].constantGas = WarmStorageReadCostEIP2929
  106. jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
  107. jt[EXTCODEHASH].constantGas = WarmStorageReadCostEIP2929
  108. jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
  109. jt[BALANCE].constantGas = WarmStorageReadCostEIP2929
  110. jt[BALANCE].dynamicGas = gasEip2929AccountCheck
  111. jt[CALL].constantGas = WarmStorageReadCostEIP2929
  112. jt[CALL].dynamicGas = gasCallEIP2929
  113. jt[CALLCODE].constantGas = WarmStorageReadCostEIP2929
  114. jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
  115. jt[STATICCALL].constantGas = WarmStorageReadCostEIP2929
  116. jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
  117. jt[DELEGATECALL].constantGas = WarmStorageReadCostEIP2929
  118. jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
  119. // This was previously part of the dynamic cost, but we're using it as a constantGas
  120. // factor here
  121. jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
  122. jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
  123. }