Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PoSt partition batching #813

Merged
merged 2 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/polka-storage-proofs/src/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ pub fn generate_window_post<S: MerkleTreeTrait + 'static>(
randomness: randomness_safe,
prover_id: prover_id_safe,
sectors: pub_sectors,
// This is all-right.
// FallbackPoStCompound::prove overrides this parameter and sets it for each partition.
// As long as the verification logic also is compatible with this logic, it's good.
// k is local to each pair of the verification/generation batch.
// i.e. if we have 20 partitions to prove, two batches of 10 partitions, k = 0..10 in each batch.
k: None,
};

Expand Down
7 changes: 4 additions & 3 deletions pallets/proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub mod pallet {
pallets::ProofVerification,
proofs::{ProverId, PublicReplicaInfo, RegisteredPoStProof, RegisteredSealProof, Ticket},
sector::SectorNumber,
MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK, MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
MAX_POREP_PROOFS_PER_BLOCK, MAX_POST_PROOFS_PER_BLOCK, MAX_POST_PROOF_BYTES,
MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
};
use sp_std::collections::btree_map::BTreeMap;

Expand Down Expand Up @@ -191,7 +192,7 @@ pub mod pallet {
seed: Ticket,
proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_SEAL_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POREP_PROOFS_PER_BLOCK>,
>,
) -> DispatchResult {
log::info!(target: LOG_TARGET, "verifying PoRep proofs: {}", proofs.len());
Expand Down Expand Up @@ -249,7 +250,7 @@ pub mod pallet {
>,
proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_POST_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POST_PROOFS_PER_BLOCK>,
>,
) -> DispatchResult {
let replica_count = replicas.len();
Expand Down
4 changes: 2 additions & 2 deletions pallets/proofs/src/porep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use primitives::{
commitment::RawCommitment,
proofs::{ProverId, RegisteredSealProof, Ticket},
sector::SectorNumber,
MAX_PROOFS_PER_BLOCK,
MAX_POREP_PROOFS_PER_BLOCK,
};
use sha2::{Digest, Sha256};

Expand Down Expand Up @@ -156,7 +156,7 @@ impl ProofScheme {
seed: &Ticket,
groth_randomness: &Ticket,
vk: VerifyingKey<Bls12>,
proofs: BoundedVec<Proof<Bls12>, ConstU32<MAX_PROOFS_PER_BLOCK>>,
proofs: BoundedVec<Proof<Bls12>, ConstU32<MAX_POREP_PROOFS_PER_BLOCK>>,
) -> Result<(), ProofError> {
let comm_d_fr = fr32::bytes_into_fr(comm_d).map_err(|_| ProofError::Conversion)?;
let comm_r_fr = fr32::bytes_into_fr(comm_r).map_err(|_| ProofError::Conversion)?;
Expand Down
4 changes: 2 additions & 2 deletions pallets/proofs/src/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use primitives::{
commitment::RawCommitment,
proofs::{PublicReplicaInfo, RegisteredPoStProof, Ticket},
sector::SectorNumber,
MAX_PROOFS_PER_BLOCK, MAX_REPLICAS_PER_BLOCK, NODE_SIZE,
MAX_POST_PROOFS_PER_BLOCK, MAX_REPLICAS_PER_BLOCK, NODE_SIZE,
};
use sha2::{Digest, Sha256};

Expand Down Expand Up @@ -50,7 +50,7 @@ impl ProofScheme {
>,
groth_randomness: Ticket,
vk: VerifyingKey<Bls12>,
proofs: BoundedVec<Proof<Bls12>, ConstU32<MAX_PROOFS_PER_BLOCK>>,
proofs: BoundedVec<Proof<Bls12>, ConstU32<MAX_POST_PROOFS_PER_BLOCK>>,
) -> Result<(), ProofError> {
let randomness = fr32::bytes_into_fr(&randomness)
.map_err(|_| ProofError::Conversion)?
Expand Down
27 changes: 24 additions & 3 deletions pallets/proofs/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::str::FromStr;
use cid::Cid;
use codec::Decode;
use polka_storage_proofs::{Bls12, Proof};
use primitives::{MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK};
use primitives::{MAX_POREP_PROOFS_PER_BLOCK, MAX_POST_PROOFS_PER_BLOCK, MAX_POST_PROOF_BYTES};
use sp_core::ConstU32;
use sp_runtime::BoundedVec;

Expand All @@ -24,9 +24,30 @@ pub fn raw_commitment_from_hex(hex_str: &str) -> [u8; 32] {
}

/// Loads a proof file generated by polka-storage-provider-client either with porep or post command.
pub fn load_proof_file(
pub fn load_porep_proof_file(
path: &str,
) -> BoundedVec<BoundedVec<u8, ConstU32<MAX_POST_PROOF_BYTES>>, ConstU32<MAX_PROOFS_PER_BLOCK>> {
) -> BoundedVec<BoundedVec<u8, ConstU32<MAX_POST_PROOF_BYTES>>, ConstU32<MAX_POREP_PROOFS_PER_BLOCK>>
{
let proof_bytes = std::fs::read(path).unwrap();
let proofs: Vec<Proof<Bls12>> = Decode::decode(&mut &proof_bytes[..]).unwrap();
let mut bounded_proofs = BoundedVec::new();

for proof in proofs {
let mut bytes_regular = vec![0u8; Proof::<Bls12>::serialised_bytes()];
proof.into_bytes(bytes_regular.as_mut_slice()).unwrap();
bounded_proofs
.try_push(bytes_regular.try_into().unwrap())
.unwrap();
}

bounded_proofs
}

/// Loads a proof file generated by polka-storage-provider-client either with porep or post command.
pub fn load_post_proof_file(
path: &str,
) -> BoundedVec<BoundedVec<u8, ConstU32<MAX_POST_PROOF_BYTES>>, ConstU32<MAX_POST_PROOFS_PER_BLOCK>>
{
let proof_bytes = std::fs::read(path).unwrap();
let proofs: Vec<Proof<Bls12>> = Decode::decode(&mut &proof_bytes[..]).unwrap();
let mut bounded_proofs = BoundedVec::new();
Expand Down
4 changes: 2 additions & 2 deletions pallets/proofs/src/tests/porep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sp_runtime::BoundedVec;

use crate::{
mock::*,
tests::{load_proof_file, raw_commitment_from_hex, TEST_SEED},
tests::{load_porep_proof_file, raw_commitment_from_hex, TEST_SEED},
Error, PoRepVerifyingKeys,
};

Expand Down Expand Up @@ -126,7 +126,7 @@ fn porep_verification_for_1gib_succeeds() {
let comm_d = raw_commitment_from_hex(
"baga6ea4seaqcjdzgezdmdynwaoursai6zwafbxjmz7k4r3fnwwioizcwbq3zwki",
);
let proofs = load_proof_file("../../examples/1.sector.proof.porep.scale");
let proofs = load_porep_proof_file("../../examples/1.sector.proof.porep.scale");

assert_ok!(<ProofsModule as ProofVerification>::verify_porep(
prover_id, seal_proof, comm_r, comm_d, sector, ticket, seed, proofs
Expand Down
4 changes: 2 additions & 2 deletions pallets/proofs/src/tests/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sp_std::collections::btree_map::BTreeMap;

use crate::{
mock::*,
tests::{load_proof_file, raw_commitment_from_hex, TEST_SEED},
tests::{load_post_proof_file, raw_commitment_from_hex, TEST_SEED},
Error, PoStVerifyingKeys,
};

Expand Down Expand Up @@ -105,7 +105,7 @@ fn post_verification_for_1gib_succeeds() {
},
);

let proofs = load_proof_file("../../examples/1.sector.proof.post.scale");
let proofs = load_post_proof_file("../../examples/1.sector.proof.post.scale");

log::debug!("Verifying PoSt...");
assert_ok!(<ProofsModule as ProofVerification>::verify_post(
Expand Down
6 changes: 3 additions & 3 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub mod pallet {
proofs::{derive_prover_id, PublicReplicaInfo, RegisteredPoStProof},
randomness::{draw_randomness, AuthorVrfHistory, DomainSeparationTag},
sector::{ProveCommitSector, SectorNumber, SectorPreCommitInfo},
PartitionNumber, MAX_PARTITIONS_PER_DEADLINE, MAX_PROOFS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
MAX_SECTORS, MAX_SECTORS_PER_CALL,
PartitionNumber, MAX_PARTITIONS_PER_DEADLINE, MAX_POREP_PROOFS_PER_BLOCK,
MAX_SEAL_PROOF_BYTES, MAX_SECTORS, MAX_SECTORS_PER_CALL,
};
use scale_info::TypeInfo;
use sp_arithmetic::traits::Zero;
Expand Down Expand Up @@ -1641,7 +1641,7 @@ pub mod pallet {
precommit: &SectorPreCommitOnChainInfo<BalanceOf<T>, BlockNumberFor<T>>,
proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_SEAL_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POREP_PROOFS_PER_BLOCK>,
>,
) -> Result<(), DispatchError> {
let max_proof_size = precommit.info.seal_proof.proof_size();
Expand Down
4 changes: 2 additions & 2 deletions pallets/storage-provider/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frame_support::{
};
use primitives::{
proofs::RegisteredPoStProof, PartitionNumber, MAX_PARTITIONS_PER_DEADLINE,
MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK,
MAX_POST_PROOFS_PER_BLOCK, MAX_POST_PROOF_BYTES,
};
use scale_info::TypeInfo;
use sp_core::blake2_64;
Expand All @@ -30,7 +30,7 @@ pub struct SubmitWindowedPoStParams {
/// The partition being proven.
pub partitions: BoundedVec<PartitionNumber, ConstU32<MAX_PARTITIONS_PER_DEADLINE>>,
/// The proof submission.
pub proofs: BoundedVec<PoStProof, ConstU32<MAX_PROOFS_PER_BLOCK>>,
pub proofs: BoundedVec<PoStProof, ConstU32<MAX_POST_PROOFS_PER_BLOCK>>,
}

/// Error type for proof operations.
Expand Down
21 changes: 12 additions & 9 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ pub const NODE_SIZE: usize = 32;
/// ref: <https://github.com/filecoin-project/builtin-actors/blob/82d02e58f9ef456aeaf2a6c737562ac97b22b244/runtime/src/runtime/policy.rs#L283>
pub const MAX_PARTITIONS_PER_DEADLINE: u32 = 3000;

/// Establishes how many PoRep partitions can we verify in a single extrinsic.
/// It's determined by the Groth16 timing limitations.
/// Verification of 10 PoRep partitions (1GiB sector size) takes a whole block.
pub const MAX_POREP_PROOFS_PER_BLOCK: u32 = 10;

/// Establishes how many partitions can we verify in a single extrinsic.
/// It's determined by the timing limitations, storage provider have an upper limit of 3000 partitions per deadline.
/// With our current verification solution, it'll take around ~30 extrinsic calls to verify all of them.
/// Verification of a single proof takes around ~100ms, block time is ~6000ms.
/// This means 10 partitions will be verified in a ~1 sec.
// TODO(@th7nder,#659,27/12/2024): possibly speed it up
pub const MAX_PROOFS_PER_BLOCK: u32 = 10;
/// It's determined by the timing limitations, storage provider have an upper limit of 300 partitions per deadline.
/// With our current verification solution, it'll take around ~300 extrinsic calls to verify all of them.
/// Verification of a single proof takes around ~2s, takes a whole block.
pub const MAX_POST_PROOFS_PER_BLOCK: u32 = 1;

/// Max number of sectors.
/// <https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/runtime/src/runtime/policy.rs#L262>
Expand Down Expand Up @@ -57,15 +60,15 @@ pub const MAX_DEALS_FOR_ALL_SECTORS: u32 = MAX_SECTORS_PER_CALL * MAX_DEALS_PER_
pub const MAX_TERMINATIONS_PER_CALL: u32 = 32; // TODO(@jmg-duarte,25/07/2024): change for a better value

/// The maximum amount of sectors allowed in proofs and replicas.
/// This value is the absolute max, when the sector size is 32 GiB.
/// This value is the absolute max, when the sector size is 1 GiB.
/// Proofs and replicas are still dynamically checked for their size depending on the sector size.
///
/// References:
/// * Filecoin docs about PoSt: <https://spec.filecoin.io/algorithms/pos/post/#section-algorithms.pos.post.windowpost>
pub const MAX_SECTORS_PER_PROOF: u32 = 2349;
pub const MAX_SECTORS_PER_PROOF: u32 = 25;

/// The maximum amount of replicas that can be processed in a single block.
pub const MAX_REPLICAS_PER_BLOCK: u32 = MAX_SECTORS_PER_PROOF * MAX_PROOFS_PER_BLOCK;
pub const MAX_REPLICAS_PER_BLOCK: u32 = MAX_SECTORS_PER_PROOF * MAX_POST_PROOFS_PER_BLOCK;

/// The absolute maximum length, in bytes, a seal proof should be for the largest sector size.
/// NOTE: Taken the value from `StackedDRG32GiBV1`,
Expand Down
8 changes: 4 additions & 4 deletions primitives/src/pallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
proofs::{ProverId, PublicReplicaInfo, RegisteredPoStProof, RegisteredSealProof, Ticket},
sector::SectorNumber,
DealId, PartitionNumber, MAX_DEALS_PER_SECTOR, MAX_PARTITIONS_PER_DEADLINE,
MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK, MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
MAX_SECTORS, MAX_SECTORS_PER_CALL,
MAX_POREP_PROOFS_PER_BLOCK, MAX_POST_PROOFS_PER_BLOCK, MAX_POST_PROOF_BYTES,
MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES, MAX_SECTORS, MAX_SECTORS_PER_CALL,
};

pub trait StorageProviderValidation<AccountId> {
Expand All @@ -30,7 +30,7 @@ pub trait ProofVerification {
seed: Ticket,
proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_SEAL_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POREP_PROOFS_PER_BLOCK>,
>,
) -> DispatchResult;

Expand All @@ -44,7 +44,7 @@ pub trait ProofVerification {
>,
proof: BoundedVec<
BoundedVec<u8, ConstU32<MAX_POST_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POST_PROOFS_PER_BLOCK>,
>,
) -> DispatchResult;
}
Expand Down
7 changes: 4 additions & 3 deletions primitives/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ pub mod testing {
Ticket,
},
sector::SectorNumber,
MAX_POST_PROOF_BYTES, MAX_PROOFS_PER_BLOCK, MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
MAX_POREP_PROOFS_PER_BLOCK, MAX_POST_PROOFS_PER_BLOCK, MAX_POST_PROOF_BYTES,
MAX_REPLICAS_PER_BLOCK, MAX_SEAL_PROOF_BYTES,
};

/// A sentinel value for an invalid proof, everything else will be considered valid.
Expand All @@ -308,7 +309,7 @@ pub mod testing {
_seed: Ticket,
_proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_SEAL_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POREP_PROOFS_PER_BLOCK>,
>,
) -> sp_runtime::DispatchResult {
Ok(())
Expand All @@ -324,7 +325,7 @@ pub mod testing {
>,
proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_POST_PROOF_BYTES>>,
ConstU32<MAX_PROOFS_PER_BLOCK>,
ConstU32<MAX_POST_PROOFS_PER_BLOCK>,
>,
) -> sp_runtime::DispatchResult {
if *proofs[0] == INVALID_PROOF {
Expand Down
8 changes: 5 additions & 3 deletions primitives/src/sector/prove_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use scale_info::TypeInfo;
use sp_core::{ConstU32, RuntimeDebug};
use sp_runtime::BoundedVec;

use crate::{sector::SectorNumber, MAX_PROOFS_PER_BLOCK, MAX_SEAL_PROOF_BYTES};
use crate::{sector::SectorNumber, MAX_POREP_PROOFS_PER_BLOCK, MAX_SEAL_PROOF_BYTES};

/// Arguments passed into the `prove_commit_sector` extrinsic.
#[derive(Clone, RuntimeDebug, Decode, Encode, PartialEq, TypeInfo)]
pub struct ProveCommitSector {
pub sector_number: SectorNumber,
pub proofs:
BoundedVec<BoundedVec<u8, ConstU32<MAX_SEAL_PROOF_BYTES>>, ConstU32<MAX_PROOFS_PER_BLOCK>>,
pub proofs: BoundedVec<
BoundedVec<u8, ConstU32<MAX_SEAL_PROOF_BYTES>>,
ConstU32<MAX_POREP_PROOFS_PER_BLOCK>,
>,
}
5 changes: 2 additions & 3 deletions storage-provider/common/src/deadline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use primitives::{
proofs::{derive_prover_id, RegisteredPoStProof},
randomness::{draw_randomness, DomainSeparationTag},
sector::SectorNumber,
PartitionNumber, MAX_PROOFS_PER_BLOCK,
PartitionNumber, MAX_POST_PROOFS_PER_BLOCK,
};
use storagext::{
runtime::runtime_types::primitives::pallets::DeadlineInfo,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Deadline {
let chunked_partitions = deadline_state
.partitions
.into_iter()
.chunks(MAX_PROOFS_PER_BLOCK as usize);
.chunks(MAX_POST_PROOFS_PER_BLOCK as usize);

// Prepare the proofs for required partitions
let proving_futures = chunked_partitions
Expand Down Expand Up @@ -247,7 +247,6 @@ impl Deadline {
where
F: Fn(SectorNumber) -> Option<ProvenSector>,
{
// Get replicas for sectors part of the partitions
let replicas = partitions
.iter()
.flat_map(|(_id, state)| {
Expand Down
Binary file modified storagext/lib/artifacts/metadata.scale
Binary file not shown.
Loading