-
Notifications
You must be signed in to change notification settings - Fork 35
/
verified_registry_state.go
290 lines (236 loc) · 8.59 KB
/
verified_registry_state.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
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
package verifreg
import (
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v9/util/adt"
cbg "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
// DataCap is an integer number of bytes.
// We can introduce policy changes and replace this in the future.
type DataCap = abi.StoragePower
var DataCapGranularity = builtin.TokenPrecision
const SignatureDomainSeparation_RemoveDataCap = "fil_removedatacap:"
type RmDcProposalID struct {
ProposalID uint64
}
type State struct {
// Root key holder multisig.
// Authorize and remove verifiers.
RootKey address.Address
// Verifiers authorize VerifiedClients.
// Verifiers delegate their DataCap.
Verifiers cid.Cid // HAMT[addr.Address]DataCap
// RemoveDataCapProposalIDs keeps the counters of the datacap removal proposal a verifier has submitted for a
//specific client. Unique proposal ids ensure that removal proposals cannot be replayed.√
// AddrPairKey is constructed as <verifier address, client address>, both using ID addresses.
RemoveDataCapProposalIDs cid.Cid // HAMT[AddrPairKey]RmDcProposalID
// Maps client IDs to allocations made by that client.
Allocations cid.Cid // HAMT[ActorID]HAMT[AllocationID]Allocation
// Next allocation identifier to use.
// The value 0 is reserved to mean "no allocation".
NextAllocationId AllocationId
// Maps provider IDs to allocations claimed by that provider.
Claims cid.Cid // HAMT[ActorID]HAMT[ClaimID]Claim
}
var MinVerifiedDealSize = abi.NewStoragePower(1 << 20)
// rootKeyAddress comes from genesis.
func ConstructState(store adt.Store, rootKeyAddress address.Address) (*State, error) {
emptyMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to create empty map: %w", err)
}
return &State{
RootKey: rootKeyAddress,
Verifiers: emptyMapCid,
RemoveDataCapProposalIDs: emptyMapCid,
Allocations: emptyMapCid,
NextAllocationId: 1,
Claims: emptyMapCid,
}, nil
}
func (st *State) FindAllocation(store adt.Store, clientIdAddr address.Address, allocationId AllocationId) (*Allocation, bool, error) {
if clientIdAddr.Protocol() != address.ID {
return nil, false, xerrors.Errorf("can only look up ID addresses")
}
innerHamtCid, err := getInnerHamtCid(store, abi.IdAddrKey(clientIdAddr), st.Allocations)
if err != nil {
return nil, false, err
}
idToAllocationMap, err := adt.AsMap(store, innerHamtCid, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, false, xerrors.Errorf("couldn't get inner map: %x", err)
}
var allocation Allocation
if found, err := idToAllocationMap.Get(allocationId, &allocation); err != nil {
return nil, false, xerrors.Errorf("looking up allocation ID: %d: %w", allocationId, err)
} else if !found {
return nil, false, nil
}
clientId, err := address.IDFromAddress(clientIdAddr)
if err != nil {
return nil, false, xerrors.Errorf("couldn't get ID from clientIdAddr: %s", clientIdAddr)
}
if uint64(allocation.Client) != clientId {
return nil, false, xerrors.Errorf("clientId: %d did not match client in allocation: %d", clientId, allocation.Client)
}
return &allocation, true, nil
}
func (st *State) FindClaim(store adt.Store, providerIdAddr address.Address, claimId ClaimId) (*Claim, bool, error) {
if providerIdAddr.Protocol() != address.ID {
return nil, false, xerrors.Errorf("can only look up ID addresses")
}
innerHamtCid, err := getInnerHamtCid(store, abi.IdAddrKey(providerIdAddr), st.Claims)
if err != nil {
return nil, false, err
}
idToClaimsMap, err := adt.AsMap(store, innerHamtCid, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, false, xerrors.Errorf("couldn't get inner map: %x", err)
}
var claim Claim
if found, err := idToClaimsMap.Get(claimId, &claim); err != nil {
return nil, false, xerrors.Errorf("looking up allocation ID: %d: %w", claimId, err)
} else if !found {
return nil, false, nil
}
providerId, err := address.IDFromAddress(providerIdAddr)
if err != nil {
return nil, false, xerrors.Errorf("couldn't get ID from providerIdAddr: %s", providerIdAddr)
}
if uint64(claim.Provider) != providerId {
return nil, false, xerrors.Errorf("providerId: %d did not match provider in claim: %d", providerId, claim.Provider)
}
return &claim, true, nil
}
func getInnerHamtCid(store adt.Store, key abi.Keyer, mapCid cid.Cid) (cid.Cid, error) {
actorToHamtMap, err := adt.AsMap(store, mapCid, builtin.DefaultHamtBitwidth)
if err != nil {
return cid.Undef, xerrors.Errorf("couldn't get outer map: %x", err)
}
var innerHamtCid cbg.CborCid
if found, err := actorToHamtMap.Get(key, &innerHamtCid); err != nil {
return cid.Undef, xerrors.Errorf("looking up key: %s: %w", key, err)
} else if !found {
return cid.Undef, xerrors.Errorf("did not find key: %s", key)
}
return cid.Cid(innerHamtCid), nil
}
func (st *State) LoadAllocationsToMap(store adt.Store, clientIdAddr address.Address) (map[AllocationId]Allocation, error) {
if clientIdAddr.Protocol() != address.ID {
return nil, xerrors.Errorf("can only look up ID addresses")
}
innerHamtCid, err := getInnerHamtCid(store, abi.IdAddrKey(clientIdAddr), st.Allocations)
if err != nil {
return nil, err
}
adtMap, err := adt.AsMap(store, innerHamtCid, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("couldn't get map: %x", err)
}
var allocIdToAlloc = make(map[AllocationId]Allocation)
var out Allocation
err = adtMap.ForEach(&out, func(key string) error {
uintKey, err := abi.ParseUIntKey(key)
if err != nil {
return xerrors.Errorf("couldn't parse key to uint: %x", err)
}
allocIdToAlloc[AllocationId(uintKey)] = out
return nil
})
if err != nil {
return nil, err
}
return allocIdToAlloc, nil
}
func (st *State) LoadClaimsToMap(store adt.Store, providerIdAddr address.Address) (map[ClaimId]Claim, error) {
if providerIdAddr.Protocol() != address.ID {
return nil, xerrors.Errorf("can only look up ID addresses")
}
innerHamtCid, err := getInnerHamtCid(store, abi.IdAddrKey(providerIdAddr), st.Claims)
if err != nil {
return nil, err
}
adtMap, err := adt.AsMap(store, innerHamtCid, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("couldn't get map: %x", err)
}
var claimIdToClaim = make(map[ClaimId]Claim)
var out Claim
err = adtMap.ForEach(&out, func(key string) error {
uintKey, err := abi.ParseUIntKey(key)
if err != nil {
return xerrors.Errorf("couldn't parse key to uint: %w", err)
}
claimIdToClaim[ClaimId(uintKey)] = out
return nil
})
if err != nil {
return nil, err
}
return claimIdToClaim, nil
}
func (st *State) GetAllClaims(store adt.Store) (map[ClaimId]Claim, error) {
allClaims := make(map[ClaimId]Claim)
actorToHamtMap, err := adt.AsMap(store, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("couldn't get outer map: %x", err)
}
var innerHamtCid cbg.CborCid
err = actorToHamtMap.ForEach(&innerHamtCid, func(idKey string) error {
innerMap, err := adt.AsMap(store, cid.Cid(innerHamtCid), builtin.DefaultHamtBitwidth)
if err != nil {
return xerrors.Errorf("couldn't get inner map: %x", err)
}
var out Claim
err = innerMap.ForEach(&out, func(key string) error {
uintKey, err := abi.ParseUIntKey(key)
if err != nil {
return xerrors.Errorf("couldn't parse idKey to uint: %w", err)
}
allClaims[ClaimId(uintKey)] = out
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return allClaims, nil
}
func (st *State) GetAllAllocations(store adt.Store) (map[AllocationId]Allocation, error) {
allAllocations := make(map[AllocationId]Allocation)
actorToHamtMap, err := adt.AsMap(store, st.Allocations, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("couldn't get outer map: %x", err)
}
var innerHamtCid cbg.CborCid
err = actorToHamtMap.ForEach(&innerHamtCid, func(idKey string) error {
innerMap, err := adt.AsMap(store, cid.Cid(innerHamtCid), builtin.DefaultHamtBitwidth)
if err != nil {
return xerrors.Errorf("couldn't get inner map: %x", err)
}
var out Allocation
err = innerMap.ForEach(&out, func(key string) error {
uintKey, err := abi.ParseUIntKey(key)
if err != nil {
return xerrors.Errorf("couldn't parse idKey to uint: %w", err)
}
allAllocations[AllocationId(uintKey)] = out
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return allAllocations, nil
}