eips.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. default:
  33. return fmt.Errorf("undefined eip %d", eipNum)
  34. }
  35. return nil
  36. }
  37. // enable1884 applies EIP-1884 to the given jump table:
  38. // - Increase cost of BALANCE to 700
  39. // - Increase cost of EXTCODEHASH to 700
  40. // - Increase cost of SLOAD to 800
  41. // - Define SELFBALANCE, with cost GasFastStep (5)
  42. func enable1884(jt *JumpTable) {
  43. // Gas cost changes
  44. jt[SLOAD].constantGas = params.SloadGasEIP1884
  45. jt[BALANCE].constantGas = params.BalanceGasEIP1884
  46. jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
  47. // New opcode
  48. jt[SELFBALANCE] = operation{
  49. execute: opSelfBalance,
  50. constantGas: GasFastStep,
  51. minStack: minStack(0, 1),
  52. maxStack: maxStack(0, 1),
  53. valid: true,
  54. }
  55. }
  56. func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  57. balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(callContext.contract.Address()))
  58. callContext.stack.push(balance)
  59. return nil, nil
  60. }
  61. // enable1344 applies EIP-1344 (ChainID Opcode)
  62. // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
  63. func enable1344(jt *JumpTable) {
  64. // New opcode
  65. jt[CHAINID] = operation{
  66. execute: opChainID,
  67. constantGas: GasQuickStep,
  68. minStack: minStack(0, 1),
  69. maxStack: maxStack(0, 1),
  70. valid: true,
  71. }
  72. }
  73. // opChainID implements CHAINID opcode
  74. func opChainID(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
  75. chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID)
  76. callContext.stack.push(chainId)
  77. return nil, nil
  78. }
  79. // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
  80. func enable2200(jt *JumpTable) {
  81. jt[SLOAD].constantGas = params.SloadGasEIP2200
  82. jt[SSTORE].dynamicGas = gasSStoreEIP2200
  83. }