eips.go 3.6 KB

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