eips.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 3855: enable3855,
  25. 3529: enable3529,
  26. 3198: enable3198,
  27. 2929: enable2929,
  28. 2200: enable2200,
  29. 1884: enable1884,
  30. 1344: enable1344,
  31. }
  32. // EnableEIP enables the given EIP on the config.
  33. // This operation writes in-place, and callers need to ensure that the globally
  34. // defined jump tables are not polluted.
  35. func EnableEIP(eipNum int, jt *JumpTable) error {
  36. enablerFn, ok := activators[eipNum]
  37. if !ok {
  38. return fmt.Errorf("undefined eip %d", eipNum)
  39. }
  40. enablerFn(jt)
  41. return nil
  42. }
  43. func ValidEip(eipNum int) bool {
  44. _, ok := activators[eipNum]
  45. return ok
  46. }
  47. func ActivateableEips() []string {
  48. var nums []string
  49. for k := range activators {
  50. nums = append(nums, fmt.Sprintf("%d", k))
  51. }
  52. sort.Strings(nums)
  53. return nums
  54. }
  55. // enable1884 applies EIP-1884 to the given jump table:
  56. // - Increase cost of BALANCE to 700
  57. // - Increase cost of EXTCODEHASH to 700
  58. // - Increase cost of SLOAD to 800
  59. // - Define SELFBALANCE, with cost GasFastStep (5)
  60. func enable1884(jt *JumpTable) {
  61. // Gas cost changes
  62. jt[SLOAD].constantGas = params.SloadGasEIP1884
  63. jt[BALANCE].constantGas = params.BalanceGasEIP1884
  64. jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
  65. // New opcode
  66. jt[SELFBALANCE] = &operation{
  67. execute: opSelfBalance,
  68. constantGas: GasFastStep,
  69. minStack: minStack(0, 1),
  70. maxStack: maxStack(0, 1),
  71. }
  72. }
  73. func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  74. balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(scope.Contract.Address()))
  75. scope.Stack.push(balance)
  76. return nil, nil
  77. }
  78. // enable1344 applies EIP-1344 (ChainID Opcode)
  79. // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
  80. func enable1344(jt *JumpTable) {
  81. // New opcode
  82. jt[CHAINID] = &operation{
  83. execute: opChainID,
  84. constantGas: GasQuickStep,
  85. minStack: minStack(0, 1),
  86. maxStack: maxStack(0, 1),
  87. }
  88. }
  89. // opChainID implements CHAINID opcode
  90. func opChainID(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  91. if interpreter.evm.chainConfig.IsEthPoWFork(interpreter.evm.Context.BlockNumber) {
  92. chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID_ALT)
  93. scope.Stack.push(chainId)
  94. } else {
  95. chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID)
  96. scope.Stack.push(chainId)
  97. }
  98. return nil, nil
  99. }
  100. // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
  101. func enable2200(jt *JumpTable) {
  102. jt[SLOAD].constantGas = params.SloadGasEIP2200
  103. jt[SSTORE].dynamicGas = gasSStoreEIP2200
  104. }
  105. // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
  106. // https://eips.ethereum.org/EIPS/eip-2929
  107. func enable2929(jt *JumpTable) {
  108. jt[SSTORE].dynamicGas = gasSStoreEIP2929
  109. jt[SLOAD].constantGas = 0
  110. jt[SLOAD].dynamicGas = gasSLoadEIP2929
  111. jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929
  112. jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
  113. jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929
  114. jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
  115. jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929
  116. jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
  117. jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929
  118. jt[BALANCE].dynamicGas = gasEip2929AccountCheck
  119. jt[CALL].constantGas = params.WarmStorageReadCostEIP2929
  120. jt[CALL].dynamicGas = gasCallEIP2929
  121. jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929
  122. jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
  123. jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929
  124. jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
  125. jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
  126. jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
  127. // This was previously part of the dynamic cost, but we're using it as a constantGas
  128. // factor here
  129. jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
  130. jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
  131. }
  132. // enable3529 enabled "EIP-3529: Reduction in refunds":
  133. // - Removes refunds for selfdestructs
  134. // - Reduces refunds for SSTORE
  135. // - Reduces max refunds to 20% gas
  136. func enable3529(jt *JumpTable) {
  137. jt[SSTORE].dynamicGas = gasSStoreEIP3529
  138. jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529
  139. }
  140. // enable3198 applies EIP-3198 (BASEFEE Opcode)
  141. // - Adds an opcode that returns the current block's base fee.
  142. func enable3198(jt *JumpTable) {
  143. // New opcode
  144. jt[BASEFEE] = &operation{
  145. execute: opBaseFee,
  146. constantGas: GasQuickStep,
  147. minStack: minStack(0, 1),
  148. maxStack: maxStack(0, 1),
  149. }
  150. }
  151. // opBaseFee implements BASEFEE opcode
  152. func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  153. baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
  154. scope.Stack.push(baseFee)
  155. return nil, nil
  156. }
  157. // enable3855 applies EIP-3855 (PUSH0 opcode)
  158. func enable3855(jt *JumpTable) {
  159. // New opcode
  160. jt[PUSH0] = &operation{
  161. execute: opPush0,
  162. constantGas: GasQuickStep,
  163. minStack: minStack(0, 1),
  164. maxStack: maxStack(0, 1),
  165. }
  166. }
  167. // opPush0 implements the PUSH0 opcode
  168. func opPush0(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
  169. scope.Stack.push(new(uint256.Int))
  170. return nil, nil
  171. }