Skip to content

Commit d2ace19

Browse files
committed
followup on last PR: include all sstore gas funcs (some were missing from last commit). expand to call opcodes
1 parent d6c444d commit d2ace19

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

core/vm/gas_table.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ var (
9797
)
9898

9999
func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
100+
if evm.readOnly {
101+
return 0, ErrWriteProtection
102+
}
100103
var (
101104
y, x = stack.Back(1), stack.Back(0)
102105
current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), x.Bytes32())
@@ -181,6 +184,9 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi
181184
// (2.2.2.1.) If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter.
182185
// (2.2.2.2.) Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter.
183186
func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
187+
if evm.readOnly {
188+
return 0, ErrWriteProtection
189+
}
184190
// If we fail the minimum gas availability invariant, fail (0)
185191
if contract.Gas <= params.SstoreSentryGasEIP2200 {
186192
return 0, errors.New("not enough gas for reentrancy sentry")
@@ -374,6 +380,10 @@ func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize
374380
transfersValue = !stack.Back(2).IsZero()
375381
address = common.Address(stack.Back(1).Bytes20())
376382
)
383+
if evm.readOnly && transfersValue {
384+
return 0, ErrWriteProtection
385+
}
386+
377387
if evm.chainRules.IsEIP158 {
378388
if transfersValue && evm.StateDB.Empty(address) {
379389
gas += params.CallNewAccountGas
@@ -462,6 +472,10 @@ func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memo
462472
}
463473

464474
func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
475+
if evm.readOnly {
476+
return 0, ErrWriteProtection
477+
}
478+
465479
var gas uint64
466480
// EIP150 homestead gas reprice fork:
467481
if evm.chainRules.IsEIP150 {

core/vm/instructions.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,6 @@ func opCall(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
740740
// Get the arguments from the memory.
741741
args := scope.Memory.GetPtr(inOffset.Uint64(), inSize.Uint64())
742742

743-
if evm.readOnly && !value.IsZero() {
744-
return nil, ErrWriteProtection
745-
}
746743
if !value.IsZero() {
747744
gas += params.CallStipend
748745
}

core/vm/operations_acl.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,15 @@ var (
261261
func makeCallVariantGasCallEIP7702(oldCalculator gasFunc) gasFunc {
262262
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
263263
var (
264-
total uint64 // total dynamic gas used
265-
addr = common.Address(stack.Back(1).Bytes20())
264+
total uint64 // total dynamic gas used
265+
addr = common.Address(stack.Back(1).Bytes20())
266+
transfersValue = !stack.Back(2).IsZero()
266267
)
267268

269+
if evm.readOnly && transfersValue {
270+
return 0, ErrWriteProtection
271+
}
272+
268273
// Check slot presence in the access list
269274
if !evm.StateDB.AddressInAccessList(addr) {
270275
evm.StateDB.AddAddressToAccessList(addr)

0 commit comments

Comments
 (0)