gas_table.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 := bigUint64(stack.Back(stackpos))
  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. y, x = stack.Back(1), stack.Back(0)
  87. current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
  88. )
  89. // The legacy gas metering only takes into consideration the current state
  90. // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283)
  91. // OR Constantinople is not active
  92. if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople {
  93. // This checks for 3 scenario's and calculates gas accordingly:
  94. //
  95. // 1. From a zero-value address to a non-zero value (NEW VALUE)
  96. // 2. From a non-zero value address to a zero-value address (DELETE)
  97. // 3. From a non-zero to a non-zero (CHANGE)
  98. switch {
  99. case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0
  100. return params.SstoreSetGas, nil
  101. case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0
  102. evm.StateDB.AddRefund(params.SstoreRefundGas)
  103. return params.SstoreClearGas, nil
  104. default: // non 0 => non 0 (or 0 => 0)
  105. return params.SstoreResetGas, nil
  106. }
  107. }
  108. // The new gas metering is based on net gas costs (EIP-1283):
  109. //
  110. // 1. If current value equals new value (this is a no-op), 200 gas is deducted.
  111. // 2. If current value does not equal new value
  112. // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
  113. // 2.1.1. If original value is 0, 20000 gas is deducted.
  114. // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
  115. // 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.
  116. // 2.2.1. If original value is not 0
  117. // 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.
  118. // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
  119. // 2.2.2. If original value equals new value (this storage slot is reset)
  120. // 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
  121. // 2.2.2.2. Otherwise, add 4800 gas to refund counter.
  122. value := common.BigToHash(y)
  123. if current == value { // noop (1)
  124. return params.NetSstoreNoopGas, nil
  125. }
  126. original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x))
  127. if original == current {
  128. if original == (common.Hash{}) { // create slot (2.1.1)
  129. return params.NetSstoreInitGas, nil
  130. }
  131. if value == (common.Hash{}) { // delete slot (2.1.2b)
  132. evm.StateDB.AddRefund(params.NetSstoreClearRefund)
  133. }
  134. return params.NetSstoreCleanGas, nil // write existing slot (2.1.2)
  135. }
  136. if original != (common.Hash{}) {
  137. if current == (common.Hash{}) { // recreate slot (2.2.1.1)
  138. evm.StateDB.SubRefund(params.NetSstoreClearRefund)
  139. } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
  140. evm.StateDB.AddRefund(params.NetSstoreClearRefund)
  141. }
  142. }
  143. if original == value {
  144. if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
  145. evm.StateDB.AddRefund(params.NetSstoreResetClearRefund)
  146. } else { // reset to original existing slot (2.2.2.2)
  147. evm.StateDB.AddRefund(params.NetSstoreResetRefund)
  148. }
  149. }
  150. return params.NetSstoreDirtyGas, nil
  151. }
  152. // 0. If *gasleft* is less than or equal to 2300, fail the current call.
  153. // 1. If current value equals new value (this is a no-op), SSTORE_NOOP_GAS gas is deducted.
  154. // 2. If current value does not equal new value:
  155. // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context):
  156. // 2.1.1. If original value is 0, SSTORE_INIT_GAS gas is deducted.
  157. // 2.1.2. Otherwise, SSTORE_CLEAN_GAS gas is deducted. If new value is 0, add SSTORE_CLEAR_REFUND to refund counter.
  158. // 2.2. If original value does not equal current value (this storage slot is dirty), SSTORE_DIRTY_GAS gas is deducted. Apply both of the following clauses:
  159. // 2.2.1. If original value is not 0:
  160. // 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEAR_REFUND gas from refund counter. We can prove that refund counter will never go below 0.
  161. // 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEAR_REFUND gas to refund counter.
  162. // 2.2.2. If original value equals new value (this storage slot is reset):
  163. // 2.2.2.1. If original value is 0, add SSTORE_INIT_REFUND to refund counter.
  164. // 2.2.2.2. Otherwise, add SSTORE_CLEAN_REFUND gas to refund counter.
  165. func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  166. // If we fail the minimum gas availability invariant, fail (0)
  167. if contract.Gas <= params.SstoreSentryGasEIP2200 {
  168. return 0, errors.New("not enough gas for reentrancy sentry")
  169. }
  170. // Gas sentry honoured, do the actual gas calculation based on the stored value
  171. var (
  172. y, x = stack.Back(1), stack.Back(0)
  173. current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
  174. )
  175. value := common.BigToHash(y)
  176. if current == value { // noop (1)
  177. return params.SstoreNoopGasEIP2200, nil
  178. }
  179. original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x))
  180. if original == current {
  181. if original == (common.Hash{}) { // create slot (2.1.1)
  182. return params.SstoreInitGasEIP2200, nil
  183. }
  184. if value == (common.Hash{}) { // delete slot (2.1.2b)
  185. evm.StateDB.AddRefund(params.SstoreClearRefundEIP2200)
  186. }
  187. return params.SstoreCleanGasEIP2200, nil // write existing slot (2.1.2)
  188. }
  189. if original != (common.Hash{}) {
  190. if current == (common.Hash{}) { // recreate slot (2.2.1.1)
  191. evm.StateDB.SubRefund(params.SstoreClearRefundEIP2200)
  192. } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
  193. evm.StateDB.AddRefund(params.SstoreClearRefundEIP2200)
  194. }
  195. }
  196. if original == value {
  197. if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
  198. evm.StateDB.AddRefund(params.SstoreInitRefundEIP2200)
  199. } else { // reset to original existing slot (2.2.2.2)
  200. evm.StateDB.AddRefund(params.SstoreCleanRefundEIP2200)
  201. }
  202. }
  203. return params.SstoreDirtyGasEIP2200, nil // dirty update (2.2)
  204. }
  205. func makeGasLog(n uint64) gasFunc {
  206. return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  207. requestedSize, overflow := bigUint64(stack.Back(1))
  208. if overflow {
  209. return 0, errGasUintOverflow
  210. }
  211. gas, err := memoryGasCost(mem, memorySize)
  212. if err != nil {
  213. return 0, err
  214. }
  215. if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
  216. return 0, errGasUintOverflow
  217. }
  218. if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
  219. return 0, errGasUintOverflow
  220. }
  221. var memorySizeGas uint64
  222. if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
  223. return 0, errGasUintOverflow
  224. }
  225. if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
  226. return 0, errGasUintOverflow
  227. }
  228. return gas, nil
  229. }
  230. }
  231. func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  232. gas, err := memoryGasCost(mem, memorySize)
  233. if err != nil {
  234. return 0, err
  235. }
  236. wordGas, overflow := bigUint64(stack.Back(1))
  237. if overflow {
  238. return 0, errGasUintOverflow
  239. }
  240. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
  241. return 0, errGasUintOverflow
  242. }
  243. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  244. return 0, errGasUintOverflow
  245. }
  246. return gas, nil
  247. }
  248. // pureMemoryGascost is used by several operations, which aside from their
  249. // static cost have a dynamic cost which is solely based on the memory
  250. // expansion
  251. func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  252. return memoryGasCost(mem, memorySize)
  253. }
  254. var (
  255. gasReturn = pureMemoryGascost
  256. gasRevert = pureMemoryGascost
  257. gasMLoad = pureMemoryGascost
  258. gasMStore8 = pureMemoryGascost
  259. gasMStore = pureMemoryGascost
  260. gasCreate = pureMemoryGascost
  261. )
  262. func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  263. gas, err := memoryGasCost(mem, memorySize)
  264. if err != nil {
  265. return 0, err
  266. }
  267. wordGas, overflow := bigUint64(stack.Back(2))
  268. if overflow {
  269. return 0, errGasUintOverflow
  270. }
  271. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
  272. return 0, errGasUintOverflow
  273. }
  274. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  275. return 0, errGasUintOverflow
  276. }
  277. return gas, nil
  278. }
  279. func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  280. expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
  281. var (
  282. gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas
  283. overflow bool
  284. )
  285. if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
  286. return 0, errGasUintOverflow
  287. }
  288. return gas, nil
  289. }
  290. func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  291. expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
  292. var (
  293. gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas
  294. overflow bool
  295. )
  296. if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
  297. return 0, errGasUintOverflow
  298. }
  299. return gas, nil
  300. }
  301. func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  302. var (
  303. gas uint64
  304. transfersValue = stack.Back(2).Sign() != 0
  305. address = common.BigToAddress(stack.Back(1))
  306. )
  307. if evm.chainRules.IsEIP158 {
  308. if transfersValue && evm.StateDB.Empty(address) {
  309. gas += params.CallNewAccountGas
  310. }
  311. } else if !evm.StateDB.Exist(address) {
  312. gas += params.CallNewAccountGas
  313. }
  314. if transfersValue {
  315. gas += params.CallValueTransferGas
  316. }
  317. memoryGas, err := memoryGasCost(mem, memorySize)
  318. if err != nil {
  319. return 0, err
  320. }
  321. var overflow bool
  322. if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
  323. return 0, errGasUintOverflow
  324. }
  325. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  326. if err != nil {
  327. return 0, err
  328. }
  329. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  330. return 0, errGasUintOverflow
  331. }
  332. return gas, nil
  333. }
  334. func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  335. memoryGas, err := memoryGasCost(mem, memorySize)
  336. if err != nil {
  337. return 0, err
  338. }
  339. var (
  340. gas uint64
  341. overflow bool
  342. )
  343. if stack.Back(2).Sign() != 0 {
  344. gas += params.CallValueTransferGas
  345. }
  346. if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
  347. return 0, errGasUintOverflow
  348. }
  349. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  350. if err != nil {
  351. return 0, err
  352. }
  353. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  354. return 0, errGasUintOverflow
  355. }
  356. return gas, nil
  357. }
  358. func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  359. gas, err := memoryGasCost(mem, memorySize)
  360. if err != nil {
  361. return 0, err
  362. }
  363. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  364. if err != nil {
  365. return 0, err
  366. }
  367. var overflow bool
  368. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  369. return 0, errGasUintOverflow
  370. }
  371. return gas, nil
  372. }
  373. func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  374. gas, err := memoryGasCost(mem, memorySize)
  375. if err != nil {
  376. return 0, err
  377. }
  378. evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
  379. if err != nil {
  380. return 0, err
  381. }
  382. var overflow bool
  383. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  384. return 0, errGasUintOverflow
  385. }
  386. return gas, nil
  387. }
  388. func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  389. var gas uint64
  390. // EIP150 homestead gas reprice fork:
  391. if evm.chainRules.IsEIP150 {
  392. gas = params.SelfdestructGasEIP150
  393. var address = common.BigToAddress(stack.Back(0))
  394. if evm.chainRules.IsEIP158 {
  395. // if empty and transfers value
  396. if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
  397. gas += params.CreateBySelfdestructGas
  398. }
  399. } else if !evm.StateDB.Exist(address) {
  400. gas += params.CreateBySelfdestructGas
  401. }
  402. }
  403. if !evm.StateDB.HasSuicided(contract.Address()) {
  404. evm.StateDB.AddRefund(params.SelfdestructRefundGas)
  405. }
  406. return gas, nil
  407. }