mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-17 04:11:37 +00:00
trie/bintrie: inline getSingle into GetValue
Gballet asked (comment 3101679920) to fold the unexported getSingle helper
into its single caller, and (comment 3101677731) to rename GetSingle ('bad
name: get single what?') with a top-level docstring.
- Inline getSingle into GetValue (one function instead of two).
- Rename GetSingle → GetValue and add a docstring.
- Drop the hasParent tracker that was only used for the 'hashed at root'
guard — that case is now handled by kindEmpty / the top-level
NewBinaryTrie-time root resolution, so remove the check rather than
keep dead state.
CE2 will later fold this into GetValuesAtStem; this commit closes the
naming + inline asks independently.
This commit is contained in:
parent
8f31f30500
commit
bbf062c746
1 changed files with 12 additions and 18 deletions
|
|
@ -26,16 +26,14 @@ import (
|
||||||
// NodeResolverFn resolves a hashed node from the database.
|
// NodeResolverFn resolves a hashed node from the database.
|
||||||
type NodeResolverFn func([]byte, common.Hash) ([]byte, error)
|
type NodeResolverFn func([]byte, common.Hash) ([]byte, error)
|
||||||
|
|
||||||
func (s *NodeStore) GetSingle(stem []byte, suffix byte, resolver NodeResolverFn) ([]byte, error) {
|
// GetValue returns the value at (stem, suffix) or nil if absent. It walks
|
||||||
return s.getSingle(s.root, stem, suffix, resolver)
|
// the trie from the root, resolving any HashedNode encountered on the path
|
||||||
}
|
// via the supplied resolver.
|
||||||
|
func (s *NodeStore) GetValue(stem []byte, suffix byte, resolver NodeResolverFn) ([]byte, error) {
|
||||||
func (s *NodeStore) getSingle(ref nodeRef, stem []byte, suffix byte, resolver NodeResolverFn) ([]byte, error) {
|
cur := s.root
|
||||||
cur := ref
|
|
||||||
// Track parent for HashedNode resolution (update parent's child ref).
|
// Track parent for HashedNode resolution (update parent's child ref).
|
||||||
var parentIdx uint32
|
var parentIdx uint32
|
||||||
var parentIsLeft bool
|
var parentIsLeft bool
|
||||||
hasParent := false
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
switch cur.Kind() {
|
switch cur.Kind() {
|
||||||
|
|
@ -46,7 +44,6 @@ func (s *NodeStore) getSingle(ref nodeRef, stem []byte, suffix byte, resolver No
|
||||||
}
|
}
|
||||||
bit := stem[node.depth/8] >> (7 - (node.depth % 8)) & 1
|
bit := stem[node.depth/8] >> (7 - (node.depth % 8)) & 1
|
||||||
parentIdx = cur.Index()
|
parentIdx = cur.Index()
|
||||||
hasParent = true
|
|
||||||
if bit == 0 {
|
if bit == 0 {
|
||||||
parentIsLeft = true
|
parentIsLeft = true
|
||||||
cur = node.left
|
cur = node.left
|
||||||
|
|
@ -63,27 +60,24 @@ func (s *NodeStore) getSingle(ref nodeRef, stem []byte, suffix byte, resolver No
|
||||||
return sn.getValue(suffix), nil
|
return sn.getValue(suffix), nil
|
||||||
|
|
||||||
case kindHashed:
|
case kindHashed:
|
||||||
if !hasParent {
|
|
||||||
return nil, errors.New("getSingle: hashed node at root")
|
|
||||||
}
|
|
||||||
if resolver == nil {
|
if resolver == nil {
|
||||||
return nil, errors.New("getSingle: cannot resolve hashed node without resolver")
|
return nil, errors.New("GetValue: cannot resolve hashed node without resolver")
|
||||||
}
|
}
|
||||||
hn := s.getHashed(cur.Index())
|
hn := s.getHashed(cur.Index())
|
||||||
parentNode := s.getInternal(parentIdx)
|
parentNode := s.getInternal(parentIdx)
|
||||||
path, err := keyToPath(int(parentNode.depth), stem)
|
path, err := keyToPath(int(parentNode.depth), stem)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("getSingle path error: %w", err)
|
return nil, fmt.Errorf("GetValue path error: %w", err)
|
||||||
}
|
}
|
||||||
data, err := resolver(path, hn.Hash())
|
data, err := resolver(path, hn.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("getSingle resolve error: %w", err)
|
return nil, fmt.Errorf("GetValue resolve error: %w", err)
|
||||||
}
|
}
|
||||||
resolved, err := s.deserializeNodeWithHash(data, int(parentNode.depth)+1, hn.Hash())
|
resolved, err := s.deserializeNodeWithHash(data, int(parentNode.depth)+1, hn.Hash())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("getSingle deserialization error: %w", err)
|
return nil, fmt.Errorf("GetValue deserialization error: %w", err)
|
||||||
}
|
}
|
||||||
// Update parent's child ref
|
// Update parent's child ref.
|
||||||
s.freeHashedNode(cur.Index())
|
s.freeHashedNode(cur.Index())
|
||||||
if parentIsLeft {
|
if parentIsLeft {
|
||||||
parentNode.left = resolved
|
parentNode.left = resolved
|
||||||
|
|
@ -96,7 +90,7 @@ func (s *NodeStore) getSingle(ref nodeRef, stem []byte, suffix byte, resolver No
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("getSingle: unexpected node kind %d", cur.Kind())
|
return nil, fmt.Errorf("GetValue: unexpected node kind %d", cur.Kind())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -555,7 +549,7 @@ func (s *NodeStore) Insert(key []byte, value []byte, resolver NodeResolverFn) er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *NodeStore) Get(key []byte, resolver NodeResolverFn) ([]byte, error) {
|
func (s *NodeStore) Get(key []byte, resolver NodeResolverFn) ([]byte, error) {
|
||||||
return s.GetSingle(key[:StemSize], key[StemSize], resolver)
|
return s.GetValue(key[:StemSize], key[StemSize], resolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *NodeStore) getHeight(ref nodeRef) int {
|
func (s *NodeStore) getHeight(ref nodeRef) int {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue