/
deal_cmd.go
394 lines (342 loc) · 11.6 KB
/
deal_cmd.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
bcli "github.com/filecoin-project/boost/cli"
clinode "github.com/filecoin-project/boost/cli/node"
"github.com/filecoin-project/boost/cmd"
"github.com/filecoin-project/boost/storagemarket/types"
types2 "github.com/filecoin-project/boost/transport/types"
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin/v9/market"
"github.com/filecoin-project/lotus/api"
chain_types "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/urfave/cli/v2"
)
const DealProtocolv120 = "/fil/storage/mk/1.2.0"
var dealFlags = []cli.Flag{
&cli.StringFlag{
Name: "provider",
Usage: "storage provider on-chain address",
Required: true,
},
&cli.StringFlag{
Name: "commp",
Usage: "commp of the CAR file",
Required: true,
},
&cli.Uint64Flag{
Name: "piece-size",
Usage: "size of the CAR file as a padded piece",
Required: true,
},
&cli.StringFlag{
Name: "payload-cid",
Usage: "root CID of the CAR file",
Required: true,
},
&cli.IntFlag{
Name: "start-epoch-head-offset",
Usage: "start epoch by when the deal should be proved by provider on-chain after current chain head",
},
&cli.IntFlag{
Name: "start-epoch",
Usage: "start epoch by when the deal should be proved by provider on-chain",
},
&cli.IntFlag{
Name: "duration",
Usage: "duration of the deal in epochs",
Value: 518400, // default is 2880 * 180 == 180 days
},
&cli.IntFlag{
Name: "provider-collateral",
Usage: "deal collateral that storage miner must put in escrow; if empty, the min collateral for the given piece size will be used",
},
&cli.Int64Flag{
Name: "storage-price",
Usage: "storage price in attoFIL per epoch per GiB",
Value: 1,
},
&cli.BoolFlag{
Name: "verified",
Usage: "whether the deal funds should come from verified client data-cap",
Value: true,
},
&cli.BoolFlag{
Name: "remove-unsealed-copy",
Usage: "indicates that an unsealed copy of the sector in not required for fast retrieval",
Value: false,
},
&cli.StringFlag{
Name: "wallet",
Usage: "wallet address to be used to initiate the deal",
},
&cli.BoolFlag{
Name: "skip-ipni-announce",
Usage: "indicates that deal index should not be announced to the IPNI(Network Indexer)",
Value: false,
},
}
var dealCmd = &cli.Command{
Name: "deal",
Usage: "Make an online deal with Boost",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "http-url",
Usage: "http url to CAR file",
Required: true,
},
&cli.StringSliceFlag{
Name: "http-headers",
Usage: "http headers to be passed with the request (e.g key=value)",
},
&cli.Uint64Flag{
Name: "car-size",
Usage: "size of the CAR file: required for online deals",
Required: true,
},
}, dealFlags...),
Before: before,
Action: func(cctx *cli.Context) error {
return dealCmdAction(cctx, true)
},
}
var offlineDealCmd = &cli.Command{
Name: "offline-deal",
Usage: "Make an offline deal with Boost",
Flags: dealFlags,
Before: before,
Action: func(cctx *cli.Context) error {
return dealCmdAction(cctx, false)
},
}
func dealCmdAction(cctx *cli.Context, isOnline bool) error {
ctx := bcli.ReqContext(cctx)
n, err := clinode.Setup(cctx.String(cmd.FlagRepo.Name))
if err != nil {
return err
}
api, closer, err := lcli.GetGatewayAPI(cctx)
if err != nil {
return fmt.Errorf("cant setup gateway connection: %w", err)
}
defer closer()
walletAddr, err := n.GetProvidedOrDefaultWallet(ctx, cctx.String("wallet"))
if err != nil {
return err
}
log.Debugw("selected wallet", "wallet", walletAddr)
maddr, err := address.NewFromString(cctx.String("provider"))
if err != nil {
return err
}
addrInfo, err := cmd.GetAddrInfo(ctx, api, maddr)
if err != nil {
return err
}
log.Debugw("found storage provider", "id", addrInfo.ID, "multiaddrs", addrInfo.Addrs, "addr", maddr)
if err := n.Host.Connect(ctx, *addrInfo); err != nil {
return fmt.Errorf("failed to connect to peer %s: %w", addrInfo.ID, err)
}
x, err := n.Host.Peerstore().FirstSupportedProtocol(addrInfo.ID, DealProtocolv120)
if err != nil {
return fmt.Errorf("getting protocols for peer %s: %w", addrInfo.ID, err)
}
if len(x) == 0 {
return fmt.Errorf("boost client cannot make a deal with storage provider %s because it does not support protocol version 1.2.0", maddr)
}
dealUuid := uuid.New()
commp := cctx.String("commp")
pieceCid, err := cid.Parse(commp)
if err != nil {
return fmt.Errorf("parsing commp '%s': %w", commp, err)
}
pieceSize := cctx.Uint64("piece-size")
if pieceSize == 0 {
return fmt.Errorf("must provide piece-size parameter for CAR url")
}
payloadCidStr := cctx.String("payload-cid")
rootCid, err := cid.Parse(payloadCidStr)
if err != nil {
return fmt.Errorf("parsing payload cid %s: %w", payloadCidStr, err)
}
transfer := types.Transfer{}
if isOnline {
carFileSize := cctx.Uint64("car-size")
if carFileSize == 0 {
return fmt.Errorf("size of car file cannot be 0")
}
transfer.Size = carFileSize
// Store the path to the CAR file as a transfer parameter
transferParams := &types2.HttpRequest{URL: cctx.String("http-url")}
if cctx.IsSet("http-headers") {
transferParams.Headers = make(map[string]string)
for _, header := range cctx.StringSlice("http-headers") {
sp := strings.Split(header, "=")
if len(sp) != 2 {
return fmt.Errorf("malformed http header: %s", header)
}
transferParams.Headers[sp[0]] = sp[1]
}
}
paramsBytes, err := json.Marshal(transferParams)
if err != nil {
return fmt.Errorf("marshalling request parameters: %w", err)
}
transfer.Type = "http"
transfer.Params = paramsBytes
}
var providerCollateral abi.TokenAmount
if cctx.IsSet("provider-collateral") {
providerCollateral = abi.NewTokenAmount(cctx.Int64("provider-collateral"))
} else {
bounds, err := api.StateDealProviderCollateralBounds(ctx, abi.PaddedPieceSize(pieceSize), cctx.Bool("verified"), chain_types.EmptyTSK)
if err != nil {
return fmt.Errorf("node error getting collateral bounds: %w", err)
}
providerCollateral = big.Div(big.Mul(bounds.Min, big.NewInt(6)), big.NewInt(5)) // add 20%
}
tipset, err := api.ChainHead(ctx)
if err != nil {
return fmt.Errorf("cannot get chain head: %w", err)
}
head := tipset.Height()
log.Debugw("current block height", "number", head)
if cctx.IsSet("start-epoch") && cctx.IsSet("start-epoch-head-offset") {
return errors.New("only one flag from `start-epoch-head-offset' or `start-epoch` can be specified")
}
var startEpoch abi.ChainEpoch
if cctx.IsSet("start-epoch-head-offset") {
startEpoch = head + abi.ChainEpoch(cctx.Int("start-epoch-head-offset"))
} else if cctx.IsSet("start-epoch") {
startEpoch = abi.ChainEpoch(cctx.Int("start-epoch"))
} else {
// default
startEpoch = head + abi.ChainEpoch(5760) // head + 2 days
}
// Create a deal proposal to storage provider using deal protocol v1.2.0 format
dealProposal, err := dealProposal(ctx, n, walletAddr, rootCid, abi.PaddedPieceSize(pieceSize), pieceCid, maddr, startEpoch, cctx.Int("duration"), cctx.Bool("verified"), providerCollateral, abi.NewTokenAmount(cctx.Int64("storage-price")))
if err != nil {
return fmt.Errorf("failed to create a deal proposal: %w", err)
}
dealParams := types.DealParams{
DealUUID: dealUuid,
ClientDealProposal: *dealProposal,
DealDataRoot: rootCid,
IsOffline: !isOnline,
Transfer: transfer,
RemoveUnsealedCopy: cctx.Bool("remove-unsealed-copy"),
SkipIPNIAnnounce: cctx.Bool("skip-ipni-announce"),
}
log.Debugw("about to submit deal proposal", "uuid", dealUuid.String())
s, err := n.Host.NewStream(ctx, addrInfo.ID, DealProtocolv120)
if err != nil {
return fmt.Errorf("failed to open stream to peer %s: %w", addrInfo.ID, err)
}
defer s.Close()
var resp types.DealResponse
if err := doRpc(ctx, s, &dealParams, &resp); err != nil {
return fmt.Errorf("send proposal rpc: %w", err)
}
if !resp.Accepted {
return fmt.Errorf("deal proposal rejected: %s", resp.Message)
}
if cctx.Bool("json") {
out := map[string]interface{}{
"dealUuid": dealUuid.String(),
"provider": maddr.String(),
"clientWallet": walletAddr.String(),
"payloadCid": rootCid.String(),
"commp": dealProposal.Proposal.PieceCID.String(),
"startEpoch": dealProposal.Proposal.StartEpoch.String(),
"endEpoch": dealProposal.Proposal.EndEpoch.String(),
"providerCollateral": dealProposal.Proposal.ProviderCollateral.String(),
}
if isOnline {
out["url"] = cctx.String("http-url")
}
return cmd.PrintJson(out)
}
msg := "sent deal proposal"
if !isOnline {
msg += " for offline deal"
}
msg += "\n"
msg += fmt.Sprintf(" deal uuid: %s\n", dealUuid)
msg += fmt.Sprintf(" storage provider: %s\n", maddr)
msg += fmt.Sprintf(" client wallet: %s\n", walletAddr)
msg += fmt.Sprintf(" payload cid: %s\n", rootCid)
if isOnline {
msg += fmt.Sprintf(" url: %s\n", cctx.String("http-url"))
}
msg += fmt.Sprintf(" commp: %s\n", dealProposal.Proposal.PieceCID)
msg += fmt.Sprintf(" start epoch: %d\n", dealProposal.Proposal.StartEpoch)
msg += fmt.Sprintf(" end epoch: %d\n", dealProposal.Proposal.EndEpoch)
msg += fmt.Sprintf(" provider collateral: %s\n", chain_types.FIL(dealProposal.Proposal.ProviderCollateral).Short())
fmt.Println(msg)
return nil
}
func dealProposal(ctx context.Context, n *clinode.Node, clientAddr address.Address, rootCid cid.Cid, pieceSize abi.PaddedPieceSize, pieceCid cid.Cid, minerAddr address.Address, startEpoch abi.ChainEpoch, duration int, verified bool, providerCollateral abi.TokenAmount, storagePrice abi.TokenAmount) (*market.ClientDealProposal, error) {
endEpoch := startEpoch + abi.ChainEpoch(duration)
// deal proposal expects total storage price for deal per epoch, therefore we
// multiply pieceSize * storagePrice (which is set per epoch per GiB) and divide by 2^30
storagePricePerEpochForDeal := big.Div(big.Mul(big.NewInt(int64(pieceSize)), storagePrice), big.NewInt(int64(1<<30)))
l, err := market.NewLabelFromString(rootCid.String())
if err != nil {
return nil, err
}
proposal := market.DealProposal{
PieceCID: pieceCid,
PieceSize: pieceSize,
VerifiedDeal: verified,
Client: clientAddr,
Provider: minerAddr,
Label: l,
StartEpoch: startEpoch,
EndEpoch: endEpoch,
StoragePricePerEpoch: storagePricePerEpochForDeal,
ProviderCollateral: providerCollateral,
}
buf, err := cborutil.Dump(&proposal)
if err != nil {
return nil, err
}
sig, err := n.Wallet.WalletSign(ctx, clientAddr, buf, api.MsgMeta{Type: api.MTDealProposal})
if err != nil {
return nil, fmt.Errorf("wallet sign failed: %w", err)
}
return &market.ClientDealProposal{
Proposal: proposal,
ClientSignature: *sig,
}, nil
}
func doRpc(ctx context.Context, s inet.Stream, req interface{}, resp interface{}) error {
errc := make(chan error)
go func() {
if err := cborutil.WriteCborRPC(s, req); err != nil {
errc <- fmt.Errorf("failed to send request: %w", err)
return
}
if err := cborutil.ReadCborRPC(s, resp); err != nil {
errc <- fmt.Errorf("failed to read response: %w", err)
return
}
errc <- nil
}()
select {
case err := <-errc:
return err
case <-ctx.Done():
return ctx.Err()
}
}