operations_acl.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2020 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. "errors"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/math"
  21. "github.com/ethereum/go-ethereum/params"
  22. )
  23. func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
  24. return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  25. // If we fail the minimum gas availability invariant, fail (0)
  26. if contract.Gas <= params.SstoreSentryGasEIP2200 {
  27. return 0, errors.New("not enough gas for reentrancy sentry")
  28. }
  29. // Gas sentry honoured, do the actual gas calculation based on the stored value
  30. var (
  31. y, x = stack.Back(1), stack.peek()
  32. slot = common.Hash(x.Bytes32())
  33. current = evm.StateDB.GetState(contract.Address(), slot)
  34. cost = uint64(0)
  35. )
  36. // Check slot presence in the access list
  37. if addrPresent, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
  38. cost = params.ColdSloadCostEIP2929
  39. // If the caller cannot afford the cost, this change will be rolled back
  40. evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
  41. if !addrPresent {
  42. // Once we're done with YOLOv2 and schedule this for mainnet, might
  43. // be good to remove this panic here, which is just really a
  44. // canary to have during testing
  45. panic("impossible case: address was not present in access list during sstore op")
  46. }
  47. }
  48. value := common.Hash(y.Bytes32())
  49. if current == value { // noop (1)
  50. // EIP 2200 original clause:
  51. // return params.SloadGasEIP2200, nil
  52. return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS
  53. }
  54. original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
  55. if original == current {
  56. if original == (common.Hash{}) { // create slot (2.1.1)
  57. return cost + params.SstoreSetGasEIP2200, nil
  58. }
  59. if value == (common.Hash{}) { // delete slot (2.1.2b)
  60. evm.StateDB.AddRefund(clearingRefund)
  61. }
  62. // EIP-2200 original clause:
  63. // return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2)
  64. return cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929), nil // write existing slot (2.1.2)
  65. }
  66. if original != (common.Hash{}) {
  67. if current == (common.Hash{}) { // recreate slot (2.2.1.1)
  68. evm.StateDB.SubRefund(clearingRefund)
  69. } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
  70. evm.StateDB.AddRefund(clearingRefund)
  71. }
  72. }
  73. if original == value {
  74. if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
  75. // EIP 2200 Original clause:
  76. //evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
  77. evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929)
  78. } else { // reset to original existing slot (2.2.2.2)
  79. // EIP 2200 Original clause:
  80. // evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
  81. // - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST)
  82. // - SLOAD_GAS redefined as WARM_STORAGE_READ_COST
  83. // Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST
  84. evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929)
  85. }
  86. }
  87. // EIP-2200 original clause:
  88. //return params.SloadGasEIP2200, nil // dirty update (2.2)
  89. return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2)
  90. }
  91. }
  92. // gasSLoadEIP2929 calculates dynamic gas for SLOAD according to EIP-2929
  93. // For SLOAD, if the (address, storage_key) pair (where address is the address of the contract
  94. // whose storage is being read) is not yet in accessed_storage_keys,
  95. // charge 2100 gas and add the pair to accessed_storage_keys.
  96. // If the pair is already in accessed_storage_keys, charge 100 gas.
  97. func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  98. loc := stack.peek()
  99. slot := common.Hash(loc.Bytes32())
  100. // Check slot presence in the access list
  101. if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
  102. // If the caller cannot afford the cost, this change will be rolled back
  103. // If he does afford it, we can skip checking the same thing later on, during execution
  104. evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
  105. return params.ColdSloadCostEIP2929, nil
  106. }
  107. return params.WarmStorageReadCostEIP2929, nil
  108. }
  109. // gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
  110. // EIP spec:
  111. // > If the target is not in accessed_addresses,
  112. // > charge COLD_ACCOUNT_ACCESS_COST gas, and add the address to accessed_addresses.
  113. // > Otherwise, charge WARM_STORAGE_READ_COST gas.
  114. func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  115. // memory expansion first (dynamic part of pre-2929 implementation)
  116. gas, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize)
  117. if err != nil {
  118. return 0, err
  119. }
  120. addr := common.Address(stack.peek().Bytes20())
  121. // Check slot presence in the access list
  122. if !evm.StateDB.AddressInAccessList(addr) {
  123. evm.StateDB.AddAddressToAccessList(addr)
  124. var overflow bool
  125. // We charge (cold-warm), since 'warm' is already charged as constantGas
  126. if gas, overflow = math.SafeAdd(gas, params.ColdAccountAccessCostEIP2929-params.WarmStorageReadCostEIP2929); overflow {
  127. return 0, ErrGasUintOverflow
  128. }
  129. return gas, nil
  130. }
  131. return gas, nil
  132. }
  133. // gasEip2929AccountCheck checks whether the first stack item (as address) is present in the access list.
  134. // If it is, this method returns '0', otherwise 'cold-warm' gas, presuming that the opcode using it
  135. // is also using 'warm' as constant factor.
  136. // This method is used by:
  137. // - extcodehash,
  138. // - extcodesize,
  139. // - (ext) balance
  140. func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  141. addr := common.Address(stack.peek().Bytes20())
  142. // Check slot presence in the access list
  143. if !evm.StateDB.AddressInAccessList(addr) {
  144. // If the caller cannot afford the cost, this change will be rolled back
  145. evm.StateDB.AddAddressToAccessList(addr)
  146. // The warm storage read cost is already charged as constantGas
  147. return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929, nil
  148. }
  149. return 0, nil
  150. }
  151. func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc {
  152. return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  153. addr := common.Address(stack.Back(1).Bytes20())
  154. // Check slot presence in the access list
  155. warmAccess := evm.StateDB.AddressInAccessList(addr)
  156. // The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
  157. // the cost to charge for cold access, if any, is Cold - Warm
  158. coldCost := params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929
  159. if !warmAccess {
  160. evm.StateDB.AddAddressToAccessList(addr)
  161. // Charge the remaining difference here already, to correctly calculate available
  162. // gas for call
  163. if !contract.UseGas(coldCost) {
  164. return 0, ErrOutOfGas
  165. }
  166. }
  167. // Now call the old calculator, which takes into account
  168. // - create new account
  169. // - transfer value
  170. // - memory expansion
  171. // - 63/64ths rule
  172. gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
  173. if warmAccess || err != nil {
  174. return gas, err
  175. }
  176. // In case of a cold access, we temporarily add the cold charge back, and also
  177. // add it to the returned gas. By adding it to the return, it will be charged
  178. // outside of this function, as part of the dynamic gas, and that will make it
  179. // also become correctly reported to tracers.
  180. contract.Gas += coldCost
  181. return gas + coldCost, nil
  182. }
  183. }
  184. var (
  185. gasCallEIP2929 = makeCallVariantGasCallEIP2929(gasCall)
  186. gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall)
  187. gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall)
  188. gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode)
  189. gasSelfdestructEIP2929 = makeSelfdestructGasFn(true)
  190. // gasSelfdestructEIP3529 implements the changes in EIP-2539 (no refunds)
  191. gasSelfdestructEIP3529 = makeSelfdestructGasFn(false)
  192. // gasSStoreEIP2929 implements gas cost for SSTORE according to EIP-2929
  193. //
  194. // When calling SSTORE, check if the (address, storage_key) pair is in accessed_storage_keys.
  195. // If it is not, charge an additional COLD_SLOAD_COST gas, and add the pair to accessed_storage_keys.
  196. // Additionally, modify the parameters defined in EIP 2200 as follows:
  197. //
  198. // Parameter Old value New value
  199. // SLOAD_GAS 800 = WARM_STORAGE_READ_COST
  200. // SSTORE_RESET_GAS 5000 5000 - COLD_SLOAD_COST
  201. //
  202. //The other parameters defined in EIP 2200 are unchanged.
  203. // see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
  204. gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200)
  205. // gasSStoreEIP2539 implements gas cost for SSTORE according to EIP-2539
  206. // Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800)
  207. gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
  208. )
  209. // makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-2539
  210. func makeSelfdestructGasFn(refundsEnabled bool) gasFunc {
  211. gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
  212. var (
  213. gas uint64
  214. address = common.Address(stack.peek().Bytes20())
  215. )
  216. if !evm.StateDB.AddressInAccessList(address) {
  217. // If the caller cannot afford the cost, this change will be rolled back
  218. evm.StateDB.AddAddressToAccessList(address)
  219. gas = params.ColdAccountAccessCostEIP2929
  220. }
  221. // if empty and transfers value
  222. if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
  223. gas += params.CreateBySelfdestructGas
  224. }
  225. if refundsEnabled && !evm.StateDB.HasSuicided(contract.Address()) {
  226. evm.StateDB.AddRefund(params.SelfdestructRefundGas)
  227. }
  228. return gas, nil
  229. }
  230. return gasFunc
  231. }