-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_market.go
99 lines (87 loc) · 2.74 KB
/
storage_market.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
package boostly
import (
"context"
"fmt"
"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/builtin/v9/market"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
)
//go:generate go run github.com/hannahhoward/cbor-gen-for@latest --map-encoding StorageAsk DealProposal Transfer DealProposalResponse
const (
FilStorageMarketProtocol_1_2_0 = "/fil/storage/mk/1.2.0"
)
// StorageAsk defines the parameters by which a miner will choose to accept or
// reject a deal. Note: making a storage deal proposal which matches the miner's
// ask is a precondition, but not sufficient to ensure the deal is accepted (the
// storage provider may run its own decision logic).
type StorageAsk struct {
// Price per GiB / Epoch
Price abi.TokenAmount
VerifiedPrice abi.TokenAmount
MinPieceSize abi.PaddedPieceSize
MaxPieceSize abi.PaddedPieceSize
Miner address.Address
}
type DealProposal struct {
DealUUID uuid.UUID
IsOffline bool
ClientDealProposal market.ClientDealProposal
DealDataRoot cid.Cid
Transfer Transfer // Transfer params will be the zero value if this is an offline deal
RemoveUnsealedCopy bool
SkipIPNIAnnounce bool
}
// Transfer has the parameters for a data transfer
type Transfer struct {
// The type of transfer eg "http"
Type string
// An optional ID that can be supplied by the client to identify the deal
ClientID string
// A byte array containing marshalled data specific to the transfer type
// eg a JSON encoded struct { URL: "<url>", Headers: {...} }
Params []byte
// The size of the data transferred in bytes
Size uint64
}
type DealProposalResponse struct {
Accepted bool
// Message is the reason the deal proposal was rejected. It is empty if
// the deal was accepted.
Message string
}
func ProposeDeal(ctx context.Context, h host.Host, spID peer.ID, proposal DealProposal) (*DealProposalResponse, error) {
stream, err := h.NewStream(ctx, spID, FilStorageMarketProtocol_1_2_0)
if err != nil {
return nil, err
}
defer func() { _ = stream.Close() }()
var resp DealProposalResponse
errc := make(chan error)
go func() {
defer close(errc)
if err := cborutil.WriteCborRPC(stream, proposal); err != nil {
errc <- fmt.Errorf("failed to send request: %w", err)
return
}
if err := cborutil.ReadCborRPC(stream, &resp); err != nil {
errc <- fmt.Errorf("failed to read response: %w", err)
return
}
errc <- nil
}()
select {
case err := <-errc:
if err != nil {
return nil, err
}
break
case <-ctx.Done():
return nil, ctx.Err()
}
return &resp, nil
}