|
| 1 | +package commitment |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/erigontech/erigon/db/kv" |
| 11 | +) |
| 12 | + |
| 13 | +// mockPatriciaContext implements PatriciaContext for testing. |
| 14 | +type mockPatriciaContext struct { |
| 15 | + branches map[string][]byte |
| 16 | +} |
| 17 | + |
| 18 | +func (m *mockPatriciaContext) Branch(prefix []byte) ([]byte, kv.Step, error) { |
| 19 | + // Return stub branch data (>=4 bytes) so prefetchBranches doesn't stop early. |
| 20 | + // Use the map when present, otherwise return a generic stub. |
| 21 | + if data, ok := m.branches[string(prefix)]; ok { |
| 22 | + return data, 0, nil |
| 23 | + } |
| 24 | + return []byte("stub-branch-data"), 0, nil |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockPatriciaContext) Account(plainKey []byte) (*Update, error) { return nil, nil } |
| 28 | +func (m *mockPatriciaContext) Storage(plainKey []byte) (*Update, error) { return nil, nil } |
| 29 | +func (m *mockPatriciaContext) PutBranch(prefix []byte, data []byte, prevData []byte) error { |
| 30 | + return nil |
| 31 | +} |
| 32 | +func (m *mockPatriciaContext) TxNum() uint64 { return 0 } |
| 33 | +func (m *mockPatriciaContext) Variant() TrieVariant { return VariantHexPatriciaTrie } |
| 34 | + |
| 35 | +func TestBranchPrefetcher_StartStop(t *testing.T) { |
| 36 | + cache := NewBranchCache(1024) |
| 37 | + factory := func() (PatriciaContext, func()) { |
| 38 | + return &mockPatriciaContext{branches: map[string][]byte{}}, nil |
| 39 | + } |
| 40 | + |
| 41 | + ctx := context.Background() |
| 42 | + p := NewBranchPrefetcher(ctx, cache, factory, 2, 4) |
| 43 | + p.Start() |
| 44 | + p.Stop() |
| 45 | + // Double-stop must be a no-op. |
| 46 | + p.Stop() |
| 47 | +} |
| 48 | + |
| 49 | +func TestBranchPrefetcher_PopulatesCache(t *testing.T) { |
| 50 | + rootKey := HexNibblesToCompactBytes(nil) // depth-0 compact key |
| 51 | + depth1Key := HexNibblesToCompactBytes([]byte{3}) // nibble 3 |
| 52 | + |
| 53 | + branchData := []byte("some-branch-data-long-enough") |
| 54 | + |
| 55 | + factory := func() (PatriciaContext, func()) { |
| 56 | + ctx := &mockPatriciaContext{ |
| 57 | + branches: map[string][]byte{ |
| 58 | + string(rootKey): branchData, |
| 59 | + string(depth1Key): branchData, |
| 60 | + }, |
| 61 | + } |
| 62 | + return ctx, nil |
| 63 | + } |
| 64 | + |
| 65 | + cache := NewBranchCache(1024) |
| 66 | + ctx := context.Background() |
| 67 | + p := NewBranchPrefetcher(ctx, cache, factory, 2, 4) |
| 68 | + p.Start() |
| 69 | + |
| 70 | + // Submit a plain key — prefetcher will hash it and walk ancestor compact keys. |
| 71 | + // Use an all-zero 32-byte key whose first nibble is 0 → depth-1 compact key [0x10]. |
| 72 | + plainKey := make([]byte, 32) |
| 73 | + p.SubmitPlainKey(plainKey) |
| 74 | + |
| 75 | + // Give workers time to process. |
| 76 | + deadline := time.Now().Add(2 * time.Second) |
| 77 | + for time.Now().Before(deadline) { |
| 78 | + if p.Prefetched() > 0 { |
| 79 | + break |
| 80 | + } |
| 81 | + time.Sleep(10 * time.Millisecond) |
| 82 | + } |
| 83 | + |
| 84 | + p.Stop() |
| 85 | + require.Positive(t, p.Prefetched(), "expected at least one branch prefetched") |
| 86 | +} |
| 87 | + |
| 88 | +func TestBranchPrefetcher_ContextCancellation(t *testing.T) { |
| 89 | + factory := func() (PatriciaContext, func()) { |
| 90 | + return &mockPatriciaContext{branches: map[string][]byte{}}, nil |
| 91 | + } |
| 92 | + |
| 93 | + ctx, cancel := context.WithCancel(context.Background()) |
| 94 | + cache := NewBranchCache(1024) |
| 95 | + p := NewBranchPrefetcher(ctx, cache, factory, 2, 4) |
| 96 | + p.Start() |
| 97 | + |
| 98 | + // Cancel parent context — workers should exit without an explicit Stop(). |
| 99 | + cancel() |
| 100 | + |
| 101 | + // Stop should still be safe to call after context cancellation. |
| 102 | + p.Stop() |
| 103 | +} |
| 104 | + |
| 105 | +func TestBranchPrefetcher_SubmitAfterStop(t *testing.T) { |
| 106 | + factory := func() (PatriciaContext, func()) { |
| 107 | + return &mockPatriciaContext{branches: map[string][]byte{}}, nil |
| 108 | + } |
| 109 | + |
| 110 | + cache := NewBranchCache(1024) |
| 111 | + p := NewBranchPrefetcher(context.Background(), cache, factory, 1, 4) |
| 112 | + p.Start() |
| 113 | + p.Stop() |
| 114 | + |
| 115 | + // Submit after Stop must not panic. |
| 116 | + p.Submit(make([]byte, 8)) |
| 117 | + p.SubmitPlainKey(make([]byte, 32)) |
| 118 | +} |
0 commit comments