forked from filecoin-project/filecoin-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sector_update.go
332 lines (297 loc) · 8.44 KB
/
sector_update.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
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
//go:build cgo
// +build cgo
package ffi
import (
"github.com/filecoin-project/filecoin-ffi/cgo"
commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/proof"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
"golang.org/x/xerrors"
)
func toFilRegisteredUpdateProof(p abi.RegisteredUpdateProof) (cgo.RegisteredUpdateProof, error) {
switch p {
case abi.RegisteredUpdateProof_StackedDrg2KiBV1:
return cgo.RegisteredUpdateProofStackedDrg2KiBV1, nil
case abi.RegisteredUpdateProof_StackedDrg8MiBV1:
return cgo.RegisteredUpdateProofStackedDrg8MiBV1, nil
case abi.RegisteredUpdateProof_StackedDrg512MiBV1:
return cgo.RegisteredUpdateProofStackedDrg512MiBV1, nil
case abi.RegisteredUpdateProof_StackedDrg32GiBV1:
return cgo.RegisteredUpdateProofStackedDrg32GiBV1, nil
case abi.RegisteredUpdateProof_StackedDrg64GiBV1:
return cgo.RegisteredUpdateProofStackedDrg64GiBV1, nil
default:
return 0, errors.Errorf("no mapping to abi.RegisteredUpdateProof value available for: %v", p)
}
}
type FunctionsSectorUpdate struct{}
var SectorUpdate = FunctionsSectorUpdate{}
func (FunctionsSectorUpdate) EncodeInto(
proofType abi.RegisteredUpdateProof,
newReplicaPath string,
newReplicaCachePath string,
sectorKeyPath string,
sectorKeyCachePath string,
stagedDataPath string,
pieces []abi.PieceInfo,
) (sealedCID cid.Cid, unsealedCID cid.Cid, err error) {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return cid.Undef, cid.Undef, err
}
filPublicPieceInfos, err := toFilPublicPieceInfos(pieces)
if err != nil {
return cid.Undef, cid.Undef, err
}
commRRaw, commDRaw, err := cgo.EmptySectorUpdateEncodeInto(
up,
cgo.AsSliceRefUint8([]byte(newReplicaPath)),
cgo.AsSliceRefUint8([]byte(newReplicaCachePath)),
cgo.AsSliceRefUint8([]byte(sectorKeyPath)),
cgo.AsSliceRefUint8([]byte(sectorKeyCachePath)),
cgo.AsSliceRefUint8([]byte(stagedDataPath)),
cgo.AsSliceRefPublicPieceInfo(filPublicPieceInfos),
)
if err != nil {
return cid.Undef, cid.Undef, err
}
commR, errCommrSize := commcid.ReplicaCommitmentV1ToCID(commRRaw)
if errCommrSize != nil {
return cid.Undef, cid.Undef, errCommrSize
}
commD, errCommdSize := commcid.DataCommitmentV1ToCID(commDRaw)
if errCommdSize != nil {
return cid.Undef, cid.Undef, errCommdSize
}
return commR, commD, nil
}
func (FunctionsSectorUpdate) DecodeFrom(
proofType abi.RegisteredUpdateProof,
outDataPath string,
replicaPath string,
sectorKeyPath string,
sectorKeyCachePath string,
unsealedCID cid.Cid,
) error {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return err
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return err
}
return cgo.EmptySectorUpdateDecodeFrom(
up,
cgo.AsSliceRefUint8([]byte(outDataPath)),
cgo.AsSliceRefUint8([]byte(replicaPath)),
cgo.AsSliceRefUint8([]byte(sectorKeyPath)),
cgo.AsSliceRefUint8([]byte(sectorKeyCachePath)),
&commD,
)
}
func (FunctionsSectorUpdate) RemoveData(
proofType abi.RegisteredUpdateProof,
sectorKeyPath string,
sectorKeyCachePath string,
replicaPath string,
replicaCachePath string,
dataPath string,
unsealedCID cid.Cid,
) error {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return err
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return err
}
return cgo.EmptySectorUpdateRemoveEncodedData(
up,
cgo.AsSliceRefUint8([]byte(sectorKeyPath)),
cgo.AsSliceRefUint8([]byte(sectorKeyCachePath)),
cgo.AsSliceRefUint8([]byte(replicaPath)),
cgo.AsSliceRefUint8([]byte(replicaCachePath)),
cgo.AsSliceRefUint8([]byte(dataPath)),
&commD,
)
}
func (FunctionsSectorUpdate) GenerateUpdateVanillaProofs(
proofType abi.RegisteredUpdateProof,
oldSealedCID cid.Cid,
newSealedCID cid.Cid,
unsealedCID cid.Cid,
newReplicaPath string,
newReplicaCachePath string,
sectorKeyPath string,
sectorKeyCachePath string,
) ([][]byte, error) {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return nil, err
}
commRold, err := to32ByteCommR(oldSealedCID)
if err != nil {
return nil, xerrors.Errorf("transforming old CommR: %w", err)
}
commRnew, err := to32ByteCommR(newSealedCID)
if err != nil {
return nil, xerrors.Errorf("transforming new CommR: %w", err)
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return nil, xerrors.Errorf("transforming new CommD: %w", err)
}
return cgo.GenerateEmptySectorUpdatePartitionProofs(
up,
&commRold,
&commRnew,
&commD,
cgo.AsSliceRefUint8([]byte(sectorKeyPath)),
cgo.AsSliceRefUint8([]byte(sectorKeyCachePath)),
cgo.AsSliceRefUint8([]byte(newReplicaPath)),
cgo.AsSliceRefUint8([]byte(newReplicaCachePath)),
)
}
func (FunctionsSectorUpdate) VerifyVanillaProofs(
proofType abi.RegisteredUpdateProof,
oldSealedCID cid.Cid,
newSealedCID cid.Cid,
unsealedCID cid.Cid,
vanillaProofs [][]byte,
) (bool, error) {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return false, err
}
commRold, err := to32ByteCommR(oldSealedCID)
if err != nil {
return false, xerrors.Errorf("transorming old CommR: %w", err)
}
commRnew, err := to32ByteCommR(newSealedCID)
if err != nil {
return false, xerrors.Errorf("transorming new CommR: %w", err)
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return false, xerrors.Errorf("transorming new CommD: %w", err)
}
proofs, cleanup := toUpdateVanillaProofs(vanillaProofs)
defer cleanup()
return cgo.VerifyEmptySectorUpdatePartitionProofs(
up,
cgo.AsSliceRefSliceBoxedUint8(proofs),
&commRold,
&commRnew,
&commD,
)
}
func (FunctionsSectorUpdate) GenerateUpdateProofWithVanilla(
proofType abi.RegisteredUpdateProof,
oldSealedCID cid.Cid,
newSealedCID cid.Cid,
unsealedCID cid.Cid,
vanillaProofs [][]byte,
) ([]byte, error) {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return nil, err
}
commRold, err := to32ByteCommR(oldSealedCID)
if err != nil {
return nil, xerrors.Errorf("transorming old CommR: %w", err)
}
commRnew, err := to32ByteCommR(newSealedCID)
if err != nil {
return nil, xerrors.Errorf("transorming new CommR: %w", err)
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return nil, xerrors.Errorf("transorming new CommD: %w", err)
}
proofs, cleanup := toUpdateVanillaProofs(vanillaProofs)
defer cleanup()
return cgo.GenerateEmptySectorUpdateProofWithVanilla(
up,
cgo.AsSliceRefSliceBoxedUint8(proofs),
&commRold,
&commRnew,
&commD,
)
}
func toUpdateVanillaProofs(src [][]byte) ([]cgo.SliceBoxedUint8, func()) {
out := make([]cgo.SliceBoxedUint8, len(src))
for idx := range out {
out[idx] = cgo.AllocSliceBoxedUint8(src[idx])
}
return out, func() {
for idx := range out {
out[idx].Destroy()
}
}
}
func (FunctionsSectorUpdate) GenerateUpdateProof(
proofType abi.RegisteredUpdateProof,
oldSealedCID cid.Cid,
newSealedCID cid.Cid,
unsealedCID cid.Cid,
newReplicaPath string,
newReplicaCachePath string,
sectorKeyPath string,
sectorKeyCachePath string,
) ([]byte, error) {
up, err := toFilRegisteredUpdateProof(proofType)
if err != nil {
return nil, err
}
commRold, err := to32ByteCommR(oldSealedCID)
if err != nil {
return nil, xerrors.Errorf("transorming old CommR: %w", err)
}
commRnew, err := to32ByteCommR(newSealedCID)
if err != nil {
return nil, xerrors.Errorf("transorming new CommR: %w", err)
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return nil, xerrors.Errorf("transorming new CommD: %w", err)
}
return cgo.GenerateEmptySectorUpdateProof(
up,
&commRold,
&commRnew,
&commD,
cgo.AsSliceRefUint8([]byte(sectorKeyPath)),
cgo.AsSliceRefUint8([]byte(sectorKeyCachePath)),
cgo.AsSliceRefUint8([]byte(newReplicaPath)),
cgo.AsSliceRefUint8([]byte(newReplicaCachePath)),
)
}
func (FunctionsSectorUpdate) VerifyUpdateProof(info proof.ReplicaUpdateInfo) (bool, error) {
up, err := toFilRegisteredUpdateProof(info.UpdateProofType)
if err != nil {
return false, err
}
commRold, err := to32ByteCommR(info.OldSealedSectorCID)
if err != nil {
return false, xerrors.Errorf("transforming old CommR: %w", err)
}
commRnew, err := to32ByteCommR(info.NewSealedSectorCID)
if err != nil {
return false, xerrors.Errorf("tranfsorming new CommR: %w", err)
}
commD, err := to32ByteCommD(info.NewUnsealedSectorCID)
if err != nil {
return false, xerrors.Errorf("transforming new CommD: %w", err)
}
return cgo.VerifyEmptySectorUpdateProof(
up,
cgo.AsSliceRefUint8(info.Proof),
&commRold,
&commRnew,
&commD,
)
}