gas_table.go 16 KB

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