From d47260ea98a8ef30a90c356d59a635f755e27b13 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Tue, 18 Nov 2025 13:49:27 +0800 Subject: [PATCH] core, trie: clean up trie interface #26388 (#1125) * all: cleanup trie interface * eth, trie: address comments Co-authored-by: rjl493456442 --- core/state/database.go | 17 ++++++++++++----- core/state/statedb.go | 6 +++--- trie/secure_trie.go | 26 +++++++++++++------------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index 75d3f8770d..efb2522196 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -73,8 +73,13 @@ type Trie interface { // trie.MissingNodeError is returned. TryGet(key []byte) ([]byte, error) - // TryGetAccount abstract an account read from the trie. - TryGetAccount(key []byte) (*types.StateAccount, error) + // TryGetAccount abstracts an account read from the trie. It retrieves the + // account blob from the trie with provided account address and decodes it + // with associated decoding algorithm. If the specified account is not in + // the trie, nil will be returned. If the trie is corrupted(e.g. some nodes + // are missing or the account blob is incorrect for decoding), an error will + // be returned. + TryGetAccount(address common.Address) (*types.StateAccount, error) // TryUpdate associates key with value in the trie. If value has length zero, any // existing value is deleted from the trie. The value bytes must not be modified @@ -82,15 +87,17 @@ type Trie interface { // database, a trie.MissingNodeError is returned. TryUpdate(key, value []byte) error - // TryUpdateAccount abstract an account write to the trie. - TryUpdateAccount(key []byte, account *types.StateAccount) error + // TryUpdateAccount abstracts an account write to the trie. It encodes the + // provided account object with associated algorithm and then updates it + // in the trie with provided address. + TryUpdateAccount(address common.Address, account *types.StateAccount) error // TryDelete removes any existing value for key from the trie. If a node was not // found in the database, a trie.MissingNodeError is returned. TryDelete(key []byte) error // TryDeleteAccount abstracts an account deletion from the trie. - TryDeleteAccount(key []byte) error + TryDeleteAccount(address common.Address) error // Hash returns the root hash of the trie. It does not write to the database and // can be used even if the trie doesn't have one. diff --git a/core/state/statedb.go b/core/state/statedb.go index a26d97e178..1849500709 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -537,7 +537,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) { // Encode the account and update the account trie addr := obj.Address() - if err := s.trie.TryUpdateAccount(addr[:], &obj.data); err != nil { + if err := s.trie.TryUpdateAccount(addr, &obj.data); err != nil { s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) } } @@ -549,7 +549,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) { // Delete the account from the trie addr := obj.Address() - if err := s.trie.TryDeleteAccount(addr[:]); err != nil { + if err := s.trie.TryDeleteAccount(addr); err != nil { s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err)) } } @@ -584,7 +584,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { } // Load the object from the database start := time.Now() - data, err := s.trie.TryGetAccount(addr.Bytes()) + data, err := s.trie.TryGetAccount(addr) s.AccountReads += time.Since(start) if err != nil { s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %w", addr.Bytes(), err)) diff --git a/trie/secure_trie.go b/trie/secure_trie.go index ef722827f2..2ee82f50c0 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -90,11 +90,11 @@ func (t *StateTrie) TryGet(key []byte) ([]byte, error) { return t.trie.TryGet(t.hashKey(key)) } -// TryGetAccount attempts to retrieve an account with provided trie path. +// TryGetAccount attempts to retrieve an account with provided account address. // If the specified account is not in the trie, nil will be returned. // If a trie node is not found in the database, a MissingNodeError is returned. -func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error) { - res, err := t.trie.TryGet(t.hashKey(key)) +func (t *StateTrie) TryGetAccount(address common.Address) (*types.StateAccount, error) { + res, err := t.trie.TryGet(t.hashKey(address.Bytes())) if res == nil || err != nil { return nil, err } @@ -103,11 +103,11 @@ func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error) { return ret, err } -// TryGetAccountWithPreHashedKey does the same thing as TryGetAccount, however -// it expects a key that is already hashed. This constitutes an abstraction leak, -// since the client code needs to know the key format. -func (t *StateTrie) TryGetAccountWithPreHashedKey(key []byte) (*types.StateAccount, error) { - res, err := t.trie.TryGet(key) +// TryGetAccountByHash does the same thing as TryGetAccount, however +// it expects an account hash that is the hash of address. This constitutes an +// abstraction leak, since the client code needs to know the key format. +func (t *StateTrie) TryGetAccountByHash(addrHash common.Hash) (*types.StateAccount, error) { + res, err := t.trie.TryGet(addrHash.Bytes()) if res == nil || err != nil { return nil, err } @@ -156,8 +156,8 @@ func (t *StateTrie) TryUpdate(key, value []byte) error { // TryUpdateAccount account will abstract the write of an account to the // secure trie. -func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error { - hk := t.hashKey(key) +func (t *StateTrie) TryUpdateAccount(address common.Address, acc *types.StateAccount) error { + hk := t.hashKey(address.Bytes()) data, err := rlp.EncodeToBytes(acc) if err != nil { return err @@ -165,7 +165,7 @@ func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error if err := t.trie.TryUpdate(hk, data); err != nil { return err } - t.getSecKeyCache()[string(hk)] = common.CopyBytes(key) + t.getSecKeyCache()[string(hk)] = address.Bytes() return nil } @@ -186,8 +186,8 @@ func (t *StateTrie) TryDelete(key []byte) error { } // TryDeleteAccount abstracts an account deletion from the trie. -func (t *StateTrie) TryDeleteAccount(key []byte) error { - hk := t.hashKey(key) +func (t *StateTrie) TryDeleteAccount(address common.Address) error { + hk := t.hashKey(address.Bytes()) delete(t.getSecKeyCache(), string(hk)) return t.trie.TryDelete(hk) }