gas.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package vm
  2. import "math/big"
  3. type req struct {
  4. stack int
  5. gas *big.Int
  6. }
  7. var (
  8. GasQuickStep = big.NewInt(2)
  9. GasFastestStep = big.NewInt(3)
  10. GasFastStep = big.NewInt(5)
  11. GasMidStep = big.NewInt(8)
  12. GasSlowStep = big.NewInt(10)
  13. GasExtStep = big.NewInt(20)
  14. GasStorageGet = big.NewInt(50)
  15. GasStorageAdd = big.NewInt(20000)
  16. GasStorageMod = big.NewInt(5000)
  17. GasLogBase = big.NewInt(375)
  18. GasLogTopic = big.NewInt(375)
  19. GasLogByte = big.NewInt(8)
  20. GasCreate = big.NewInt(32000)
  21. GasCreateByte = big.NewInt(200)
  22. GasCall = big.NewInt(40)
  23. GasCallValueTransfer = big.NewInt(9000)
  24. GasStipend = big.NewInt(2300)
  25. GasCallNewAccount = big.NewInt(25000)
  26. GasReturn = big.NewInt(0)
  27. GasStop = big.NewInt(0)
  28. GasJumpDest = big.NewInt(1)
  29. RefundStorage = big.NewInt(15000)
  30. RefundSuicide = big.NewInt(24000)
  31. GasMemWord = big.NewInt(3)
  32. GasQuadCoeffDenom = big.NewInt(512)
  33. GasContractByte = big.NewInt(200)
  34. GasTransaction = big.NewInt(21000)
  35. GasTxDataNonzeroByte = big.NewInt(68)
  36. GasTxDataZeroByte = big.NewInt(4)
  37. GasTx = big.NewInt(21000)
  38. GasExp = big.NewInt(10)
  39. GasExpByte = big.NewInt(10)
  40. GasSha3Base = big.NewInt(30)
  41. GasSha3Word = big.NewInt(6)
  42. GasSha256Base = big.NewInt(60)
  43. GasSha256Word = big.NewInt(12)
  44. GasRipemdBase = big.NewInt(600)
  45. GasRipemdWord = big.NewInt(12)
  46. GasEcrecover = big.NewInt(3000)
  47. GasIdentityBase = big.NewInt(15)
  48. GasIdentityWord = big.NewInt(3)
  49. GasCopyWord = big.NewInt(3)
  50. )
  51. var _baseCheck = map[OpCode]req{
  52. // Req stack Gas price
  53. ADD: {2, GasFastestStep},
  54. LT: {2, GasFastestStep},
  55. GT: {2, GasFastestStep},
  56. SLT: {2, GasFastestStep},
  57. SGT: {2, GasFastestStep},
  58. EQ: {2, GasFastestStep},
  59. ISZERO: {1, GasFastestStep},
  60. SUB: {2, GasFastestStep},
  61. AND: {2, GasFastestStep},
  62. OR: {2, GasFastestStep},
  63. XOR: {2, GasFastestStep},
  64. NOT: {1, GasFastestStep},
  65. BYTE: {2, GasFastestStep},
  66. CALLDATALOAD: {1, GasFastestStep},
  67. CALLDATACOPY: {3, GasFastestStep},
  68. MLOAD: {1, GasFastestStep},
  69. MSTORE: {2, GasFastestStep},
  70. MSTORE8: {2, GasFastestStep},
  71. CODECOPY: {3, GasFastestStep},
  72. MUL: {2, GasFastStep},
  73. DIV: {2, GasFastStep},
  74. SDIV: {2, GasFastStep},
  75. MOD: {2, GasFastStep},
  76. SMOD: {2, GasFastStep},
  77. SIGNEXTEND: {2, GasFastStep},
  78. ADDMOD: {3, GasMidStep},
  79. MULMOD: {3, GasMidStep},
  80. JUMP: {1, GasMidStep},
  81. JUMPI: {2, GasSlowStep},
  82. EXP: {2, GasSlowStep},
  83. ADDRESS: {0, GasQuickStep},
  84. ORIGIN: {0, GasQuickStep},
  85. CALLER: {0, GasQuickStep},
  86. CALLVALUE: {0, GasQuickStep},
  87. CODESIZE: {0, GasQuickStep},
  88. GASPRICE: {0, GasQuickStep},
  89. COINBASE: {0, GasQuickStep},
  90. TIMESTAMP: {0, GasQuickStep},
  91. NUMBER: {0, GasQuickStep},
  92. CALLDATASIZE: {0, GasQuickStep},
  93. DIFFICULTY: {0, GasQuickStep},
  94. GASLIMIT: {0, GasQuickStep},
  95. POP: {0, GasQuickStep},
  96. PC: {0, GasQuickStep},
  97. MSIZE: {0, GasQuickStep},
  98. GAS: {0, GasQuickStep},
  99. BLOCKHASH: {1, GasExtStep},
  100. BALANCE: {0, GasExtStep},
  101. EXTCODESIZE: {1, GasExtStep},
  102. EXTCODECOPY: {4, GasExtStep},
  103. SLOAD: {1, GasStorageGet},
  104. SSTORE: {2, Zero},
  105. SHA3: {1, GasSha3Base},
  106. CREATE: {3, GasCreate},
  107. CALL: {7, GasCall},
  108. CALLCODE: {7, GasCall},
  109. JUMPDEST: {0, GasJumpDest},
  110. SUICIDE: {1, Zero},
  111. RETURN: {2, Zero},
  112. }
  113. func baseCheck(op OpCode, stack *stack, gas *big.Int) {
  114. if r, ok := _baseCheck[op]; ok {
  115. stack.require(r.stack)
  116. gas.Add(gas, r.gas)
  117. }
  118. }
  119. func toWordSize(size *big.Int) *big.Int {
  120. tmp := new(big.Int)
  121. tmp.Add(size, u256(31))
  122. tmp.Div(tmp, u256(32))
  123. return tmp
  124. }