|
| 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 | + |
| 17 | +package vm |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/ethereum/go-ethereum/common" |
| 23 | + "github.com/ethereum/go-ethereum/params" |
| 24 | +) |
| 25 | + |
| 26 | +// Computes the cost of doing a state load in wasm |
| 27 | +// Note: the code here is adapted from gasSLoadEIP2929 |
| 28 | +func WasmStateLoadCost(db StateDB, program common.Address, key common.Hash) uint64 { |
| 29 | + // Check slot presence in the access list |
| 30 | + if _, slotPresent := db.SlotInAccessList(program, key); !slotPresent { |
| 31 | + // If the caller cannot afford the cost, this change will be rolled back |
| 32 | + // If he does afford it, we can skip checking the same thing later on, during execution |
| 33 | + db.AddSlotToAccessList(program, key) |
| 34 | + return params.ColdSloadCostEIP2929 |
| 35 | + } |
| 36 | + return params.WarmStorageReadCostEIP2929 |
| 37 | +} |
| 38 | + |
| 39 | +// Computes the cost of doing a state store in wasm |
| 40 | +// Note: the code here is adapted from makeGasSStoreFunc with the most recent parameters as of The Merge |
| 41 | +// Note: the sentry check must be done by the caller |
| 42 | +func WasmStateStoreCost(db StateDB, program common.Address, key, value common.Hash) uint64 { |
| 43 | + clearingRefund := params.SstoreClearsScheduleRefundEIP3529 |
| 44 | + |
| 45 | + cost := uint64(0) |
| 46 | + current := db.GetState(program, key) |
| 47 | + |
| 48 | + // Check slot presence in the access list |
| 49 | + if addrPresent, slotPresent := db.SlotInAccessList(program, key); !slotPresent { |
| 50 | + cost = params.ColdSloadCostEIP2929 |
| 51 | + // If the caller cannot afford the cost, this change will be rolled back |
| 52 | + db.AddSlotToAccessList(program, key) |
| 53 | + if !addrPresent { |
| 54 | + panic(fmt.Sprintf("impossible case: address %v was not present in access list", program)) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if current == value { // noop (1) |
| 59 | + // EIP 2200 original clause: |
| 60 | + // return params.SloadGasEIP2200, nil |
| 61 | + return cost + params.WarmStorageReadCostEIP2929 // SLOAD_GAS |
| 62 | + } |
| 63 | + original := db.GetCommittedState(program, key) |
| 64 | + if original == current { |
| 65 | + if original == (common.Hash{}) { // create slot (2.1.1) |
| 66 | + return cost + params.SstoreSetGasEIP2200 |
| 67 | + } |
| 68 | + if value == (common.Hash{}) { // delete slot (2.1.2b) |
| 69 | + db.AddRefund(clearingRefund) |
| 70 | + } |
| 71 | + // EIP-2200 original clause: |
| 72 | + // return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) |
| 73 | + return cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) // write existing slot (2.1.2) |
| 74 | + } |
| 75 | + if original != (common.Hash{}) { |
| 76 | + if current == (common.Hash{}) { // recreate slot (2.2.1.1) |
| 77 | + db.SubRefund(clearingRefund) |
| 78 | + } else if value == (common.Hash{}) { // delete slot (2.2.1.2) |
| 79 | + db.AddRefund(clearingRefund) |
| 80 | + } |
| 81 | + } |
| 82 | + if original == value { |
| 83 | + if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1) |
| 84 | + // EIP 2200 Original clause: |
| 85 | + //evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200) |
| 86 | + db.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929) |
| 87 | + } else { // reset to original existing slot (2.2.2.2) |
| 88 | + // EIP 2200 Original clause: |
| 89 | + // evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200) |
| 90 | + // - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST) |
| 91 | + // - SLOAD_GAS redefined as WARM_STORAGE_READ_COST |
| 92 | + // Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST |
| 93 | + db.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929) |
| 94 | + } |
| 95 | + } |
| 96 | + // EIP-2200 original clause: |
| 97 | + //return params.SloadGasEIP2200, nil // dirty update (2.2) |
| 98 | + return cost + params.WarmStorageReadCostEIP2929 // dirty update (2.2) |
| 99 | +} |
0 commit comments