Skip to content

Commit

Permalink
alternative verify post
Browse files Browse the repository at this point in the history
  • Loading branch information
laser committed May 4, 2020
1 parent a0014b1 commit 841ff78
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 21 deletions.
31 changes: 20 additions & 11 deletions generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,27 @@ func VerifyWinningPoSt(info abi.WinningPoStVerifyInfo) (bool, error) {
return false, err
}

var flattenedProofs string
proofTypes := make([]generated.FilRegisteredPoStProof, filPoStProofsLen)
proofChunkSizes := make([]uint16, filPoStProofsLen)

for idx := range filPoStProofs {
flattenedProofs = flattenedProofs + filPoStProofs[idx].ProofPtr
proofTypes[idx] = filPoStProofs[idx].RegisteredProof
proofChunkSizes[idx] = uint16(filPoStProofs[idx].ProofLen)
}

resp := generated.FilVerifyWinningPost(
to32ByteArray(info.Randomness),
proverID,
filPublicReplicaInfos,
filPublicReplicaInfosLen,
filPoStProofs,
filPoStProofsLen,
proverID,
proofTypes,
uint(len(proofTypes)),
proofChunkSizes,
uint(len(proofChunkSizes)),
flattenedProofs,
uint(len(flattenedProofs)),
)
resp.Deref()

Expand Down
47 changes: 40 additions & 7 deletions rust/src/proofs/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::slice::from_raw_parts;

use super::helpers::{c_to_rust_post_proofs, to_private_replica_info_map};
use super::types::*;
use crate::proofs::helpers::c_to_rust_post_proofs_meow;
use crate::util::api::init_log;

/// TODO: document
Expand Down Expand Up @@ -484,11 +485,15 @@ pub unsafe extern "C" fn fil_verify_seal(
#[no_mangle]
pub unsafe extern "C" fn fil_verify_winning_post(
randomness: fil_32ByteArray,
prover_id: fil_32ByteArray,
replicas_ptr: *const fil_PublicReplicaInfo,
replicas_len: libc::size_t,
proofs_ptr: *const fil_PoStProof,
proofs_len: libc::size_t,
prover_id: fil_32ByteArray,
proof_types_ptr: *const fil_RegisteredPoStProof,
proof_types_len: libc::size_t,
proof_chunk_sizes_ptr: *const u16,
proof_chunk_sizes_len: libc::size_t,
flattened_proofs_ptr: *const u8,
flattened_proofs_len: libc::size_t,
) -> *mut fil_VerifyWinningPoStResponse {
catch_panic_response(|| {
init_log();
Expand All @@ -500,7 +505,15 @@ pub unsafe extern "C" fn fil_verify_winning_post(
let convert = super::helpers::to_public_replica_info_map(replicas_ptr, replicas_len);

let result = convert.and_then(|replicas| {
let post_proofs = c_to_rust_post_proofs(proofs_ptr, proofs_len)?;
let post_proofs = c_to_rust_post_proofs_meow(
proof_types_ptr,
proof_types_len,
proof_chunk_sizes_ptr,
proof_chunk_sizes_len,
flattened_proofs_ptr,
flattened_proofs_len,
)?;

let proofs: Vec<u8> = post_proofs.iter().flat_map(|pp| pp.clone().proof).collect();

filecoin_proofs_api::post::verify_winning_post(
Expand Down Expand Up @@ -1594,13 +1607,33 @@ pub mod tests {
comm_r: (*resp_b2).comm_r,
}];

let mut ptypes: Vec<fil_RegisteredPoStProof> = Default::default();
let mut pchunks: Vec<u16> = Default::default();
let mut flattened: Vec<u8> = Default::default();

for pp in std::slice::from_raw_parts((*resp_h).proofs_ptr, (*resp_h).proofs_len).iter()
{
ptypes.push(pp.registered_proof.clone());

let mut proof: Vec<u8> = Default::default();
proof.extend_from_slice(std::slice::from_raw_parts(pp.proof_ptr, pp.proof_len));

flattened.append(&mut proof);

pchunks.push(pp.proof_len as u16);
}

let resp_i = fil_verify_winning_post(
randomness,
prover_id,
public_replicas.as_ptr(),
public_replicas.len(),
(*resp_h).proofs_ptr,
(*resp_h).proofs_len,
prover_id,
ptypes.as_ptr(),
ptypes.len(),
pchunks.as_ptr(),
pchunks.len(),
flattened.as_ptr(),
flattened.len(),
);

if (*resp_i).status_code != FCPResponseStatus::FCPNoError {
Expand Down
45 changes: 45 additions & 0 deletions rust/src/proofs/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,48 @@ pub unsafe fn c_to_rust_post_proofs(

Ok(out)
}

pub unsafe fn c_to_rust_post_proofs_meow(
proof_types_ptr: *const fil_RegisteredPoStProof,
proof_types_len: libc::size_t,
proof_chunk_sizes_ptr: *const u16,
proof_chunk_sizes_len: libc::size_t,
flattened_proofs_ptr: *const u8,
flattened_proofs_len: libc::size_t,
) -> Result<Vec<PoStProof>> {
ensure!(
!flattened_proofs_ptr.is_null(),
"flattened_proofs_ptr must not be null"
);

ensure!(
!proof_chunk_sizes_ptr.is_null(),
"proof_chunk_sizes_ptr must not be null"
);

ensure!(
!proof_types_ptr.is_null(),
"proof_types_ptr must not be null"
);

let flattened_proofs = from_raw_parts(flattened_proofs_ptr, flattened_proofs_len);
let chunk_sizes = from_raw_parts(proof_chunk_sizes_ptr, proof_chunk_sizes_len);
let proof_types = from_raw_parts(proof_types_ptr, proof_types_len);

let mut out: Vec<PoStProof> = Vec::with_capacity(chunk_sizes.len());

let mut offset: usize = 0;
for (idx, chunk_size) in chunk_sizes.iter().enumerate() {
let mut proof: Vec<u8> = Default::default();
proof.extend_from_slice(&flattened_proofs[offset..offset + *chunk_size as usize]);

out.push(PoStProof {
registered_proof: proof_types[idx].into(),
proof,
});

offset += *chunk_size as usize
}

Ok(out)
}

0 comments on commit 841ff78

Please sign in to comment.