/
api_import.go
70 lines (60 loc) · 1.53 KB
/
api_import.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
package api
import (
"fmt"
"time"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/ffs"
)
// ImportDeal contains information of an imported deal.
type ImportDeal struct {
ProposalCid *cid.Cid
MinerAddress string
}
// ImportStorage imports deals existing in the Filecoin network. The StorageConfig
// attached to this Cid will be the default one with HotStorage disabled.
func (i *API) ImportStorage(payloadCid cid.Cid, pieceCid cid.Cid, deals []ImportDeal, opts ...ImportOption) error {
i.lock.Lock()
defer i.lock.Unlock()
cfg := importConfig{}
for _, o := range opts {
o(&cfg)
}
scfg := ffs.StorageConfig{
Repairable: false,
Hot: ffs.HotConfig{
Enabled: false,
},
Cold: i.cfg.DefaultStorageConfig.Cold,
}
filStorage := make([]ffs.FilStorage, len(deals))
for i, d := range deals {
filStorage[i] = ffs.FilStorage{
PieceCid: pieceCid,
Miner: d.MinerAddress,
}
if d.ProposalCid != nil {
filStorage[i].ProposalCid = *d.ProposalCid
}
}
cinfo := ffs.StorageInfo{
APIID: i.cfg.ID,
JobID: ffs.EmptyJobID,
Cid: payloadCid,
Created: time.Now(),
Hot: ffs.HotInfo{Enabled: false},
Cold: ffs.ColdInfo{
Enabled: true,
Filecoin: ffs.FilInfo{
DataCid: payloadCid,
Proposals: filStorage,
},
},
}
if err := i.sched.ImportStorageInfo(i.cfg.ID, cinfo); err != nil {
return fmt.Errorf("importing cid info in scheduler: %s", err)
}
if err := i.is.putStorageConfig(payloadCid, scfg); err != nil {
return fmt.Errorf("saving new imported config: %s", err)
}
return nil
}