gas_table.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // Copyright 2017 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. "errors"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/math"
  21. "github.com/ethereum/go-ethereum/params"
  22. )
  23. // memoryGasCost calculates the quadratic gas for memory expansion. It does so
  24. // only for the memory region that is expanded, not the total memory.
  25. func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
  26. if newMemSize == 0 {
  27. return 0, nil
  28. }
  29. // The maximum that will fit in a uint64 is max_word_count - 1. Anything above
  30. // that will result in an overflow. Additionally, a newMemSize which results in
  31. // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to
  32. // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used
  33. // without overflowing the gas calculation.
  34. if newMemSize > 0x1FFFFFFFE0 {
  35. return 0, ErrGasUintOverflow
  36. }
  37. newMemSizeWords := toWordSize(newMemSize)
  38. newMemSize = newMemSizeWords * 32
  39. if newMemSize > uint64(mem.Len()) {
  40. square := newMemSizeWords * newMemSizeWords
  41. linCoef := newMemSizeWords * params.MemoryGas
  42. quadCoef := square / params.QuadCoeffDiv
  43. newTotalFee := linCoef + quadCoef
  44. fee := newTotalFee - mem.lastGasCost
  45. mem.lastGasCost = newTotalFee
  46. return fee, nil
  47. }
  48. return 0, nil
  49. }
  50. // memoryCopierGas creates the gas functions for the following opcodes, and takes
  51. // the stack position of the operand which determines the size of the data to copy
  52. // as argument:
  53. // CALLDATACOPY (stack position 2)
  54. // CODECOPY (stack position 2)
  55. // EXTCODECOPY (stack poition 3)
  56. // RETURNDATACOPY (stack position 2)
  57. func memoryCopierGas(stackpos int) gasFunc {
  58. return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  59. // Gas for expanding the memory
  60. gas, err := memoryGasCost(mem, memorySize)
  61. if err != nil {
  62. return 0, err
  63. }
  64. // And gas for copying data, charged per word at param.CopyGas
  65. words, overflow := stack.Back(stackpos).Uint64WithOverflow()
  66. if overflow {
  67. return 0, ErrGasUintOverflow
  68. }
  69. if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
  70. return 0, ErrGasUintOverflow
  71. }
  72. if gas, overflow = math.SafeAdd(gas, words); overflow {
  73. return 0, ErrGasUintOverflow
  74. }
  75. return gas, nil
  76. }
  77. }
  78. var (
  79. gasCallDataCopy = memoryCopierGas(2)
  80. gasCodeCopy = memoryCopierGas(2)
  81. gasExtCodeCopy = memoryCopierGas(3)
  82. gasReturnDataCopy = memoryCopierGas(2)
  83. )
  84. func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  85. var (
  86. db = getDualState(evm, contract.Address())
  87. y, x = stack.Back(1), stack.Back(0)
  88. current = db.GetState(contract.Address(), x.Bytes32())
  89. )
  90. // The legacy gas metering only takes into consideration the current state
  91. // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283)
  92. // OR Constantinople is not active
  93. if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople {
  94. // This checks for 3 scenario's and calculates gas accordingly:
  95. //
  96. // 1. From a zero-value address to a non-zero value (NEW VALUE)
  97. // 2. From a non-zero value address to a zero-value address (DELETE)
  98. // 3. From a non-zero to a non-zero (CHANGE)
  99. switch {
  100. case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0
  101. return params.SstoreSetGas, nil
  102. case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0
  103. evm.StateDB.AddRefund(params.SstoreRefundGas)
  104. return params.SstoreClearGas, nil
  105. default: // non 0 => non 0 (or 0 => 0)
  106. return params.SstoreResetGas, nil
  107. }
  108. }
  109. // The new gas metering is based on net gas costs (EIP-1283):
  110. //
  111. // 1. If current value equals new value (this is a no-op), 200 gas is deducted.
  112. // 2. If current value does not equal new value
  113. // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
  114. // 2.1.1. If original value is 0, 20000 gas is deducted.
  115. // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
  116. // 2.2. If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
  117. // 2.2.1. If original value is not 0
  118. // 2.2.1.1. If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
  119. // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
  120. // 2.2.2. If original value equals new value (this storage slot is reset)
  121. // 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
  122. // 2.2.2.2. Otherwise, add 4800 gas to refund counter.
  123. value := common.Hash(y.Bytes32())
  124. if current == value { // noop (1)
  125. return params.NetSstoreNoopGas, nil
  126. }
  127. original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
  128. if original == current {
  129. if original == (common.Hash{}) { // create slot (2.1.1)
  130. return params.NetSstoreInitGas, nil
  131. }
  132. if value == (common.Hash{}) { // delete slot (2.1.2b)
  133. evm.StateDB.AddRefund(params.NetSstoreClearRefund)
  134. }
  135. return params.NetSstoreCleanGas, nil // write existing slot (2.1.2)
  136. }
  137. if original != (common.Hash{}) {
  138. if current == (common.Hash{}) { // recreate slot (2.2.1.1)
  139. evm.StateDB.SubRefund(params.NetSstoreClearRefund)
  140. } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
  141. evm.StateDB.AddRefund(params.NetSstoreClearRefund)
  142. }
  143. }
  144. if original == value {
  145. if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
  146. evm.StateDB.AddRefund(params.NetSstoreResetClearRefund)
  147. } else { // reset to original existing slot (2.2.2.2)
  148. evm.StateDB.AddRefund(params.NetSstoreResetRefund)
  149. }
  150. }
  151. return params.NetSstoreDirtyGas, nil
  152. }
  153. // 0. If *gasleft* is less than or equal to 2300, fail the current call.
  154. // 1. If current value equals new value (this is a no-op), SLOAD_GAS is deducted.
  155. // 2. If current value does not equal new value:
  156. // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context):
  157. // 2.1.1. If original value is 0, SSTORE_SET_GAS (20K) gas is deducted.
  158. // 2.1.2. Otherwise, SSTORE_RESET_GAS gas is deducted. If new value is 0, add SSTORE_CLEARS_SCHEDULE to refund counter.
  159. // 2.2. If original value does not equal current value (this storage slot is dirty), SLOAD_GAS gas is deducted. Apply both of the following clauses:
  160. // 2.2.1. If original value is not 0:
  161. // 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEARS_SCHEDULE gas from refund counter.
  162. // 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEARS_SCHEDULE gas to refund counter.
  163. // 2.2.2. If original value equals new value (this storage slot is reset):
  164. // 2.2.2.1. If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter.
  165. // 2.2.2.2. Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter.
  166. func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  167. // If we fail the minimum gas availability invariant, fail (0)
  168. if contract.Gas <= params.SstoreSentryGasEIP2200 {
  169. return 0, errors.New("not enough gas for reentrancy sentry")
  170. }
  171. // Gas sentry honoured, do the actual gas calculation based on the stored value
  172. var (
  173. y, x = stack.Back(1), stack.Back(0)
  174. current = evm.StateDB.GetState(contract.Address(), x.Bytes32())
  175. )
  176. value := common.Hash(y.Bytes32())
  177. if current == value { // noop (1)
  178. return params.SloadGasEIP2200, nil
  179. }
  180. original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
  181. if original == current {
  182. if original == (common.Hash{}) { // create slot (2.1.1)
  183. return params.SstoreSetGasEIP2200, nil
  184. }
  185. if value == (common.Hash{}) { // delete slot (2.1.2b)
  186. evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200)
  187. }
  188. return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2)
  189. }
  190. if original != (common.Hash{}) {
  191. if current == (common.Hash{}) { // recreate slot (2.2.1.1)
  192. evm.StateDB.SubRefund(params.SstoreClearsScheduleRefundEIP2200)
  193. } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
  194. evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200)
  195. }
  196. }
  197. if original == value {
  198. if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
  199. evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
  200. } else { // reset to original existing slot (2.2.2.2)
  201. evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
  202. }
  203. }
  204. return params.SloadGasEIP2200, nil // dirty update (2.2)
  205. }
  206. func makeGasLog(n uint64) gasFunc {
  207. return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  208. requestedSize, overflow := stack.Back(1).Uint64WithOverflow()
  209. if overflow {
  210. return 0, ErrGasUintOverflow
  211. }
  212. gas, err := memoryGasCost(mem, memorySize)
  213. if err != nil {
  214. return 0, err
  215. }
  216. if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
  217. return 0, ErrGasUintOverflow
  218. }
  219. if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
  220. return 0, ErrGasUintOverflow
  221. }
  222. var memorySizeGas uint64
  223. if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
  224. return 0, ErrGasUintOverflow
  225. }
  226. if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
  227. return 0, ErrGasUintOverflow
  228. }
  229. return gas, nil
  230. }
  231. }
  232. func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  233. gas, err := memoryGasCost(mem, memorySize)
  234. if err != nil {
  235. return 0, err
  236. }
  237. wordGas, overflow := stack.Back(1).Uint64WithOverflow()
  238. if overflow {
  239. return 0, ErrGasUintOverflow
  240. }
  241. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
  242. return 0, ErrGasUintOverflow
  243. }
  244. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  245. return 0, ErrGasUintOverflow
  246. }
  247. return gas, nil
  248. }
  249. // pureMemoryGascost is used by several operations, which aside from their
  250. // static cost have a dynamic cost which is solely based on the memory
  251. // expansion
  252. func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  253. return memoryGasCost(mem, memorySize)
  254. }
  255. var (
  256. gasReturn = pureMemoryGascost
  257. gasRevert = pureMemoryGascost
  258. gasMLoad = pureMemoryGascost
  259. gasMStore8 = pureMemoryGascost
  260. gasMStore = pureMemoryGascost
  261. gasCreate = pureMemoryGascost
  262. )
  263. func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  264. gas, err := memoryGasCost(mem, memorySize)
  265. if err != nil {
  266. return 0, err
  267. }
  268. wordGas, overflow := stack.Back(2).Uint64WithOverflow()
  269. if overflow {
  270. return 0, ErrGasUintOverflow
  271. }
  272. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
  273. return 0, ErrGasUintOverflow
  274. }
  275. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  276. return 0, ErrGasUintOverflow
  277. }
  278. return gas, nil
  279. }
  280. func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  281. expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
  282. var (
  283. gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas
  284. overflow bool
  285. )
  286. if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
  287. return 0, ErrGasUintOverflow
  288. }
  289. return gas, nil
  290. }
  291. func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  292. expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
  293. var (
  294. gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas
  295. overflow bool
  296. )
  297. if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
  298. return 0, ErrGasUintOverflow
  299. }
  300. return gas, nil
  301. }
  302. func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  303. var (
  304. gas uint64
  305. transfersValue = !stack.Back(2).IsZero()
  306. address = common.Address(stack.Back(1).Bytes20())
  307. )
  308. if evm.chainRules.IsEIP158 {
  309. if transfersValue && getDualState(evm, address).Empty(address) {
  310. gas += params.CallNewAccountGas
  311. }
  312. } else if !getDualState(evm, address).Exist(address) {
  313. gas += params.CallNewAccountGas
  314. }
  315. if transfersValue {
  316. gas += params.CallValueTransferGas
  317. }
  318. memoryGas, err := memoryGasCost(mem, memorySize)
  319. if err != nil {
  320. return 0, err
  321. }
  322. var overflow bool
  323. if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
  324. return 0, ErrGasUintOverflow
  325. }
  326. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  327. if err != nil {
  328. return 0, err
  329. }
  330. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  331. return 0, ErrGasUintOverflow
  332. }
  333. return gas, nil
  334. }
  335. func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  336. memoryGas, err := memoryGasCost(mem, memorySize)
  337. if err != nil {
  338. return 0, err
  339. }
  340. var (
  341. gas uint64
  342. overflow bool
  343. )
  344. if stack.Back(2).Sign() != 0 {
  345. gas += params.CallValueTransferGas
  346. }
  347. if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
  348. return 0, ErrGasUintOverflow
  349. }
  350. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  351. if err != nil {
  352. return 0, err
  353. }
  354. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  355. return 0, ErrGasUintOverflow
  356. }
  357. return gas, nil
  358. }
  359. func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  360. gas, err := memoryGasCost(mem, memorySize)
  361. if err != nil {
  362. return 0, err
  363. }
  364. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  365. if err != nil {
  366. return 0, err
  367. }
  368. var overflow bool
  369. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  370. return 0, ErrGasUintOverflow
  371. }
  372. return gas, nil
  373. }
  374. func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  375. gas, err := memoryGasCost(mem, memorySize)
  376. if err != nil {
  377. return 0, err
  378. }
  379. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  380. if err != nil {
  381. return 0, err
  382. }
  383. var overflow bool
  384. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  385. return 0, ErrGasUintOverflow
  386. }
  387. return gas, nil
  388. }
  389. func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  390. var gas uint64
  391. // EIP150 homestead gas reprice fork:
  392. if evm.chainRules.IsEIP150 {
  393. gas = params.SelfdestructGasEIP150
  394. var address = common.Address(stack.Back(0).Bytes20())
  395. if evm.chainRules.IsEIP158 {
  396. // if empty and transfers value
  397. if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
  398. gas += params.CreateBySelfdestructGas
  399. }
  400. } else if !evm.StateDB.Exist(address) {
  401. gas += params.CreateBySelfdestructGas
  402. }
  403. }
  404. if !evm.StateDB.HasSuicided(contract.Address()) {
  405. evm.StateDB.AddRefund(params.SelfdestructRefundGas)
  406. }
  407. return gas, nil
  408. }