eips.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 2200: enable2200,
  25. 1884: enable1884,
  26. 1344: enable1344,
  27. 2315: enable2315,
  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. valid: true,
  69. }
  70. }
  71. func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  72. balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(callContext.contract.Address()))
  73. callContext.stack.push(balance)
  74. return nil, nil
  75. }
  76. // enable1344 applies EIP-1344 (ChainID Opcode)
  77. // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
  78. func enable1344(jt *JumpTable) {
  79. // New opcode
  80. jt[CHAINID] = operation{
  81. execute: opChainID,
  82. constantGas: GasQuickStep,
  83. minStack: minStack(0, 1),
  84. maxStack: maxStack(0, 1),
  85. valid: true,
  86. }
  87. }
  88. // opChainID implements CHAINID opcode
  89. func opChainID(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  90. chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID)
  91. callContext.stack.push(chainId)
  92. return nil, nil
  93. }
  94. // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
  95. func enable2200(jt *JumpTable) {
  96. jt[SLOAD].constantGas = params.SloadGasEIP2200
  97. jt[SSTORE].dynamicGas = gasSStoreEIP2200
  98. }
  99. // enable2315 applies EIP-2315 (Simple Subroutines)
  100. // - Adds opcodes that jump to and return from subroutines
  101. func enable2315(jt *JumpTable) {
  102. // New opcode
  103. jt[BEGINSUB] = operation{
  104. execute: opBeginSub,
  105. constantGas: GasQuickStep,
  106. minStack: minStack(0, 0),
  107. maxStack: maxStack(0, 0),
  108. valid: true,
  109. }
  110. // New opcode
  111. jt[JUMPSUB] = operation{
  112. execute: opJumpSub,
  113. constantGas: GasSlowStep,
  114. minStack: minStack(1, 0),
  115. maxStack: maxStack(1, 0),
  116. jumps: true,
  117. valid: true,
  118. }
  119. // New opcode
  120. jt[RETURNSUB] = operation{
  121. execute: opReturnSub,
  122. constantGas: GasFastStep,
  123. minStack: minStack(0, 0),
  124. maxStack: maxStack(0, 0),
  125. valid: true,
  126. jumps: true,
  127. }
  128. }