gas_table.go 16 KB

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