|
| 1 | +// Copyright 2025 The Swarm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package joiner_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "context" |
| 10 | + "strconv" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/ethersphere/bee/v2/pkg/cac" |
| 14 | + "github.com/ethersphere/bee/v2/pkg/file/joiner" |
| 15 | + "github.com/ethersphere/bee/v2/pkg/file/redundancy/getter" |
| 16 | + "github.com/ethersphere/bee/v2/pkg/log" |
| 17 | + "github.com/ethersphere/bee/v2/pkg/storage" |
| 18 | + "github.com/ethersphere/bee/v2/pkg/storage/inmemchunkstore" |
| 19 | + "github.com/ethersphere/bee/v2/pkg/swarm" |
| 20 | + "github.com/klauspost/reedsolomon" |
| 21 | +) |
| 22 | + |
| 23 | +// TestReDecoderFlow tests the complete flow of: |
| 24 | +// 1. Loading data with redundancy getter |
| 25 | +// 2. Successful recovery which nulls the decoder |
| 26 | +// 3. Chunk eviction from cache |
| 27 | +// 4. Reloading the same data through ReDecoder fallback |
| 28 | +func TestReDecoderFlow(t *testing.T) { |
| 29 | + ctx := context.Background() |
| 30 | + dataShardCount := 4 |
| 31 | + parityShardCount := 2 |
| 32 | + totalShardCount := dataShardCount + parityShardCount |
| 33 | + |
| 34 | + // Create real data chunks with proper content |
| 35 | + dataShards := make([][]byte, dataShardCount) |
| 36 | + for i := 0; i < dataShardCount; i++ { |
| 37 | + // Create chunks with simpler test data |
| 38 | + dataShards[i] = make([]byte, swarm.ChunkWithSpanSize) |
| 39 | + // Create a unique string for this shard |
| 40 | + testData := []byte("test-data-" + strconv.Itoa(i)) |
| 41 | + // Copy as much as will fit |
| 42 | + copy(dataShards[i], testData) |
| 43 | + } |
| 44 | + |
| 45 | + // Create parity chunks using Reed-Solomon encoding |
| 46 | + parityShards := make([][]byte, parityShardCount) |
| 47 | + for i := 0; i < parityShardCount; i++ { |
| 48 | + parityShards[i] = make([]byte, swarm.ChunkWithSpanSize) |
| 49 | + } |
| 50 | + |
| 51 | + // Create Reed-Solomon encoder |
| 52 | + enc, err := reedsolomon.New(dataShardCount, parityShardCount) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Failed to create Reed-Solomon encoder: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Combine data and parity shards |
| 58 | + allShards := make([][]byte, totalShardCount) |
| 59 | + copy(allShards, dataShards) |
| 60 | + copy(allShards[dataShardCount:], parityShards) |
| 61 | + |
| 62 | + // Encode to generate parity chunks |
| 63 | + if err := enc.Encode(allShards); err != nil { |
| 64 | + t.Fatalf("Failed to encode data: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Create content-addressed chunks for all shards |
| 68 | + addresses := make([]swarm.Address, totalShardCount) |
| 69 | + chunks := make([]swarm.Chunk, totalShardCount) |
| 70 | + |
| 71 | + for i := 0; i < totalShardCount; i++ { |
| 72 | + // Create proper content-addressed chunks |
| 73 | + chunk, err := cac.NewWithDataSpan(allShards[i]) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("Failed to create content-addressed chunk %d: %v", i, err) |
| 76 | + } |
| 77 | + chunks[i] = chunk |
| 78 | + addresses[i] = chunk.Address() |
| 79 | + } |
| 80 | + |
| 81 | + // Select a data chunk to be missing (which will be recovered) |
| 82 | + missingChunkIndex := 2 // The third data chunk will be missing |
| 83 | + mockStore := inmemchunkstore.New() |
| 84 | + netFetcher := newMockNetworkFetcher(addresses, addresses[missingChunkIndex]) |
| 85 | + config := getter.Config{ |
| 86 | + Strategy: getter.RACE, |
| 87 | + Logger: log.Noop, |
| 88 | + } |
| 89 | + |
| 90 | + j := joiner.NewDecoderCache(netFetcher, mockStore, config) |
| 91 | + |
| 92 | + // Step 1: Initializing decoder and triggering recovery |
| 93 | + decoder := j.GetOrCreate(addresses, dataShardCount) |
| 94 | + if decoder == nil { |
| 95 | + t.Fatal("Failed to create decoder") |
| 96 | + } |
| 97 | + |
| 98 | + // Verify we can now fetch the previously missing chunk through recovery |
| 99 | + recoveredChunk, err := decoder.Get(ctx, addresses[missingChunkIndex]) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("Failed to recover missing chunk: %v", err) |
| 102 | + } |
| 103 | + // Verify the recovered chunk has the correct content |
| 104 | + if !recoveredChunk.Address().Equal(addresses[missingChunkIndex]) { |
| 105 | + t.Fatalf("Recovered chunk has incorrect address") |
| 106 | + } |
| 107 | + |
| 108 | + // Verify the recovered chunk has the correct content |
| 109 | + recoveredData := recoveredChunk.Data() |
| 110 | + expectedData := chunks[missingChunkIndex].Data() |
| 111 | + if len(recoveredData) != len(expectedData) { |
| 112 | + t.Fatalf("Recovered chunk has incorrect data length: got %d, want %d", len(recoveredData), len(expectedData)) |
| 113 | + } |
| 114 | + if !bytes.Equal(recoveredData, expectedData) { |
| 115 | + t.Fatalf("Recovered chunk has incorrect data") |
| 116 | + } |
| 117 | + // Check if the recovered chunk is now in the store |
| 118 | + _, err = mockStore.Get(ctx, addresses[missingChunkIndex]) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Recovered chunk not saved to store: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Step 2: The original decoder should be automatically nulled after successful recovery |
| 124 | + // This is an internal state check, we can't directly test it but we can verify that |
| 125 | + // we can still access the chunks |
| 126 | + |
| 127 | + // Sanity check - verify we can still fetch chunks through the cache |
| 128 | + for i := 0; i < dataShardCount; i++ { |
| 129 | + _, err := decoder.Get(ctx, addresses[i]) |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("Failed to get chunk %d after recovery: %v", i, err) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Step 3: Testing ReDecoder fallback |
| 136 | + newDecoder := j.GetOrCreate(addresses, dataShardCount) |
| 137 | + if newDecoder == nil { |
| 138 | + t.Fatal("Failed to create ReDecoder") |
| 139 | + } |
| 140 | + |
| 141 | + // Verify all chunks can be fetched through the ReDecoder |
| 142 | + for i := 0; i < dataShardCount; i++ { |
| 143 | + _, err := newDecoder.Get(ctx, addresses[i]) |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("Failed to get chunk %d through ReDecoder: %v", i, err) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Verify that we can also access the first missing chunk - now from the store |
| 150 | + // This would be using the local store and not network or recovery mechanisms |
| 151 | + retrievedChunk, err := newDecoder.Get(ctx, addresses[missingChunkIndex]) |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("Failed to retrieve previously recovered chunk: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + if !retrievedChunk.Address().Equal(addresses[missingChunkIndex]) { |
| 157 | + t.Fatalf("Retrieved chunk has incorrect address") |
| 158 | + } |
| 159 | + |
| 160 | + // Also verify the data content matches |
| 161 | + retrievedData := retrievedChunk.Data() |
| 162 | + expectedData = chunks[missingChunkIndex].Data() |
| 163 | + if len(retrievedData) != len(expectedData) { |
| 164 | + t.Fatalf("Retrieved chunk has incorrect data length: got %d, want %d", len(retrievedData), len(expectedData)) |
| 165 | + } |
| 166 | + if !bytes.Equal(retrievedData, expectedData) { |
| 167 | + t.Fatalf("Retrieved chunk has incorrect data") |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Mock implementation of storage.Getter for testing |
| 172 | +type mockNetworkFetcher struct { |
| 173 | + allAddresses []swarm.Address |
| 174 | + missingAddr swarm.Address |
| 175 | +} |
| 176 | + |
| 177 | +// newMockNetworkFetcher creates a new mock fetcher that will return ErrNotFound for specific addresses |
| 178 | +func newMockNetworkFetcher(allAddrs []swarm.Address, missingAddr swarm.Address) *mockNetworkFetcher { |
| 179 | + return &mockNetworkFetcher{ |
| 180 | + allAddresses: allAddrs, |
| 181 | + missingAddr: missingAddr, |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// Get implements storage.Getter interface |
| 186 | +func (m *mockNetworkFetcher) Get(ctx context.Context, addr swarm.Address) (swarm.Chunk, error) { |
| 187 | + // Simulate network fetch - fail for the missing chunk |
| 188 | + if addr.Equal(m.missingAddr) { |
| 189 | + return nil, storage.ErrNotFound |
| 190 | + } |
| 191 | + |
| 192 | + // Find the index of this address in the all addresses list |
| 193 | + var index int |
| 194 | + for i, a := range m.allAddresses { |
| 195 | + if addr.Equal(a) { |
| 196 | + index = i |
| 197 | + break |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Generate data using the same pattern as in the test |
| 202 | + data := make([]byte, swarm.ChunkWithSpanSize) |
| 203 | + // Create a unique string for this shard |
| 204 | + testData := []byte("test-data-" + strconv.Itoa(index)) |
| 205 | + // Copy as much as will fit |
| 206 | + copy(data, testData) |
| 207 | + |
| 208 | + return swarm.NewChunk(addr, data), nil |
| 209 | +} |
0 commit comments