forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
176 lines (153 loc) · 5.06 KB
/
api.go
File metadata and controls
176 lines (153 loc) · 5.06 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
package api
import (
"context"
"errors"
"fmt"
"sync"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
logging "github.com/ipfs/go-log/v2"
"github.com/textileio/powergate/ffs"
"github.com/textileio/powergate/ffs/scheduler"
)
var (
log = logging.Logger("ffs-api")
)
var (
// ErrMustOverrideConfig returned when trying to push config for storing a Cid
// without the override flag.
ErrMustOverrideConfig = errors.New("cid already pinned, consider using override flag")
// ErrReplacedCidNotFound returns when replacing a Cid that isn't stored.
ErrReplacedCidNotFound = errors.New("provided replaced cid wasn't found")
// ErrActiveInStorage returns when a Cid is trying to be removed but still defined as active
// on Hot or Cold storage.
ErrActiveInStorage = errors.New("can't remove Cid, disable from Hot and Cold storage")
// ErrHotStorageDisabled returned when trying to fetch a Cid when disabled on Hot Storage.
// To retrieve the data, is necessary to call unfreeze by enabling the Enabled flag in
// the Hot Storage for that Cid.
ErrHotStorageDisabled = errors.New("cid disabled in hot storage")
)
// API is an Api instance, which owns a Lotus Address and allows to
// Store and Retrieve Cids from Hot and Cold layers.
type API struct {
is *instanceStore
wm ffs.WalletManager
pm ffs.PaychManager
drm ffs.DealRecordsManager
sched *scheduler.Scheduler
lock sync.Mutex
closed bool
cfg InstanceConfig
ctx context.Context
cancel context.CancelFunc
}
// New returns a new Api instance.
func New(ds datastore.Datastore, iid ffs.APIID, sch *scheduler.Scheduler, wm ffs.WalletManager, pm ffs.PaychManager, drm ffs.DealRecordsManager, dc ffs.StorageConfig, addrInfo AddrInfo) (*API, error) {
is := newInstanceStore(namespace.Wrap(ds, datastore.NewKey("istore")))
dc.Cold.Filecoin.Addr = addrInfo.Addr
if err := dc.Validate(); err != nil {
return nil, fmt.Errorf("default storage config is invalid: %s", err)
}
config := InstanceConfig{
ID: iid,
Addrs: map[string]AddrInfo{addrInfo.Addr: addrInfo},
DefaultStorageConfig: dc,
}
ctx, cancel := context.WithCancel(context.Background())
i := new(ctx, is, wm, pm, drm, config, sch, cancel)
if err := i.is.putInstanceConfig(config); err != nil {
return nil, fmt.Errorf("saving new instance %s: %s", i.cfg.ID, err)
}
return i, nil
}
// Load loads a saved Api instance from its ConfigStore.
func Load(ds datastore.Datastore, iid ffs.APIID, sched *scheduler.Scheduler, wm ffs.WalletManager, pm ffs.PaychManager, drm ffs.DealRecordsManager) (*API, error) {
is := newInstanceStore(namespace.Wrap(ds, datastore.NewKey("istore")))
c, err := is.getInstanceConfig()
if err != nil {
return nil, fmt.Errorf("loading instance: %s", err)
}
ctx, cancel := context.WithCancel(context.Background())
return new(ctx, is, wm, pm, drm, c, sched, cancel), nil
}
func new(ctx context.Context, is *instanceStore, wm ffs.WalletManager, pm ffs.PaychManager, drm ffs.DealRecordsManager, config InstanceConfig, sch *scheduler.Scheduler, cancel context.CancelFunc) *API {
i := &API{
is: is,
wm: wm,
pm: pm,
drm: drm,
cfg: config,
sched: sch,
cancel: cancel,
ctx: ctx,
}
return i
}
// ID returns the ID.
func (i *API) ID() ffs.APIID {
return i.cfg.ID
}
// DefaultStorageConfig returns the default StorageConfig.
func (i *API) DefaultStorageConfig() ffs.StorageConfig {
return i.cfg.DefaultStorageConfig
}
// GetStorageConfig returns the current StorageConfig for a Cid.
func (i *API) GetStorageConfig(c cid.Cid) (ffs.StorageConfig, error) {
conf, err := i.is.getStorageConfig(c)
if err == ErrNotFound {
return ffs.StorageConfig{}, err
}
if err != nil {
return ffs.StorageConfig{}, fmt.Errorf("getting cid config from store: %s", err)
}
return conf, nil
}
// SetDefaultStorageConfig sets a new default StorageConfig.
func (i *API) SetDefaultStorageConfig(c ffs.StorageConfig) error {
i.lock.Lock()
defer i.lock.Unlock()
if err := c.Validate(); err != nil {
return fmt.Errorf("default cid config is invalid: %s", err)
}
i.cfg.DefaultStorageConfig = c
return i.is.putInstanceConfig(i.cfg)
}
// Info returns instance information.
func (i *API) Info(ctx context.Context) (InstanceInfo, error) {
i.lock.Lock()
defer i.lock.Unlock()
pins, err := i.is.getCids()
if err != nil {
return InstanceInfo{}, fmt.Errorf("getting pins from instance: %s", err)
}
var balances []BalanceInfo
for _, addr := range i.cfg.Addrs {
balance, err := i.wm.Balance(ctx, addr.Addr)
if err != nil {
return InstanceInfo{}, fmt.Errorf("getting balance of %s: %s", addr.Addr, err)
}
info := BalanceInfo{
AddrInfo: addr,
Balance: balance,
}
balances = append(balances, info)
}
return InstanceInfo{
ID: i.cfg.ID,
DefaultStorageConfig: i.cfg.DefaultStorageConfig,
Balances: balances,
Pins: pins,
}, nil
}
// Close terminates the running Api.
func (i *API) Close() error {
i.lock.Lock()
defer i.lock.Unlock()
if i.closed {
return nil
}
i.cancel()
i.closed = true
return nil
}