Skip to content

Commit

Permalink
Fill fixes continued (#519)
Browse files Browse the repository at this point in the history
* fix selfdestruct

Signed-off-by: Ignacio Hagopian <[email protected]>

* extcodecopy witness fix

Signed-off-by: Ignacio Hagopian <[email protected]>

* blockhash fix

Signed-off-by: Ignacio Hagopian <[email protected]>

* run ci

Signed-off-by: Ignacio Hagopian <[email protected]>

* Apply suggestions from code review

remove diff used for test

* same thing in other file

---------

Signed-off-by: Ignacio Hagopian <[email protected]>
Co-authored-by: Guillaume Ballet <[email protected]>
  • Loading branch information
jsign and gballet authored Oct 24, 2024
1 parent b5f8705 commit 99b78be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
7 changes: 3 additions & 4 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/binary"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -459,11 +458,11 @@ func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
return nil, nil
}

func getBlockHashFromContract(number uint64, statedb StateDB, witness *state.AccessWitness) (common.Hash, uint64) {
func getBlockHashFromContract(number uint64, statedb StateDB, witness *state.AccessWitness, availableGas uint64) (common.Hash, uint64) {
ringIndex := number % params.Eip2935BlockHashHistorySize
var pnum common.Hash
binary.BigEndian.PutUint64(pnum[24:], ringIndex)
statelessGas := witness.TouchSlotAndChargeGas(params.HistoryStorageAddress[:], pnum, false, math.MaxUint64, false)
statelessGas := witness.TouchSlotAndChargeGas(params.HistoryStorageAddress[:], pnum, false, availableGas, false)
return statedb.GetState(params.HistoryStorageAddress, pnum), statelessGas
}

Expand All @@ -487,7 +486,7 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
if num64 >= lower && num64 < upper {
// if Verkle is active, read it from the history contract (EIP 2935).
if evm.chainRules.IsVerkle {
blockHash, statelessGas := getBlockHashFromContract(num64, evm.StateDB, evm.Accesses)
blockHash, statelessGas := getBlockHashFromContract(num64, evm.StateDB, evm.Accesses, scope.Contract.Gas)
if interpreter.evm.chainRules.IsEIP4762 {
if !scope.Contract.UseGas(statelessGas) {
return nil, ErrOutOfGas
Expand Down
34 changes: 24 additions & 10 deletions core/vm/operations_verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Mem
beneficiaryAddr := common.Address(stack.peek().Bytes20())
contractAddr := contract.Address()

statelessGas := evm.Accesses.TouchBasicData(contractAddr[:], false, contract.Gas, false)
wanted := evm.Accesses.TouchBasicData(contractAddr[:], false, contract.Gas, false)
if wanted > contract.Gas {
return wanted, nil
}
statelessGas := wanted

balanceIsZero := evm.StateDB.GetBalance(contractAddr).Sign() == 0
_, isPrecompile := evm.precompile(beneficiaryAddr)
isSystemContract := evm.isSystemContract(beneficiaryAddr)
Expand All @@ -121,21 +126,30 @@ func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Mem
}

if contractAddr != beneficiaryAddr {
statelessGas += evm.Accesses.TouchBasicData(beneficiaryAddr[:], false, contract.Gas-statelessGas, false)
wanted := evm.Accesses.TouchBasicData(beneficiaryAddr[:], false, contract.Gas-statelessGas, false)
if wanted > contract.Gas-statelessGas {
return statelessGas + wanted, nil
}
statelessGas += wanted
}
// Charge write costs if it transfers value
if !balanceIsZero {
statelessGas += evm.Accesses.TouchBasicData(contractAddr[:], true, contract.Gas-statelessGas, false)
wanted := evm.Accesses.TouchBasicData(contractAddr[:], true, contract.Gas-statelessGas, false)
if wanted > contract.Gas-statelessGas {
return statelessGas + wanted, nil
}
statelessGas += wanted

if contractAddr != beneficiaryAddr {
if evm.StateDB.Exist(beneficiaryAddr) {
statelessGas += evm.Accesses.TouchBasicData(beneficiaryAddr[:], true, contract.Gas-statelessGas, false)
wanted = evm.Accesses.TouchBasicData(beneficiaryAddr[:], true, contract.Gas-statelessGas, false)
} else {
wanted := evm.Accesses.TouchFullAccount(beneficiaryAddr[:], true, contract.Gas-statelessGas)
if wanted > contract.Gas-statelessGas {
return statelessGas + wanted, nil
}
statelessGas += wanted
wanted = evm.Accesses.TouchFullAccount(beneficiaryAddr[:], true, contract.Gas-statelessGas)
}
if wanted > contract.Gas-statelessGas {
return statelessGas + wanted, nil
}
statelessGas += wanted
}
}
return statelessGas, nil
Expand All @@ -158,7 +172,7 @@ func gasExtCodeCopyEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memo
}
return gas, nil
}
wgas := evm.Accesses.TouchBasicData(addr[:], false, contract.Gas, true)
wgas := evm.Accesses.TouchBasicData(addr[:], false, contract.Gas-gas, true)
var overflow bool
if gas, overflow = math.SafeAdd(gas, wgas); overflow {
return 0, ErrGasUintOverflow
Expand Down

0 comments on commit 99b78be

Please sign in to comment.