diff --git a/trie/bintrie/empty.go b/trie/bintrie/empty.go index 252146a4a7..c47e284dac 100644 --- a/trie/bintrie/empty.go +++ b/trie/bintrie/empty.go @@ -36,6 +36,7 @@ func (e Empty) Insert(key []byte, value []byte, _ NodeResolverFn, depth int) (Bi Values: values[:], depth: depth, mustRecompute: true, + dirty: true, }, nil } @@ -58,6 +59,7 @@ func (e Empty) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolverFn, Values: values, depth: depth, mustRecompute: true, + dirty: true, }, nil } diff --git a/trie/bintrie/empty_test.go b/trie/bintrie/empty_test.go index 574ae1830b..4da1ed15a0 100644 --- a/trie/bintrie/empty_test.go +++ b/trie/bintrie/empty_test.go @@ -220,3 +220,45 @@ func TestEmptyGetHeight(t *testing.T) { t.Errorf("Expected height 0 for empty node, got %d", height) } } + +// TestEmptyInsertMarksDirty verifies that a StemNode produced by Empty.Insert +// is marked dirty. Without this, CollectNodes would skip the freshly created +// stem and its blob would never reach disk, producing "missing trie node" +// errors on subsequent reads. +func TestEmptyInsertMarksDirty(t *testing.T) { + key := make([]byte, 32) + key[0] = 0xaa + val := make([]byte, 32) + val[0] = 0xbb + n, err := Empty{}.Insert(key, val, nil, 0) + if err != nil { + t.Fatalf("Insert: %v", err) + } + sn, ok := n.(*StemNode) + if !ok { + t.Fatalf("expected *StemNode, got %T", n) + } + if !sn.dirty { + t.Fatalf("stem produced by Empty.Insert must have dirty=true") + } +} + +// TestEmptyInsertValuesAtStemMarksDirty is the analogous guard for the +// bulk-insert entry point. Fresh stems created here must be dirty. +func TestEmptyInsertValuesAtStemMarksDirty(t *testing.T) { + key := make([]byte, 32) + key[0] = 0xcc + values := make([][]byte, 256) + values[0] = make([]byte, 32) + n, err := Empty{}.InsertValuesAtStem(key, values, nil, 3) + if err != nil { + t.Fatalf("InsertValuesAtStem: %v", err) + } + sn, ok := n.(*StemNode) + if !ok { + t.Fatalf("expected *StemNode, got %T", n) + } + if !sn.dirty { + t.Fatalf("stem produced by Empty.InsertValuesAtStem must have dirty=true") + } +}