Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill fixes continued #519

Merged
merged 6 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/spec-tests-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on:
push:
branches: [master, kaustinen-with-shapella]
pull_request:
branches: [master, kaustinen-with-shapella]
branches:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this diff when the PR is about to be merged.

gballet marked this conversation as resolved.
Show resolved Hide resolved
[master, kaustinen-with-shapella, witness-charging-order-with-limits]
gballet marked this conversation as resolved.
Show resolved Hide resolved
workflow_dispatch:

env:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/stable-spec-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on:
push:
branches: [master]
pull_request:
branches: [master, kaustinen-with-shapella]
branches:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

[master, kaustinen-with-shapella, witness-charging-order-with-limits]
gballet marked this conversation as resolved.
Show resolved Hide resolved
workflow_dispatch:

env:
Expand Down
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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrect -- we don't have infinite amount of gas. This method is only called from opBlockHash. If we don't pass the right limit, even if you ran OOG it would always add it to the witness (when it shouldn't).

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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: everything in SELFDESTRUCT was ignoring if we had enough gas.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could change TouchBasicData to return both the wanted and the actually charged


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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but it would only save one line and not be used for anything afterwards. No point in making the PR footprint larger for that.

}
// 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)
Comment on lines -161 to +175
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We where using more gas than truly available.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

var overflow bool
if gas, overflow = math.SafeAdd(gas, wgas); overflow {
return 0, ErrGasUintOverflow
Expand Down