gas_table.go 16 KB

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