forked from filecoin-project/boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiecedirectory_fuzz_test_util.go
110 lines (96 loc) · 2.79 KB
/
piecedirectory_fuzz_test_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package piecedirectory
import (
"context"
"fmt"
"github.com/filecoin-project/boost/extern/boostd-data/client"
"github.com/filecoin-project/boost/extern/boostd-data/model"
"github.com/filecoin-project/boost/extern/boostd-data/svc"
"github.com/filecoin-project/go-state-types/abi"
"github.com/google/uuid"
"github.com/ipld/go-car/v2"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"math/rand"
"os"
"testing"
"time"
)
func testPieceDirectoryFuzz(ctx context.Context, t *testing.T, bdsvc *svc.Service) {
ln, err := bdsvc.Start(ctx, "localhost:0")
require.NoError(t, err)
cl := client.NewStore()
err = cl.Dial(ctx, fmt.Sprintf("ws://%s", ln))
require.NoError(t, err)
defer cl.Close(ctx)
t.Run("blockstore get", func(t *testing.T) {
testPieceDirectoryBlockstoreGetFuzz(ctx, t, cl)
})
}
func testPieceDirectoryBlockstoreGetFuzz(ctx context.Context, t *testing.T, cl *client.Store) {
pieceCount := 5
readers := make(map[abi.SectorNumber]car.SectionReader)
for i := 0; i < pieceCount; i++ {
// Create a random CAR file
_, carFilePath := CreateCarFile(t, i+1)
carFile, err := os.Open(carFilePath)
require.NoError(t, err)
defer carFile.Close()
carReader, err := car.OpenReader(carFilePath)
require.NoError(t, err)
defer carReader.Close()
carv1Reader, err := carReader.DataReader()
require.NoError(t, err)
readers[abi.SectorNumber(i+1)] = carv1Reader
}
// Any calls to get a reader over data should return a reader over the random CAR file
pr := CreateMockPieceReaders(t, readers)
pm := NewPieceDirectory(cl, pr, 1)
pm.Start(ctx)
for sectorNumber, reader := range readers {
pieceCid := CalculateCommp(t, reader).PieceCID
// Add deal info for each piece
di := model.DealInfo{
DealUuid: uuid.New().String(),
ChainDealID: 1,
SectorID: sectorNumber,
PieceOffset: 0,
PieceLength: 0,
}
err := pm.AddDealForPiece(ctx, pieceCid, di)
require.NoError(t, err)
}
var eg errgroup.Group
for _, reader := range readers {
reader := reader
eg.Go(func() error {
// Get the index (offset and size information)
recs := GetRecords(t, reader)
// Fetch many blocks in parallel
var fetchEg errgroup.Group
for _, rec := range recs {
rec := rec
fetchEg.Go(func() error {
// Add a bit of random delay so that we don't always hit the cache
time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
blk, err := pm.BlockstoreGet(ctx, rec.Cid)
if err != nil {
return err
}
if len(blk) != int(rec.Size) {
return fmt.Errorf("wrong block size")
}
chkc, err := rec.Cid.Prefix().Sum(blk)
if err != nil {
return err
}
if !chkc.Equals(rec.Cid) {
return fmt.Errorf("wrong cid")
}
return nil
})
}
return fetchEg.Wait()
})
}
require.NoError(t, eg.Wait())
}