-
Notifications
You must be signed in to change notification settings - Fork 136
/
proofs.go
1110 lines (933 loc) · 31.4 KB
/
proofs.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build cgo
// +build cgo
package ffi
// #cgo linux LDFLAGS: ${SRCDIR}/libfilcrypto.a -Wl,-unresolved-symbols=ignore-all
// #cgo darwin LDFLAGS: ${SRCDIR}/libfilcrypto.a -Wl,-undefined,dynamic_lookup
// #cgo pkg-config: ${SRCDIR}/filcrypto.pc
// #include "./filcrypto.h"
import "C"
import (
"os"
"runtime"
"github.com/filecoin-project/go-state-types/proof"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/filecoin-ffi/cgo"
)
// VerifySeal returns true if the sealing operation from which its inputs were
// derived was valid, and false if not.
func VerifySeal(info proof.SealVerifyInfo) (bool, error) {
sp, err := toFilRegisteredSealProof(info.SealProof)
if err != nil {
return false, err
}
commR, err := to32ByteCommR(info.SealedCID)
if err != nil {
return false, err
}
commD, err := to32ByteCommD(info.UnsealedCID)
if err != nil {
return false, err
}
proverID, err := toProverID(info.Miner)
if err != nil {
return false, err
}
randomness := cgo.AsByteArray32(info.Randomness)
interactiveRandomness := cgo.AsByteArray32(info.InteractiveRandomness)
return cgo.VerifySeal(sp, &commR, &commD, &proverID, &randomness, &interactiveRandomness, uint64(info.SectorID.Number), cgo.AsSliceRefUint8(info.Proof))
}
func VerifyAggregateSeals(aggregate proof.AggregateSealVerifyProofAndInfos) (bool, error) {
if len(aggregate.Infos) == 0 {
return false, xerrors.New("no seal verify infos")
}
// TODO: assuming this needs to be the same for all sectors, potentially makes sense to put in AggregateSealVerifyProofAndInfos
spt := aggregate.SealProof
inputs := make([]cgo.AggregationInputs, len(aggregate.Infos))
for i, info := range aggregate.Infos {
commR, err := to32ByteCommR(info.SealedCID)
if err != nil {
return false, err
}
commD, err := to32ByteCommD(info.UnsealedCID)
if err != nil {
return false, err
}
inputs[i] = cgo.NewAggregationInputs(
commR,
commD,
uint64(info.Number),
cgo.AsByteArray32(info.Randomness),
cgo.AsByteArray32(info.InteractiveRandomness),
)
}
sp, err := toFilRegisteredSealProof(spt)
if err != nil {
return false, err
}
proverID, err := toProverID(aggregate.Miner)
if err != nil {
return false, err
}
rap, err := toFilRegisteredAggregationProof(aggregate.AggregateProof)
if err != nil {
return false, err
}
return cgo.VerifyAggregateSealProof(sp, rap, &proverID, cgo.AsSliceRefUint8(aggregate.Proof), cgo.AsSliceRefAggregationInputs(inputs))
}
// VerifyWinningPoSt returns true if the Winning PoSt-generation operation from which its
// inputs were derived was valid, and false if not.
func VerifyWinningPoSt(info proof.WinningPoStVerifyInfo) (bool, error) {
filPublicReplicaInfos, err := toFilPublicReplicaInfosForWinningPoSt(info.ChallengedSectors)
if err != nil {
return false, errors.Wrap(err, "failed to create public replica info array for FFI")
}
filPoStProofs, cleanup, err := toFilPoStProofs(info.Proofs)
if err != nil {
return false, errors.Wrap(err, "failed to create PoSt proofs array for FFI")
}
defer cleanup()
proverID, err := toProverID(info.Prover)
if err != nil {
return false, err
}
randomness := cgo.AsByteArray32(info.Randomness)
return cgo.VerifyWinningPoSt(
&randomness,
cgo.AsSliceRefPublicReplicaInfo(filPublicReplicaInfos),
cgo.AsSliceRefPoStProof(filPoStProofs),
&proverID,
)
}
// VerifyWindowPoSt returns true if the Winning PoSt-generation operation from which its
// inputs were derived was valid, and false if not.
func VerifyWindowPoSt(info proof.WindowPoStVerifyInfo) (bool, error) {
if len(info.Proofs) == 0 {
return false, errors.New("nothing to verify")
}
filPublicReplicaInfos, err := toFilPublicReplicaInfosForWindowPoSt(info.ChallengedSectors, info.Proofs[0].PoStProof)
if err != nil {
return false, errors.Wrap(err, "failed to create public replica info array for FFI")
}
filPoStProofs, cleanup, err := toFilPoStProofs(info.Proofs)
if err != nil {
return false, errors.Wrap(err, "failed to create PoSt proofs array for FFI")
}
defer cleanup()
proverID, err := toProverID(info.Prover)
if err != nil {
return false, err
}
randomness := cgo.AsByteArray32(info.Randomness)
return cgo.VerifyWindowPoSt(
&randomness,
cgo.AsSliceRefPublicReplicaInfo(filPublicReplicaInfos),
cgo.AsSliceRefPoStProof(filPoStProofs),
&proverID,
)
}
// GeneratePieceCommitment produces a piece commitment for the provided data
// stored at a given path.
func GeneratePieceCID(proofType abi.RegisteredSealProof, piecePath string, pieceSize abi.UnpaddedPieceSize) (cid.Cid, error) {
pieceFile, err := os.Open(piecePath)
if err != nil {
return cid.Undef, err
}
pcd, err := GeneratePieceCIDFromFile(proofType, pieceFile, pieceSize)
if err != nil {
return cid.Undef, pieceFile.Close()
}
return pcd, pieceFile.Close()
}
// GenerateDataCommitment produces a commitment for the sector containing the
// provided pieces.
func GenerateUnsealedCID(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return cid.Undef, err
}
filPublicPieceInfos, err := toFilPublicPieceInfos(pieces)
if err != nil {
return cid.Undef, err
}
resp, err := cgo.GenerateDataCommitment(sp, cgo.AsSliceRefPublicPieceInfo(filPublicPieceInfos))
if err != nil {
return cid.Undef, err
}
return commcid.DataCommitmentV1ToCID(resp)
}
// GeneratePieceCIDFromFile produces a piece CID for the provided data stored in a given file.
func GeneratePieceCIDFromFile(proofType abi.RegisteredSealProof, pieceFile *os.File, pieceSize abi.UnpaddedPieceSize) (cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return cid.Undef, err
}
pieceFd := pieceFile.Fd()
defer runtime.KeepAlive(pieceFile)
resp, err := cgo.GeneratePieceCommitment(sp, int32(pieceFd), uint64(pieceSize))
if err != nil {
return cid.Undef, err
}
return commcid.PieceCommitmentV1ToCID(resp)
}
// WriteWithAlignment
func WriteWithAlignment(
proofType abi.RegisteredSealProof,
pieceFile *os.File,
pieceBytes abi.UnpaddedPieceSize,
stagedSectorFile *os.File,
existingPieceSizes []abi.UnpaddedPieceSize,
) (leftAlignment, total abi.UnpaddedPieceSize, pieceCID cid.Cid, retErr error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return 0, 0, cid.Undef, err
}
pieceFd := pieceFile.Fd()
defer runtime.KeepAlive(pieceFile)
stagedSectorFd := stagedSectorFile.Fd()
defer runtime.KeepAlive(stagedSectorFile)
filExistingPieceSizes := toFilExistingPieceSizes(existingPieceSizes)
leftAlignmentUnpadded, totalWriteUnpadded, commPRaw, err := cgo.WriteWithAlignment(sp, int32(pieceFd), uint64(pieceBytes), int32(stagedSectorFd), cgo.AsSliceRefUint64(filExistingPieceSizes))
if err != nil {
return 0, 0, cid.Undef, err
}
commP, errCommpSize := commcid.PieceCommitmentV1ToCID(commPRaw)
if errCommpSize != nil {
return 0, 0, cid.Undef, errCommpSize
}
return abi.UnpaddedPieceSize(leftAlignmentUnpadded), abi.UnpaddedPieceSize(totalWriteUnpadded), commP, nil
}
// WriteWithoutAlignment
func WriteWithoutAlignment(
proofType abi.RegisteredSealProof,
pieceFile *os.File,
pieceBytes abi.UnpaddedPieceSize,
stagedSectorFile *os.File,
) (abi.UnpaddedPieceSize, cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return 0, cid.Undef, err
}
pieceFd := pieceFile.Fd()
defer runtime.KeepAlive(pieceFile)
stagedSectorFd := stagedSectorFile.Fd()
defer runtime.KeepAlive(stagedSectorFile)
totalWriteUnpadded, commPRaw, err := cgo.WriteWithoutAlignment(sp, int32(pieceFd), uint64(pieceBytes), int32(stagedSectorFd))
if err != nil {
return 0, cid.Undef, err
}
commP, errCommpSize := commcid.PieceCommitmentV1ToCID(commPRaw)
if errCommpSize != nil {
return 0, cid.Undef, errCommpSize
}
return abi.UnpaddedPieceSize(totalWriteUnpadded), commP, nil
}
// SealPreCommitPhase1
func SealPreCommitPhase1(
proofType abi.RegisteredSealProof,
cacheDirPath string,
stagedSectorPath string,
sealedSectorPath string,
sectorNum abi.SectorNumber,
minerID abi.ActorID,
ticket abi.SealRandomness,
pieces []abi.PieceInfo,
) (phase1Output []byte, err error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return nil, err
}
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
filPublicPieceInfos, err := toFilPublicPieceInfos(pieces)
if err != nil {
return nil, err
}
ticketBytes := cgo.AsByteArray32(ticket)
return cgo.SealPreCommitPhase1(
sp,
cgo.AsSliceRefUint8([]byte(cacheDirPath)),
cgo.AsSliceRefUint8([]byte(stagedSectorPath)),
cgo.AsSliceRefUint8([]byte(sealedSectorPath)),
uint64(sectorNum),
&proverID,
&ticketBytes,
cgo.AsSliceRefPublicPieceInfo(filPublicPieceInfos),
)
}
// SealPreCommitPhase2
func SealPreCommitPhase2(
phase1Output []byte,
cacheDirPath string,
sealedSectorPath string,
) (sealedCID cid.Cid, unsealedCID cid.Cid, err error) {
commRRaw, commDRaw, err := cgo.SealPreCommitPhase2(
cgo.AsSliceRefUint8(phase1Output),
cgo.AsSliceRefUint8([]byte(cacheDirPath)),
cgo.AsSliceRefUint8([]byte(sealedSectorPath)),
)
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
}
// SealCommitPhase1
func SealCommitPhase1(
proofType abi.RegisteredSealProof,
sealedCID cid.Cid,
unsealedCID cid.Cid,
cacheDirPath string,
sealedSectorPath string,
sectorNum abi.SectorNumber,
minerID abi.ActorID,
ticket abi.SealRandomness,
seed abi.InteractiveSealRandomness,
pieces []abi.PieceInfo,
) (phase1Output []byte, err error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return nil, err
}
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
commR, err := to32ByteCommR(sealedCID)
if err != nil {
return nil, err
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return nil, err
}
filPublicPieceInfos, err := toFilPublicPieceInfos(pieces)
if err != nil {
return nil, err
}
ticketBytes := cgo.AsByteArray32(ticket)
seedBytes := cgo.AsByteArray32(seed)
return cgo.SealCommitPhase1(
sp,
&commR,
&commD,
cgo.AsSliceRefUint8([]byte(cacheDirPath)),
cgo.AsSliceRefUint8([]byte(sealedSectorPath)),
uint64(sectorNum),
&proverID,
&ticketBytes,
&seedBytes,
cgo.AsSliceRefPublicPieceInfo(filPublicPieceInfos),
)
}
// SealCommitPhase2
func SealCommitPhase2(
phase1Output []byte,
sectorNum abi.SectorNumber,
minerID abi.ActorID,
) ([]byte, error) {
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
return cgo.SealCommitPhase2(cgo.AsSliceRefUint8(phase1Output), uint64(sectorNum), &proverID)
}
// SealCommitPhase2CircuitProofs runs a non-interactive proof and returns the circuit proof bytes
// rather than the aggregated proof bytes. This is used to aggregate multiple non-interactive
// proofs as the aggregated single proof outputs can't further be aggregated.
func SealCommitPhase2CircuitProofs(phase1Output []byte, sectorNum abi.SectorNumber) ([]byte, error) {
return cgo.SealCommitPhase2CircuitProofs(cgo.AsSliceRefUint8(phase1Output), uint64(sectorNum))
}
// TODO AggregateSealProofs it only needs InteractiveRandomness out of the aggregateInfo.Infos
func AggregateSealProofs(aggregateInfo proof.AggregateSealVerifyProofAndInfos, proofs [][]byte) (out []byte, err error) {
sp, err := toFilRegisteredSealProof(aggregateInfo.SealProof)
if err != nil {
return nil, err
}
commRs := make([]cgo.ByteArray32, len(aggregateInfo.Infos))
seeds := make([]cgo.ByteArray32, len(aggregateInfo.Infos))
for i, info := range aggregateInfo.Infos {
seeds[i] = cgo.AsByteArray32(info.InteractiveRandomness)
commRs[i], err = to32ByteCommR(info.SealedCID)
if err != nil {
return nil, err
}
}
pfs, cleaner := toVanillaProofs(proofs)
defer cleaner()
rap, err := toFilRegisteredAggregationProof(aggregateInfo.AggregateProof)
if err != nil {
return nil, err
}
return cgo.AggregateSealProofs(
sp,
rap,
cgo.AsSliceRefByteArray32(commRs),
cgo.AsSliceRefByteArray32(seeds),
cgo.AsSliceRefSliceBoxedUint8(pfs),
)
}
// Unseal
func Unseal(
proofType abi.RegisteredSealProof,
cacheDirPath string,
sealedSector *os.File,
unsealOutput *os.File,
sectorNum abi.SectorNumber,
minerID abi.ActorID,
ticket abi.SealRandomness,
unsealedCID cid.Cid,
) error {
sectorSize, err := proofType.SectorSize()
if err != nil {
return err
}
unpaddedBytesAmount := abi.PaddedPieceSize(sectorSize).Unpadded()
return UnsealRange(proofType, cacheDirPath, sealedSector, unsealOutput, sectorNum, minerID, ticket, unsealedCID, 0, uint64(unpaddedBytesAmount))
}
// UnsealRange
func UnsealRange(
proofType abi.RegisteredSealProof,
cacheDirPath string,
sealedSector *os.File,
unsealOutput *os.File,
sectorNum abi.SectorNumber,
minerID abi.ActorID,
ticket abi.SealRandomness,
unsealedCID cid.Cid,
unpaddedByteIndex uint64,
unpaddedBytesAmount uint64,
) error {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return err
}
proverID, err := toProverID(minerID)
if err != nil {
return err
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return err
}
sealedSectorFd := sealedSector.Fd()
defer runtime.KeepAlive(sealedSector)
unsealOutputFd := unsealOutput.Fd()
defer runtime.KeepAlive(unsealOutput)
ticketBytes := cgo.AsByteArray32(ticket)
return cgo.UnsealRange(
sp,
cgo.AsSliceRefUint8([]byte(cacheDirPath)),
int32(sealedSectorFd),
int32(unsealOutputFd),
uint64(sectorNum),
&proverID,
&ticketBytes,
&commD,
unpaddedByteIndex,
unpaddedBytesAmount,
)
}
func GenerateSDR(
proofType abi.RegisteredSealProof,
cacheDirPath string,
replicaId [32]byte,
) (err error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return err
}
replicaIdtBytes := cgo.AsByteArray32(replicaId[:])
return cgo.GenerateSDR(
sp,
cgo.AsSliceRefUint8([]byte(cacheDirPath)),
&replicaIdtBytes,
)
}
// GenerateWinningPoStSectorChallenge
func GenerateWinningPoStSectorChallenge(
proofType abi.RegisteredPoStProof,
minerID abi.ActorID,
randomness abi.PoStRandomness,
eligibleSectorsLen uint64,
) ([]uint64, error) {
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
pp, err := toFilRegisteredPoStProof(proofType)
if err != nil {
return nil, err
}
randomnessBytes := cgo.AsByteArray32(randomness)
return cgo.GenerateWinningPoStSectorChallenge(pp, &randomnessBytes, eligibleSectorsLen, &proverID)
}
// GenerateWinningPoSt
func GenerateWinningPoSt(
minerID abi.ActorID,
privateSectorInfo SortedPrivateSectorInfo,
randomness abi.PoStRandomness,
) ([]proof.PoStProof, error) {
filReplicas, cleanup, err := toFilPrivateReplicaInfos(privateSectorInfo.Values(), "winning")
if err != nil {
return nil, errors.Wrap(err, "failed to create private replica info array for FFI")
}
defer cleanup()
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
randomnessBytes := cgo.AsByteArray32(randomness)
rawProofs, err := cgo.GenerateWinningPoSt(&randomnessBytes, cgo.AsSliceRefPrivateReplicaInfo(filReplicas), &proverID)
if err != nil {
return nil, err
}
proofs, err := fromFilPoStProofs(rawProofs)
if err != nil {
return nil, err
}
return proofs, nil
}
// GenerateWindowPoSt
func GenerateWindowPoSt(
minerID abi.ActorID,
privateSectorInfo SortedPrivateSectorInfo,
randomness abi.PoStRandomness,
) ([]proof.PoStProof, []abi.SectorNumber, error) {
filReplicas, cleanup, err := toFilPrivateReplicaInfos(privateSectorInfo.Values(), "window")
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create private replica info array for FFI")
}
defer cleanup()
proverID, err := toProverID(minerID)
if err != nil {
return nil, nil, err
}
randomnessBytes := cgo.AsByteArray32(randomness)
proofsRaw, faultsRaw, err := cgo.GenerateWindowPoSt(&randomnessBytes, cgo.AsSliceRefPrivateReplicaInfo(filReplicas), &proverID)
if err != nil {
faultySectors := fromFilPoStFaultySectors(faultsRaw)
return nil, faultySectors, err
}
proofs, err := fromFilPoStProofs(proofsRaw)
if err != nil {
return nil, nil, err
}
return proofs, nil, nil
}
// GetGPUDevices produces a slice of strings, each representing the name of a
// detected GPU device.
func GetGPUDevices() ([]string, error) {
return cgo.GetGpuDevices()
}
// GetSealVersion
func GetSealVersion(proofType abi.RegisteredSealProof) (string, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return "", err
}
return cgo.GetSealVersion(sp)
}
// GetPoStVersion
func GetPoStVersion(proofType abi.RegisteredPoStProof) (string, error) {
pp, err := toFilRegisteredPoStProof(proofType)
if err != nil {
return "", err
}
return cgo.GetPoStVersion(pp)
}
func GetNumPartitionForFallbackPost(proofType abi.RegisteredPoStProof, numSectors uint) (uint, error) {
pp, err := toFilRegisteredPoStProof(proofType)
if err != nil {
return 0, err
}
return cgo.GetNumPartitionForFallbackPost(pp, numSectors)
}
// ClearCache
func ClearCache(sectorSize uint64, cacheDirPath string) error {
return cgo.ClearCache(sectorSize, cgo.AsSliceRefUint8([]byte(cacheDirPath)))
}
// ClearSyntheticProofs
func ClearSyntheticProofs(sectorSize uint64, cacheDirPath string) error {
return cgo.ClearSyntheticProofs(sectorSize, cgo.AsSliceRefUint8([]byte(cacheDirPath)))
}
func GenerateSynthProofs(
proofType abi.RegisteredSealProof,
sealedCID, unsealedCID cid.Cid,
cacheDirPath, replicaPath string,
sector_id abi.SectorNumber,
minerID abi.ActorID,
ticket []byte,
pieces []abi.PieceInfo,
) error {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return err
}
filPublicPieceInfos, err := toFilPublicPieceInfos(pieces)
if err != nil {
return err
}
commR, err := to32ByteCommR(sealedCID)
if err != nil {
return err
}
commD, err := to32ByteCommD(unsealedCID)
if err != nil {
return err
}
proverID, err := toProverID(minerID)
if err != nil {
return err
}
return cgo.GenerateSynthProofs(sp,
commR, commD,
cgo.AsSliceRefUint8([]byte(cacheDirPath)), cgo.AsSliceRefUint8([]byte(replicaPath)),
uint64(sector_id),
proverID, cgo.AsByteArray32(ticket),
cgo.AsSliceRefPublicPieceInfo(filPublicPieceInfos))
}
func FauxRep(proofType abi.RegisteredSealProof, cacheDirPath string, sealedSectorPath string) (cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return cid.Undef, err
}
rawCid, err := cgo.Fauxrep(sp, cgo.AsSliceRefUint8([]byte(cacheDirPath)), cgo.AsSliceRefUint8([]byte(sealedSectorPath)))
if err != nil {
return cid.Undef, err
}
return commcid.ReplicaCommitmentV1ToCID(rawCid)
}
func FauxRep2(proofType abi.RegisteredSealProof, cacheDirPath string, existingPAuxPath string) (cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return cid.Undef, err
}
rawCid, err := cgo.Fauxrep2(sp, cgo.AsSliceRefUint8([]byte(cacheDirPath)), cgo.AsSliceRefUint8([]byte(existingPAuxPath)))
if err != nil {
return cid.Undef, err
}
return commcid.ReplicaCommitmentV1ToCID(rawCid)
}
func toFilExistingPieceSizes(src []abi.UnpaddedPieceSize) []uint64 {
out := make([]uint64, len(src))
for idx := range out {
out[idx] = uint64(src[idx])
}
return out
}
func toFilPublicPieceInfos(src []abi.PieceInfo) ([]cgo.PublicPieceInfo, error) {
out := make([]cgo.PublicPieceInfo, len(src))
for idx := range out {
commP, err := to32ByteCommP(src[idx].PieceCID)
if err != nil {
return nil, err
}
out[idx] = cgo.NewPublicPieceInfo(uint64(src[idx].Size.Unpadded()), commP)
}
return out, nil
}
func toFilPublicReplicaInfosForWinningPoSt(src []proof.SectorInfo) ([]cgo.PublicReplicaInfo, error) {
out := make([]cgo.PublicReplicaInfo, len(src))
for idx := range out {
commR, err := to32ByteCommR(src[idx].SealedCID)
if err != nil {
return nil, err
}
p, err := src[idx].SealProof.RegisteredWinningPoStProof()
if err != nil {
return nil, err
}
pp, err := toFilRegisteredPoStProof(p)
if err != nil {
return nil, err
}
out[idx] = cgo.NewPublicReplicaInfo(pp, commR, uint64(src[idx].SectorNumber))
}
return out, nil
}
func toFilPublicReplicaInfosForWindowPoSt(src []proof.SectorInfo, postProofType abi.RegisteredPoStProof) ([]cgo.PublicReplicaInfo, error) {
out := make([]cgo.PublicReplicaInfo, len(src))
for idx := range out {
commR, err := to32ByteCommR(src[idx].SealedCID)
if err != nil {
return nil, err
}
pp, err := toFilRegisteredPoStProof(postProofType)
if err != nil {
return nil, err
}
out[idx] = cgo.NewPublicReplicaInfo(pp, commR, uint64(src[idx].SectorNumber))
}
return out, nil
}
func toFilPrivateReplicaInfo(src PrivateSectorInfo) (cgo.PrivateReplicaInfo, error) {
commR, err := to32ByteCommR(src.SealedCID)
if err != nil {
return cgo.PrivateReplicaInfo{}, err
}
pp, err := toFilRegisteredPoStProof(src.PoStProofType)
if err != nil {
return cgo.PrivateReplicaInfo{}, err
}
return cgo.NewPrivateReplicaInfo(
pp,
src.CacheDirPath,
commR,
src.SealedSectorPath,
uint64(src.SectorNumber),
), nil
}
func makeCleanerPRI(src []cgo.PrivateReplicaInfo, limit int) func() {
return func() {
for i := 0; i < limit; i++ {
src[i].Destroy()
}
}
}
func toFilPrivateReplicaInfos(src []PrivateSectorInfo, typ string) ([]cgo.PrivateReplicaInfo, func(), error) {
out := make([]cgo.PrivateReplicaInfo, len(src))
for idx := range out {
commR, err := to32ByteCommR(src[idx].SealedCID)
if err != nil {
makeCleanerPRI(out, idx)()
return nil, nil, err
}
pp, err := toFilRegisteredPoStProof(src[idx].PoStProofType)
if err != nil {
makeCleanerPRI(out, idx)()
return nil, nil, err
}
out[idx] = cgo.NewPrivateReplicaInfo(
pp,
src[idx].CacheDirPath,
commR,
src[idx].SealedSectorPath,
uint64(src[idx].SectorNumber),
)
}
return out, makeCleanerPRI(out, len(src)), nil
}
func fromFilPoStFaultySectors(ptr []uint64) []abi.SectorNumber {
snums := make([]abi.SectorNumber, len(ptr))
for i := range ptr {
snums[i] = abi.SectorNumber(ptr[i])
}
return snums
}
func fromFilPoStProofs(src []cgo.PoStProofGo) ([]proof.PoStProof, error) {
out := make([]proof.PoStProof, len(src))
for idx := range out {
pp, err := fromFilRegisteredPoStProof(src[idx].RegisteredProof)
if err != nil {
return nil, err
}
out[idx] = proof.PoStProof{
PoStProof: pp,
ProofBytes: src[idx].Proof,
}
}
return out, nil
}
func toFilPoStProofs(src []proof.PoStProof) ([]cgo.PoStProof, func(), error) {
out := make([]cgo.PoStProof, len(src))
for idx := range out {
pp, err := toFilRegisteredPoStProof(src[idx].PoStProof)
if err != nil {
makeCleanerPOST(out, idx)()
return nil, nil, err
}
out[idx] = cgo.NewPoStProof(pp, src[idx].ProofBytes)
}
return out, makeCleanerPOST(out, len(src)), nil
}
func makeCleanerPOST(src []cgo.PoStProof, limit int) func() {
return func() {
for i := 0; i < limit; i++ {
src[i].Destroy()
}
}
}
func toProverID(minerID abi.ActorID) (cgo.ByteArray32, error) {
maddr, err := address.NewIDAddress(uint64(minerID))
if err != nil {
return cgo.ByteArray32{}, errors.Wrap(err, "failed to convert ActorID to prover id ([32]byte) for FFI")
}
return cgo.AsByteArray32(maddr.Payload()), nil
}
func fromFilRegisteredPoStProof(p cgo.RegisteredPoStProof) (abi.RegisteredPoStProof, error) {
switch p {
case cgo.RegisteredPoStProofStackedDrgWinning2KiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning2KiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWinning8MiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning8MiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWinning512MiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning512MiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWinning32GiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning32GiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWinning64GiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning64GiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWindow2KiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow2KiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWindow8MiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow8MiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWindow512MiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow512MiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWindow32GiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow32GiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWindow64GiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow64GiBV1, nil
case cgo.RegisteredPoStProofStackedDrgWindow2KiBV1_1:
return abi.RegisteredPoStProof_StackedDrgWindow2KiBV1_1, nil
case cgo.RegisteredPoStProofStackedDrgWindow8MiBV1_1:
return abi.RegisteredPoStProof_StackedDrgWindow8MiBV1_1, nil
case cgo.RegisteredPoStProofStackedDrgWindow512MiBV1_1:
return abi.RegisteredPoStProof_StackedDrgWindow512MiBV1_1, nil
case cgo.RegisteredPoStProofStackedDrgWindow32GiBV1_1:
return abi.RegisteredPoStProof_StackedDrgWindow32GiBV1_1, nil
case cgo.RegisteredPoStProofStackedDrgWindow64GiBV1_1:
return abi.RegisteredPoStProof_StackedDrgWindow64GiBV1_1, nil
default:
return 0, errors.Errorf("no mapping to abi.RegisteredPoStProof value available for: %v", p)
}
}
func toFilRegisteredPoStProof(p abi.RegisteredPoStProof) (cgo.RegisteredPoStProof, error) {
switch p {
case abi.RegisteredPoStProof_StackedDrgWinning2KiBV1:
return cgo.RegisteredPoStProofStackedDrgWinning2KiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning8MiBV1:
return cgo.RegisteredPoStProofStackedDrgWinning8MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning512MiBV1:
return cgo.RegisteredPoStProofStackedDrgWinning512MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning32GiBV1:
return cgo.RegisteredPoStProofStackedDrgWinning32GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning64GiBV1:
return cgo.RegisteredPoStProofStackedDrgWinning64GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow2KiBV1:
return cgo.RegisteredPoStProofStackedDrgWindow2KiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow8MiBV1:
return cgo.RegisteredPoStProofStackedDrgWindow8MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow512MiBV1:
return cgo.RegisteredPoStProofStackedDrgWindow512MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow32GiBV1:
return cgo.RegisteredPoStProofStackedDrgWindow32GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow64GiBV1:
return cgo.RegisteredPoStProofStackedDrgWindow64GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow2KiBV1_1:
return cgo.RegisteredPoStProofStackedDrgWindow2KiBV1_1, nil
case abi.RegisteredPoStProof_StackedDrgWindow8MiBV1_1:
return cgo.RegisteredPoStProofStackedDrgWindow8MiBV1_1, nil
case abi.RegisteredPoStProof_StackedDrgWindow512MiBV1_1:
return cgo.RegisteredPoStProofStackedDrgWindow512MiBV1_1, nil
case abi.RegisteredPoStProof_StackedDrgWindow32GiBV1_1:
return cgo.RegisteredPoStProofStackedDrgWindow32GiBV1_1, nil
case abi.RegisteredPoStProof_StackedDrgWindow64GiBV1_1:
return cgo.RegisteredPoStProofStackedDrgWindow64GiBV1_1, nil
default: