operations_acl.go 10.0 KB

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