/
utils.go
203 lines (172 loc) · 5.62 KB
/
utils.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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
package utils
import (
"context"
"io/ioutil"
"net/http"
"os"
"os/signal"
"syscall"
datatransfer "github.com/filecoin-project/go-data-transfer"
dtimpl "github.com/filecoin-project/go-data-transfer/impl"
dtnet "github.com/filecoin-project/go-data-transfer/network"
dtgstransport "github.com/filecoin-project/go-data-transfer/transport/graphsync"
"github.com/filecoin-project/go-data-transfer/transport/graphsync/extension"
"github.com/filecoin-project/go-fil-markets/retrievalmarket/discovery"
"github.com/filecoin-project/go-fil-markets/storagemarket/impl/funds"
"github.com/filecoin-project/go-multistore"
"github.com/filecoin-project/go-storedcounter"
"github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
badger "github.com/ipfs/go-ds-badger2"
"github.com/ipfs/go-graphsync"
graphsyncimpl "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/v2"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/urfave/cli/v2"
)
var log = logging.Logger("utils")
type MarketParams struct {
Host host.Host
Cbs dtypes.ClientBlockstore
Ds dtypes.MetadataDS
Mds dtypes.ClientMultiDstore
DataTransfer datatransfer.Manager
Discovery *discovery.Local
Deals dtypes.ClientDatastore
DealFunds funds.DealFunds
}
func AuthHeader(token string) http.Header {
if len(token) != 0 {
headers := http.Header{}
headers.Add("Authorization", "Bearer "+string(token))
return headers
}
log.Warn("API Token not set and requested, capabilities might be limited.")
return nil
}
func DaemonContext(cctx *cli.Context) context.Context {
return context.Background()
}
func ReqContext(cctx *cli.Context) context.Context {
tCtx := DaemonContext(cctx)
ctx, done := context.WithCancel(tCtx)
sigChan := make(chan os.Signal, 2)
go func() {
<-sigChan
done()
}()
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
return ctx
}
func NewChainBlockStore(ds dtypes.MetadataDS) (dtypes.ChainBlockstore, error) {
bs := blockstore.NewBlockstore(ds)
cbs, err := blockstore.CachedBlockstore(context.Background(), bs, blockstore.DefaultCacheOpts())
if err != nil {
return nil, err
}
return cbs, nil
}
func NewLibP2PHost() (host.Host, error) {
ctx := context.Background()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return nil, err
}
opts := []libp2p.Option{
libp2p.Identity(priv),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.DefaultSecurity,
libp2p.NATPortMap(),
}
return libp2p.New(ctx, opts...)
}
func InitMarketParams() (*MarketParams, error) {
host, err := NewLibP2PHost()
if err != nil {
return nil, err
}
ctx := context.Background()
tdir, err := ioutil.TempDir("", "market-client")
if err != nil {
return nil, err
}
ds, err := badger.NewDatastore(tdir, nil)
if err != nil {
return nil, err
}
mds, err := multistore.NewMultiDstore(ds)
if err != nil {
return nil, err
}
imgr := modules.ClientImportMgr(mds, namespace.Wrap(ds, datastore.NewKey("/client")))
clientBs := modules.ClientBlockstore(imgr)
loader := storeutil.LoaderForBlockstore(clientBs)
storer := storeutil.StorerForBlockstore(clientBs)
graphSyncNetwork := gsnet.NewFromLibp2pHost(host)
chainBs, err := NewChainBlockStore(ds)
if err != nil {
return nil, err
}
graphSync := graphsyncimpl.New(ctx, graphSyncNetwork, loader, storer, graphsyncimpl.RejectAllRequestsByDefault())
chainLoader := storeutil.LoaderForBlockstore(chainBs)
chainStorer := storeutil.StorerForBlockstore(chainBs)
err = graphSync.RegisterPersistenceOption("chainstore", chainLoader, chainStorer)
if err != nil {
return nil, err
}
graphSync.RegisterIncomingRequestHook(func(p peer.ID, requestData graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
_, has := requestData.Extension("chainsync")
if has {
// TODO: we should confirm the selector is a reasonable one before we validate
// TODO: this code will get more complicated and should probably not live here eventually
hookActions.ValidateRequest()
hookActions.UsePersistenceOption("chainstore")
}
_, has = requestData.Extension(extension.ExtensionDataTransfer)
if has {
hookActions.ValidateRequest()
}
})
graphSync.RegisterOutgoingRequestHook(func(p peer.ID, requestData graphsync.RequestData, hookActions graphsync.OutgoingRequestHookActions) {
_, has := requestData.Extension("chainsync")
if has {
hookActions.UsePersistenceOption("chainstore")
}
})
sc := storedcounter.New(ds, datastore.NewKey("/datatransfer/client/counter"))
net := dtnet.NewFromLibp2pHost(host)
dtDs := namespace.Wrap(ds, datastore.NewKey("/datatransfer/client/transfers"))
transport := dtgstransport.NewTransport(host.ID(), graphSync)
dt, err := dtimpl.NewDataTransfer(dtDs, net, transport, sc)
if err != nil {
return nil, err
}
if err := dt.Start(ctx); err != nil {
return nil, err
}
clientDealFunds, err := modules.NewClientDealFunds(ds)
if err != nil {
return nil, err
}
return &MarketParams{
Host: host,
Cbs: clientBs,
Ds: ds,
Mds: mds,
DataTransfer: dt,
Discovery: modules.NewLocalDiscovery(ds),
Deals: modules.NewClientDatastore(ds),
DealFunds: clientDealFunds,
}, nil
}