From fcdbf89159916702688d038f223ef2ca32b64b51 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 25 Oct 2021 16:04:45 +0200 Subject: [PATCH] Add Go side bindings to partitioned WindowPoSt Signed-off-by: Jakub Sztandera --- distributed.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++- proofs.go | 42 +++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/distributed.go b/distributed.go index 71d94711..bfc24273 100644 --- a/distributed.go +++ b/distributed.go @@ -5,7 +5,7 @@ package ffi import ( "github.com/filecoin-project/filecoin-ffi/generated" "github.com/filecoin-project/go-state-types/abi" - "github.com/filecoin-project/specs-actors/actors/runtime/proof" + "github.com/filecoin-project/specs-actors/v5/actors/runtime/proof" "github.com/pkg/errors" ) @@ -174,3 +174,92 @@ func GenerateWindowPoStWithVanilla( return out, nil } + +type PartitionProof proof.PoStProof + +func GenerateSinglePartitionWindowPoStWithVanilla( + proofType abi.RegisteredPoStProof, + minerID abi.ActorID, + randomness abi.PoStRandomness, + proofs [][]byte, + partitionIndex uint, +) (*PartitionProof, error) { + pp, err := toFilRegisteredPoStProof(proofType) + if err != nil { + return nil, err + } + + proverID, err := toProverID(minerID) + if err != nil { + return nil, err + } + fproofs, discard := toVanillaProofs(proofs) + defer discard() + + resp := generated.FilGenerateSingleWindowPostWithVanilla( + pp, + to32ByteArray(randomness), + proverID, + fproofs, uint(len(proofs)), + partitionIndex, + ) + resp.Deref() + + defer generated.FilDestroyGenerateSingleWindowPostWithVanillaResponse(resp) + + if resp.StatusCode != generated.FCPResponseStatusFCPNoError { + return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy()) + } + + dpp, err := fromFilRegisteredPoStProof(resp.PartitionProof.RegisteredProof) + if err != nil { + return nil, err + } + + out := PartitionProof{ + PoStProof: dpp, + ProofBytes: copyBytes(resp.PartitionProof.ProofPtr, resp.PartitionProof.ProofLen), + } + + return &out, nil +} + +func MergeWindowPoStPartitionProofs( + proofType abi.RegisteredPoStProof, + partitionProofs []PartitionProof, +) (*proof.PoStProof, error) { + pp, err := toFilRegisteredPoStProof(proofType) + if err != nil { + return nil, err + } + + fproofs, discard, err := toPartitionProofs(partitionProofs) + defer discard() + if err != nil { + return nil, err + } + + resp := generated.FilMergeWindowPostPartitionProofs( + pp, + fproofs, uint(len(fproofs)), + ) + resp.Deref() + + defer generated.FilDestroyMergeWindowPostPartitionProofsResponse(resp) + + if resp.StatusCode != generated.FCPResponseStatusFCPNoError { + return nil, errors.New(generated.RawString(resp.ErrorMsg).Copy()) + } + + dpp, err := fromFilRegisteredPoStProof(resp.Proof.RegisteredProof) + if err != nil { + return nil, err + } + + out := proof.PoStProof{ + PoStProof: dpp, + ProofBytes: copyBytes(resp.Proof.ProofPtr, resp.Proof.ProofLen), + } + + return &out, nil +} diff --git a/proofs.go b/proofs.go index 2ed3f57f..f6c9fd48 100644 --- a/proofs.go +++ b/proofs.go @@ -748,6 +748,21 @@ func GetPoStVersion(proofType abi.RegisteredPoStProof) (string, error) { return generated.RawString(resp.StringVal).Copy(), nil } +func GetNumPartitionForFallbackPost(proofType abi.RegisteredPoStProof, numSectors uint) (uint, error) { + pp, err := toFilRegisteredPoStProof(proofType) + if err != nil { + return 0, err + } + resp := generated.FilGetNumPartitionForFallbackPost(pp, numSectors) + defer generated.FilDestroyGetNumPartitionForFallbackPostResponse(resp) + + if resp.StatusCode != generated.FCPResponseStatusFCPNoError { + return 0, errors.New(generated.RawString(resp.ErrorMsg).Copy()) + } + + return resp.NumPartition, nil +} + // ClearCache func ClearCache(sectorSize uint64, cacheDirPath string) error { resp := generated.FilClearCache(sectorSize, cacheDirPath) @@ -1158,3 +1173,30 @@ func toVanillaProofs(src [][]byte) ([]generated.FilVanillaProof, func()) { } } } + +func toPartitionProofs(src []PartitionProof) ([]generated.FilPartitionSnarkProof, func(), error) { + allocs := make([]AllocationManager, len(src)) + cleanup := func() { + for idx := range allocs { + allocs[idx].Free() + } + } + + out := make([]generated.FilPartitionSnarkProof, len(src)) + for idx := range out { + rp, err := toFilRegisteredPoStProof(src[idx].PoStProof) + if err != nil { + return nil, cleanup, err + } + + out[idx] = generated.FilPartitionSnarkProof{ + RegisteredProof: rp, + ProofLen: uint(len(src[idx].ProofBytes)), + ProofPtr: src[idx].ProofBytes, + } + + _, allocs[idx] = out[idx].PassRef() + } + + return out, cleanup, nil +}