gas_table.go 17 KB

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