forked from filecoin-project/go-fil-markets
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generators.go
304 lines (260 loc) · 9.39 KB
/
generators.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package shared_testutil
import (
"math/rand"
"testing"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/test"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin/v8/paych"
"github.com/filecoin-project/go-state-types/builtin/v9/market"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/boost-gfm/retrievalmarket"
"github.com/filecoin-project/boost-gfm/storagemarket"
smnet "github.com/filecoin-project/boost-gfm/storagemarket/network"
)
// MakeTestSignedVoucher generates a random SignedVoucher that has all non-zero fields
func MakeTestSignedVoucher() *paych.SignedVoucher {
return &paych.SignedVoucher{
ChannelAddr: address.TestAddress,
TimeLockMin: abi.ChainEpoch(rand.Int63()),
TimeLockMax: 0,
SecretHash: []byte("secret-preimage"),
Extra: MakeTestModVerifyParams(),
Lane: rand.Uint64(),
Nonce: rand.Uint64(),
Amount: MakeTestTokenAmount(),
Merges: []paych.Merge{MakeTestMerge()},
Signature: MakeTestSignature(),
}
}
// MakeTestModVerifyParams generates a random ModVerifyParams that has all non-zero fields
func MakeTestModVerifyParams() *paych.ModVerifyParams {
return &paych.ModVerifyParams{
Actor: address.TestAddress,
Method: abi.MethodNum(rand.Int63()),
Data: []byte("ModVerifyParams data"),
}
}
// MakeTestMerge generates a random Merge that has all non-zero fields
func MakeTestMerge() paych.Merge {
return paych.Merge{
Lane: rand.Uint64(),
Nonce: rand.Uint64(),
}
}
// MakeTestSignature generates a valid yet random Signature with all non-zero fields
func MakeTestSignature() *crypto.Signature {
return &crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: []byte("signature data"),
}
}
// MakeTestTokenAmount generates a valid yet random TokenAmount with a non-zero value.
func MakeTestTokenAmount() abi.TokenAmount {
return abi.TokenAmount(big.NewInt(rand.Int63()))
}
// MakeTestQueryResponse generates a valid, random QueryResponse with no non-zero fields
func MakeTestQueryResponse() retrievalmarket.QueryResponse {
return retrievalmarket.QueryResponse{
Status: retrievalmarket.QueryResponseUnavailable,
Size: rand.Uint64(),
PaymentAddress: address.TestAddress2,
MinPricePerByte: MakeTestTokenAmount(),
MaxPaymentInterval: rand.Uint64(),
MaxPaymentIntervalIncrease: rand.Uint64(),
UnsealPrice: big.Zero(),
}
}
// MakeTestDealProposal generates a valid, random DealProposal
func MakeTestDealProposal() retrievalmarket.DealProposal {
cid := GenerateCids(1)[0]
return retrievalmarket.DealProposal{
PayloadCID: cid,
ID: retrievalmarket.DealID(rand.Uint64()),
Params: retrievalmarket.NewParamsV0(MakeTestTokenAmount(), rand.Uint64(), rand.Uint64()),
}
}
// MakeTestChannelID makes a new empty data transfer channel ID
func MakeTestChannelID() datatransfer.ChannelID {
testPeers := GeneratePeers(2)
transferID := datatransfer.TransferID(rand.Uint64())
return datatransfer.ChannelID{ID: transferID, Initiator: testPeers[0], Responder: testPeers[1]}
}
// MakeTestUnsignedDealProposal generates a deal proposal with no signature
func MakeTestUnsignedDealProposal() market.DealProposal {
start := uint64(rand.Int31())
end := start + uint64(rand.Int31())
l, _ := market.NewLabelFromString("")
return market.DealProposal{
PieceCID: GenerateCids(1)[0],
PieceSize: abi.PaddedPieceSize(rand.Int63()),
Client: address.TestAddress,
Provider: address.TestAddress2,
Label: l,
StartEpoch: abi.ChainEpoch(start),
EndEpoch: abi.ChainEpoch(end),
StoragePricePerEpoch: MakeTestTokenAmount(),
ProviderCollateral: MakeTestTokenAmount(),
ClientCollateral: MakeTestTokenAmount(),
}
}
// MakeTestClientDealProposal generates a valid storage deal proposal
func MakeTestClientDealProposal() *market.ClientDealProposal {
return &market.ClientDealProposal{
Proposal: MakeTestUnsignedDealProposal(),
ClientSignature: *MakeTestSignature(),
}
}
// MakeTestDataRef returns a storage market data ref
func MakeTestDataRef(manualXfer bool) *storagemarket.DataRef {
out := &storagemarket.DataRef{
Root: GenerateCids(1)[0],
}
if manualXfer {
out.TransferType = storagemarket.TTManual
}
return out
}
// MakeTestClientDeal returns a storage market client deal
func MakeTestClientDeal(state storagemarket.StorageDealStatus, clientDealProposal *market.ClientDealProposal, manualXfer bool) (*storagemarket.ClientDeal, error) {
proposalNd, err := cborutil.AsIpld(clientDealProposal)
if err != nil {
return nil, err
}
p, err := test.RandPeerID()
if err != nil {
return nil, err
}
return &storagemarket.ClientDeal{
ProposalCid: proposalNd.Cid(),
ClientDealProposal: *clientDealProposal,
State: state,
Miner: p,
MinerWorker: address.TestAddress2,
DataRef: MakeTestDataRef(manualXfer),
DealStages: storagemarket.NewDealStages(),
}, nil
}
// MakeTestMinerDeal returns a storage market provider deal
func MakeTestMinerDeal(state storagemarket.StorageDealStatus, clientDealProposal *market.ClientDealProposal, dataRef *storagemarket.DataRef) (*storagemarket.MinerDeal, error) {
proposalNd, err := cborutil.AsIpld(clientDealProposal)
if err != nil {
return nil, err
}
p, err := test.RandPeerID()
if err != nil {
return nil, err
}
return &storagemarket.MinerDeal{
ProposalCid: proposalNd.Cid(),
ClientDealProposal: *clientDealProposal,
State: state,
Client: p,
Ref: dataRef,
}, nil
}
// MakeTestStorageAsk generates a storage ask
func MakeTestStorageAsk() *storagemarket.StorageAsk {
return &storagemarket.StorageAsk{
Price: MakeTestTokenAmount(),
VerifiedPrice: MakeTestTokenAmount(),
MinPieceSize: abi.PaddedPieceSize(rand.Uint64()),
Miner: address.TestAddress2,
Timestamp: abi.ChainEpoch(rand.Int63()),
Expiry: abi.ChainEpoch(rand.Int63()),
SeqNo: rand.Uint64(),
}
}
// MakeTestSignedStorageAsk generates a signed storage ask
func MakeTestSignedStorageAsk() *storagemarket.SignedStorageAsk {
return &storagemarket.SignedStorageAsk{
Ask: MakeTestStorageAsk(),
Signature: MakeTestSignature(),
}
}
// MakeTestStorageNetworkProposal generates a proposal that can be sent over the
// network to a provider
func MakeTestStorageNetworkProposal() smnet.Proposal {
return smnet.Proposal{
DealProposal: MakeTestClientDealProposal(),
Piece: &storagemarket.DataRef{Root: GenerateCids(1)[0]},
}
}
// MakeTestStorageNetworkResponse generates a response to a proposal sent over
// the network
func MakeTestStorageNetworkResponse() smnet.Response {
return smnet.Response{
State: storagemarket.StorageDealSealing,
Proposal: GenerateCids(1)[0],
PublishMessage: &(GenerateCids(1)[0]),
}
}
// MakeTestStorageNetworkSignedResponse generates a response to a proposal sent over
// the network that is signed
func MakeTestStorageNetworkSignedResponse() smnet.SignedResponse {
return smnet.SignedResponse{
Response: MakeTestStorageNetworkResponse(),
Signature: MakeTestSignature(),
}
}
// MakeTestStorageAskRequest generates a request to get a provider's ask
func MakeTestStorageAskRequest() smnet.AskRequest {
return smnet.AskRequest{
Miner: address.TestAddress2,
}
}
// MakeTestStorageAskResponse generates a response to an ask request
func MakeTestStorageAskResponse() smnet.AskResponse {
return smnet.AskResponse{
Ask: MakeTestSignedStorageAsk(),
}
}
// MakeTestDealStatusRequest generates a request to get a provider's query
func MakeTestDealStatusRequest() smnet.DealStatusRequest {
return smnet.DealStatusRequest{
Proposal: GenerateCids(1)[0],
Signature: *MakeTestSignature(),
}
}
// MakeTestDealStatusResponse generates a response to an query request
func MakeTestDealStatusResponse() smnet.DealStatusResponse {
proposal := MakeTestUnsignedDealProposal()
ds := storagemarket.ProviderDealState{
Proposal: &proposal,
ProposalCid: &GenerateCids(1)[0],
State: storagemarket.StorageDealActive,
}
return smnet.DealStatusResponse{
DealState: ds,
Signature: *MakeTestSignature(),
}
}
func RequireGenerateRetrievalPeers(t *testing.T, numPeers int) []retrievalmarket.RetrievalPeer {
peers := make([]retrievalmarket.RetrievalPeer, numPeers)
for i := range peers {
pid, err := test.RandPeerID()
require.NoError(t, err)
addr, err := address.NewIDAddress(uint64(rand.Int63()))
require.NoError(t, err)
peers[i] = retrievalmarket.RetrievalPeer{
Address: addr,
ID: pid,
}
}
return peers
}
type FakeDTValidator struct{}
func (v *FakeDTValidator) ValidatePush(isRestart bool, _ datatransfer.ChannelID, sender peer.ID, voucher datatransfer.Voucher, baseCid cid.Cid, selector ipld.Node) (datatransfer.VoucherResult, error) {
return nil, nil
}
func (v *FakeDTValidator) ValidatePull(isRestart bool, _ datatransfer.ChannelID, receiver peer.ID, voucher datatransfer.Voucher, baseCid cid.Cid, selector ipld.Node) (datatransfer.VoucherResult, error) {
return nil, nil
}
var _ datatransfer.RequestValidator = (*FakeDTValidator)(nil)