-
Notifications
You must be signed in to change notification settings - Fork 1
/
proofs.go
966 lines (776 loc) · 27.3 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
//+build cgo
package ffi
// #cgo LDFLAGS: ${SRCDIR}/libfilcrypto.a
// #cgo pkg-config: ${SRCDIR}/filcrypto.pc
// #include "./filcrypto.h"
import "C"
import (
"os"
"runtime"
"unsafe"
"github.com/filecoin-project/go-address"
commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
"github.com/filecoin-project/filecoin-ffi/generated"
)
// VerifySeal returns true if the sealing operation from which its inputs were
// derived was valid, and false if not.
func VerifySeal(info abi.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
}
resp := generated.FilVerifySeal(sp, commR, commD, proverID, to32ByteArray(info.Randomness), to32ByteArray(info.InteractiveRandomness), uint64(info.SectorID.Number), string(info.Proof), uint(len(info.Proof)))
resp.Deref()
defer generated.FilDestroyVerifySealResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return false, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return resp.IsValid, nil
}
// VerifyWinningPoSt returns true if the Winning PoSt-generation operation from which its
// inputs were derived was valid, and false if not.
func VerifyWinningPoSt(info abi.WinningPoStVerifyInfo) (bool, error) {
filPublicReplicaInfos, filPublicReplicaInfosLen, err := toFilPublicReplicaInfos(info.ChallengedSectors, "winning")
if err != nil {
return false, errors.Wrap(err, "failed to create public replica info array for FFI")
}
filPoStProofs, filPoStProofsLen, free, err := toFilPoStProofs(info.Proofs)
if err != nil {
return false, errors.Wrap(err, "failed to create PoSt proofs array for FFI")
}
defer free()
proverID, err := toProverID(info.Prover)
if err != nil {
return false, err
}
resp := generated.FilVerifyWinningPost(
to32ByteArray(info.Randomness),
filPublicReplicaInfos,
filPublicReplicaInfosLen,
filPoStProofs,
filPoStProofsLen,
proverID,
)
resp.Deref()
defer generated.FilDestroyVerifyWinningPostResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return false, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return resp.IsValid, nil
}
// VerifyWindowPoSt returns true if the Winning PoSt-generation operation from which its
// inputs were derived was valid, and false if not.
func VerifyWindowPoSt(info abi.WindowPoStVerifyInfo) (bool, error) {
filPublicReplicaInfos, filPublicReplicaInfosLen, err := toFilPublicReplicaInfos(info.ChallengedSectors, "window")
if err != nil {
return false, errors.Wrap(err, "failed to create public replica info array for FFI")
}
filPoStProofs, filPoStProofsLen, free, err := toFilPoStProofs(info.Proofs)
if err != nil {
return false, errors.Wrap(err, "failed to create PoSt proofs array for FFI")
}
defer free()
proverID, err := toProverID(info.Prover)
if err != nil {
return false, err
}
resp := generated.FilVerifyWindowPost(
to32ByteArray(info.Randomness),
filPublicReplicaInfos, filPublicReplicaInfosLen,
filPoStProofs, filPoStProofsLen,
proverID,
)
resp.Deref()
defer generated.FilDestroyVerifyWindowPostResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return false, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return resp.IsValid, nil
}
// 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, filPublicPieceInfosLen, err := toFilPublicPieceInfos(pieces)
if err != nil {
return cid.Undef, err
}
resp := generated.FilGenerateDataCommitment(sp, filPublicPieceInfos, filPublicPieceInfosLen)
resp.Deref()
defer generated.FilDestroyGenerateDataCommitmentResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return commcid.DataCommitmentV1ToCID(resp.CommD[:])
}
// 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 := generated.FilGeneratePieceCommitment(sp, int32(pieceFd), uint64(pieceSize))
resp.Deref()
defer generated.FilDestroyGeneratePieceCommitmentResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return commcid.PieceCommitmentV1ToCID(resp.CommP[:])
}
// 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, filExistingPieceSizesLen := toFilExistingPieceSizes(existingPieceSizes)
resp := generated.FilWriteWithAlignment(sp, int32(pieceFd), uint64(pieceBytes), int32(stagedSectorFd), filExistingPieceSizes, filExistingPieceSizesLen)
resp.Deref()
defer generated.FilDestroyWriteWithAlignmentResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return 0, 0, cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
commP, errCommpSize := commcid.PieceCommitmentV1ToCID(resp.CommP[:])
if errCommpSize != nil {
return 0, 0, cid.Undef, errCommpSize
}
return abi.UnpaddedPieceSize(resp.LeftAlignmentUnpadded), abi.UnpaddedPieceSize(resp.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)
resp := generated.FilWriteWithoutAlignment(sp, int32(pieceFd), uint64(pieceBytes), int32(stagedSectorFd))
resp.Deref()
defer generated.FilDestroyWriteWithoutAlignmentResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return 0, cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
commP, errCommpSize := commcid.PieceCommitmentV1ToCID(resp.CommP[:])
if errCommpSize != nil {
return 0, cid.Undef, errCommpSize
}
return abi.UnpaddedPieceSize(resp.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, filPublicPieceInfosLen, err := toFilPublicPieceInfos(pieces)
if err != nil {
return nil, err
}
resp := generated.FilSealPreCommitPhase1(sp, cacheDirPath, stagedSectorPath, sealedSectorPath, uint64(sectorNum), proverID, to32ByteArray(ticket), filPublicPieceInfos, filPublicPieceInfosLen)
resp.Deref()
defer generated.FilDestroySealPreCommitPhase1Response(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return []byte(toGoStringCopy(resp.SealPreCommitPhase1OutputPtr, resp.SealPreCommitPhase1OutputLen)), nil
}
// SealPreCommitPhase2
func SealPreCommitPhase2(
phase1Output []byte,
cacheDirPath string,
sealedSectorPath string,
) (sealedCID cid.Cid, unsealedCID cid.Cid, err error) {
resp := generated.FilSealPreCommitPhase2(string(phase1Output), uint(len(phase1Output)), cacheDirPath, sealedSectorPath)
resp.Deref()
defer generated.FilDestroySealPreCommitPhase2Response(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return cid.Undef, cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
commR, errCommrSize := commcid.ReplicaCommitmentV1ToCID(resp.CommR[:])
if errCommrSize != nil {
return cid.Undef, cid.Undef, errCommrSize
}
commD, errCommdSize := commcid.DataCommitmentV1ToCID(resp.CommD[:])
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, filPublicPieceInfosLen, err := toFilPublicPieceInfos(pieces)
if err != nil {
return nil, err
}
resp := generated.FilSealCommitPhase1(sp, commR, commD, cacheDirPath, sealedSectorPath, uint64(sectorNum), proverID, to32ByteArray(ticket), to32ByteArray(seed), filPublicPieceInfos, filPublicPieceInfosLen)
resp.Deref()
defer generated.FilDestroySealCommitPhase1Response(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return []byte(toGoStringCopy(resp.SealCommitPhase1OutputPtr, resp.SealCommitPhase1OutputLen)), nil
}
// SealCommitPhase2
func SealCommitPhase2(
phase1Output []byte,
sectorNum abi.SectorNumber,
minerID abi.ActorID,
) ([]byte, error) {
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
resp := generated.FilSealCommitPhase2(string(phase1Output), uint(len(phase1Output)), uint64(sectorNum), proverID)
resp.Deref()
defer generated.FilDestroySealCommitPhase2Response(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return []byte(toGoStringCopy(resp.ProofPtr, resp.ProofLen)), nil
}
// 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)
resp := generated.FilUnsealRange(sp, cacheDirPath, int32(sealedSectorFd), int32(unsealOutputFd), uint64(sectorNum), proverID, to32ByteArray(ticket), commD, unpaddedByteIndex, unpaddedBytesAmount)
resp.Deref()
defer generated.FilDestroyUnsealRangeResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return nil
}
// 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
}
resp := generated.FilGenerateWinningPostSectorChallenge(
pp, to32ByteArray(randomness),
eligibleSectorsLen, proverID,
)
resp.Deref()
resp.IdsPtr = make([]uint64, resp.IdsLen)
resp.Deref()
defer generated.FilDestroyGenerateWinningPostSectorChallenge(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
// copy from C memory space to Go
out := make([]uint64, resp.IdsLen)
for idx := range out {
out[idx] = resp.IdsPtr[idx]
}
return out, nil
}
// GenerateWinningPoSt
func GenerateWinningPoSt(
minerID abi.ActorID,
privateSectorInfo SortedPrivateSectorInfo,
randomness abi.PoStRandomness,
) ([]abi.PoStProof, error) {
filReplicas, filReplicasLen, free, err := toFilPrivateReplicaInfos(privateSectorInfo.Values(), "winning")
if err != nil {
return nil, errors.Wrap(err, "failed to create private replica info array for FFI")
}
defer free()
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
resp := generated.FilGenerateWinningPost(
to32ByteArray(randomness),
filReplicas, filReplicasLen,
proverID,
)
resp.Deref()
resp.ProofsPtr = make([]generated.FilPoStProof, resp.ProofsLen)
resp.Deref()
defer generated.FilDestroyGenerateWinningPostResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
proofs, err := fromFilPoStProofs(resp.ProofsPtr)
if err != nil {
return nil, err
}
return proofs, nil
}
// GenerateWindowPoSt
func GenerateWindowPoSt(
minerID abi.ActorID,
privateSectorInfo SortedPrivateSectorInfo,
randomness abi.PoStRandomness,
) ([]abi.PoStProof, error) {
filReplicas, filReplicasLen, free, err := toFilPrivateReplicaInfos(privateSectorInfo.Values(), "window")
if err != nil {
return nil, errors.Wrap(err, "failed to create private replica info array for FFI")
}
defer free()
proverID, err := toProverID(minerID)
if err != nil {
return nil, err
}
resp := generated.FilGenerateWindowPost(to32ByteArray(randomness), filReplicas, filReplicasLen, proverID)
resp.Deref()
resp.ProofsPtr = make([]generated.FilPoStProof, resp.ProofsLen)
resp.Deref()
defer generated.FilDestroyGenerateWindowPostResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
proofs, err := fromFilPoStProofs(resp.ProofsPtr)
if err != nil {
return nil, err
}
return proofs, nil
}
// GetGPUDevices produces a slice of strings, each representing the name of a
// detected GPU device.
func GetGPUDevices() ([]string, error) {
resp := generated.FilGetGpuDevices()
resp.Deref()
resp.DevicesPtr = make([]string, resp.DevicesLen)
resp.Deref()
defer generated.FilDestroyGpuDeviceResponse(resp)
out := make([]string, len(resp.DevicesPtr))
for idx := range out {
out[idx] = generated.RawString(resp.DevicesPtr[idx]).Copy()
}
return out, nil
}
// GetSealVersion
func GetSealVersion(proofType abi.RegisteredSealProof) (string, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return "", err
}
resp := generated.FilGetSealVersion(sp)
resp.Deref()
defer generated.FilDestroyStringResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return "", errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return generated.RawString(resp.StringVal).Copy(), nil
}
// GetPoStVersion
func GetPoStVersion(proofType abi.RegisteredPoStProof) (string, error) {
pp, err := toFilRegisteredPoStProof(proofType)
if err != nil {
return "", err
}
resp := generated.FilGetPostVersion(pp)
resp.Deref()
defer generated.FilDestroyStringResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return "", errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return generated.RawString(resp.StringVal).Copy(), nil
}
// ClearCache
func ClearCache(sectorSize uint64, cacheDirPath string) error {
resp := generated.FilClearCache(sectorSize, cacheDirPath)
resp.Deref()
defer generated.FilDestroyClearCacheResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return nil
}
func FauxRep(proofType abi.RegisteredSealProof, cacheDirPath string, sealedSectorPath string) (cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return cid.Undef, err
}
resp := generated.FilFauxrep(sp, cacheDirPath, sealedSectorPath)
resp.Deref()
defer generated.FilDestroyFauxrepResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return commcid.ReplicaCommitmentV1ToCID(resp.Commitment[:])
}
func FauxRep2(proofType abi.RegisteredSealProof, cacheDirPath string, existingPAuxPath string) (cid.Cid, error) {
sp, err := toFilRegisteredSealProof(proofType)
if err != nil {
return cid.Undef, err
}
resp := generated.FilFauxrep2(sp, cacheDirPath, existingPAuxPath)
resp.Deref()
defer generated.FilDestroyFauxrepResponse(resp)
if resp.StatusCode != generated.FCPResponseStatusFCPNoError {
return cid.Undef, errors.New(generated.RawString(resp.ErrorMsg).Copy())
}
return commcid.ReplicaCommitmentV1ToCID(resp.Commitment[:])
}
func toFilExistingPieceSizes(src []abi.UnpaddedPieceSize) ([]uint64, uint) {
out := make([]uint64, len(src))
for idx := range out {
out[idx] = uint64(src[idx])
}
return out, uint(len(out))
}
func toFilPublicPieceInfos(src []abi.PieceInfo) ([]generated.FilPublicPieceInfo, uint, error) {
out := make([]generated.FilPublicPieceInfo, len(src))
for idx := range out {
commP, err := to32ByteCommP(src[idx].PieceCID)
if err != nil {
return nil, 0, err
}
out[idx] = generated.FilPublicPieceInfo{
NumBytes: uint64(src[idx].Size.Unpadded()),
CommP: commP.Inner,
}
}
return out, uint(len(out)), nil
}
func toFilPublicReplicaInfos(src []abi.SectorInfo, typ string) ([]generated.FilPublicReplicaInfo, uint, error) {
out := make([]generated.FilPublicReplicaInfo, len(src))
for idx := range out {
commR, err := to32ByteCommR(src[idx].SealedCID)
if err != nil {
return nil, 0, err
}
out[idx] = generated.FilPublicReplicaInfo{
CommR: commR.Inner,
SectorId: uint64(src[idx].SectorNumber),
}
switch typ {
case "window":
p, err := src[idx].SealProof.RegisteredWindowPoStProof()
if err != nil {
return nil, 0, err
}
out[idx].RegisteredProof, err = toFilRegisteredPoStProof(p)
if err != nil {
return nil, 0, err
}
case "winning":
p, err := src[idx].SealProof.RegisteredWinningPoStProof()
if err != nil {
return nil, 0, err
}
out[idx].RegisteredProof, err = toFilRegisteredPoStProof(p)
if err != nil {
return nil, 0, err
}
}
}
return out, uint(len(out)), nil
}
func toFilPrivateReplicaInfos(src []PrivateSectorInfo, typ string) ([]generated.FilPrivateReplicaInfo, uint, func(), error) {
frees := make([]func(), len(src))
out := make([]generated.FilPrivateReplicaInfo, len(src))
for idx := range out {
commR, err := to32ByteCommR(src[idx].SealedCID)
if err != nil {
return nil, 0, func() {}, err
}
pp, err := toFilRegisteredPoStProof(src[idx].PoStProofType)
if err != nil {
return nil, 0, func() {}, err
}
out[idx] = generated.FilPrivateReplicaInfo{
RegisteredProof: pp,
CacheDirPath: src[idx].CacheDirPath,
CommR: commR.Inner,
ReplicaPath: src[idx].SealedSectorPath,
SectorId: uint64(src[idx].SectorNumber),
}
frees[idx] = out[idx].AllocateProxy()
}
return out, uint(len(out)), func() {
for idx := range frees {
frees[idx]()
}
}, nil
}
func fromFilPoStProofs(src []generated.FilPoStProof) ([]abi.PoStProof, error) {
out := make([]abi.PoStProof, len(src))
for idx := range out {
src[idx].Deref()
pp, err := fromFilRegisteredPoStProof(src[idx].RegisteredProof)
if err != nil {
return nil, err
}
out[idx] = abi.PoStProof{
PoStProof: pp,
ProofBytes: []byte(toGoStringCopy(src[idx].ProofPtr, src[idx].ProofLen)),
}
}
return out, nil
}
func toFilPoStProofs(src []abi.PoStProof) ([]generated.FilPoStProof, uint, func(), error) {
frees := make([]func(), len(src))
out := make([]generated.FilPoStProof, len(src))
for idx := range out {
pp, err := toFilRegisteredPoStProof(src[idx].PoStProof)
if err != nil {
return nil, 0, func() {}, err
}
out[idx] = generated.FilPoStProof{
RegisteredProof: pp,
ProofLen: uint(len(src[idx].ProofBytes)),
ProofPtr: string(src[idx].ProofBytes),
}
frees[idx] = out[idx].AllocateProxy()
}
return out, uint(len(out)), func() {
for idx := range frees {
frees[idx]()
}
}, nil
}
func to32ByteArray(in []byte) generated.Fil32ByteArray {
var out generated.Fil32ByteArray
copy(out.Inner[:], in)
return out
}
func toProverID(minerID abi.ActorID) (generated.Fil32ByteArray, error) {
maddr, err := address.NewIDAddress(uint64(minerID))
if err != nil {
return generated.Fil32ByteArray{}, errors.Wrap(err, "failed to convert ActorID to prover id ([32]byte) for FFI")
}
return to32ByteArray(maddr.Payload()), nil
}
func fromFilRegisteredPoStProof(p generated.FilRegisteredPoStProof) (abi.RegisteredPoStProof, error) {
switch p {
case generated.FilRegisteredPoStProofStackedDrgWinning2KiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning2KiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWinning8MiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning8MiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWinning512MiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning512MiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWinning32GiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning32GiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWinning64GiBV1:
return abi.RegisteredPoStProof_StackedDrgWinning64GiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWindow2KiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow2KiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWindow8MiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow8MiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWindow512MiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow512MiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWindow32GiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow32GiBV1, nil
case generated.FilRegisteredPoStProofStackedDrgWindow64GiBV1:
return abi.RegisteredPoStProof_StackedDrgWindow64GiBV1, nil
default:
return 0, errors.Errorf("no mapping to abi.RegisteredPoStProof value available for: %v", p)
}
}
func toFilRegisteredPoStProof(p abi.RegisteredPoStProof) (generated.FilRegisteredPoStProof, error) {
switch p {
case abi.RegisteredPoStProof_StackedDrgWinning2KiBV1:
return generated.FilRegisteredPoStProofStackedDrgWinning2KiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning8MiBV1:
return generated.FilRegisteredPoStProofStackedDrgWinning8MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning512MiBV1:
return generated.FilRegisteredPoStProofStackedDrgWinning512MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning32GiBV1:
return generated.FilRegisteredPoStProofStackedDrgWinning32GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWinning64GiBV1:
return generated.FilRegisteredPoStProofStackedDrgWinning64GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow2KiBV1:
return generated.FilRegisteredPoStProofStackedDrgWindow2KiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow8MiBV1:
return generated.FilRegisteredPoStProofStackedDrgWindow8MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow512MiBV1:
return generated.FilRegisteredPoStProofStackedDrgWindow512MiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow32GiBV1:
return generated.FilRegisteredPoStProofStackedDrgWindow32GiBV1, nil
case abi.RegisteredPoStProof_StackedDrgWindow64GiBV1:
return generated.FilRegisteredPoStProofStackedDrgWindow64GiBV1, nil
default:
return 0, errors.Errorf("no mapping to abi.RegisteredPoStProof value available for: %v", p)
}
}
func toFilRegisteredSealProof(p abi.RegisteredSealProof) (generated.FilRegisteredSealProof, error) {
switch p {
case abi.RegisteredSealProof_StackedDrg2KiBV1:
return generated.FilRegisteredSealProofStackedDrg2KiBV1, nil
case abi.RegisteredSealProof_StackedDrg8MiBV1:
return generated.FilRegisteredSealProofStackedDrg8MiBV1, nil
case abi.RegisteredSealProof_StackedDrg512MiBV1:
return generated.FilRegisteredSealProofStackedDrg512MiBV1, nil
case abi.RegisteredSealProof_StackedDrg32GiBV1:
return generated.FilRegisteredSealProofStackedDrg32GiBV1, nil
case abi.RegisteredSealProof_StackedDrg64GiBV1:
return generated.FilRegisteredSealProofStackedDrg64GiBV1, nil
default:
return 0, errors.Errorf("no mapping to C.FFIRegisteredSealProof value available for: %v", p)
}
}
func to32ByteCommD(unsealedCID cid.Cid) (generated.Fil32ByteArray, error) {
commD, err := commcid.CIDToDataCommitmentV1(unsealedCID)
if err != nil {
return generated.Fil32ByteArray{}, errors.Wrap(err, "failed to transform sealed CID to CommD")
}
return to32ByteArray(commD), nil
}
func to32ByteCommR(sealedCID cid.Cid) (generated.Fil32ByteArray, error) {
commD, err := commcid.CIDToReplicaCommitmentV1(sealedCID)
if err != nil {
return generated.Fil32ByteArray{}, errors.Wrap(err, "failed to transform sealed CID to CommR")
}
return to32ByteArray(commD), nil
}
func to32ByteCommP(pieceCID cid.Cid) (generated.Fil32ByteArray, error) {
commP, err := commcid.CIDToPieceCommitmentV1(pieceCID)
if err != nil {
return generated.Fil32ByteArray{}, errors.Wrap(err, "failed to transform sealed CID to CommP")
}
return to32ByteArray(commP), nil
}
func toGoStringCopy(raw string, rawLen uint) string {
h := (*stringHeader)(unsafe.Pointer(&raw))
return C.GoStringN((*C.char)(h.Data), C.int(rawLen))
}
type stringHeader struct {
Data unsafe.Pointer
Len int
}