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
420 lines (366 loc) · 11.7 KB
/
types.go
File metadata and controls
420 lines (366 loc) · 11.7 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
package ffs
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/util"
)
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",
}
// Job is a task executed by the Scheduler.
type Job struct {
ID JobID
APIID APIID
Cid cid.Cid
Status JobStatus
ErrCause string
DealErrors []DealError
}
// 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
}
// 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
}
// 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
}
// 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)
}
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
// 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
}
// 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
}
// CidInfo contains information about the current storage state
// of a Cid.
type CidInfo struct {
JobID JobID
Cid cid.Cid
Created time.Time
Hot HotInfo
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 cid.Cid
Size uint64
Proposals []FilStorage
}
// FilStorage contains Deal information of a storage in Filecoin.
type FilStorage struct {
ProposalCid cid.Cid
Renewed bool
Duration int64
ActivationEpoch int64
StartEpoch uint64
Miner string
EpochPrice uint64
}
// CidLoggerCtxKey is a type to use in ctx values for CidLogger.
type CidLoggerCtxKey int
const (
// CtxKeyJid is the key to store Jid metadata.
CtxKeyJid CidLoggerCtxKey = iota
)
// CidLogger saves log information about a Cid executions.
type CidLogger interface {
Log(context.Context, cid.Cid, string, ...interface{})
Watch(context.Context, chan<- LogEntry) error
Get(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
}