-
Notifications
You must be signed in to change notification settings - Fork 71
/
net.go
638 lines (533 loc) · 21.4 KB
/
net.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package lp2pimpl
import (
"context"
"errors"
"fmt"
"time"
"github.com/filecoin-project/boost/api"
"github.com/filecoin-project/boost/db"
"github.com/filecoin-project/boost/markets/shared"
"github.com/filecoin-project/boost/safe"
"github.com/filecoin-project/boost/storagemarket"
"github.com/filecoin-project/boost/storagemarket/sealingpipeline"
"github.com/filecoin-project/boost/storagemarket/types"
"github.com/filecoin-project/boost/storagemarket/types/legacytypes"
mig "github.com/filecoin-project/boost/storagemarket/types/legacytypes/migrations"
gfm_network "github.com/filecoin-project/boost/storagemarket/types/legacytypes/network"
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/api/v1api"
chaintypes "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/google/uuid"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
typegen "github.com/whyrusleeping/cbor-gen"
"go.uber.org/zap"
)
var log = logging.Logger("boost-net")
var propLog = logging.Logger("boost-prop")
const DealProtocolv120ID = "/fil/storage/mk/1.2.0"
const DealProtocolv121ID = "/fil/storage/mk/1.2.1"
const DealStatusV12ProtocolID = "/fil/storage/status/1.2.0"
// The time limit to read a message from the client when the client opens a stream
const providerReadDeadline = 10 * time.Second
// The time limit to write a response to the client
const providerWriteDeadline = 10 * time.Second
// The time limit to wait for the provider to send a response to a client's request.
// This includes the time it takes for the provider to process the request and
// send a response.
const clientReadDeadline = 60 * time.Second
// The time limit to write a message to the provider
const clientWriteDeadline = 10 * time.Second
// DealClientOption is an option for configuring the libp2p storage deal client
type DealClientOption func(*DealClient)
// RetryParameters changes the default parameters around connection reopening
func RetryParameters(minDuration time.Duration, maxDuration time.Duration, attempts float64, backoffFactor float64) DealClientOption {
return func(c *DealClient) {
c.retryStream.SetOptions(shared.RetryParameters(minDuration, maxDuration, attempts, backoffFactor))
}
}
// DealClient sends deal proposals over libp2p
type DealClient struct {
addr address.Address
retryStream *shared.RetryStream
walletApi api.Wallet
}
// SendDealProposal sends a deal proposal over a libp2p stream to the peer
func (c *DealClient) SendDealProposal(ctx context.Context, id peer.ID, params types.DealParams) (*types.DealResponse, error) {
log.Debugw("send deal proposal", "id", params.DealUUID, "provider-peer", id)
// Create a libp2p stream to the provider
s, err := c.retryStream.OpenStream(ctx, id, []protocol.ID{DealProtocolv121ID, DealProtocolv120ID})
if err != nil {
return nil, err
}
defer s.Close() // nolint
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(clientWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
// Write the deal proposal to the stream
if err = cborutil.WriteCborRPC(s, ¶ms); err != nil {
return nil, fmt.Errorf("sending deal proposal: %w", err)
}
// Set a deadline on reading from the stream so it doesn't hang
_ = s.SetReadDeadline(time.Now().Add(clientReadDeadline))
defer s.SetReadDeadline(time.Time{}) // nolint
// Read the response from the stream
var resp types.DealResponse
if err := resp.UnmarshalCBOR(s); err != nil {
return nil, fmt.Errorf("reading proposal response: %w", err)
}
log.Debugw("received deal proposal response", "id", params.DealUUID, "accepted", resp.Accepted, "reason", resp.Message)
return &resp, nil
}
func (c *DealClient) SendDealStatusRequest(ctx context.Context, id peer.ID, dealUUID uuid.UUID) (*types.DealStatusResponse, error) {
log.Debugw("send deal status req", "deal-uuid", dealUUID, "id", id)
uuidBytes, err := dealUUID.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("getting uuid bytes: %w", err)
}
sig, err := c.walletApi.WalletSign(ctx, c.addr, uuidBytes)
if err != nil {
return nil, fmt.Errorf("signing uuid bytes: %w", err)
}
// Create a libp2p stream to the provider
s, err := c.retryStream.OpenStream(ctx, id, []protocol.ID{DealStatusV12ProtocolID})
if err != nil {
return nil, err
}
defer s.Close() // nolint
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(clientWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
// Write the deal status request to the stream
req := types.DealStatusRequest{DealUUID: dealUUID, Signature: *sig}
if err = cborutil.WriteCborRPC(s, &req); err != nil {
return nil, fmt.Errorf("sending deal status req: %w", err)
}
// Set a deadline on reading from the stream so it doesn't hang
_ = s.SetReadDeadline(time.Now().Add(clientReadDeadline))
defer s.SetReadDeadline(time.Time{}) // nolint
// Read the response from the stream
var resp types.DealStatusResponse
if err := resp.UnmarshalCBOR(s); err != nil {
return nil, fmt.Errorf("reading deal status response: %w", err)
}
log.Debugw("received deal status response", "id", resp.DealUUID, "status", resp.DealStatus)
return &resp, nil
}
func NewDealClient(h host.Host, addr address.Address, walletApi api.Wallet, options ...DealClientOption) *DealClient {
c := &DealClient{
addr: addr,
retryStream: shared.NewRetryStream(h),
walletApi: walletApi,
}
for _, option := range options {
option(c)
}
return c
}
// DealProvider listens for incoming deal proposals over libp2p
type DealProvider struct {
ctx context.Context
host host.Host
prov *storagemarket.Provider
fullNode v1api.FullNode
plDB *db.ProposalLogsDB
spApi sealingpipeline.API
}
func NewDealProvider(h host.Host, prov *storagemarket.Provider, fullNodeApi v1api.FullNode, plDB *db.ProposalLogsDB, spApi sealingpipeline.API) *DealProvider {
p := &DealProvider{
host: h,
prov: prov,
fullNode: fullNodeApi,
plDB: plDB,
spApi: spApi,
}
return p
}
func (p *DealProvider) Start(ctx context.Context) {
p.ctx = ctx
// Note that the handling for deal protocol v1.2.0 and v1.2.1 is the same.
// Deal protocol v1.2.1 has a couple of new fields: SkipIPNIAnnounce and
// RemoveUnsealedCopy.
// If a client that supports deal protocol v1.2.0 sends a request to a
// boostd server that supports deal protocol v1.2.1, the DealParams struct
// will be missing these new fields.
// When the DealParams struct is unmarshalled the missing fields will be
// set to false, which maintains the previous behaviour:
// - SkipIPNIAnnounce=false: announce deal to IPNI
// - RemoveUnsealedCopy=false: keep unsealed copy of deal data
p.host.SetStreamHandler(DealProtocolv121ID, safe.Handle(p.handleNewDealStream))
p.host.SetStreamHandler(DealProtocolv120ID, safe.Handle(p.handleNewDealStream))
p.host.SetStreamHandler(DealStatusV12ProtocolID, safe.Handle(p.handleNewDealStatusStream))
// Handle legacy deal stream here and reject all legacy deals
p.host.SetStreamHandler(legacytypes.DealProtocolID101, safe.Handle(p.handleLegacyDealStream))
p.host.SetStreamHandler(legacytypes.DealProtocolID110, safe.Handle(p.handleLegacyDealStream))
p.host.SetStreamHandler(legacytypes.DealProtocolID111, safe.Handle(p.handleLegacyDealStream))
// Handle Query Ask
p.host.SetStreamHandler(legacytypes.AskProtocolID, safe.Handle(p.handleNewAskStream))
p.host.SetStreamHandler(legacytypes.OldAskProtocolID, safe.Handle(p.handleOldAskStream))
}
func (p *DealProvider) Stop() {
p.host.RemoveStreamHandler(DealProtocolv121ID)
p.host.RemoveStreamHandler(DealProtocolv120ID)
p.host.RemoveStreamHandler(DealStatusV12ProtocolID)
p.host.RemoveStreamHandler(legacytypes.DealProtocolID101)
p.host.RemoveStreamHandler(legacytypes.DealProtocolID110)
p.host.RemoveStreamHandler(legacytypes.DealProtocolID111)
p.host.RemoveStreamHandler(legacytypes.AskProtocolID)
p.host.RemoveStreamHandler(legacytypes.OldAskProtocolID)
}
// Called when the client opens a libp2p stream with a new deal proposal
func (p *DealProvider) handleNewDealStream(s network.Stream) {
start := time.Now()
reqLogUuid := uuid.New()
reqLog := log.With("reqlog-uuid", reqLogUuid.String(), "client-peer", s.Conn().RemotePeer())
reqLog.Debugw("new deal proposal request")
defer func() {
err := s.Close()
if err != nil {
reqLog.Infow("closing stream", "err", err)
}
reqLog.Debugw("handled deal proposal request", "duration", time.Since(start).String())
}()
// Set a deadline on reading from the stream so it doesn't hang
_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))
// Read the deal proposal from the stream
var proposal types.DealParams
err := proposal.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Warnw("reading storage deal proposal from stream", "err", err)
return
}
reqLog = reqLog.With("id", proposal.DealUUID)
reqLog.Infow("received deal proposal")
// Start executing the deal.
// Note: This method just waits for the deal to be accepted, it doesn't
// wait for deal execution to complete.
startExec := time.Now()
res, err := p.prov.ExecuteDeal(context.Background(), &proposal, s.Conn().RemotePeer())
reqLog.Debugw("processed deal proposal accept")
if err != nil {
reqLog.Warnw("deal proposal failed", "err", err, "reason", res.Reason)
}
// Log the response
propLog.Infow("send deal proposal response",
"id", proposal.DealUUID,
"accepted", res.Accepted,
"msg", res.Reason,
"peer id", s.Conn().RemotePeer(),
"client address", proposal.ClientDealProposal.Proposal.Client,
"provider address", proposal.ClientDealProposal.Proposal.Provider,
"piece cid", proposal.ClientDealProposal.Proposal.PieceCID.String(),
"piece size", proposal.ClientDealProposal.Proposal.PieceSize,
"verified", proposal.ClientDealProposal.Proposal.VerifiedDeal,
"label", proposal.ClientDealProposal.Proposal.Label,
"start epoch", proposal.ClientDealProposal.Proposal.StartEpoch,
"end epoch", proposal.ClientDealProposal.Proposal.EndEpoch,
"price per epoch", proposal.ClientDealProposal.Proposal.StoragePricePerEpoch,
"duration", time.Since(startExec).String(),
)
_ = p.plDB.InsertLog(p.ctx, proposal, res.Accepted, res.Reason) //nolint:errcheck
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
// Write the response to the client
err = cborutil.WriteCborRPC(s, &types.DealResponse{Accepted: res.Accepted, Message: res.Reason})
if err != nil {
reqLog.Warnw("writing deal response", "err", err)
}
}
func (p *DealProvider) handleNewDealStatusStream(s network.Stream) {
start := time.Now()
reqLogUuid := uuid.New()
reqLog := log.With("reqlog-uuid", reqLogUuid.String(), "client-peer", s.Conn().RemotePeer())
reqLog.Debugw("new deal status request")
defer func() {
err := s.Close()
if err != nil {
reqLog.Infow("closing stream", "err", err)
}
reqLog.Debugw("handled deal status request", "duration", time.Since(start).String())
}()
// Read the deal status request from the stream
_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))
var req types.DealStatusRequest
err := req.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Warnw("reading deal status request from stream", "err", err)
return
}
reqLog = reqLog.With("id", req.DealUUID)
reqLog.Debugw("received deal status request")
resp := p.getDealStatus(req, reqLog)
reqLog.Debugw("processed deal status request")
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
if err := cborutil.WriteCborRPC(s, &resp); err != nil {
reqLog.Errorw("failed to write deal status response", "err", err)
}
}
func (p *DealProvider) getDealStatus(req types.DealStatusRequest, reqLog *zap.SugaredLogger) types.DealStatusResponse {
errResp := func(err string) types.DealStatusResponse {
return types.DealStatusResponse{DealUUID: req.DealUUID, Error: err}
}
pds, err := p.prov.Deal(p.ctx, req.DealUUID)
if err != nil && errors.Is(err, storagemarket.ErrDealNotFound) {
return errResp(fmt.Sprintf("no storage deal found with deal UUID %s", req.DealUUID))
}
if err != nil {
reqLog.Errorw("failed to fetch deal status", "err", err)
return errResp("failed to fetch deal status")
}
// verify request signature
uuidBytes, err := req.DealUUID.MarshalBinary()
if err != nil {
reqLog.Errorw("failed to serialize request deal UUID", "err", err)
return errResp("failed to serialize request deal UUID")
}
clientAddr := pds.ClientDealProposal.Proposal.Client
addr, err := p.fullNode.StateAccountKey(p.ctx, clientAddr, chaintypes.EmptyTSK)
if err != nil {
reqLog.Errorw("failed to get account key for client addr", "client", clientAddr.String(), "err", err)
msg := fmt.Sprintf("failed to get account key for client addr %s", clientAddr.String())
return errResp(msg)
}
err = sigs.Verify(&req.Signature, addr, uuidBytes)
if err != nil {
reqLog.Warnw("signature verification failed", "err", err)
return errResp("signature verification failed")
}
signedPropCid, err := pds.SignedProposalCid()
if err != nil {
reqLog.Errorw("getting signed proposal cid", "err", err)
return errResp("getting signed proposal cid")
}
bts := p.prov.NBytesReceived(req.DealUUID)
si, err := p.spApi.SectorsStatus(p.ctx, pds.SectorID, false)
if err != nil {
reqLog.Errorw("getting sector status from sealer", "err", err)
return errResp("getting sector status from sealer")
}
sealingStatus := string(si.State)
if storagemarket.IsFinalSealingState(si.State) {
if !storagemarket.HasDeal(si.Deals, pds.ChainDealID) {
sealingStatus = storagemarket.ErrDealNotFound.Error()
}
}
return types.DealStatusResponse{
DealUUID: req.DealUUID,
DealStatus: &types.DealStatus{
Error: pds.Err,
Status: pds.Checkpoint.String(),
SealingStatus: sealingStatus,
Proposal: pds.ClientDealProposal.Proposal,
SignedProposalCid: signedPropCid,
PublishCid: pds.PublishCID,
ChainDealID: pds.ChainDealID,
},
IsOffline: pds.IsOffline,
TransferSize: pds.Transfer.Size,
NBytesReceived: bts,
}
}
func (p *DealProvider) handleLegacyDealStream(s network.Stream) {
start := time.Now()
reqLogUuid := uuid.New()
reqLog := log.With("reqlog-uuid", reqLogUuid.String(), "client-peer", s.Conn().RemotePeer())
reqLog.Debugw("new legacy deal status request")
defer func() {
err := s.Close()
if err != nil {
reqLog.Infow("closing stream", "err", err)
}
reqLog.Debugw("handled legacy deal status request", "duration", time.Since(start).String())
}()
rejMsg := fmt.Sprintf("deal proposals made over the legacy %s protocol are deprecated"+
" - please use the %s deal proposal protocol", s.Protocol(), DealProtocolv121ID)
const rejState = 2
var signedResponse typegen.CBORMarshaler
_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))
switch s.Protocol() {
case legacytypes.DealProtocolID101:
var prop mig.Proposal0
err := prop.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Errorf("failed to unmarshal the proposal message: %s", err)
return
}
pcid, err := prop.DealProposal.Proposal.Cid()
if err != nil {
reqLog.Errorf("failed to get the proposal cid: %s", err)
return
}
resp := mig.Response0{State: rejState, Message: rejMsg, Proposal: pcid}
sig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("getting signed response: %s", err)
return
}
signedResponse = &mig.SignedResponse0{Response: resp, Signature: sig}
case legacytypes.DealProtocolID110:
var prop mig.Proposal1
err := prop.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Errorf("failed to unmarshal the proposal message: %s", err)
return
}
pcid, err := prop.DealProposal.Proposal.Cid()
if err != nil {
reqLog.Errorf("failed to get the proposal cid: %s", err)
return
}
resp := gfm_network.Response{State: rejState, Message: rejMsg, Proposal: pcid}
sig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("getting signed response: %s", err)
return
}
signedResponse = &gfm_network.SignedResponse{Response: resp, Signature: sig}
case legacytypes.DealProtocolID111:
var prop gfm_network.Proposal
err := prop.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Errorf("failed to unmarshal the proposal message: %s", err)
return
}
pcid, err := prop.DealProposal.Proposal.Cid()
if err != nil {
reqLog.Errorf("failed to get the proposal cid: %s", err)
return
}
resp := gfm_network.Response{State: rejState, Message: rejMsg, Proposal: pcid}
sig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("getting signed response: %s", err)
return
}
signedResponse = &gfm_network.SignedResponse{Response: resp, Signature: sig}
}
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
err := signedResponse.MarshalCBOR(s)
if err != nil {
reqLog.Errorf("error writing response to the stream: %s", err)
}
}
func (p *DealProvider) signLegacyResponse(resp typegen.CBORMarshaler) (*crypto.Signature, error) {
ts, err := p.fullNode.ChainHead(p.ctx)
if err != nil {
return nil, fmt.Errorf("getting chain head: %w", err)
}
maddr, err := p.spApi.ActorAddress(p.ctx)
if err != nil {
return nil, fmt.Errorf("getting miner actor address: %w", err)
}
mi, err := p.fullNode.StateMinerInfo(p.ctx, maddr, ts.Key())
if err != nil {
return nil, fmt.Errorf("getting miner info: %w", err)
}
msg, err := cborutil.Dump(resp)
if err != nil {
return nil, fmt.Errorf("could not convert response to bytes: %w", err)
}
localSignature, err := p.fullNode.WalletSign(p.ctx, mi.Worker, msg)
if err != nil {
return nil, fmt.Errorf("failed to sign the message: %w", err)
}
return localSignature, err
}
func (p *DealProvider) handleNewAskStream(s network.Stream) {
start := time.Now()
reqLog := log.With("client-peer", s.Conn().RemotePeer())
reqLog.Debugw("new queryAsk request")
defer func() {
err := s.Close()
if err != nil {
reqLog.Infow("closing stream", "err", err)
}
reqLog.Debugw("handled queryAsk request", "duration", time.Since(start).String())
}()
// Read the deal status request from the stream
_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))
var req gfm_network.AskRequest
err := req.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Warnw("reading queryAsk request from stream", "err", err)
return
}
var resp gfm_network.AskResponse
if req.Miner.String() == p.prov.Address.String() {
resp.Ask = p.prov.GetAsk()
} else {
reqLog.Warnw("storage provider for address %s receive ask for miner with address %s", p.prov.Address, req.Miner)
}
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
if err := cborutil.WriteCborRPC(s, &resp); err != nil {
reqLog.Errorw("failed to write queryAsk response", "err", err)
}
}
func (p *DealProvider) handleOldAskStream(s network.Stream) {
start := time.Now()
reqLog := log.With("client-peer", s.Conn().RemotePeer())
reqLog.Debugw("new queryAsk request")
defer func() {
err := s.Close()
if err != nil {
reqLog.Infow("closing stream", "err", err)
}
reqLog.Debugw("handled queryAsk request", "duration", time.Since(start).String())
}()
// Read the deal status request from the stream
_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))
var req mig.AskRequest0
err := req.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Warnw("reading queryAsk request from stream", "err", err)
return
}
var resp mig.AskResponse0
if req.Miner.String() == p.prov.Address.String() {
ask := p.prov.GetAsk()
newAsk := ask.Ask
resp.Ask.Ask = &mig.StorageAsk0{
Price: newAsk.Price,
VerifiedPrice: newAsk.VerifiedPrice,
MinPieceSize: newAsk.MinPieceSize,
MaxPieceSize: newAsk.MaxPieceSize,
Miner: newAsk.Miner,
Timestamp: newAsk.Timestamp,
Expiry: newAsk.Expiry,
SeqNo: newAsk.SeqNo,
}
oldSig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("getting signed response: %s", err)
}
resp.Ask.Signature = oldSig
} else {
reqLog.Warnw("storage provider for address %s receive ask for miner with address %s", p.prov.Address, req.Miner)
}
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
if err := cborutil.WriteCborRPC(s, &resp); err != nil {
reqLog.Errorw("failed to write queryAsk response", "err", err)
}
}