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

View file

@ -196,7 +196,6 @@ func (it *binaryNodeIterator) LeafKey() []byte {
if !ok { if !ok {
panic("Leaf() called on an binary node iterator not at a leaf location") panic("Leaf() called on an binary node iterator not at a leaf location")
} }
return leaf.Key(it.stack[len(it.stack)-1].Index - 1) return leaf.Key(it.stack[len(it.stack)-1].Index - 1)
} }
@ -208,7 +207,6 @@ func (it *binaryNodeIterator) LeafBlob() []byte {
if !ok { if !ok {
panic("LeafBlob() called on an binary node iterator not at a leaf location") panic("LeafBlob() called on an binary node iterator not at a leaf location")
} }
return leaf.Values[it.stack[len(it.stack)-1].Index-1] 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]) { if !bytes.Equal(bt.Stem, key[:31]) {
bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1
new := &InternalNode{depth: bt.depth} n := &InternalNode{depth: bt.depth}
bt.depth++ bt.depth++
var child, other *BinaryNode var child, other *BinaryNode
if bitStem == 0 { if bitStem == 0 {
new.left = bt n.left = bt
child = &new.left child = &n.left
other = &new.right other = &n.right
} else { } else {
new.right = bt n.right = bt
child = &new.right child = &n.right
other = &new.left 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 { if bitKey == bitStem {
var err error var err error
*child, err = (*child).Insert(key, value, nil, depth+1) *child, err = (*child).Insert(key, value, nil, depth+1)
if err != nil { if err != nil {
return new, fmt.Errorf("insert error: %w", err) return n, fmt.Errorf("insert error: %w", err)
} }
*other = Empty{} *other = Empty{}
} else { } else {
@ -73,7 +73,7 @@ func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int
depth: depth + 1, depth: depth + 1,
} }
} }
return new, nil return n, nil
} }
if len(value) != 32 { if len(value) != 32 {
return bt, errors.New("invalid insertion: value length") 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]) { if !bytes.Equal(bt.Stem, key[:31]) {
bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1
new := &InternalNode{depth: bt.depth} n := &InternalNode{depth: bt.depth}
bt.depth++ bt.depth++
var child, other *BinaryNode var child, other *BinaryNode
if bitStem == 0 { if bitStem == 0 {
new.left = bt n.left = bt
child = &new.left child = &n.left
other = &new.right other = &n.right
} else { } else {
new.right = bt n.right = bt
child = &new.right child = &n.right
other = &new.left 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 { if bitKey == bitStem {
var err error var err error
*child, err = (*child).InsertValuesAtStem(key, values, nil, depth+1) *child, err = (*child).InsertValuesAtStem(key, values, nil, depth+1)
if err != nil { if err != nil {
return new, fmt.Errorf("insert error: %w", err) return n, fmt.Errorf("insert error: %w", err)
} }
*other = Empty{} *other = Empty{}
} else { } else {
*other = &StemNode{ *other = &StemNode{
Stem: slices.Clone(key[:31]), Stem: slices.Clone(key[:31]),
Values: values, Values: values,
depth: new.depth + 1, depth: n.depth + 1,
} }
} }
return new, nil return n, nil
} }
// same stem, just merge the two value lists // 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 { if err != nil {
panic(fmt.Errorf("CollectNodes failed: %v", err)) panic(fmt.Errorf("CollectNodes failed: %v", err))
} }
// Serialize root commitment form // Serialize root commitment form
return t.Hash(), nodeset return t.Hash(), nodeset
} }
@ -283,6 +282,7 @@ func (t *BinaryTrie) Copy() *BinaryTrie {
return &BinaryTrie{ return &BinaryTrie{
root: t.root.Copy(), root: t.root.Copy(),
reader: t.reader, reader: t.reader,
tracer: t.tracer.Copy(),
} }
} }
@ -325,6 +325,8 @@ func (t *BinaryTrie) UpdateContractCode(addr common.Address, codeHash common.Has
return nil return nil
} }
// PrefetchAccount attempts to resolve specific accounts from the database
// to accelerate subsequent trie operations.
func (t *BinaryTrie) PrefetchAccount(addresses []common.Address) error { func (t *BinaryTrie) PrefetchAccount(addresses []common.Address) error {
for _, addr := range addresses { for _, addr := range addresses {
if _, err := t.GetAccount(addr); err != nil { if _, err := t.GetAccount(addr); err != nil {