trie/bintrie: fix copy

This commit is contained in:
Gary Rong 2025-09-01 18:44:17 +08:00
parent 4506867106
commit fa5158753f
4 changed files with 24 additions and 26 deletions

View file

@ -128,7 +128,6 @@ func (bt *InternalNode) InsertValuesAtStem(stem []byte, values [][]byte, resolve
} else {
child = &bt.right
}
*child, err = (*child).InsertValuesAtStem(stem, values, resolver, depth+1)
return bt, err
}
@ -186,6 +185,5 @@ func (bt *InternalNode) toDot(parent, path string) string {
if bt.right != nil {
ret = fmt.Sprintf("%s%s", ret, bt.right.toDot(me, fmt.Sprintf("%s%02x", path, 1)))
}
return ret
}

View file

@ -196,7 +196,6 @@ func (it *binaryNodeIterator) LeafKey() []byte {
if !ok {
panic("Leaf() called on an binary node iterator not at a leaf location")
}
return leaf.Key(it.stack[len(it.stack)-1].Index - 1)
}
@ -208,7 +207,6 @@ func (it *binaryNodeIterator) LeafBlob() []byte {
if !ok {
panic("LeafBlob() called on an binary node iterator not at a leaf location")
}
return leaf.Values[it.stack[len(it.stack)-1].Index-1]
}

View file

@ -43,25 +43,25 @@ func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int
if !bytes.Equal(bt.Stem, key[:31]) {
bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1
new := &InternalNode{depth: bt.depth}
n := &InternalNode{depth: bt.depth}
bt.depth++
var child, other *BinaryNode
if bitStem == 0 {
new.left = bt
child = &new.left
other = &new.right
n.left = bt
child = &n.left
other = &n.right
} else {
new.right = bt
child = &new.right
other = &new.left
n.right = bt
child = &n.right
other = &n.left
}
bitKey := key[new.depth/8] >> (7 - (new.depth % 8)) & 1
bitKey := key[n.depth/8] >> (7 - (n.depth % 8)) & 1
if bitKey == bitStem {
var err error
*child, err = (*child).Insert(key, value, nil, depth+1)
if err != nil {
return new, fmt.Errorf("insert error: %w", err)
return n, fmt.Errorf("insert error: %w", err)
}
*other = Empty{}
} else {
@ -73,7 +73,7 @@ func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int
depth: depth + 1,
}
}
return new, nil
return n, nil
}
if len(value) != 32 {
return bt, errors.New("invalid insertion: value length")
@ -151,35 +151,35 @@ func (bt *StemNode) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolv
if !bytes.Equal(bt.Stem, key[:31]) {
bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1
new := &InternalNode{depth: bt.depth}
n := &InternalNode{depth: bt.depth}
bt.depth++
var child, other *BinaryNode
if bitStem == 0 {
new.left = bt
child = &new.left
other = &new.right
n.left = bt
child = &n.left
other = &n.right
} else {
new.right = bt
child = &new.right
other = &new.left
n.right = bt
child = &n.right
other = &n.left
}
bitKey := key[new.depth/8] >> (7 - (new.depth % 8)) & 1
bitKey := key[n.depth/8] >> (7 - (n.depth % 8)) & 1
if bitKey == bitStem {
var err error
*child, err = (*child).InsertValuesAtStem(key, values, nil, depth+1)
if err != nil {
return new, fmt.Errorf("insert error: %w", err)
return n, fmt.Errorf("insert error: %w", err)
}
*other = Empty{}
} else {
*other = &StemNode{
Stem: slices.Clone(key[:31]),
Values: values,
depth: new.depth + 1,
depth: n.depth + 1,
}
}
return new, nil
return n, nil
}
// same stem, just merge the two value lists

View file

@ -256,7 +256,6 @@ func (t *BinaryTrie) Commit(_ bool) (common.Hash, *trienode.NodeSet) {
if err != nil {
panic(fmt.Errorf("CollectNodes failed: %v", err))
}
// Serialize root commitment form
return t.Hash(), nodeset
}
@ -283,6 +282,7 @@ func (t *BinaryTrie) Copy() *BinaryTrie {
return &BinaryTrie{
root: t.root.Copy(),
reader: t.reader,
tracer: t.tracer.Copy(),
}
}
@ -325,6 +325,8 @@ func (t *BinaryTrie) UpdateContractCode(addr common.Address, codeHash common.Has
return nil
}
// PrefetchAccount attempts to resolve specific accounts from the database
// to accelerate subsequent trie operations.
func (t *BinaryTrie) PrefetchAccount(addresses []common.Address) error {
for _, addr := range addresses {
if _, err := t.GetAccount(addr); err != nil {