gas_table.go 16 KB

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