Skip to content

Commit

Permalink
Add Go side bindings to partitioned WindowPoSt
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
  • Loading branch information
Jakub Sztandera committed Oct 25, 2021
1 parent d21dba8 commit fcdbf89
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
91 changes: 90 additions & 1 deletion distributed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
42 changes: 42 additions & 0 deletions proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit fcdbf89

Please sign in to comment.