/
ribs.go
362 lines (281 loc) · 9.16 KB
/
ribs.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
package rbdeal
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/fatih/color"
"github.com/google/uuid"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
iface "github.com/lotus-web3/ribs"
"github.com/lotus-web3/ribs/rbstor"
"github.com/lotus-web3/ribs/ributil"
"golang.org/x/xerrors"
)
var log = logging.Logger("ribs")
type openOptions struct {
hostGetter func(...libp2p.Option) (host.Host, error)
localWalletOpener func(path string) (*ributil.LocalWallet, error)
localWalletPath string
fileCoinAPIEndpoint string
}
type OpenOption func(*openOptions)
// WithHostGetter sets the function used to instantiate the libp2p host used by RIBS.
// Defaults to libp2p.New.
func WithHostGetter(hg func(...libp2p.Option) (host.Host, error)) OpenOption {
return func(o *openOptions) {
o.hostGetter = hg
}
}
// WithLocalWalletOpener sets the function used to open the local wallet path.
// Defaults to using ributil.OpenWallet, where the wallet is instantiated if it does not exist.
// In a case where it is auto generated, the wallet path must be backed up elsewhere.
//
// See: WithLocalWalletPath.
func WithLocalWalletOpener(wg func(path string) (*ributil.LocalWallet, error)) OpenOption {
return func(o *openOptions) {
o.localWalletOpener = wg
}
}
// WithLocalWalletPath sets the path to the local directory containing the wallet.
// Care must be taken in backing up this directory.
// Defaults to `.ribswallet` under user home directory.
func WithLocalWalletPath(wp string) OpenOption {
return func(o *openOptions) {
o.localWalletPath = wp
}
}
// WithFileCoinApiEndpoint sets the FileCoin API endpoint used to probe the chain.
// Defaults to "https://api.chain.love/rpc/v1".
func WithFileCoinApiEndpoint(wp string) OpenOption {
return func(o *openOptions) {
o.fileCoinAPIEndpoint = wp
}
}
type ribs struct {
iface.RBS
db *ribsDB
host host.Host
wallet *ributil.LocalWallet
lotusRPCAddr string
msgSendLk sync.Mutex
marketFundsLk sync.Mutex
cachedWalletInfo *iface.WalletInfo
lastWalletInfoUpdate time.Time
//
close chan struct{}
//workerClosed chan struct{}
spCrawlClosed chan struct{}
marketWatchClosed chan struct{}
//
/* sp crawl */
crawlHost host.Host
/* sp tracker */
crawlState atomic.Pointer[iface.CrawlState]
/* car uploads */
uploadStats map[iface.GroupKey]*iface.GroupUploadStats
uploadStatsSnap map[iface.GroupKey]*iface.GroupUploadStats
activeUploads map[uuid.UUID]struct{}
uploadStatsLk sync.Mutex
rateCounters *ributil.RateCounters[peer.ID]
/* car upload offload (S3) */
s3 *s3.S3
s3Bucket string
s3BucketUrl *url.URL
s3Uploads map[iface.GroupKey]struct{}
s3Lk sync.Mutex
/* s3 stats */
s3UploadBytes, s3UploadStarted, s3UploadDone, s3UploadErr, s3Redirects, s3ReadReqs, s3ReadBytes atomic.Int64
/* dealmaking */
dealsLk sync.Mutex
moreDealsLocks map[iface.GroupKey]struct{}
/* retrieval */
retrHost host.Host
retrProv *retrievalProvider
retrSuccess, retrBytes, retrFail, retrCacheHit, retrCacheMiss, retrHttpTries, retrHttpSuccess, retrHttpBytes, retrActive atomic.Int64
/* retrieval checker */
rckToDo, rckStarted, rckSuccess, rckFail, rckSuccessAll, rckFailAll atomic.Int64
/* repair */
repairDir string
repairStats map[int]*iface.RepairJob // workerid -> repair job
repairStatsLk sync.Mutex
repairFetchCounters *ributil.RateCounters[iface.GroupKey]
}
func (r *ribs) Wallet() iface.Wallet {
return r
}
func Open(root string, opts ...OpenOption) (iface.RIBS, error) {
if err := os.Mkdir(root, 0755); err != nil && !os.IsExist(err) {
return nil, xerrors.Errorf("make root dir: %w", err)
}
opt := &openOptions{
hostGetter: libp2p.New,
localWalletOpener: ributil.OpenWallet,
localWalletPath: "~/.ribswallet",
fileCoinAPIEndpoint: "https://api.chain.love/rpc/v1",
}
if os.Getenv("RIBS_FILECOIN_API_ENDPOINT") != "" {
opt.fileCoinAPIEndpoint = os.Getenv("RIBS_FILECOIN_API_ENDPOINT")
}
for _, o := range opts {
o(opt)
}
db, err := openRibsDB(root)
if err != nil {
return nil, xerrors.Errorf("open db: %w", err)
}
rbs, err := rbstor.Open(root, rbstor.WithDB(db.db))
if err != nil {
return nil, xerrors.Errorf("open RBS: %w", err)
}
if err := db.startDB(); err != nil {
return nil, xerrors.Errorf("start db: %w", err)
}
r := &ribs{
RBS: rbs,
db: db,
lotusRPCAddr: opt.fileCoinAPIEndpoint,
uploadStats: map[iface.GroupKey]*iface.GroupUploadStats{},
uploadStatsSnap: map[iface.GroupKey]*iface.GroupUploadStats{},
activeUploads: map[uuid.UUID]struct{}{},
rateCounters: ributil.NewRateCounters[peer.ID](ributil.MinAvgGlobalLogPeerRate(float64(minTransferMbps), float64(linkSpeedMbps))),
s3Uploads: map[iface.GroupKey]struct{}{},
repairDir: filepath.Join(root, "repair"),
repairStats: map[int]*iface.RepairJob{},
close: make(chan struct{}),
//workerClosed: make(chan struct{}),
spCrawlClosed: make(chan struct{}),
marketWatchClosed: make(chan struct{}),
moreDealsLocks: map[iface.GroupKey]struct{}{},
repairFetchCounters: ributil.NewRateCounters[iface.GroupKey](ributil.MinAvgGlobalLogPeerRate(float64(minTransferMbps), float64(linkSpeedMbps/4))),
}
rp, err := newRetrievalProvider(context.TODO(), r)
if err != nil {
return nil, xerrors.Errorf("creating retrieval provider: %w", err)
}
r.retrProv = rp
{
wallet, err := opt.localWalletOpener(opt.localWalletPath)
if err != nil {
return nil, xerrors.Errorf("open wallet: %w", err)
}
defWallet, err := wallet.GetDefault()
if err != nil {
wl, err := wallet.WalletList(context.TODO())
if err != nil {
return nil, xerrors.Errorf("get wallet list: %w", err)
}
if len(wl) == 0 {
a, err := wallet.WalletNew(context.TODO(), "secp256k1")
if err != nil {
return nil, xerrors.Errorf("creating wallet: %w", err)
}
color.Yellow("--------------------------------------------------------------")
fmt.Println("CREATED NEW RIBS WALLET")
fmt.Println("ADDRESS: ", color.GreenString("%s", a))
fmt.Println("")
fmt.Printf("BACKUP YOUR WALLET DIRECTORY (%s)\n", opt.localWalletPath)
fmt.Println("")
fmt.Println("Before using RIBS, you must fund your wallet with FIL.")
fmt.Println("You can also supply it with DataCap if you want to make")
fmt.Println("FIL+ deals.")
color.Yellow("--------------------------------------------------------------")
wl = append(wl, a)
}
if len(wl) != 1 {
return nil, xerrors.Errorf("no default wallet or more than one wallet: %#v", wl)
}
if err := wallet.SetDefault(wl[0]); err != nil {
return nil, xerrors.Errorf("setting default wallet: %w", err)
}
defWallet, err = wallet.GetDefault()
if err != nil {
return nil, xerrors.Errorf("getting default wallet: %w", err)
}
}
fmt.Println("RIBS Wallet: ", defWallet)
r.wallet = wallet
r.host, err = opt.hostGetter()
if err != nil {
return nil, xerrors.Errorf("creating host: %w", err)
}
}
if err := r.maybeInitS3Offload(); err != nil {
return nil, xerrors.Errorf("trying to initialize S3 offload: %w", err)
}
r.RBS.ExternalStorage().InstallProvider(rp)
if err := r.RBS.Start(); err != nil {
return nil, xerrors.Errorf("start storage: %w", err)
}
go r.spCrawler()
go r.dealTracker(context.TODO())
go r.watchMarket(context.TODO())
go r.retrievalChecker(context.TODO())
if err := r.setupCarServer(context.TODO(), r.host); err != nil {
return nil, xerrors.Errorf("setup car server: %w", err)
}
go r.repairWorker(context.TODO(), 0)
go r.repairWorker(context.TODO(), 1)
go r.repairWorker(context.TODO(), 2)
go r.repairWorker(context.TODO(), 3)
/*go r.repairWorker(context.TODO(), 4)
go r.repairWorker(context.TODO(), 5)
go r.repairWorker(context.TODO(), 6)
go r.repairWorker(context.TODO(), 7)
go r.repairWorker(context.TODO(), 8)
/*go r.repairWorker(context.TODO(), 9)
go r.repairWorker(context.TODO(), 10)*/
r.subGroupChanges()
go r.claimChecker()
return r, nil
}
func (r *ribs) subGroupChanges() {
r.Storage().Subscribe(func(group iface.GroupKey, from, to iface.GroupState) {
go r.onSub(group, from, to)
})
}
func (r *ribs) onSub(group iface.GroupKey, from, to iface.GroupState) {
if to == iface.GroupStateLocalReadyForDeals {
c, err := r.db.GetNonFailedDealCount(group)
if err != nil {
log.Errorf("getting non-failed deal count: %s", err)
return
}
if c >= targetReplicaCount {
return
}
go func() {
err = r.makeMoreDeals(context.TODO(), group, r.host, r.wallet)
if err != nil {
log.Errorf("starting new deals: %s", err)
}
}()
}
}
func (r *ribs) RetrStats() (iface.RetrStats, error) {
return iface.RetrStats{
Success: r.retrSuccess.Load(),
Bytes: r.retrBytes.Load(),
Fail: r.retrFail.Load(),
CacheHit: r.retrCacheHit.Load(),
CacheMiss: r.retrCacheMiss.Load(),
Active: r.retrActive.Load(),
HTTPTries: r.retrHttpTries.Load(),
HTTPSuccess: r.retrHttpSuccess.Load(),
HTTPBytes: r.retrHttpBytes.Load(),
}, nil
}
func (r *ribs) Close() error {
close(r.close)
<-r.spCrawlClosed
<-r.marketWatchClosed
return r.RBS.Close()
}