go-ethereum/trie/bintrie/empty.go
CPerezz f57dd20461
trie/bintrie: mark stems created via Empty.Insert* as dirty
Empty.Insert and Empty.InsertValuesAtStem construct a fresh StemNode with
mustRecompute=true but left the new `dirty` field at its zero value. With
the skip-clean CollectNodes optimization enabled, the resulting stem was
treated as already-persisted and never flushed to disk. A parent
InternalNode's blob would be written referencing a hash for which no
blob existed, causing "missing trie node" errors on subsequent reads.

This is the path hit whenever a key is inserted into an Empty subtree —
the common case on the first insert, and frequently thereafter on splits
that leave one side Empty. A long-running deployment surfaced the bug
after ~15 hours of random ERC20 writes.

Add `dirty: true` to both struct literals, and add regression guards
TestEmptyInsertMarksDirty / TestEmptyInsertValuesAtStemMarksDirty that
assert the returned stem is dirty.
2026-04-18 09:06:11 +02:00

76 lines
2 KiB
Go

// Copyright 2025 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 bintrie
import (
"slices"
"github.com/ethereum/go-ethereum/common"
)
type Empty struct{}
func (e Empty) Get(_ []byte, _ NodeResolverFn) ([]byte, error) {
return nil, nil
}
func (e Empty) Insert(key []byte, value []byte, _ NodeResolverFn, depth int) (BinaryNode, error) {
var values [256][]byte
values[key[31]] = value
return &StemNode{
Stem: slices.Clone(key[:31]),
Values: values[:],
depth: depth,
mustRecompute: true,
dirty: true,
}, nil
}
func (e Empty) Copy() BinaryNode {
return Empty{}
}
func (e Empty) Hash() common.Hash {
return common.Hash{}
}
func (e Empty) GetValuesAtStem(_ []byte, _ NodeResolverFn) ([][]byte, error) {
var values [256][]byte
return values[:], nil
}
func (e Empty) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolverFn, depth int) (BinaryNode, error) {
return &StemNode{
Stem: slices.Clone(key[:31]),
Values: values,
depth: depth,
mustRecompute: true,
dirty: true,
}, nil
}
func (e Empty) CollectNodes(_ []byte, _ NodeFlushFn) error {
return nil
}
func (e Empty) toDot(parent string, path string) string {
return ""
}
func (e Empty) GetHeight() int {
return 0
}