gas_table.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/common/math"
  20. "github.com/ethereum/go-ethereum/params"
  21. )
  22. // memoryGasCost calculates the quadratic gas for memory expansion. It does so
  23. // only for the memory region that is expanded, not the total memory.
  24. func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
  25. if newMemSize == 0 {
  26. return 0, nil
  27. }
  28. // The maximum that will fit in a uint64 is max_word_count - 1. Anything above
  29. // that will result in an overflow. Additionally, a newMemSize which results in
  30. // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to
  31. // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used
  32. // without overflowing the gas calculation.
  33. if newMemSize > 0x1FFFFFFFE0 {
  34. return 0, errGasUintOverflow
  35. }
  36. newMemSizeWords := toWordSize(newMemSize)
  37. newMemSize = newMemSizeWords * 32
  38. if newMemSize > uint64(mem.Len()) {
  39. square := newMemSizeWords * newMemSizeWords
  40. linCoef := newMemSizeWords * params.MemoryGas
  41. quadCoef := square / params.QuadCoeffDiv
  42. newTotalFee := linCoef + quadCoef
  43. fee := newTotalFee - mem.lastGasCost
  44. mem.lastGasCost = newTotalFee
  45. return fee, nil
  46. }
  47. return 0, nil
  48. }
  49. func gasCallDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  50. gas, err := memoryGasCost(mem, memorySize)
  51. if err != nil {
  52. return 0, err
  53. }
  54. var overflow bool
  55. if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
  56. return 0, errGasUintOverflow
  57. }
  58. words, overflow := bigUint64(stack.Back(2))
  59. if overflow {
  60. return 0, errGasUintOverflow
  61. }
  62. if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
  63. return 0, errGasUintOverflow
  64. }
  65. if gas, overflow = math.SafeAdd(gas, words); overflow {
  66. return 0, errGasUintOverflow
  67. }
  68. return gas, nil
  69. }
  70. func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  71. gas, err := memoryGasCost(mem, memorySize)
  72. if err != nil {
  73. return 0, err
  74. }
  75. var overflow bool
  76. if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
  77. return 0, errGasUintOverflow
  78. }
  79. words, overflow := bigUint64(stack.Back(2))
  80. if overflow {
  81. return 0, errGasUintOverflow
  82. }
  83. if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
  84. return 0, errGasUintOverflow
  85. }
  86. if gas, overflow = math.SafeAdd(gas, words); overflow {
  87. return 0, errGasUintOverflow
  88. }
  89. return gas, nil
  90. }
  91. func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  92. var (
  93. y, x = stack.Back(1), stack.Back(0)
  94. current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
  95. )
  96. // The legacy gas metering only takes into consideration the current state
  97. // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283)
  98. // OR Constantinople is not active
  99. if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople {
  100. // This checks for 3 scenario's and calculates gas accordingly:
  101. //
  102. // 1. From a zero-value address to a non-zero value (NEW VALUE)
  103. // 2. From a non-zero value address to a zero-value address (DELETE)
  104. // 3. From a non-zero to a non-zero (CHANGE)
  105. switch {
  106. case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0
  107. return params.SstoreSetGas, nil
  108. case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0
  109. evm.StateDB.AddRefund(params.SstoreRefundGas)
  110. return params.SstoreClearGas, nil
  111. default: // non 0 => non 0 (or 0 => 0)
  112. return params.SstoreResetGas, nil
  113. }
  114. }
  115. // The new gas metering is based on net gas costs (EIP-1283):
  116. //
  117. // 1. If current value equals new value (this is a no-op), 200 gas is deducted.
  118. // 2. If current value does not equal new value
  119. // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
  120. // 2.1.1. If original value is 0, 20000 gas is deducted.
  121. // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
  122. // 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.
  123. // 2.2.1. If original value is not 0
  124. // 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.
  125. // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
  126. // 2.2.2. If original value equals new value (this storage slot is reset)
  127. // 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
  128. // 2.2.2.2. Otherwise, add 4800 gas to refund counter.
  129. value := common.BigToHash(y)
  130. if current == value { // noop (1)
  131. return params.NetSstoreNoopGas, nil
  132. }
  133. original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x))
  134. if original == current {
  135. if original == (common.Hash{}) { // create slot (2.1.1)
  136. return params.NetSstoreInitGas, nil
  137. }
  138. if value == (common.Hash{}) { // delete slot (2.1.2b)
  139. evm.StateDB.AddRefund(params.NetSstoreClearRefund)
  140. }
  141. return params.NetSstoreCleanGas, nil // write existing slot (2.1.2)
  142. }
  143. if original != (common.Hash{}) {
  144. if current == (common.Hash{}) { // recreate slot (2.2.1.1)
  145. evm.StateDB.SubRefund(params.NetSstoreClearRefund)
  146. } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
  147. evm.StateDB.AddRefund(params.NetSstoreClearRefund)
  148. }
  149. }
  150. if original == value {
  151. if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
  152. evm.StateDB.AddRefund(params.NetSstoreResetClearRefund)
  153. } else { // reset to original existing slot (2.2.2.2)
  154. evm.StateDB.AddRefund(params.NetSstoreResetRefund)
  155. }
  156. }
  157. return params.NetSstoreDirtyGas, nil
  158. }
  159. func makeGasLog(n uint64) gasFunc {
  160. return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  161. requestedSize, overflow := bigUint64(stack.Back(1))
  162. if overflow {
  163. return 0, errGasUintOverflow
  164. }
  165. gas, err := memoryGasCost(mem, memorySize)
  166. if err != nil {
  167. return 0, err
  168. }
  169. if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
  170. return 0, errGasUintOverflow
  171. }
  172. if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
  173. return 0, errGasUintOverflow
  174. }
  175. var memorySizeGas uint64
  176. if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
  177. return 0, errGasUintOverflow
  178. }
  179. if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
  180. return 0, errGasUintOverflow
  181. }
  182. return gas, nil
  183. }
  184. }
  185. func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  186. var overflow bool
  187. gas, err := memoryGasCost(mem, memorySize)
  188. if err != nil {
  189. return 0, err
  190. }
  191. if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow {
  192. return 0, errGasUintOverflow
  193. }
  194. wordGas, overflow := bigUint64(stack.Back(1))
  195. if overflow {
  196. return 0, errGasUintOverflow
  197. }
  198. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
  199. return 0, errGasUintOverflow
  200. }
  201. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  202. return 0, errGasUintOverflow
  203. }
  204. return gas, nil
  205. }
  206. func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  207. gas, err := memoryGasCost(mem, memorySize)
  208. if err != nil {
  209. return 0, err
  210. }
  211. var overflow bool
  212. if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
  213. return 0, errGasUintOverflow
  214. }
  215. wordGas, overflow := bigUint64(stack.Back(2))
  216. if overflow {
  217. return 0, errGasUintOverflow
  218. }
  219. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow {
  220. return 0, errGasUintOverflow
  221. }
  222. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  223. return 0, errGasUintOverflow
  224. }
  225. return gas, nil
  226. }
  227. func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  228. gas, err := memoryGasCost(mem, memorySize)
  229. if err != nil {
  230. return 0, err
  231. }
  232. var overflow bool
  233. if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow {
  234. return 0, errGasUintOverflow
  235. }
  236. wordGas, overflow := bigUint64(stack.Back(3))
  237. if overflow {
  238. return 0, errGasUintOverflow
  239. }
  240. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); 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. func gasExtCodeHash(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  249. return gt.ExtcodeHash, nil
  250. }
  251. func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  252. var overflow bool
  253. gas, err := memoryGasCost(mem, memorySize)
  254. if err != nil {
  255. return 0, errGasUintOverflow
  256. }
  257. if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
  258. return 0, errGasUintOverflow
  259. }
  260. return gas, nil
  261. }
  262. func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  263. var overflow bool
  264. gas, err := memoryGasCost(mem, memorySize)
  265. if err != nil {
  266. return 0, errGasUintOverflow
  267. }
  268. if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
  269. return 0, errGasUintOverflow
  270. }
  271. return gas, nil
  272. }
  273. func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  274. var overflow bool
  275. gas, err := memoryGasCost(mem, memorySize)
  276. if err != nil {
  277. return 0, errGasUintOverflow
  278. }
  279. if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
  280. return 0, errGasUintOverflow
  281. }
  282. return gas, nil
  283. }
  284. func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  285. var overflow bool
  286. gas, err := memoryGasCost(mem, memorySize)
  287. if err != nil {
  288. return 0, err
  289. }
  290. if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow {
  291. return 0, errGasUintOverflow
  292. }
  293. return gas, nil
  294. }
  295. func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  296. var overflow bool
  297. gas, err := memoryGasCost(mem, memorySize)
  298. if err != nil {
  299. return 0, err
  300. }
  301. if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow {
  302. return 0, errGasUintOverflow
  303. }
  304. wordGas, overflow := bigUint64(stack.Back(2))
  305. if overflow {
  306. return 0, errGasUintOverflow
  307. }
  308. if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
  309. return 0, errGasUintOverflow
  310. }
  311. if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
  312. return 0, errGasUintOverflow
  313. }
  314. return gas, nil
  315. }
  316. func gasBalance(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  317. return gt.Balance, nil
  318. }
  319. func gasExtCodeSize(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  320. return gt.ExtcodeSize, nil
  321. }
  322. func gasSLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  323. return gt.SLoad, nil
  324. }
  325. func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  326. expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
  327. var (
  328. gas = expByteLen * gt.ExpByte // no overflow check required. Max is 256 * ExpByte gas
  329. overflow bool
  330. )
  331. if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
  332. return 0, errGasUintOverflow
  333. }
  334. return gas, nil
  335. }
  336. func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  337. var (
  338. gas = gt.Calls
  339. transfersValue = stack.Back(2).Sign() != 0
  340. address = common.BigToAddress(stack.Back(1))
  341. eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber)
  342. )
  343. if eip158 {
  344. if transfersValue && evm.StateDB.Empty(address) {
  345. gas += params.CallNewAccountGas
  346. }
  347. } else if !evm.StateDB.Exist(address) {
  348. gas += params.CallNewAccountGas
  349. }
  350. if transfersValue {
  351. gas += params.CallValueTransferGas
  352. }
  353. memoryGas, err := memoryGasCost(mem, memorySize)
  354. if err != nil {
  355. return 0, err
  356. }
  357. var overflow bool
  358. if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
  359. return 0, errGasUintOverflow
  360. }
  361. evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
  362. if err != nil {
  363. return 0, err
  364. }
  365. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  366. return 0, errGasUintOverflow
  367. }
  368. return gas, nil
  369. }
  370. func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  371. gas := gt.Calls
  372. if stack.Back(2).Sign() != 0 {
  373. gas += params.CallValueTransferGas
  374. }
  375. memoryGas, err := memoryGasCost(mem, memorySize)
  376. if err != nil {
  377. return 0, err
  378. }
  379. var overflow bool
  380. if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
  381. return 0, errGasUintOverflow
  382. }
  383. evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
  384. if err != nil {
  385. return 0, err
  386. }
  387. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  388. return 0, errGasUintOverflow
  389. }
  390. return gas, nil
  391. }
  392. func gasReturn(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  393. return memoryGasCost(mem, memorySize)
  394. }
  395. func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  396. return memoryGasCost(mem, memorySize)
  397. }
  398. func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  399. var gas uint64
  400. // EIP150 homestead gas reprice fork:
  401. if evm.ChainConfig().IsEIP150(evm.BlockNumber) {
  402. gas = gt.Suicide
  403. var (
  404. address = common.BigToAddress(stack.Back(0))
  405. eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber)
  406. )
  407. if eip158 {
  408. // if empty and transfers value
  409. if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
  410. gas += gt.CreateBySuicide
  411. }
  412. } else if !evm.StateDB.Exist(address) {
  413. gas += gt.CreateBySuicide
  414. }
  415. }
  416. if !evm.StateDB.HasSuicided(contract.Address()) {
  417. evm.StateDB.AddRefund(params.SuicideRefundGas)
  418. }
  419. return gas, nil
  420. }
  421. func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  422. gas, err := memoryGasCost(mem, memorySize)
  423. if err != nil {
  424. return 0, err
  425. }
  426. var overflow bool
  427. if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
  428. return 0, errGasUintOverflow
  429. }
  430. evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
  431. if err != nil {
  432. return 0, err
  433. }
  434. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  435. return 0, errGasUintOverflow
  436. }
  437. return gas, nil
  438. }
  439. func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  440. gas, err := memoryGasCost(mem, memorySize)
  441. if err != nil {
  442. return 0, err
  443. }
  444. var overflow bool
  445. if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
  446. return 0, errGasUintOverflow
  447. }
  448. evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
  449. if err != nil {
  450. return 0, err
  451. }
  452. if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
  453. return 0, errGasUintOverflow
  454. }
  455. return gas, nil
  456. }