forked from filecoin-project/go-fil-markets
/
mockdagstorewrapper.go
176 lines (140 loc) · 4.65 KB
/
mockdagstorewrapper.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package shared_testutil
import (
"context"
"io"
"os"
"sync"
"github.com/ipfs/go-cid"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
carindex "github.com/ipld/go-car/v2/index"
"golang.org/x/xerrors"
"github.com/filecoin-project/dagstore"
"github.com/filecoin-project/boost-gfm/piecestore"
"github.com/filecoin-project/boost-gfm/retrievalmarket"
"github.com/filecoin-project/boost-gfm/storagemarket"
"github.com/filecoin-project/boost-gfm/stores"
)
type registration struct {
CarPath string
EagerInit bool
}
// MockDagStoreWrapper is used to mock out the DAG store wrapper operations
// for the tests.
// It simulates getting deal info from a piece store and unsealing the data for
// the deal from a retrieval provider node.
type MockDagStoreWrapper struct {
pieceStore piecestore.PieceStore
sa retrievalmarket.SectorAccessor
lk sync.Mutex
registrations map[cid.Cid]registration
piecesWithBlock map[cid.Cid][]cid.Cid
}
var _ stores.DAGStoreWrapper = (*MockDagStoreWrapper)(nil)
func NewMockDagStoreWrapper(pieceStore piecestore.PieceStore, sa retrievalmarket.SectorAccessor) *MockDagStoreWrapper {
return &MockDagStoreWrapper{
pieceStore: pieceStore,
sa: sa,
registrations: make(map[cid.Cid]registration),
piecesWithBlock: make(map[cid.Cid][]cid.Cid),
}
}
func (m *MockDagStoreWrapper) RegisterShard(ctx context.Context, pieceCid cid.Cid, carPath string, eagerInit bool, resch chan dagstore.ShardResult) error {
m.lk.Lock()
defer m.lk.Unlock()
m.registrations[pieceCid] = registration{
CarPath: carPath,
EagerInit: eagerInit,
}
resch <- dagstore.ShardResult{}
return nil
}
func (m *MockDagStoreWrapper) DestroyShard(ctx context.Context, pieceCid cid.Cid, resch chan dagstore.ShardResult) error {
m.lk.Lock()
defer m.lk.Unlock()
delete(m.registrations, pieceCid)
resch <- dagstore.ShardResult{}
return nil
}
func (m *MockDagStoreWrapper) GetIterableIndexForPiece(c cid.Cid) (carindex.IterableIndex, error) {
return nil, nil
}
func (m *MockDagStoreWrapper) MigrateDeals(ctx context.Context, deals []storagemarket.MinerDeal) (bool, error) {
return true, nil
}
func (m *MockDagStoreWrapper) LenRegistrations() int {
m.lk.Lock()
defer m.lk.Unlock()
return len(m.registrations)
}
func (m *MockDagStoreWrapper) GetRegistration(pieceCid cid.Cid) (registration, bool) {
m.lk.Lock()
defer m.lk.Unlock()
reg, ok := m.registrations[pieceCid]
return reg, ok
}
func (m *MockDagStoreWrapper) ClearRegistrations() {
m.lk.Lock()
defer m.lk.Unlock()
m.registrations = make(map[cid.Cid]registration)
}
func (m *MockDagStoreWrapper) LoadShard(ctx context.Context, pieceCid cid.Cid) (stores.ClosableBlockstore, error) {
m.lk.Lock()
defer m.lk.Unlock()
_, ok := m.registrations[pieceCid]
if !ok {
return nil, xerrors.Errorf("no shard for piece CID %s", pieceCid)
}
// Get the piece info from the piece store
pi, err := m.pieceStore.GetPieceInfo(pieceCid)
if err != nil {
return nil, err
}
// Unseal the sector data for the deal
deal := pi.Deals[0]
r, err := m.sa.UnsealSector(ctx, deal.SectorID, deal.Offset.Unpadded(), deal.Length.Unpadded())
if err != nil {
return nil, xerrors.Errorf("error unsealing deal for piece %s: %w", pieceCid, err)
}
return getBlockstoreFromReader(r, pieceCid)
}
func getBlockstoreFromReader(r io.ReadCloser, pieceCid cid.Cid) (stores.ClosableBlockstore, error) {
// Write the piece to a file
tmpFile, err := os.CreateTemp("", "dagstoretmp")
if err != nil {
return nil, xerrors.Errorf("creating temp file for piece CID %s: %w", pieceCid, err)
}
_, err = io.Copy(tmpFile, r)
if err != nil {
return nil, xerrors.Errorf("copying read stream to temp file for piece CID %s: %w", pieceCid, err)
}
err = tmpFile.Close()
if err != nil {
return nil, xerrors.Errorf("closing temp file for piece CID %s: %w", pieceCid, err)
}
// Get a blockstore from the CAR file
return blockstore.OpenReadOnly(tmpFile.Name(), carv2.ZeroLengthSectionAsEOF(true), blockstore.UseWholeCIDs(true))
}
func (m *MockDagStoreWrapper) Close() error {
return nil
}
func (m *MockDagStoreWrapper) GetPiecesContainingBlock(blockCID cid.Cid) ([]cid.Cid, error) {
m.lk.Lock()
defer m.lk.Unlock()
pieces, ok := m.piecesWithBlock[blockCID]
if !ok {
return nil, retrievalmarket.ErrNotFound
}
return pieces, nil
}
// Used by the tests to add an entry to the index of block CID -> []piece CID
func (m *MockDagStoreWrapper) AddBlockToPieceIndex(blockCID cid.Cid, pieceCid cid.Cid) {
m.lk.Lock()
defer m.lk.Unlock()
pieces, ok := m.piecesWithBlock[blockCID]
if !ok {
m.piecesWithBlock[blockCID] = []cid.Cid{pieceCid}
} else {
m.piecesWithBlock[blockCID] = append(pieces, pieceCid)
}
}