eips.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "github.com/ethereum/go-ethereum/params"
  20. )
  21. // EnableEIP enables the given EIP on the config.
  22. // This operation writes in-place, and callers need to ensure that the globally
  23. // defined jump tables are not polluted.
  24. func EnableEIP(eipNum int, jt *JumpTable) error {
  25. switch eipNum {
  26. case 2200:
  27. enable2200(jt)
  28. case 1884:
  29. enable1884(jt)
  30. case 1344:
  31. enable1344(jt)
  32. case 2315:
  33. enable2315(jt)
  34. default:
  35. return fmt.Errorf("undefined eip %d", eipNum)
  36. }
  37. return nil
  38. }
  39. // enable1884 applies EIP-1884 to the given jump table:
  40. // - Increase cost of BALANCE to 700
  41. // - Increase cost of EXTCODEHASH to 700
  42. // - Increase cost of SLOAD to 800
  43. // - Define SELFBALANCE, with cost GasFastStep (5)
  44. func enable1884(jt *JumpTable) {
  45. // Gas cost changes
  46. jt[SLOAD].constantGas = params.SloadGasEIP1884
  47. jt[BALANCE].constantGas = params.BalanceGasEIP1884
  48. jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
  49. // New opcode
  50. jt[SELFBALANCE] = operation{
  51. execute: opSelfBalance,
  52. constantGas: GasFastStep,
  53. minStack: minStack(0, 1),
  54. maxStack: maxStack(0, 1),
  55. valid: true,
  56. }
  57. }
  58. func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  59. balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(callContext.contract.Address()))
  60. callContext.stack.push(balance)
  61. return nil, nil
  62. }
  63. // enable1344 applies EIP-1344 (ChainID Opcode)
  64. // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
  65. func enable1344(jt *JumpTable) {
  66. // New opcode
  67. jt[CHAINID] = operation{
  68. execute: opChainID,
  69. constantGas: GasQuickStep,
  70. minStack: minStack(0, 1),
  71. maxStack: maxStack(0, 1),
  72. valid: true,
  73. }
  74. }
  75. // opChainID implements CHAINID opcode
  76. func opChainID(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  77. chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID)
  78. callContext.stack.push(chainId)
  79. return nil, nil
  80. }
  81. // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
  82. func enable2200(jt *JumpTable) {
  83. jt[SLOAD].constantGas = params.SloadGasEIP2200
  84. jt[SSTORE].dynamicGas = gasSStoreEIP2200
  85. }
  86. // enable2315 applies EIP-2315 (Simple Subroutines)
  87. // - Adds opcodes that jump to and return from subroutines
  88. func enable2315(jt *JumpTable) {
  89. // New opcode
  90. jt[BEGINSUB] = operation{
  91. execute: opBeginSub,
  92. constantGas: GasQuickStep,
  93. minStack: minStack(0, 0),
  94. maxStack: maxStack(0, 0),
  95. valid: true,
  96. }
  97. // New opcode
  98. jt[JUMPSUB] = operation{
  99. execute: opJumpSub,
  100. constantGas: GasSlowStep,
  101. minStack: minStack(1, 0),
  102. maxStack: maxStack(1, 0),
  103. jumps: true,
  104. valid: true,
  105. }
  106. // New opcode
  107. jt[RETURNSUB] = operation{
  108. execute: opReturnSub,
  109. constantGas: GasFastStep,
  110. minStack: minStack(0, 0),
  111. maxStack: maxStack(0, 0),
  112. valid: true,
  113. jumps: true,
  114. }
  115. }