mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-12 01:41:36 +00:00
core: track the state access footprint
This commit is contained in:
parent
e447a2696d
commit
3b15cf18f7
5 changed files with 92 additions and 20 deletions
|
|
@ -180,6 +180,9 @@ func (s *stateObject) getState(key common.Hash) (common.Hash, common.Hash) {
|
|||
// GetCommittedState retrieves the value associated with the specific key
|
||||
// without any mutations caused in the current execution.
|
||||
func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
|
||||
// Record slot access regardless of whether the storage slot exists.
|
||||
s.db.stateReadList.AddState(s.address, key)
|
||||
|
||||
// If we have a pending write or clean cached, return that
|
||||
if value, pending := s.pendingStorage[key]; pending {
|
||||
return value
|
||||
|
|
@ -194,19 +197,6 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
|
|||
// have been handles via pendingStorage above.
|
||||
// 2) we don't have new values, and can deliver empty response back
|
||||
if _, destructed := s.db.stateObjectsDestruct[s.address]; destructed {
|
||||
// Invoke the reader regardless and discard the returned value.
|
||||
// The returned value may not be empty, as it could belong to a
|
||||
// self-destructed contract.
|
||||
//
|
||||
// The read operation is still essential for correctly building
|
||||
// the block-level access list.
|
||||
//
|
||||
// TODO(rjl493456442) the reader interface can be extended with
|
||||
// Touch, recording the read access without the actual disk load.
|
||||
_, err := s.db.reader.Storage(s.address, key)
|
||||
if err != nil {
|
||||
s.db.setError(err)
|
||||
}
|
||||
s.originStorage[key] = common.Hash{} // track the empty slot as origin value
|
||||
return common.Hash{}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/stateless"
|
||||
"github.com/ethereum/go-ethereum/core/tracing"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/types/bal"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
|
|
@ -126,6 +127,9 @@ type StateDB struct {
|
|||
accessList *accessList
|
||||
accessEvents *AccessEvents
|
||||
|
||||
// Per-transaction state access footprint for EIP-7928
|
||||
stateReadList *bal.StateAccessList
|
||||
|
||||
// Transient storage
|
||||
transientStorage transientStorage
|
||||
|
||||
|
|
@ -579,6 +583,9 @@ func (s *StateDB) deleteStateObject(addr common.Address) {
|
|||
// getStateObject retrieves a state object given by the address, returning nil if
|
||||
// the object is not found or was deleted in this execution context.
|
||||
func (s *StateDB) getStateObject(addr common.Address) *stateObject {
|
||||
// Record state access regardless of whether the account exists.
|
||||
s.stateReadList.AddAccount(addr)
|
||||
|
||||
// Prefer live objects if any is available
|
||||
if obj := s.stateObjects[addr]; obj != nil {
|
||||
return obj
|
||||
|
|
@ -784,7 +791,7 @@ func (s *StateDB) LogsForBurnAccounts() []*types.Log {
|
|||
// Finalise finalises the state by removing the destructed objects and clears
|
||||
// the journal as well as the refunds. Finalise, however, will not push any updates
|
||||
// into the tries just yet. Only IntermediateRoot or Commit will do that.
|
||||
func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
||||
func (s *StateDB) Finalise(deleteEmptyObjects bool) *bal.StateAccessList {
|
||||
addressesToPrefetch := make([]common.Address, 0, len(s.journal.dirties))
|
||||
for addr := range s.journal.dirties {
|
||||
obj, exist := s.stateObjects[addr]
|
||||
|
|
@ -800,6 +807,7 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
|||
if obj.selfDestructed || (deleteEmptyObjects && obj.empty()) {
|
||||
delete(s.stateObjects, obj.address)
|
||||
s.markDelete(addr)
|
||||
|
||||
// We need to maintain account deletions explicitly (will remain
|
||||
// set indefinitely). Note only the first occurred self-destruct
|
||||
// event is tracked.
|
||||
|
|
@ -822,6 +830,8 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
|||
}
|
||||
// Invalidate journal because reverting across transactions is not allowed.
|
||||
s.clearJournalAndRefund()
|
||||
|
||||
return s.stateReadList
|
||||
}
|
||||
|
||||
// IntermediateRoot computes the current root hash of the state trie.
|
||||
|
|
@ -1415,6 +1425,7 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
|
|||
}
|
||||
// Reset transient storage at the beginning of transaction execution
|
||||
s.transientStorage = newTransientStorage()
|
||||
s.stateReadList = bal.NewStateAccessList()
|
||||
}
|
||||
|
||||
// AddAddressToAccessList adds the given address to the access list
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/stateless"
|
||||
"github.com/ethereum/go-ethereum/core/tracing"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/types/bal"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/holiman/uint256"
|
||||
|
|
@ -229,11 +230,10 @@ func (s *hookedStateDB) LogsForBurnAccounts() []*types.Log {
|
|||
return s.inner.LogsForBurnAccounts()
|
||||
}
|
||||
|
||||
func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) {
|
||||
func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) *bal.StateAccessList {
|
||||
if s.hooks.OnBalanceChange == nil && s.hooks.OnNonceChangeV2 == nil && s.hooks.OnNonceChange == nil && s.hooks.OnCodeChangeV2 == nil && s.hooks.OnCodeChange == nil {
|
||||
// Short circuit if no relevant hooks are set.
|
||||
s.inner.Finalise(deleteEmptyObjects)
|
||||
return
|
||||
return s.inner.Finalise(deleteEmptyObjects)
|
||||
}
|
||||
|
||||
// Collect all self-destructed addresses first, then sort them to ensure
|
||||
|
|
@ -282,6 +282,5 @@ func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) {
|
|||
s.hooks.OnCodeChange(addr, prevCodeHash, s.inner.GetCode(addr), types.EmptyCodeHash, nil)
|
||||
}
|
||||
}
|
||||
|
||||
s.inner.Finalise(deleteEmptyObjects)
|
||||
return s.inner.Finalise(deleteEmptyObjects)
|
||||
}
|
||||
|
|
|
|||
71
core/types/bal/access_list.go
Normal file
71
core/types/bal/access_list.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// Copyright 2026 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
package bal
|
||||
|
||||
import (
|
||||
"maps"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
// StorageAccessList represents a set of storage slots accessed within an account.
|
||||
type StorageAccessList map[common.Hash]struct{}
|
||||
|
||||
// StateAccessList records the set of accounts and storage slots that have been
|
||||
// accessed. An entry with an empty StorageAccessList denotes an account access
|
||||
// without any storage slot access.
|
||||
type StateAccessList struct {
|
||||
list map[common.Address]StorageAccessList
|
||||
}
|
||||
|
||||
// NewStateAccessList returns an empty StateAccessList ready for use.
|
||||
func NewStateAccessList() *StateAccessList {
|
||||
return &StateAccessList{
|
||||
list: make(map[common.Address]StorageAccessList),
|
||||
}
|
||||
}
|
||||
|
||||
// AddAccount records an access to the given account. It is a no-op if the
|
||||
// account is already present.
|
||||
func (s *StateAccessList) AddAccount(addr common.Address) {
|
||||
if _, exists := s.list[addr]; !exists {
|
||||
s.list[addr] = make(StorageAccessList)
|
||||
}
|
||||
}
|
||||
|
||||
// AddState records an access to the given storage slot. The owning account is
|
||||
// implicitly recorded as well.
|
||||
func (s *StateAccessList) AddState(addr common.Address, slot common.Hash) {
|
||||
slots, exists := s.list[addr]
|
||||
if !exists {
|
||||
slots = make(StorageAccessList)
|
||||
s.list[addr] = slots
|
||||
}
|
||||
slots[slot] = struct{}{}
|
||||
}
|
||||
|
||||
// Merge merges the entries from other into the receiver.
|
||||
func (s *StateAccessList) Merge(other *StateAccessList) {
|
||||
for addr, otherSlots := range other.list {
|
||||
slots, exists := s.list[addr]
|
||||
if !exists {
|
||||
s.list[addr] = otherSlots
|
||||
continue
|
||||
}
|
||||
maps.Copy(slots, otherSlots)
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/stateless"
|
||||
"github.com/ethereum/go-ethereum/core/tracing"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/types/bal"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/holiman/uint256"
|
||||
)
|
||||
|
|
@ -94,5 +95,5 @@ type StateDB interface {
|
|||
AccessEvents() *state.AccessEvents
|
||||
|
||||
// Finalise must be invoked at the end of a transaction
|
||||
Finalise(bool)
|
||||
Finalise(bool) *bal.StateAccessList
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue