forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_retrieval.go
More file actions
120 lines (103 loc) · 3.18 KB
/
api_retrieval.go
File metadata and controls
120 lines (103 loc) · 3.18 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
package api
import (
"fmt"
"time"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/ffs"
)
type retrievalConfig struct {
walletAddress string
maxPrice uint64
}
// Retrieval indicates information of a retrieval in the
// Filecoin network.
type Retrieval struct {
ID ffs.RetrievalID
PayloadCid cid.Cid
PieceCid cid.Cid
Selector string
Miners []string
WalletAddress string
MaxPrice uint64
JID ffs.JobID
CreatedAt time.Time
// Possibly empty
RetrievalMiner string
DataCid cid.Cid
Size uint64
}
// StartRetrieval schedules a new job to do a data retrieval.
func (i *API) StartRetrieval(payloadCid, pieceCid cid.Cid, selector string, miners []string, opts ...RetrievalOption) (Retrieval, error) {
i.lock.Lock()
defer i.lock.Unlock()
if !payloadCid.Defined() {
return Retrieval{}, fmt.Errorf("payload cid is undefined")
}
if !pieceCid.Defined() {
return Retrieval{}, fmt.Errorf("piece cid is undefined")
}
// ToDo: whenever text-based selectors are ready
// we should try checking the prc.Selector is well-formated.
defWalletAddress := i.cfg.DefaultStorageConfig.Cold.Filecoin.Addr
rc := retrievalConfig{walletAddress: defWalletAddress}
for _, o := range opts {
o(&rc)
}
if !i.isManagedAddress(rc.walletAddress) {
return Retrieval{}, fmt.Errorf("%s isn't a managed address", rc.walletAddress)
}
rID := ffs.NewRetrievalID()
jid, err := i.sched.StartRetrieval(i.cfg.ID, rID, payloadCid, pieceCid, selector, miners, rc.walletAddress, rc.maxPrice)
if err != nil {
return Retrieval{}, fmt.Errorf("starting retrieval in scheduler: %s", err)
}
rr, err := i.is.putRetrievalRequest(rID, payloadCid, pieceCid, selector, miners, rc.walletAddress, rc.maxPrice, jid)
if err != nil {
return Retrieval{}, fmt.Errorf("saving new partial retrieval config: %s", err)
}
return retrievalRequestToRetrieval(rr), nil
}
// GetRetrieval returns information from a Retrieval using its ID.
func (i *API) GetRetrieval(prID ffs.RetrievalID) (Retrieval, error) {
i.lock.Lock()
defer i.lock.Unlock()
rr, err := i.is.getRetrievalRequest(prID)
if err == ErrNotFound {
return Retrieval{}, err
}
if err != nil {
return Retrieval{}, fmt.Errorf("getting retrieval request from store: %s", err)
}
r := retrievalRequestToRetrieval(rr)
ri, err := i.sched.GetRetrievalInfo(r.ID)
if err != nil {
return Retrieval{}, fmt.Errorf("getting retrieval info: %s", err)
}
r.RetrievalMiner = ri.MinerAddr
r.DataCid = ri.DataCid
return r, nil
}
// RemoveRetrieval removes information and data from a executed retrieval.
func (i *API) RemoveRetrieval(partialCid cid.Cid) error {
i.lock.Lock()
defer i.lock.Unlock()
// ToDo:
// Check retrieval exists
// Check retrieval ID was done by this API
// Remove from scheduler (reference counting)
// Remove from instance-store
return nil
}
func retrievalRequestToRetrieval(rr retrievalRequest) Retrieval {
return Retrieval{
ID: rr.ID,
PayloadCid: rr.PayloadCid,
PieceCid: rr.PieceCid,
Selector: rr.Selector,
Miners: rr.Miners,
WalletAddress: rr.WalletAddress,
MaxPrice: rr.MaxPrice,
JID: rr.JID,
CreatedAt: rr.CreatedAt,
}
}