forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
525 lines (462 loc) · 15.1 KB
/
Copy pathtypes.go
File metadata and controls
525 lines (462 loc) · 15.1 KB
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
package ffs
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/util"
)
var (
// EmptyRetrievalID is an undef retrieval id.
EmptyRetrievalID = RetrievalID("")
)
// RetrievalID is the identifier of a Filecoin retrieval.
type RetrievalID string
// NewRetrievalID returns an new retrieval ID.
func NewRetrievalID() RetrievalID {
return RetrievalID(uuid.New().String())
}
// String returns a string representation of RetrievalID.
func (ri RetrievalID) String() string {
return string(ri)
}
var (
// EmptyJobID represents an empty JobID.
EmptyJobID = JobID("")
)
// JobID is an identifier for a ffs.Job.
type JobID string
// NewJobID returns a new JobID.
func NewJobID() JobID {
return JobID(uuid.New().String())
}
// String returns a string representation of JobID.
func (jid JobID) String() string {
return string(jid)
}
var (
// EmptyInstanceID represents an empty/invalid Instance ID.
EmptyInstanceID = APIID("")
)
// APIID is an identifier for a Api instance.
type APIID string
// NewAPIID returns a new InstanceID.
func NewAPIID() APIID {
return APIID(uuid.New().String())
}
// Valid returns true if the InstanceID is valid, false
// otherwise.
func (i APIID) Valid() bool {
_, err := uuid.Parse(string(i))
return err == nil
}
// String returns a string representation of InstanceID.
func (i APIID) String() string {
return string(i)
}
// JobStatus is a type for Job statuses.
type JobStatus int
const (
// Unspecified indicates a default or empty value
Unspecified JobStatus = iota
// Queued indicates the Job is queued in the Scheduler.
Queued
// Executing indicates that the Job is currently being
// executed.
Executing
// Failed indicates the Job failed, with job.ErrCause with
// the error cause.
Failed
// Canceled indicates the Job was canceled from Queued,
// and didn't reach execution.
Canceled
// Success indicates the Job was successfully executed.
Success
)
// JobStatusStr maps JobStatus to describing string.
var JobStatusStr = map[JobStatus]string{
Unspecified: "Unspecified",
Queued: "Queued",
Executing: "Executing",
Failed: "Failed",
Canceled: "Canceled",
Success: "Success",
}
// StorageJob is a task executed by the Scheduler.
type StorageJob struct {
ID JobID
APIID APIID
Cid cid.Cid
Status JobStatus
ErrCause string
DealErrors []DealError
}
// RetrievalJob is a retrieval task executed by the Scheduler.
type RetrievalJob struct {
ID JobID
APIID APIID
RetrievalID RetrievalID
Status JobStatus
ErrCause string
}
// StorageConfig contains a default storage configuration for an Api instance.
type StorageConfig struct {
Hot HotConfig
Cold ColdConfig
Repairable bool
}
// WithRepairable allows to enable/disable auto-repair.
func (s StorageConfig) WithRepairable(enabled bool) StorageConfig {
s.Repairable = enabled
return s
}
// WithColdEnabled allows to enable/disable Cold storage usage.
func (s StorageConfig) WithColdEnabled(enabled bool) StorageConfig {
s.Cold.Enabled = enabled
return s
}
// WithColdFastRetrieval sets the Fast Retrieval feature for new deals.
func (s StorageConfig) WithColdFastRetrieval(enabled bool) StorageConfig {
s.Cold.Filecoin.FastRetrieval = enabled
return s
}
// WithColdFilCountryCodes defines a list of allowed country codes to select miners
// for deals.
func (s StorageConfig) WithColdFilCountryCodes(countryCodes []string) StorageConfig {
s.Cold.Filecoin.CountryCodes = make([]string, len(countryCodes))
copy(s.Cold.Filecoin.CountryCodes, countryCodes)
return s
}
// WithColdFilExcludedMiners defines a list of miner addresses which won't be selected for
// making deals, no matter if they comply to other filters in the configuration.
func (s StorageConfig) WithColdFilExcludedMiners(miners []string) StorageConfig {
s.Cold.Filecoin.ExcludedMiners = make([]string, len(miners))
copy(s.Cold.Filecoin.ExcludedMiners, miners)
return s
}
// WithColdFilTrustedMiners defines a list of trusted miners addresses which will be
// returned if available. If more miners reusults are needed, other filters will be
// applied as usual.
func (s StorageConfig) WithColdFilTrustedMiners(miners []string) StorageConfig {
s.Cold.Filecoin.TrustedMiners = make([]string, len(miners))
copy(s.Cold.Filecoin.TrustedMiners, miners)
return s
}
// WithColdFilRepFactor defines the replication factor for Filecoin storage.
func (s StorageConfig) WithColdFilRepFactor(repFactor int) StorageConfig {
s.Cold.Filecoin.RepFactor = repFactor
return s
}
// WithColdFilDealDuration defines the duration used for deals for Filecoin storage.
func (s StorageConfig) WithColdFilDealDuration(duration int64) StorageConfig {
s.Cold.Filecoin.DealMinDuration = duration
return s
}
// WithColdFilRenew specifies if deals should be renewed before they expire with a particular
// threshold chain epochs.
func (s StorageConfig) WithColdFilRenew(enabled bool, threshold int) StorageConfig {
s.Cold.Filecoin.Renew.Enabled = enabled
s.Cold.Filecoin.Renew.Threshold = threshold
return s
}
// WithColdMaxPrice specifies the max price that should be considered for
// deal asks even when all other filers match.
func (s StorageConfig) WithColdMaxPrice(maxPrice uint64) StorageConfig {
s.Cold.Filecoin.MaxPrice = maxPrice
return s
}
// WithFastRetrieval specifies if deal fast retrieval flag on new deals
// is enabled.
func (s StorageConfig) WithFastRetrieval(enabled bool) StorageConfig {
s.Cold.Filecoin.FastRetrieval = enabled
return s
}
// WithColdAddr specifies the wallet address that should be used for transactions.
func (s StorageConfig) WithColdAddr(addr string) StorageConfig {
s.Cold.Filecoin.Addr = addr
return s
}
// WithHotEnabled allows to enable/disable Hot storage usage.
func (s StorageConfig) WithHotEnabled(enabled bool) StorageConfig {
s.Hot.Enabled = enabled
return s
}
// WithHotIpfsAddTimeout specifies a timeout for fetching data in Ipfs.
func (s StorageConfig) WithHotIpfsAddTimeout(seconds int) StorageConfig {
s.Hot.Ipfs.AddTimeout = seconds
return s
}
// WithHotAllowUnfreeze allows the Scheduler to fetch data from the Cold Storage,
// if the Enabled flag of the Hot Storage switches from false->true.
func (s StorageConfig) WithHotAllowUnfreeze(allow bool) StorageConfig {
s.Hot.AllowUnfreeze = true
return s
}
// WithUnfreezeMaxPrice indicates the maximum price to pay for an unfreeze
// procedure.
func (s StorageConfig) WithUnfreezeMaxPrice(maxPrice uint64) StorageConfig {
s.Hot.UnfreezeMaxPrice = maxPrice
return s
}
// Validate validates a StorageConfig.
func (s StorageConfig) Validate() error {
if err := s.Hot.Validate(); err != nil {
return fmt.Errorf("hot-ipfs config is invalid: %s", err)
}
if err := s.Cold.Validate(); err != nil {
return fmt.Errorf("cold-filecoin config is invalid: %s", err)
}
// We can't accept being renewable without the hot storage enabled.
// See the (**) note in scheduler.go
if s.Cold.Enabled && s.Cold.Filecoin.Renew.Enabled && !s.Hot.Enabled {
return fmt.Errorf("hot storage should be enabled to enable renewals")
}
return nil
}
// HotConfig is the desired storage of a Cid in a Hot Storage.
type HotConfig struct {
// Enable indicates if Cid data is stored. If true, it will consider
// further configurations to execute actions.
Enabled bool
// AllowUnfreeze indicates that if data isn't available in the Hot Storage,
// it's allowed to be feeded by Cold Storage if available.
AllowUnfreeze bool
// UnfreezeMaxPrice indicates the maximum amount of attoFil to pay for
// retrieval of data to unfreeze.
UnfreezeMaxPrice uint64
// Ipfs contains configuration related to storing Cid data in a IPFS node.
Ipfs IpfsConfig
}
// Validate validates a HotConfig.
func (hc HotConfig) Validate() error {
if !hc.Enabled {
return nil
}
if err := hc.Ipfs.Validate(); err != nil {
return fmt.Errorf("invalid ipfs config: %s", err)
}
return nil
}
// IpfsConfig is the desired storage of a Cid in IPFS.
type IpfsConfig struct {
// AddTimeout is an upper bound on adding data to IPFS node from
// the network before failing.
AddTimeout int
}
// Validate validates an IpfsConfig.
func (ic *IpfsConfig) Validate() error {
if ic.AddTimeout <= 0 {
return fmt.Errorf("add timeout should be greater than 0 seconds, got %d", ic.AddTimeout)
}
return nil
}
// ColdConfig is the desired state of a Cid in a cold layer.
type ColdConfig struct {
// Enabled indicates that data will be saved in Cold storage.
// If is switched from false->true, it will consider the other attributes
// as the desired state of the data in this Storage.
Enabled bool
// Filecoin describes the desired Filecoin configuration for a Cid in the
// Filecoin network.
Filecoin FilConfig
}
// Validate validates a ColdConfig.
func (cc ColdConfig) Validate() error {
if !cc.Enabled {
return nil
}
if err := cc.Filecoin.Validate(); err != nil {
return fmt.Errorf("invalid Filecoin config: %s", err)
}
if cc.Filecoin.Addr == "" {
return fmt.Errorf("invalid wallet address")
}
return nil
}
// FilConfig is the desired state of a Cid in the Filecoin network.
type FilConfig struct {
// RepFactor indicates the desired amount of active deals
// with different miners to store the data. While making deals
// the other attributes of FilConfig are considered for miner selection.
RepFactor int
// DealMinDuration indicates the duration to be used when making new deals.
DealMinDuration int64
// ExcludedMiners is a set of miner addresses won't be ever be selected
// when making new deals, even if they comply to other filters.
ExcludedMiners []string
// TrustedMiners is a set of miner addresses which will be forcibly used
// when making new deals. An empty/nil list disables this feature.
TrustedMiners []string
// CountryCodes indicates that new deals should select miners on specific
// countries.
CountryCodes []string
// Renew indicates deal-renewal configuration.
Renew FilRenew
// Addr is the wallet address used to store the data in filecoin
Addr string
// MaxPrice is the maximum price that will be spent to store the data
MaxPrice uint64
// FastRetrieval indicates that created deals should enable the
// fast retrieval feature.
FastRetrieval bool
}
// Validate returns a non-nil error if the configuration is invalid.
func (fc *FilConfig) Validate() error {
if fc.RepFactor <= 0 {
return fmt.Errorf("replication factor should be greater than zero, got %d", fc.RepFactor)
}
if fc.DealMinDuration < util.MinDealDuration {
return fmt.Errorf("deal duration should be greater than minimum, got %d", fc.DealMinDuration)
}
if err := fc.Renew.Validate(); err != nil {
return fmt.Errorf("invalid renew config: %s", err)
}
return nil
}
// FilRenew contains renew configuration for a Cid Cold Storage deals.
type FilRenew struct {
// Enabled indicates that deal-renewal is enabled for this Cid.
Enabled bool
// Threshold indicates how many epochs before expiring should trigger
// deal renewal. e.g: 100 epoch before expiring.
Threshold int
}
// Validate returns a non-nil error if the configuration is invalid.
func (fr *FilRenew) Validate() error {
if fr.Enabled && fr.Threshold <= 0 {
return fmt.Errorf("renew threshold should be positive: %d", fr.Threshold)
}
return nil
}
// RetrievalInfo has data about an executed Filecoin retrieval.
type RetrievalInfo struct {
ID RetrievalID
DataCid cid.Cid
TotalPaid uint64
MinerAddr string
Size int64
CreatedAt time.Time
}
// CidInfo contains information about the current storage state
// of a Cid.
type CidInfo struct {
// JobID indicates the Job ID which updated
// the current information. It *may be empty* if
// the data was imported manually.
JobID JobID
// Cid of payload.
Cid cid.Cid
// Created is the timestamp of the data.
Created time.Time
// Hot contains hot storage information.
Hot HotInfo
// Cold contains cold storage information.
Cold ColdInfo
}
// HotInfo contains information about the current storage state
// of a Cid in the hot layer.
type HotInfo struct {
Enabled bool
Size int
Ipfs IpfsHotInfo
}
// IpfsHotInfo contains information about the current storage state
// of a Cid in an IPFS node.
type IpfsHotInfo struct {
Created time.Time
}
// ColdInfo contains information about the current storage state
// of a Cid in the cold layer.
type ColdInfo struct {
Enabled bool
Filecoin FilInfo
}
// FilInfo contains information about the current storage state
// of a Cid in the Filecoin network.
type FilInfo struct {
// DataCid corresponds to the PayloadCid of the deal.
DataCid cid.Cid
// Size is the size of the Piece. Recall that this size
// is which is accounted for payment. Also is equal or
// greater than the original data size.
// This value might be zero for imported deals; if that's
// the case, will be re-calculated in the next made deal.
Size uint64
// Proposals contains known deals for the data.
Proposals []FilStorage
}
// FilStorage contains Deal information of a storage in Filecoin.
// This information is used in FFS may be used by FFS logic to
// provide repair, renwal, or retrieval tasks.
type FilStorage struct {
// ProposalCid of the deal.
ProposalCid cid.Cid
// PieceCid is the piece Cid.
PieceCid cid.Cid
// Renewed indicates if this deal was
// already renewed, so it can expiry
// safely if renewals are enabled.
Renewed bool
// Duration is the duration of the deal.
Duration int64
// ActivationEpoch is the epoch in which
// the deal was activated.
ActivationEpoch int64
// StartEpoch is the starting epoch in which
// the deal is considered active on-chain.
StartEpoch uint64
// Miner is the miner address which is storing
// deals data.
Miner string
// EpochPrice is the price of attoFil per GiB
// per epoch paid in this deal.
EpochPrice uint64
}
// JobLoggerCtxKey is a type to use in ctx values for CidLogger.
type JobLoggerCtxKey int
const (
// CtxKeyJid is a context-key to indicate the Job ID for JobLogger.
CtxKeyJid JobLoggerCtxKey = iota
// CtxStorageCid is the context-key to indicate the Cid of a
// StorageJob for JobLogger.
CtxStorageCid
// CtxRetrievalID is the context-key to indicate the RetrievalID of
// a RetrievalJob for JobLogger.
CtxRetrievalID
)
// JobLogger saves log information about a storage and retrieval tasks.
type JobLogger interface {
Log(context.Context, string, ...interface{})
Watch(context.Context, chan<- LogEntry) error
GetByCid(context.Context, cid.Cid) ([]LogEntry, error)
}
// LogEntry is a log entry from a Cid execution.
type LogEntry struct {
Cid cid.Cid
Timestamp time.Time
Jid JobID
Msg string
}
// PaychDir specifies the direction of a payment channel.
type PaychDir int
const (
// PaychDirUnspecified is an undefined direction.
PaychDirUnspecified PaychDir = iota
// PaychDirInbound is an inbound direction.
PaychDirInbound
// PaychDirOutbound is an outbound direction.
PaychDirOutbound
)
// PaychDirStr maps PaychDirs to describing string.
var PaychDirStr = map[PaychDir]string{
PaychDirUnspecified: "Unspecified",
PaychDirInbound: "Inbound",
PaychDirOutbound: "Outbound",
}
// PaychInfo holds information about a payment channel.
type PaychInfo struct {
CtlAddr string
Addr string
Direction PaychDir
}