-
Notifications
You must be signed in to change notification settings - Fork 0
/
filclient.go
150 lines (124 loc) · 3.41 KB
/
filclient.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
package filclient
import (
"context"
"errors"
"sync"
"github.com/filecoin-project/go-address"
datatransfer "github.com/filecoin-project/go-data-transfer"
dtimpl "github.com/filecoin-project/go-data-transfer/impl"
"github.com/filecoin-project/go-data-transfer/network"
"github.com/filecoin-project/go-data-transfer/transport/graphsync"
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation"
"github.com/filecoin-project/lotus/api"
"github.com/ipfs/go-datastore"
gsimpl "github.com/ipfs/go-graphsync/impl"
gsnet "github.com/ipfs/go-graphsync/network"
"github.com/ipfs/go-graphsync/storeutil"
blockstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p/core/host"
)
// filclient.go - code related to initialization and management of the core
// FilClient struct
var log = logging.Logger("filclient")
var (
ErrLotusError = errors.New("lotus error")
)
type Config struct {
}
type Client struct {
host host.Host
api api.Gateway
dt datatransfer.Manager
dtUnsubscribe datatransfer.Unsubscribe
bs blockstore.Blockstore
ds datastore.Datastore
// TODO(@elijaharita): this shouldn't be in the main Client struct
retrievalTransfers map[datatransfer.ChannelID]*RetrievalTransfer
retrievalTransfersLk sync.Mutex
}
func New(
ctx context.Context,
h host.Host,
api api.Gateway,
addr address.Address,
bs blockstore.Blockstore,
ds datastore.Batching,
opts ...Option,
) (*Client, error) {
cfg := Config{}
for _, opt := range opts {
opt(&cfg)
}
// ctx, cancel := context.WithCancel(ctx)
// rpc := rpcstmgr.NewRPCStateManager(api)
// paychDS := paychmgr.NewStore(namespace.Wrap(ds, datastore.NewKey("paych")))
dt, err := initDataTransfer(ctx, h, bs, ds)
if err != nil {
return nil, err
}
client := &Client{
host: h,
api: api,
dt: dt,
// dtUnsubscribe: assigned below
bs: bs,
ds: ds,
retrievalTransfers: make(map[datatransfer.ChannelID]*RetrievalTransfer),
}
client.dtUnsubscribe = dt.SubscribeToEvents(func(
event datatransfer.Event,
channelState datatransfer.ChannelState,
) {
client.handleDataTransferRetrievalEvent(ctx, event, channelState)
})
return client, nil
}
func (client *Client) Close() {
if client.dtUnsubscribe != nil {
client.dtUnsubscribe()
}
}
func initDataTransfer(
ctx context.Context,
h host.Host,
bs blockstore.Blockstore,
ds datastore.Batching,
) (datatransfer.Manager, error) {
dtNetwork := network.NewFromLibp2pHost(h)
gsNetwork := gsnet.NewFromLibp2pHost(h)
gsExchange := gsimpl.New(ctx, gsNetwork, storeutil.LinkSystemForBlockstore(bs))
gsTransport := graphsync.NewTransport(h.ID(), gsExchange)
dt, err := dtimpl.NewDataTransfer(ds, dtNetwork, gsTransport)
if err != nil {
return nil, err
}
if err := dt.RegisterVoucherType(
&requestvalidation.StorageDataTransferVoucher{},
nil,
); err != nil {
return nil, err
}
if err := dt.RegisterVoucherType(
&retrievalmarket.DealProposal{},
nil,
); err != nil {
return nil, err
}
if err := dt.RegisterVoucherType(
&retrievalmarket.DealPayment{},
nil,
); err != nil {
return nil, err
}
if err := dt.RegisterVoucherResultType(
&retrievalmarket.DealResponse{},
); err != nil {
return nil, err
}
if err := dt.Start(ctx); err != nil {
return nil, err
}
return dt, nil
}