From 88971bb0112fd7896feeaeeaca4937e26d10fa0c Mon Sep 17 00:00:00 2001 From: th7nder Date: Tue, 19 Nov 2024 05:34:06 -0300 Subject: [PATCH 01/22] feat: submit_windowed_post --- pallets/storage-provider/src/lib.rs | 45 +++++++++++++++---- pallets/storage-provider/src/proofs.rs | 4 +- pallets/storage-provider/src/tests/mod.rs | 8 +++- .../src/tests/submit_windowed_post.rs | 7 +-- primitives/proofs/src/randomness.rs | 2 + 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 3ba72c802..af82f0d94 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -59,7 +59,7 @@ pub mod pallet { use primitives_proofs::{ derive_prover_id, randomness::{draw_randomness, DomainSeparationTag}, - Market, ProofVerification, Randomness, RegisteredPoStProof, SectorNumber, + Market, ProofVerification, PublicReplicaInfo, Randomness, RegisteredPoStProof, SectorNumber, StorageProviderValidation, MAX_SEAL_PROOF_BYTES, MAX_SECTORS_PER_CALL, }; use scale_info::TypeInfo; @@ -658,8 +658,6 @@ pub mod pallet { } /// The SP uses this extrinsic to submit their Proof-of-Spacetime. - /// - /// * Currently the proof is considered valid when `proof.len() > 0`. pub fn submit_windowed_post( origin: OriginFor, windowed_post: SubmitWindowedPoStParams, @@ -686,7 +684,7 @@ pub mod pallet { // Ensure a valid proof size // TODO(@jmg-duarte,#91,19/8/24): correctly check the length // https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/miner/src/lib.rs#L565-L573 - ensure!(windowed_post.proof.proof_bytes.len() > 0, { + ensure!(windowed_post.proof.proof_bytes.len() <= primitives_proofs::MAX_POST_PROOF_BYTES as usize, { log::error!("submit_window_post: invalid proof size"); Error::::PoStProofInvalid }); @@ -734,8 +732,6 @@ pub mod pallet { // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/curiosrc/window/compute_do.go#L68-L72 // * https://github.com/filecoin-project/curio/blob/45373f7fc0431e41f987ad348df7ae6e67beaff9/tasks/window/compute_do.go#L71-L75 - // TODO(@aidan46, #91, 2024-07-03): Validate the proof after research is done - // record sector as proven let all_sectors = sp.sectors.clone(); let deadlines = sp.get_deadlines_mut(); @@ -743,15 +739,46 @@ pub mod pallet { .record_proven( windowed_post.deadline as usize, &all_sectors, - windowed_post.partitions, + windowed_post.partitions.clone(), ) .map_err(|e| Error::::GeneralPalletError(e))?; - // Store new storage provider state - StorageProviders::::set(owner.clone(), Some(sp)); + + + // TODO(@th7nder,#592, 19/11/2024): handle faulty and recovered sectors, we don't take them into account now + let deadlines = &sp.deadlines; + let mut replicas = BoundedBTreeMap::new(); + // Take all the sectors that were assigned to all of the partitions + for partition in &windowed_post.partitions { + let sectors = &deadlines.due[windowed_post.deadline as usize].partitions[partition].sectors; + for sector_number in sectors { + let sector_info = sp.sectors.get(sector_number).unwrap(); + let _ = replicas.try_insert(*sector_number, PublicReplicaInfo { + comm_r: sector_info.unsealed_cid[..32].try_into().expect("cid to be 32 bytes") + }).unwrap(); + } + } + let entropy = owner.encode(); + let randomness = get_randomness::( + DomainSeparationTag::WindowedPoStChallengeSeed, + current_deadline.challenge, + &entropy, + )?; + + // Questions: + // * How do we know the partition the sector was assigned to (as a caller) ? + // * Can we handle proving multiple partitions in `verify_post?` + T::ProofVerification::verify_post( + windowed_post.proof.post_proof, + randomness, + replicas, + windowed_post.proof.proof_bytes + )?; log::debug!(target: LOG_TARGET, "submit_windowed_post: proof recorded"); + // Store new storage provider state + StorageProviders::::set(owner.clone(), Some(sp)); Self::deposit_event(Event::ValidPoStSubmitted { owner }); Ok(()) diff --git a/pallets/storage-provider/src/proofs.rs b/pallets/storage-provider/src/proofs.rs index 1cb519a47..e34eae98a 100644 --- a/pallets/storage-provider/src/proofs.rs +++ b/pallets/storage-provider/src/proofs.rs @@ -3,7 +3,7 @@ use frame_support::{ pallet_prelude::{ConstU32, RuntimeDebug}, sp_runtime::BoundedVec, }; -use primitives_proofs::RegisteredPoStProof; +use primitives_proofs::{RegisteredPoStProof, MAX_POST_PROOF_BYTES}; use scale_info::TypeInfo; use sp_core::blake2_64; @@ -15,7 +15,7 @@ pub struct PoStProof { /// The proof type, currently only one type is supported. pub post_proof: RegisteredPoStProof, /// The proof submission, to be checked in the storage provider pallet. - pub proof_bytes: BoundedVec>, // Arbitrary length + pub proof_bytes: BoundedVec>, } /// Parameter type for `submit_windowed_post` extrinsic. diff --git a/pallets/storage-provider/src/tests/mod.rs b/pallets/storage-provider/src/tests/mod.rs index f034ae414..92be6437e 100644 --- a/pallets/storage-provider/src/tests/mod.rs +++ b/pallets/storage-provider/src/tests/mod.rs @@ -80,6 +80,9 @@ impl pallet_balances::Config for Test { type AccountStore = System; } + +pub const INVALID_PROOF: [u8; 2] = [0xd, 0xe]; + /// This is dummy proofs pallet implementation. All proofs are accepted as valid pub struct DummyProofsVerification; impl ProofVerification for DummyProofsVerification { @@ -104,8 +107,11 @@ impl ProofVerification for DummyProofsVerification { PublicReplicaInfo, ConstU32, >, - _proof: BoundedVec>, + proof: BoundedVec>, ) -> sp_runtime::DispatchResult { + if *proof == INVALID_PROOF { + return Err(sp_runtime::DispatchError::Other("invalid proof")) + } Ok(()) } } diff --git a/pallets/storage-provider/src/tests/submit_windowed_post.rs b/pallets/storage-provider/src/tests/submit_windowed_post.rs index 37bed583c..0e66aa244 100644 --- a/pallets/storage-provider/src/tests/submit_windowed_post.rs +++ b/pallets/storage-provider/src/tests/submit_windowed_post.rs @@ -12,7 +12,7 @@ use crate::{ account, declare_faults::setup_sp_with_many_sectors_multiple_partitions, events, new_test_ext, register_storage_provider, run_to_block, DealProposalBuilder, Market, RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, StorageProvider, - SubmitWindowedPoStBuilder, System, Test, ALICE, BOB, + SubmitWindowedPoStBuilder, System, Test, ALICE, BOB, INVALID_PROOF }, Config, }; @@ -349,7 +349,8 @@ fn fail_windowed_post_wrong_signature() { // Build window post proof let windowed_post = SubmitWindowedPoStBuilder::default() - .proof_bytes(vec![]) // Wrong proof + .partition(0) + .proof_bytes(INVALID_PROOF.into()) .build(); // Run extrinsic @@ -358,7 +359,7 @@ fn fail_windowed_post_wrong_signature() { RuntimeOrigin::signed(account(ALICE)), windowed_post, ), - Error::::PoStProofInvalid + DispatchError::Other("invalid proof") ); }); } diff --git a/primitives/proofs/src/randomness.rs b/primitives/proofs/src/randomness.rs index bc6f603a0..7ccbd9c85 100644 --- a/primitives/proofs/src/randomness.rs +++ b/primitives/proofs/src/randomness.rs @@ -8,6 +8,7 @@ use sp_core::blake2_256; pub enum DomainSeparationTag { SealRandomness, InteractiveSealChallengeSeed, + WindowedPoStChallengeSeed, } impl DomainSeparationTag { @@ -16,6 +17,7 @@ impl DomainSeparationTag { let value: i64 = match self { DomainSeparationTag::SealRandomness => 1, DomainSeparationTag::InteractiveSealChallengeSeed => 2, + DomainSeparationTag::WindowedPoStChallengeSeed => 3, }; value.to_be_bytes() From dee813ef8b2d93e04da7035f889680b09c976785 Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 05:55:37 -0300 Subject: [PATCH 02/22] fix: set porep-verifying-key in rpc publish --- examples/rpc_publish.sh | 1 + pallets/storage-provider/src/lib.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/examples/rpc_publish.sh b/examples/rpc_publish.sh index ffab76a4f..a512fd7ea 100755 --- a/examples/rpc_publish.sh +++ b/examples/rpc_publish.sh @@ -37,6 +37,7 @@ wait # Register the SP target/release/storagext-cli --sr25519-key "//Charlie" storage-provider register "peer_id" +target/release/storagext-cli --sr25519-key "//Charlie" proofs set-porep-verifying-key @2KiB.porep.vk.scale DEAL_JSON=$( jq -n \ diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index af82f0d94..61f05b23b 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -744,6 +744,12 @@ pub mod pallet { .map_err(|e| Error::::GeneralPalletError(e))?; + // TODO: + // - run rpc publish + // - submit windowed post transaction to see whether it fails and sets the correct replicas + // - generate a windowed post proof for this shit (by hand) + // - generate it automatically in the pipeline + // TODO(@th7nder,#592, 19/11/2024): handle faulty and recovered sectors, we don't take them into account now let deadlines = &sp.deadlines; @@ -765,6 +771,11 @@ pub mod pallet { &entropy, )?; + log::debug!(target: LOG_TARGET, "submit_windowed_post: index {:?} challenge {:?}, replicas: {:?}", + current_deadline.idx, + current_deadline.challenge, + replicas + ); // Questions: // * How do we know the partition the sector was assigned to (as a caller) ? // * Can we handle proving multiple partitions in `verify_post?` From 7a400a63a576b3bb48021f73d9aaea839c4d16c7 Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 06:40:24 -0300 Subject: [PATCH 03/22] refactor: clean-up unwraps and expects --- pallets/storage-provider/src/lib.rs | 99 +++++++++++-------- pallets/storage-provider/src/tests/mod.rs | 3 +- .../src/tests/submit_windowed_post.rs | 2 +- primitives/proofs/src/traits.rs | 4 +- 4 files changed, 61 insertions(+), 47 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 61f05b23b..a5d0ea693 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -59,8 +59,8 @@ pub mod pallet { use primitives_proofs::{ derive_prover_id, randomness::{draw_randomness, DomainSeparationTag}, - Market, ProofVerification, PublicReplicaInfo, Randomness, RegisteredPoStProof, SectorNumber, - StorageProviderValidation, MAX_SEAL_PROOF_BYTES, MAX_SECTORS_PER_CALL, + Market, ProofVerification, PublicReplicaInfo, Randomness, RegisteredPoStProof, + SectorNumber, StorageProviderValidation, MAX_SEAL_PROOF_BYTES, MAX_SECTORS_PER_CALL, }; use scale_info::TypeInfo; use sp_arithmetic::traits::Zero; @@ -316,6 +316,7 @@ pub mod pallet { /// extrinsic but is not registered as one. StorageProviderNotFound, /// Emitted when trying to access an invalid sector. + InvalidPartition, InvalidSector, /// Emitted when submitting an invalid proof type. InvalidProofType, @@ -358,6 +359,7 @@ pub mod pallet { CouldNotTerminateDeals, /// Tried to terminate sectors that are not mutable. CannotTerminateImmutableDeadline, + TooManyReplicas, /// Inner pallet errors GeneralPalletError(crate::error::GeneralPalletError), } @@ -684,10 +686,14 @@ pub mod pallet { // Ensure a valid proof size // TODO(@jmg-duarte,#91,19/8/24): correctly check the length // https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/miner/src/lib.rs#L565-L573 - ensure!(windowed_post.proof.proof_bytes.len() <= primitives_proofs::MAX_POST_PROOF_BYTES as usize, { - log::error!("submit_window_post: invalid proof size"); - Error::::PoStProofInvalid - }); + ensure!( + windowed_post.proof.proof_bytes.len() + <= primitives_proofs::MAX_POST_PROOF_BYTES as usize, + { + log::error!("submit_window_post: invalid proof size"); + Error::::PoStProofInvalid + } + ); // If the proving period is in the future, we can't submit a proof yet // Related issue: https://github.com/filecoin-project/specs-actors/issues/946 @@ -712,26 +718,6 @@ pub mod pallet { Self::validate_deadline(¤t_deadline, &windowed_post)?; - // The `chain_commit_epoch` should be `current_deadline.challenge` as per: - // - // These issues that were filed against the original implementation: - // * https://github.com/filecoin-project/specs-actors/issues/1094 - // * https://github.com/filecoin-project/specs-actors/issues/1376 - // - // The Go actors have this note: - // https://github.com/filecoin-project/specs-actors/blob/985cd0fa04578e262d68e0ef196f17df6f2434f2/actors/builtin/miner/miner_actor.go#L329-L332 - // - // The fact that both Go and Rust actor implementations use the deadline challenge: - // * https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/miner/tests/miner_actor_test_wpost.rs#L99-L492 - // * https://github.com/filecoin-project/specs-actors/blob/985cd0fa04578e262d68e0ef196f17df6f2434f2/actors/test/commit_post_test.go#L204-L215 - // * https://github.com/filecoin-project/specs-actors/blob/985cd0fa04578e262d68e0ef196f17df6f2434f2/actors/test/terminate_sectors_scenario_test.go#L117-L128 - // - // Further supported by the fact that Lotus and Curio (Lotus' replacement) don't use - // the ChainCommitEpoch variable from the SubmitWindowedPostParams - // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/storage/wdpost/wdpost_run.go#L334-L338 - // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/curiosrc/window/compute_do.go#L68-L72 - // * https://github.com/filecoin-project/curio/blob/45373f7fc0431e41f987ad348df7ae6e67beaff9/tasks/window/compute_do.go#L71-L75 - // record sector as proven let all_sectors = sp.sectors.clone(); let deadlines = sp.get_deadlines_mut(); @@ -743,47 +729,76 @@ pub mod pallet { ) .map_err(|e| Error::::GeneralPalletError(e))?; - // TODO: - // - run rpc publish - // - submit windowed post transaction to see whether it fails and sets the correct replicas // - generate a windowed post proof for this shit (by hand) // - generate it automatically in the pipeline - // TODO(@th7nder,#592, 19/11/2024): handle faulty and recovered sectors, we don't take them into account now let deadlines = &sp.deadlines; let mut replicas = BoundedBTreeMap::new(); // Take all the sectors that were assigned to all of the partitions for partition in &windowed_post.partitions { - let sectors = &deadlines.due[windowed_post.deadline as usize].partitions[partition].sectors; + // Deadline is validated by `Self::validate_deadline`, so we're sure it can be used as an index. + let deadline = &deadlines.due[windowed_post.deadline as usize]; + let sectors = &deadline + .partitions + .get(&partition) + .ok_or(Error::::InvalidPartition)? + .sectors; for sector_number in sectors { - let sector_info = sp.sectors.get(sector_number).unwrap(); - let _ = replicas.try_insert(*sector_number, PublicReplicaInfo { - comm_r: sector_info.unsealed_cid[..32].try_into().expect("cid to be 32 bytes") - }).unwrap(); + // Sectors stored in the Storage Provider struct should be consistently stored, without breaking invariants. + let sector_info = &sp.sectors[sector_number]; + let _ = replicas + .try_insert( + *sector_number, + PublicReplicaInfo { + comm_r: sector_info.unsealed_cid[..] + .try_into() + .expect("CID on-chain to be stored as 32 bytes RawCommitment"), + }, + ).map_err(|_| Error::::TooManyReplicas); } } + + log::debug!(target: LOG_TARGET, "submit_windowed_post: index {:?} challenge {:?}, replicas: {:?}", + current_deadline.idx, + current_deadline.challenge, + replicas + ); + let entropy = owner.encode(); + // The `chain_commit_epoch` should be `current_deadline.challenge` as per: + // + // These issues that were filed against the original implementation: + // * https://github.com/filecoin-project/specs-actors/issues/1094 + // * https://github.com/filecoin-project/specs-actors/issues/1376 + // + // The Go actors have this note: + // https://github.com/filecoin-project/specs-actors/blob/985cd0fa04578e262d68e0ef196f17df6f2434f2/actors/builtin/miner/miner_actor.go#L329-L332 + // + // The fact that both Go and Rust actor implementations use the deadline challenge: + // * https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/miner/tests/miner_actor_test_wpost.rs#L99-L492 + // * https://github.com/filecoin-project/specs-actors/blob/985cd0fa04578e262d68e0ef196f17df6f2434f2/actors/test/commit_post_test.go#L204-L215 + // * https://github.com/filecoin-project/specs-actors/blob/985cd0fa04578e262d68e0ef196f17df6f2434f2/actors/test/terminate_sectors_scenario_test.go#L117-L128 + // + // Further supported by the fact that Lotus and Curio (Lotus' replacement) don't use + // the ChainCommitEpoch variable from the SubmitWindowedPostParams + // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/storage/wdpost/wdpost_run.go#L334-L338 + // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/curiosrc/window/compute_do.go#L68-L72 + // * https://github.com/filecoin-project/curio/blob/45373f7fc0431e41f987ad348df7ae6e67beaff9/tasks/window/compute_do.go#L71-L75 let randomness = get_randomness::( DomainSeparationTag::WindowedPoStChallengeSeed, current_deadline.challenge, &entropy, )?; - log::debug!(target: LOG_TARGET, "submit_windowed_post: index {:?} challenge {:?}, replicas: {:?}", - current_deadline.idx, - current_deadline.challenge, - replicas - ); // Questions: // * How do we know the partition the sector was assigned to (as a caller) ? - // * Can we handle proving multiple partitions in `verify_post?` T::ProofVerification::verify_post( windowed_post.proof.post_proof, randomness, replicas, - windowed_post.proof.proof_bytes + windowed_post.proof.proof_bytes, )?; log::debug!(target: LOG_TARGET, "submit_windowed_post: proof recorded"); diff --git a/pallets/storage-provider/src/tests/mod.rs b/pallets/storage-provider/src/tests/mod.rs index 92be6437e..4e566b19d 100644 --- a/pallets/storage-provider/src/tests/mod.rs +++ b/pallets/storage-provider/src/tests/mod.rs @@ -80,7 +80,6 @@ impl pallet_balances::Config for Test { type AccountStore = System; } - pub const INVALID_PROOF: [u8; 2] = [0xd, 0xe]; /// This is dummy proofs pallet implementation. All proofs are accepted as valid @@ -110,7 +109,7 @@ impl ProofVerification for DummyProofsVerification { proof: BoundedVec>, ) -> sp_runtime::DispatchResult { if *proof == INVALID_PROOF { - return Err(sp_runtime::DispatchError::Other("invalid proof")) + return Err(sp_runtime::DispatchError::Other("invalid proof")); } Ok(()) } diff --git a/pallets/storage-provider/src/tests/submit_windowed_post.rs b/pallets/storage-provider/src/tests/submit_windowed_post.rs index 0e66aa244..1d005bb27 100644 --- a/pallets/storage-provider/src/tests/submit_windowed_post.rs +++ b/pallets/storage-provider/src/tests/submit_windowed_post.rs @@ -12,7 +12,7 @@ use crate::{ account, declare_faults::setup_sp_with_many_sectors_multiple_partitions, events, new_test_ext, register_storage_provider, run_to_block, DealProposalBuilder, Market, RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, StorageProvider, - SubmitWindowedPoStBuilder, System, Test, ALICE, BOB, INVALID_PROOF + SubmitWindowedPoStBuilder, System, Test, ALICE, BOB, INVALID_PROOF, }, Config, }; diff --git a/primitives/proofs/src/traits.rs b/primitives/proofs/src/traits.rs index 3e39bb85a..31dbae5cf 100644 --- a/primitives/proofs/src/traits.rs +++ b/primitives/proofs/src/traits.rs @@ -8,8 +8,8 @@ use crate::types::{ DealId, ProverId, RawCommitment, RegisteredPoStProof, RegisteredSealProof, SectorNumber, Ticket, }; -/// Size of a CID with a 512-bit multihash — i.e. the default CID size. -pub const CID_SIZE_IN_BYTES: u32 = 64; +/// Size of a CID with a 256-bit multihash — i.e. the size of CommR/CommD/CommP +pub const CID_SIZE_IN_BYTES: u32 = 32; /// Number of Sectors that can be provided in a single extrinsics call. /// Required for BoundedVec. From 69ca9e82fe353ab0a5e16ab4645bb353dc0888d6 Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 06:41:43 -0300 Subject: [PATCH 04/22] fix: set post verifying key in rpc publish --- examples/rpc_publish.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/rpc_publish.sh b/examples/rpc_publish.sh index a512fd7ea..ac1351e5d 100755 --- a/examples/rpc_publish.sh +++ b/examples/rpc_publish.sh @@ -38,6 +38,7 @@ wait # Register the SP target/release/storagext-cli --sr25519-key "//Charlie" storage-provider register "peer_id" target/release/storagext-cli --sr25519-key "//Charlie" proofs set-porep-verifying-key @2KiB.porep.vk.scale +target/release/storagext-cli --sr25519-key "//Charlie" proofs set-post-verifying-key @2KiB.post.vk.scale DEAL_JSON=$( jq -n \ From 82c9bd1c25fe3447224e3b6daaf9617b4ad6773a Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 08:53:23 -0300 Subject: [PATCH 05/22] feat: add set_post_verifying_key to storagext cli --- storagext/cli/src/cmd/proofs.rs | 40 ++++++++++++++++++++++++++++- storagext/lib/src/clients/proofs.rs | 33 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/storagext/cli/src/cmd/proofs.rs b/storagext/cli/src/cmd/proofs.rs index a4c4442b1..7695e10c7 100644 --- a/storagext/cli/src/cmd/proofs.rs +++ b/storagext/cli/src/cmd/proofs.rs @@ -18,6 +18,12 @@ pub(crate) enum ProofsCommand { #[arg(value_parser = VerifyingKey::value_parser)] verifying_key: VerifyingKey, }, + /// Set PoRep verifying key + SetPoStVerifyingKey { + /// Verifying key. Either hex encoded as string or, if prepended with @, a path to a file containing the key's raw bytes. + #[arg(value_parser = VerifyingKey::value_parser)] + verifying_key: VerifyingKey, + }, } impl ProofsCommand { @@ -52,6 +58,19 @@ impl ProofsCommand { wait_for_finalization, ) .await? + }, + ProofsCommand::SetPoStVerifyingKey { verifying_key } => { + let Some(account_keypair) = account_keypair else { + return Err(missing_keypair_error::().into()); + }; + + Self::set_post_verifying_key( + client, + account_keypair, + verifying_key, + wait_for_finalization, + ) + .await? } }; @@ -98,7 +117,26 @@ impl ProofsCommand { .set_porep_verifying_key(&account_keypair, verifying_key, wait_for_finalization) .await? .inspect(|result| { - tracing::debug!("[{}] Key successfully set", result.hash); + tracing::debug!("[{}] PoRep Key successfully set", result.hash); + }); + + Ok(submission_result) + } + + async fn set_post_verifying_key( + client: Client, + account_keypair: MultiPairSigner, + verifying_key: VerifyingKey, + wait_for_finalization: bool, + ) -> Result>, subxt::Error> + where + Client: ProofsClientExt, + { + let submission_result = client + .set_post_verifying_key(&account_keypair, verifying_key, wait_for_finalization) + .await? + .inspect(|result| { + tracing::debug!("[{}] PoSt Key successfully set", result.hash); }); Ok(submission_result) diff --git a/storagext/lib/src/clients/proofs.rs b/storagext/lib/src/clients/proofs.rs index b83aee5b1..41fca6702 100644 --- a/storagext/lib/src/clients/proofs.rs +++ b/storagext/lib/src/clients/proofs.rs @@ -16,6 +16,15 @@ pub trait ProofsClientExt { ) -> impl Future>, subxt::Error>> where Keypair: subxt::tx::Signer; + + fn set_post_verifying_key( + &self, + account_keypair: &Keypair, + verifying_key: VerifyingKey, + wait_for_finalization: bool, + ) -> impl Future>, subxt::Error>> + where + Keypair: subxt::tx::Signer; } impl ProofsClientExt for crate::runtime::client::Client { @@ -42,4 +51,28 @@ impl ProofsClientExt for crate::runtime::client::Client { self.traced_submission(&payload, account_keypair, wait_for_finalization) .await } + + #[tracing::instrument( + level = "debug", + skip_all, + fields( + address = account_keypair.account_id().to_ss58check(), + ) + )] + async fn set_post_verifying_key( + &self, + account_keypair: &Keypair, + verifying_key: VerifyingKey, + wait_for_finalization: bool, + ) -> Result>, subxt::Error> + where + Keypair: subxt::tx::Signer, + { + let payload = runtime::tx() + .proofs() + .set_post_verifying_key(verifying_key.into()); + + self.traced_submission(&payload, account_keypair, wait_for_finalization) + .await + } } From bf92567de612a65b6095d39e8427a4b203fae208 Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 09:28:12 -0300 Subject: [PATCH 06/22] fix: set_post_verifying_key --- storagext/cli/src/cmd/proofs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/storagext/cli/src/cmd/proofs.rs b/storagext/cli/src/cmd/proofs.rs index 7695e10c7..857c15e76 100644 --- a/storagext/cli/src/cmd/proofs.rs +++ b/storagext/cli/src/cmd/proofs.rs @@ -19,7 +19,7 @@ pub(crate) enum ProofsCommand { verifying_key: VerifyingKey, }, /// Set PoRep verifying key - SetPoStVerifyingKey { + SetPostVerifyingKey { /// Verifying key. Either hex encoded as string or, if prepended with @, a path to a file containing the key's raw bytes. #[arg(value_parser = VerifyingKey::value_parser)] verifying_key: VerifyingKey, @@ -59,7 +59,7 @@ impl ProofsCommand { ) .await? }, - ProofsCommand::SetPoStVerifyingKey { verifying_key } => { + ProofsCommand::SetPostVerifyingKey { verifying_key } => { let Some(account_keypair) = account_keypair else { return Err(missing_keypair_error::().into()); }; From 299325458295e2c8f8af721a5c934bb6a2d43ff5 Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 10:03:15 -0300 Subject: [PATCH 07/22] fix: restore 64 bytes --- primitives/proofs/src/traits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/proofs/src/traits.rs b/primitives/proofs/src/traits.rs index 31dbae5cf..9f37a4a7f 100644 --- a/primitives/proofs/src/traits.rs +++ b/primitives/proofs/src/traits.rs @@ -8,8 +8,8 @@ use crate::types::{ DealId, ProverId, RawCommitment, RegisteredPoStProof, RegisteredSealProof, SectorNumber, Ticket, }; -/// Size of a CID with a 256-bit multihash — i.e. the size of CommR/CommD/CommP -pub const CID_SIZE_IN_BYTES: u32 = 32; +/// Size of a CID with a 512-bit multihash — i.e. the size of CommR/CommD/CommP +pub const CID_SIZE_IN_BYTES: u32 = 64; /// Number of Sectors that can be provided in a single extrinsics call. /// Required for BoundedVec. From d8ddfa71b9c9bd6f92267ca6a41d3c7bc02d8fd7 Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 10:06:43 -0300 Subject: [PATCH 08/22] fix(pallet-storage-provider): commr --- pallets/storage-provider/src/lib.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index a5d0ea693..a26cc78be 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -748,13 +748,12 @@ pub mod pallet { for sector_number in sectors { // Sectors stored in the Storage Provider struct should be consistently stored, without breaking invariants. let sector_info = &sp.sectors[sector_number]; + let comm_r = Commitment::::from_cid_bytes(§or_info.sealed_cid).unwrap(); let _ = replicas .try_insert( *sector_number, PublicReplicaInfo { - comm_r: sector_info.unsealed_cid[..] - .try_into() - .expect("CID on-chain to be stored as 32 bytes RawCommitment"), + comm_r: comm_r.raw(), }, ).map_err(|_| Error::::TooManyReplicas); } @@ -766,7 +765,7 @@ pub mod pallet { replicas ); - let entropy = owner.encode(); + // let entropy = owner.encode(); // The `chain_commit_epoch` should be `current_deadline.challenge` as per: // // These issues that were filed against the original implementation: @@ -786,11 +785,13 @@ pub mod pallet { // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/storage/wdpost/wdpost_run.go#L334-L338 // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/curiosrc/window/compute_do.go#L68-L72 // * https://github.com/filecoin-project/curio/blob/45373f7fc0431e41f987ad348df7ae6e67beaff9/tasks/window/compute_do.go#L71-L75 - let randomness = get_randomness::( - DomainSeparationTag::WindowedPoStChallengeSeed, - current_deadline.challenge, - &entropy, - )?; + // let randomness = get_randomness::( + // DomainSeparationTag::WindowedPoStChallengeSeed, + // current_deadline.challenge, + // &entropy, + // )?; + + let randomness = [1u8; 32]; // Questions: // * How do we know the partition the sector was assigned to (as a caller) ? From 9fb24f93a1b7295602524a8680929075605ce1ed Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 10:37:54 -0300 Subject: [PATCH 09/22] fix: make it work with fixed randomness --- .../client/src/commands/proofs.rs | 21 +++++++++++++++---- .../server/src/pipeline/mod.rs | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index f320f85f9..b302fd332 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -122,6 +122,10 @@ pub enum ProofsCommand { #[arg(short, long)] /// Directory where the PoSt proof will be stored. Defaults to the current directory. output_path: Option, + #[arg(short, long)] + sector_number: u64, + #[arg(short, long)] + randomness: Option, }, } @@ -405,6 +409,8 @@ impl ProofsCommand { replica_path, comm_r, output_path, + sector_number, + randomness, } => { let Some(signer) = Option::::from(signer_key) else { return Err(UtilsCommandError::NoSigner)?; @@ -413,8 +419,15 @@ impl ProofsCommand { // Those are hardcoded for the showcase only. // They should come from Storage Provider Node, precommits and other information. - let sector_id = 77.into(); - let randomness = [1u8; 32]; + + // how do I get the same randomness as on-chain? :( + // need to be able to calculate deadline info and challenge + // I'll hardcode it for now + let randomness: [u8; 32] = if let Some(randomness) = randomness { + hex::decode(randomness).unwrap().try_into().unwrap() + } else { + [1u8; 32] + }; let output_path = if let Some(output_path) = output_path { output_path @@ -424,7 +437,7 @@ impl ProofsCommand { let (proof_scale_filename, mut proof_scale_file) = file_with_extension( &output_path, - format!("{}", sector_id).as_str(), + format!("{}", sector_number).as_str(), POST_PROOF_EXT, )?; @@ -432,7 +445,7 @@ impl ProofsCommand { cid::Cid::from_str(&comm_r).map_err(|_| UtilsCommandError::CommRError)?; let replicas = vec![ReplicaInfo { - sector_id, + sector_id: sector_number, comm_r: comm_r .hash() .digest() diff --git a/cli/polka-storage-provider/server/src/pipeline/mod.rs b/cli/polka-storage-provider/server/src/pipeline/mod.rs index 483151505..da46d2ddf 100644 --- a/cli/polka-storage-provider/server/src/pipeline/mod.rs +++ b/cli/polka-storage-provider/server/src/pipeline/mod.rs @@ -325,7 +325,7 @@ async fn precommit( }) }; let sealing_output = sealing_handle.await??; - tracing::info!("Created sector's replica: {:?}", sealing_output); + tracing::info!("Created sector's replica, CommD: {}, CommR: {}", sealing_output.comm_d.cid(), sealing_output.comm_r.cid()); let sealing_output_commr = Commitment::::from(sealing_output.comm_r); let sealing_output_commd = Commitment::::from(sealing_output.comm_d); From 99d2dfc6590e07c35863e90758c7c7392352effd Mon Sep 17 00:00:00 2001 From: th7nder Date: Wed, 20 Nov 2024 11:52:04 -0300 Subject: [PATCH 10/22] fix: add expect instead of unwrap: --- pallets/storage-provider/src/lib.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index a26cc78be..4a9489846 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -748,7 +748,8 @@ pub mod pallet { for sector_number in sectors { // Sectors stored in the Storage Provider struct should be consistently stored, without breaking invariants. let sector_info = &sp.sectors[sector_number]; - let comm_r = Commitment::::from_cid_bytes(§or_info.sealed_cid).unwrap(); + let comm_r = Commitment::::from_cid_bytes(§or_info.sealed_cid) + .expect("CommR to be validated on pre-commit"); let _ = replicas .try_insert( *sector_number, @@ -785,11 +786,11 @@ pub mod pallet { // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/storage/wdpost/wdpost_run.go#L334-L338 // * https://github.com/filecoin-project/lotus/blob/4f70204342ce83671a7a261147a18865f1618967/curiosrc/window/compute_do.go#L68-L72 // * https://github.com/filecoin-project/curio/blob/45373f7fc0431e41f987ad348df7ae6e67beaff9/tasks/window/compute_do.go#L71-L75 - // let randomness = get_randomness::( - // DomainSeparationTag::WindowedPoStChallengeSeed, - // current_deadline.challenge, - // &entropy, - // )?; + let randomness = get_randomness::( + DomainSeparationTag::WindowedPoStChallengeSeed, + current_deadline.challenge, + &entropy, + )?; let randomness = [1u8; 32]; From 478f19d33799b49f030309bd95e5ae35e35ee4fc Mon Sep 17 00:00:00 2001 From: th7nder Date: Thu, 21 Nov 2024 09:10:10 -0300 Subject: [PATCH 11/22] try to implement runtime apis --- .../client/src/commands/proofs.rs | 4 ++++ pallets/storage-provider/src/lib.rs | 10 +--------- primitives/proofs/Cargo.toml | 3 ++- primitives/proofs/src/lib.rs | 9 ++++++++- primitives/proofs/src/traits.rs | 2 +- runtime/Cargo.toml | 3 +++ storagext/lib/Cargo.toml | 2 +- storagext/lib/src/clients/storage_provider.rs | 10 ++++++++++ 8 files changed, 30 insertions(+), 13 deletions(-) diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index b302fd332..4fa717ed6 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -424,6 +424,10 @@ impl ProofsCommand { // need to be able to calculate deadline info and challenge // I'll hardcode it for now let randomness: [u8; 32] = if let Some(randomness) = randomness { + // 1. We need to know deadline_index as an input, which was assigned on ProveCommit, treat it as an index + // 2. We can impl_runtime_apis for current_deadline info, if it matches the index, if not, just wait another proving window. + // 3. Then get randomness for this current_deadline info block + // 4. Make the proof work. hex::decode(randomness).unwrap().try_into().unwrap() } else { [1u8; 32] diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 4a9489846..30225d7a3 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -729,10 +729,6 @@ pub mod pallet { ) .map_err(|e| Error::::GeneralPalletError(e))?; - // TODO: - // - generate a windowed post proof for this shit (by hand) - // - generate it automatically in the pipeline - // TODO(@th7nder,#592, 19/11/2024): handle faulty and recovered sectors, we don't take them into account now let deadlines = &sp.deadlines; let mut replicas = BoundedBTreeMap::new(); @@ -766,7 +762,7 @@ pub mod pallet { replicas ); - // let entropy = owner.encode(); + let entropy = owner.encode(); // The `chain_commit_epoch` should be `current_deadline.challenge` as per: // // These issues that were filed against the original implementation: @@ -792,10 +788,6 @@ pub mod pallet { &entropy, )?; - let randomness = [1u8; 32]; - - // Questions: - // * How do we know the partition the sector was assigned to (as a caller) ? T::ProofVerification::verify_post( windowed_post.proof.post_proof, randomness, diff --git a/primitives/proofs/Cargo.toml b/primitives/proofs/Cargo.toml index df9e1f702..7139af8a8 100644 --- a/primitives/proofs/Cargo.toml +++ b/primitives/proofs/Cargo.toml @@ -14,6 +14,7 @@ scale-decode = { workspace = true, default-features = false, features = ["derive scale-encode = { workspace = true, default-features = false, features = ["derive"] } scale-info = { workspace = true, default-features = false, features = ["derive"] } +sp-api = { workspace = true, default-features = false } sp-core = { workspace = true, default-features = false } sp-runtime = { workspace = true, default-features = false } sp-std = { workspace = true, default-features = false } @@ -30,4 +31,4 @@ workspace = true [features] clap = ["dep:clap", "std"] default = ["std"] -std = ["cid/scale-codec", "codec/std", "scale-info/std", "sp-core/std", "sp-runtime/std", "sp-std/std"] +std = ["cid/scale-codec", "codec/std", "scale-info/std", "ap-api/std", "sp-core/std", "sp-runtime/std", "sp-std/std"] diff --git a/primitives/proofs/src/lib.rs b/primitives/proofs/src/lib.rs index 55f0d1cd2..cf2aab1e8 100644 --- a/primitives/proofs/src/lib.rs +++ b/primitives/proofs/src/lib.rs @@ -4,7 +4,7 @@ pub mod randomness; mod traits; mod types; -use codec::Encode; +use codec::{Codec, Encode}; pub use traits::*; pub use types::*; @@ -23,3 +23,10 @@ where encoded[31] &= 0x3f; encoded } + +sp_api::decl_runtime_apis! { + pub trait StorageProviderApi where AccountId: Codec + { + fn current_deadline(storage_provider: AccountId) -> u64; + } +} \ No newline at end of file diff --git a/primitives/proofs/src/traits.rs b/primitives/proofs/src/traits.rs index 9f37a4a7f..eb248dde0 100644 --- a/primitives/proofs/src/traits.rs +++ b/primitives/proofs/src/traits.rs @@ -163,4 +163,4 @@ pub struct ActiveDeal { pub piece_cid: Cid, /// Real size of the data pub piece_size: u64, -} +} \ No newline at end of file diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 800f20769..709189fd7 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -26,6 +26,8 @@ pallet-proofs = { workspace = true, default-features = false } pallet-randomness = { workspace = true, default-features = false } pallet-storage-provider = { workspace = true, default-features = false } +primitives-proofs = { workspace = true, default-features = false } + codec = { workspace = true, default-features = false, features = ["chain-error", "derive"] } docify = { workspace = true } hex-literal = { workspace = true, optional = true } @@ -128,6 +130,7 @@ std = [ "parachains-common/std", "polkadot-parachain-primitives/std", "polkadot-runtime-common/std", + "primitives-proofs/std", "scale-info/std", "sp-api/std", "sp-block-builder/std", diff --git a/storagext/lib/Cargo.toml b/storagext/lib/Cargo.toml index 19569cd63..150d13550 100644 --- a/storagext/lib/Cargo.toml +++ b/storagext/lib/Cargo.toml @@ -20,7 +20,7 @@ frame-support = { workspace = true, features = ["std"] } futures.workspace = true hex = { workspace = true, features = ["serde"] } itertools = { workspace = true } -primitives-proofs = { workspace = true, features = ["serde"] } +primitives-proofs = { workspace = true, features = ["std", "serde"] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } sha2 = { workspace = true } diff --git a/storagext/lib/src/clients/storage_provider.rs b/storagext/lib/src/clients/storage_provider.rs index 790740d7a..7d1d3b8d4 100644 --- a/storagext/lib/src/clients/storage_provider.rs +++ b/storagext/lib/src/clients/storage_provider.rs @@ -98,9 +98,19 @@ pub trait StorageProviderClientExt { fn retrieve_registered_storage_providers( &self, ) -> impl Future, subxt::Error>>; + + + fn current_deadline(&self, account_id: &AccountId32) -> impl Future>; } impl StorageProviderClientExt for crate::runtime::client::Client { + async fn current_deadline(&self, account_id: &AccountId32) -> Result { + // let _ = runtime::apis().storage_provider_api().current_deadline(account_id); + // let _ = runtime::apis().account_nonce_api().account_nonce(account_id); + + Ok(0) + } + #[tracing::instrument( level = "debug", skip_all, From c19c47428c8866bc593e5e0ed41f4707d0a98fc7 Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Mon, 25 Nov 2024 12:30:41 +0100 Subject: [PATCH 12/22] fix: remove randomness param --- cli/polka-storage-provider/client/Cargo.toml | 1 - .../client/src/commands/proofs.rs | 19 +------------------ 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/cli/polka-storage-provider/client/Cargo.toml b/cli/polka-storage-provider/client/Cargo.toml index 0a5657353..302d4126d 100644 --- a/cli/polka-storage-provider/client/Cargo.toml +++ b/cli/polka-storage-provider/client/Cargo.toml @@ -21,7 +21,6 @@ bls12_381 = { workspace = true } cid = { workspace = true, features = ["std"] } clap = { workspace = true, features = ["derive"] } codec = { workspace = true } -hex = { workspace = true } jsonrpsee = { workspace = true, features = ["http-client", "macros", "server", "ws-client"] } sc-cli = { workspace = true } serde = { workspace = true } diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index 4fa717ed6..74ef791f6 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -124,8 +124,6 @@ pub enum ProofsCommand { output_path: Option, #[arg(short, long)] sector_number: u64, - #[arg(short, long)] - randomness: Option, }, } @@ -417,22 +415,7 @@ impl ProofsCommand { }; let prover_id = derive_prover_id(signer.account_id()); - // Those are hardcoded for the showcase only. - // They should come from Storage Provider Node, precommits and other information. - - // how do I get the same randomness as on-chain? :( - // need to be able to calculate deadline info and challenge - // I'll hardcode it for now - let randomness: [u8; 32] = if let Some(randomness) = randomness { - // 1. We need to know deadline_index as an input, which was assigned on ProveCommit, treat it as an index - // 2. We can impl_runtime_apis for current_deadline info, if it matches the index, if not, just wait another proving window. - // 3. Then get randomness for this current_deadline info block - // 4. Make the proof work. - hex::decode(randomness).unwrap().try_into().unwrap() - } else { - [1u8; 32] - }; - + let randomness: [u8; 32] = [1u8; 32]; let output_path = if let Some(output_path) = output_path { output_path } else { From 5fdd3979ed40fd81fc1995bf9379b0c4588054f6 Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Mon, 25 Nov 2024 12:50:01 +0100 Subject: [PATCH 13/22] fix: impl example runtime api --- Cargo.lock | 2 ++ runtime/src/lib.rs | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 2e66792b0..2014b38d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12943,6 +12943,7 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", "polkadot-runtime-common 17.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", + "primitives-proofs", "scale-info", "smallvec", "sp-api 34.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", @@ -14912,6 +14913,7 @@ dependencies = [ "scale-info", "serde", "serde_json", + "sp-api 34.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", "sp-core 34.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", "sp-runtime 39.0.1", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-stable2409-2)", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 0516ca889..4abc3fbf9 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -412,6 +412,12 @@ impl Runtime { } impl_runtime_apis! { + impl primitives_proofs::StorageProviderApi for Runtime { + fn current_deadline(storage_provider: AccountId) -> u64 { + 0 + } + } + impl sp_consensus_aura::AuraApi for Runtime { fn slot_duration() -> sp_consensus_aura::SlotDuration { Runtime::impl_slot_duration() From 4a7bece0963248bdd646e9678ea5936b3bb2a26b Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Mon, 25 Nov 2024 18:50:45 +0100 Subject: [PATCH 14/22] feat: implement current_deadline runtime api --- .../client/src/commands/proofs.rs | 5 ++-- .../server/src/pipeline/mod.rs | 6 +++- pallets/storage-provider/Cargo.toml | 1 + pallets/storage-provider/src/lib.rs | 28 ++++++++++++++++- primitives/proofs/Cargo.toml | 2 +- primitives/proofs/src/lib.rs | 19 ++++++++++-- primitives/proofs/src/traits.rs | 2 +- runtime/src/lib.rs | 4 +-- storagext/cli/src/cmd/proofs.rs | 2 +- storagext/lib/src/clients/storage_provider.rs | 30 ++++++++++++++----- 10 files changed, 78 insertions(+), 21 deletions(-) diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index 74ef791f6..2fd4ae73e 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -123,7 +123,7 @@ pub enum ProofsCommand { /// Directory where the PoSt proof will be stored. Defaults to the current directory. output_path: Option, #[arg(short, long)] - sector_number: u64, + sector_number: u32, }, } @@ -408,7 +408,6 @@ impl ProofsCommand { comm_r, output_path, sector_number, - randomness, } => { let Some(signer) = Option::::from(signer_key) else { return Err(UtilsCommandError::NoSigner)?; @@ -432,7 +431,7 @@ impl ProofsCommand { cid::Cid::from_str(&comm_r).map_err(|_| UtilsCommandError::CommRError)?; let replicas = vec![ReplicaInfo { - sector_id: sector_number, + sector_id: sector_number.try_into().unwrap(), comm_r: comm_r .hash() .digest() diff --git a/cli/polka-storage-provider/server/src/pipeline/mod.rs b/cli/polka-storage-provider/server/src/pipeline/mod.rs index da46d2ddf..f3fb11ad3 100644 --- a/cli/polka-storage-provider/server/src/pipeline/mod.rs +++ b/cli/polka-storage-provider/server/src/pipeline/mod.rs @@ -325,7 +325,11 @@ async fn precommit( }) }; let sealing_output = sealing_handle.await??; - tracing::info!("Created sector's replica, CommD: {}, CommR: {}", sealing_output.comm_d.cid(), sealing_output.comm_r.cid()); + tracing::info!( + "Created sector's replica, CommD: {}, CommR: {}", + sealing_output.comm_d.cid(), + sealing_output.comm_r.cid() + ); let sealing_output_commr = Commitment::::from(sealing_output.comm_r); let sealing_output_commd = Commitment::::from(sealing_output.comm_d); diff --git a/pallets/storage-provider/Cargo.toml b/pallets/storage-provider/Cargo.toml index c9e88f193..86393e528 100644 --- a/pallets/storage-provider/Cargo.toml +++ b/pallets/storage-provider/Cargo.toml @@ -61,5 +61,6 @@ std = [ "sp-core/std", "sp-io/std", "sp-runtime/std", + "primitives-proofs/std", ] try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime"] diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 30225d7a3..c7a662c62 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -752,7 +752,8 @@ pub mod pallet { PublicReplicaInfo { comm_r: comm_r.raw(), }, - ).map_err(|_| Error::::TooManyReplicas); + ) + .map_err(|_| Error::::TooManyReplicas); } } @@ -1062,6 +1063,31 @@ pub mod pallet { } impl Pallet { + pub fn current_deadline( + storage_provider: &T::AccountId, + ) -> core::option::Option>> { + let sp = StorageProviders::::try_get(storage_provider).ok()?; + let current_block = >::block_number(); + + let deadline = sp + .deadline_info( + current_block, + T::WPoStPeriodDeadlines::get(), + T::WPoStProvingPeriod::get(), + T::WPoStChallengeWindow::get(), + T::WPoStChallengeLookBack::get(), + T::FaultDeclarationCutoff::get(), + ) + .ok()?; + + Some(primitives_proofs::CurrentDeadline { + deadline_index: deadline.idx, + open: deadline.is_open(), + challenge_block: deadline.challenge, + start: deadline.open_at, + }) + } + fn validate_expiration( curr_block: BlockNumberFor, activation: BlockNumberFor, diff --git a/primitives/proofs/Cargo.toml b/primitives/proofs/Cargo.toml index 7139af8a8..3916659cd 100644 --- a/primitives/proofs/Cargo.toml +++ b/primitives/proofs/Cargo.toml @@ -31,4 +31,4 @@ workspace = true [features] clap = ["dep:clap", "std"] default = ["std"] -std = ["cid/scale-codec", "codec/std", "scale-info/std", "ap-api/std", "sp-core/std", "sp-runtime/std", "sp-std/std"] +std = ["cid/scale-codec", "codec/std", "scale-info/std", "sp-api/std", "sp-core/std", "sp-runtime/std", "sp-std/std"] diff --git a/primitives/proofs/src/lib.rs b/primitives/proofs/src/lib.rs index cf2aab1e8..d7aad6d85 100644 --- a/primitives/proofs/src/lib.rs +++ b/primitives/proofs/src/lib.rs @@ -4,7 +4,8 @@ pub mod randomness; mod traits; mod types; -use codec::{Codec, Encode}; +use codec::{Codec, Decode, Encode}; +use scale_info::TypeInfo; pub use traits::*; pub use types::*; @@ -24,9 +25,21 @@ where encoded } +#[derive(Encode, Decode, TypeInfo)] +pub struct CurrentDeadline { + pub deadline_index: u64, + pub open: bool, + pub challenge_block: BlockNumber, + pub start: BlockNumber, +} + sp_api::decl_runtime_apis! { pub trait StorageProviderApi where AccountId: Codec { - fn current_deadline(storage_provider: AccountId) -> u64; + fn current_deadline(storage_provider: AccountId) -> core::option::Option< + CurrentDeadline< + <::Header as sp_runtime::traits::Header>::Number + > + >; } -} \ No newline at end of file +} diff --git a/primitives/proofs/src/traits.rs b/primitives/proofs/src/traits.rs index eb248dde0..9f37a4a7f 100644 --- a/primitives/proofs/src/traits.rs +++ b/primitives/proofs/src/traits.rs @@ -163,4 +163,4 @@ pub struct ActiveDeal { pub piece_cid: Cid, /// Real size of the data pub piece_size: u64, -} \ No newline at end of file +} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 4abc3fbf9..d6fe82974 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -413,8 +413,8 @@ impl Runtime { impl_runtime_apis! { impl primitives_proofs::StorageProviderApi for Runtime { - fn current_deadline(storage_provider: AccountId) -> u64 { - 0 + fn current_deadline(storage_provider: AccountId) -> Option> { + StorageProvider::current_deadline(&storage_provider) } } diff --git a/storagext/cli/src/cmd/proofs.rs b/storagext/cli/src/cmd/proofs.rs index 857c15e76..60c5ed066 100644 --- a/storagext/cli/src/cmd/proofs.rs +++ b/storagext/cli/src/cmd/proofs.rs @@ -58,7 +58,7 @@ impl ProofsCommand { wait_for_finalization, ) .await? - }, + } ProofsCommand::SetPostVerifyingKey { verifying_key } => { let Some(account_keypair) = account_keypair else { return Err(missing_keypair_error::().into()); diff --git a/storagext/lib/src/clients/storage_provider.rs b/storagext/lib/src/clients/storage_provider.rs index 7d1d3b8d4..9a3043e6e 100644 --- a/storagext/lib/src/clients/storage_provider.rs +++ b/storagext/lib/src/clients/storage_provider.rs @@ -12,8 +12,11 @@ use crate::{ self, bounded_vec::IntoBoundedByteVec, client::SubmissionResult, - runtime_types::pallet_storage_provider::{ - proofs::SubmitWindowedPoStParams, storage_provider::StorageProviderState, + runtime_types::{ + pallet_storage_provider::{ + proofs::SubmitWindowedPoStParams, storage_provider::StorageProviderState, + }, + primitives_proofs::CurrentDeadline, }, storage_provider::calls::types::register_storage_provider::PeerId, }, @@ -99,16 +102,27 @@ pub trait StorageProviderClientExt { &self, ) -> impl Future, subxt::Error>>; - - fn current_deadline(&self, account_id: &AccountId32) -> impl Future>; + fn current_deadline( + &self, + account_id: &AccountId32, + ) -> impl Future>, subxt::Error>>; } impl StorageProviderClientExt for crate::runtime::client::Client { - async fn current_deadline(&self, account_id: &AccountId32) -> Result { - // let _ = runtime::apis().storage_provider_api().current_deadline(account_id); - // let _ = runtime::apis().account_nonce_api().account_nonce(account_id); + async fn current_deadline( + &self, + account_id: &AccountId32, + ) -> Result>, subxt::Error> { + let payload = runtime::apis() + .storage_provider_api() + .current_deadline(account_id.clone()); - Ok(0) + self.client + .runtime_api() + .at_latest() + .await? + .call(payload) + .await } #[tracing::instrument( From 87f1e5fc1a57c7eb533b61926d2f4c11cfac9c7f Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Mon, 25 Nov 2024 19:13:14 +0100 Subject: [PATCH 15/22] fix: regenerate scale --- Cargo.lock | 1 - storagext/lib/artifacts/metadata.scale | Bin 163504 -> 163750 bytes 2 files changed, 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2014b38d5..24a13d555 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12822,7 +12822,6 @@ dependencies = [ "bls12_381", "cid 0.11.1", "clap", - "hex", "jsonrpsee", "mater", "parity-scale-codec", diff --git a/storagext/lib/artifacts/metadata.scale b/storagext/lib/artifacts/metadata.scale index 39038327f429a42636881cc5f8cb06979df7cfeb..87e88b6e059cc9569261ec029417e0522ba6b49b 100644 GIT binary patch delta 1466 zcmZWpT}%{L7`^B2z+EWlZei;ZYbixkjI?M`K~X`aiiY1^s5Bv)ahbXUyEE(TE`klI zJQ(AHtqsO6rm;RWZDLsjY})Bh(xiWh589aY2`LuJQ)7Qp)Ap^}JFGNPlF5Ac-Z}To z`ObI0JMmA>`ww%nV_H&O`POjuN7={MIEEqjcmFwiWed1#wa@Brs6aXxpI^=jis@g&ZJR_TpCTI9Z%4MG=j*Z zz!(lApRSCd1}kW43`1B+FJF^#JaqM%Wbs}c$4xv*x5n`^3do*7x2sU;rcWnOMzxdJ z2Opi9!~xe+%55r1V-?L!Vvh_Z*RiFbHfWknsxjEE#iM#yO)x*Bz;*0Ek^RPXe6a3y z#orQlQdN{7vIN|oX4B9ZW1fH+(-f8^7z2~Ry+3a0G3iRbmI%e2#1pmqQkJC|$^Dwz z6V(k(*|axmhWnaRv7?%WO6Eaz&!bNv-RRKj~k0Q}TDm!l^>X~@ zEVh)#me3wwPJ28%;6LU$@&TBHx4dg7+TISlwlYWBNNe$)H#o0 z3{(F+HsLhAJCF6bXC29toShRRU))$_-4sU{`Q;CBfBnt9G0mu`@nsC zWp!2`49E*ZJ*KrcW|>2`%=9QkTe}oVHqkF~}c1U7xa4xxrnPh?>bz zkDM)>fE>G2<5#ofopGy|Z*zIY8p`hDAIkIjSs&kzGW%g4AA`#$%4s~tN8qJ>2H)wb z5k)j?@ZQ{f;pvg}t`Uv&m%)E6Y!dF|fKO~#XuVKGO+>2N?7vN3$KkcNTl{UH%lEC8)dQ#ECeLhGgTS+Wq#flw=nqQg z&|$I|jZqVponE*+MNN#xM5Bw|QZyqZH}9Uj`#qm? z?z!jQcmHvu?>llvwV1m1mFb)ha$xiGyg%qgKMvASKa?hBL#5Ibk3=**7EUU$pr(vP z3}aXcMHRoHM?(QEq6L%*H5>{kiJ+z{LtE>|!kS*;RT`D!LuQ*Fb%J;TZc9yVojHF2 zUpHdNyflkW6!=>8E$8!^KW0Q|?KXTxr?&q+n~Bt&9MhPU>gLc08^!1F7;7#i}p@8ZaWaPc{>Vkwg zX=4F*P)G~!;T|5M>uC&FimU^4BMlFgETRefNL|Dk6w~Y?`tUIECDhQFC7i&1^Q9%+ zK1@G+gDP$lq<$gA-enX&+Th!R&AaHB}bNA>cZ@WieD}|4E{S0`?Hwalc2$a5UP~lmWuC0qL)Ggz zL^oD(7+(5h6^9N~37qZGM|2~hcSlq`s`@iAGh}a8tlY(isLI-Z-9-ng>D(GTsHO2W zRLbe*8eFah8GvOz{0YJ2U{<>q_3Sj=SwmM*wZQRKJt$jCk2R|?m9dr#u&=6hNA~UF z!SvfR$OrSMW9#w=SJsg}*}0=SJ-e&ZBc0_QG869?Ob(e*eUFU|(ESaR(YqA z+`tiN^!5g7yxICd z(ds1k(BLHhd{0u8Yfzqyhr$6Wy~Ov^FG-$bKAq%W0#j!3^Ss}JY14Rt`(U{u*uM$E BX7m66 From 88acdaba9dfc21f5d877902347acd9d35f11f017 Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Mon, 25 Nov 2024 19:25:00 +0100 Subject: [PATCH 16/22] fix: change post randomness --- .../client/src/commands/proofs.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index 2fd4ae73e..eaaec402d 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -19,7 +19,7 @@ use primitives_commitment::{ }; use primitives_proofs::{ derive_prover_id, - randomness::{draw_randomness, DomainSeparationTag}, + randomness::{self, draw_randomness, DomainSeparationTag}, RegisteredPoStProof, RegisteredSealProof, SectorNumber, }; use storagext::multipair::{MultiPairArgs, MultiPairSigner}; @@ -122,8 +122,12 @@ pub enum ProofsCommand { #[arg(short, long)] /// Directory where the PoSt proof will be stored. Defaults to the current directory. output_path: Option, - #[arg(short, long)] + /// Sector Number used in the PoRep command. + #[arg(long)] sector_number: u32, + /// Block Number at which the randomness should be fetched from. + /// It comes from the [`pallet_storage_provider::DeadlineInfo::challenge`] field. + challenge_block: u64, }, } @@ -412,9 +416,14 @@ impl ProofsCommand { let Some(signer) = Option::::from(signer_key) else { return Err(UtilsCommandError::NoSigner)?; }; - let prover_id = derive_prover_id(signer.account_id()); - let randomness: [u8; 32] = [1u8; 32]; + let entropy = derive_prover_id(signer.account_id()); + let randomness = get_randomness( + DomainSeparationTag::WindowedPoStChallengeSeed, + seal_randomness_height, + &entropy, + ); + let output_path = if let Some(output_path) = output_path { output_path } else { From 42a5b00b4e598ddbb3a0a5a64e6a27f7289f337c Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Mon, 25 Nov 2024 21:28:40 +0100 Subject: [PATCH 17/22] fix: fixes after e2e test --- Cargo.lock | 1 + cli/polka-storage-provider/client/Cargo.toml | 1 + .../client/src/commands/proofs.rs | 17 ++++++++++------- primitives/proofs/src/randomness.rs | 5 ++++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 24a13d555..2014b38d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12822,6 +12822,7 @@ dependencies = [ "bls12_381", "cid 0.11.1", "clap", + "hex", "jsonrpsee", "mater", "parity-scale-codec", diff --git a/cli/polka-storage-provider/client/Cargo.toml b/cli/polka-storage-provider/client/Cargo.toml index 302d4126d..0a5657353 100644 --- a/cli/polka-storage-provider/client/Cargo.toml +++ b/cli/polka-storage-provider/client/Cargo.toml @@ -21,6 +21,7 @@ bls12_381 = { workspace = true } cid = { workspace = true, features = ["std"] } clap = { workspace = true, features = ["derive"] } codec = { workspace = true } +hex = { workspace = true } jsonrpsee = { workspace = true, features = ["http-client", "macros", "server", "ws-client"] } sc-cli = { workspace = true } serde = { workspace = true } diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index eaaec402d..71afef2c1 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -19,7 +19,7 @@ use primitives_commitment::{ }; use primitives_proofs::{ derive_prover_id, - randomness::{self, draw_randomness, DomainSeparationTag}, + randomness::{draw_randomness, DomainSeparationTag}, RegisteredPoStProof, RegisteredSealProof, SectorNumber, }; use storagext::multipair::{MultiPairArgs, MultiPairSigner}; @@ -115,10 +115,6 @@ pub enum ProofsCommand { /// It must be the same, or else it won't work. #[arg(short, long)] cache_directory: PathBuf, - /// Replica file generated with `porep` command e.g. `77.sector.sealed`. - replica_path: PathBuf, - /// CID - CommR of a replica (output of `porep` command) - comm_r: String, #[arg(short, long)] /// Directory where the PoSt proof will be stored. Defaults to the current directory. output_path: Option, @@ -127,7 +123,12 @@ pub enum ProofsCommand { sector_number: u32, /// Block Number at which the randomness should be fetched from. /// It comes from the [`pallet_storage_provider::DeadlineInfo::challenge`] field. + #[arg(long)] challenge_block: u64, + /// Replica file generated with `porep` command e.g. `77.sector.sealed`. + replica_path: PathBuf, + /// CID - CommR of a replica (output of `porep` command) + comm_r: String, }, } @@ -412,15 +413,16 @@ impl ProofsCommand { comm_r, output_path, sector_number, + challenge_block, } => { let Some(signer) = Option::::from(signer_key) else { return Err(UtilsCommandError::NoSigner)?; }; - let entropy = derive_prover_id(signer.account_id()); + let entropy = signer.account_id().encode(); let randomness = get_randomness( DomainSeparationTag::WindowedPoStChallengeSeed, - seal_randomness_height, + challenge_block, &entropy, ); @@ -453,6 +455,7 @@ impl ProofsCommand { let proof_parameters = post::load_groth16_parameters(proof_parameters_path) .map_err(|e| UtilsCommandError::GeneratePoStError(e))?; + let prover_id = derive_prover_id(signer.account_id()); let proofs = post::generate_window_post( post_type, &proof_parameters, diff --git a/primitives/proofs/src/randomness.rs b/primitives/proofs/src/randomness.rs index 7ccbd9c85..cb1bf5551 100644 --- a/primitives/proofs/src/randomness.rs +++ b/primitives/proofs/src/randomness.rs @@ -48,7 +48,10 @@ pub fn draw_randomness( data.extend_from_slice(entropy); // Hash the data - blake2_256(&data) + let mut hashed = blake2_256(&data); + // Necessary to be valid bls12 381 element. + hashed[31] &= 0x3f; + hashed } #[cfg(test)] From 9de38414067722908db5d1e77252c15c4756166f Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Tue, 26 Nov 2024 10:41:42 +0100 Subject: [PATCH 18/22] docs: improve em --- .../client/src/commands/proofs.rs | 5 ++++- pallets/storage-provider/Cargo.toml | 2 +- pallets/storage-provider/src/lib.rs | 9 ++++++++- pallets/storage-provider/src/proofs.rs | 2 +- primitives/proofs/src/lib.rs | 16 ++++++++++++++++ storagext/lib/Cargo.toml | 2 +- 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cli/polka-storage-provider/client/src/commands/proofs.rs b/cli/polka-storage-provider/client/src/commands/proofs.rs index 71afef2c1..61e4de364 100644 --- a/cli/polka-storage-provider/client/src/commands/proofs.rs +++ b/cli/polka-storage-provider/client/src/commands/proofs.rs @@ -441,8 +441,11 @@ impl ProofsCommand { let comm_r = cid::Cid::from_str(&comm_r).map_err(|_| UtilsCommandError::CommRError)?; + let sector_number = SectorNumber::try_from(sector_number) + .map_err(|_| UtilsCommandError::InvalidSectorId)?; + let replicas = vec![ReplicaInfo { - sector_id: sector_number.try_into().unwrap(), + sector_id: sector_number, comm_r: comm_r .hash() .digest() diff --git a/pallets/storage-provider/Cargo.toml b/pallets/storage-provider/Cargo.toml index 86393e528..e55088d8a 100644 --- a/pallets/storage-provider/Cargo.toml +++ b/pallets/storage-provider/Cargo.toml @@ -57,10 +57,10 @@ std = [ "frame-support/std", "frame-system/std", "pallet-balances/std", + "primitives-proofs/std", "scale-info/std", "sp-core/std", "sp-io/std", "sp-runtime/std", - "primitives-proofs/std", ] try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime"] diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index c7a662c62..b655bc3e4 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -316,8 +316,9 @@ pub mod pallet { /// extrinsic but is not registered as one. StorageProviderNotFound, /// Emitted when trying to access an invalid sector. - InvalidPartition, InvalidSector, + /// Emitted when trying to submit PoSt for an not-existing partition for a deadline. + InvalidPartition, /// Emitted when submitting an invalid proof type. InvalidProofType, /// Emitted when the proof is invalid @@ -359,6 +360,7 @@ pub mod pallet { CouldNotTerminateDeals, /// Tried to terminate sectors that are not mutable. CannotTerminateImmutableDeadline, + /// Emitted when trying to submit PoSt with partitions containing too many sectors (>2349). TooManyReplicas, /// Inner pallet errors GeneralPalletError(crate::error::GeneralPalletError), @@ -1063,6 +1065,11 @@ pub mod pallet { } impl Pallet { + /// Gets the current deadline of the storage provider. + /// + /// If there is no Storage Provider of given AccountId returns [`Option::None`]. + /// May exceptionally return [`Option::None`] when + /// conversion between BlockNumbers fails, but technically should not ever happen. pub fn current_deadline( storage_provider: &T::AccountId, ) -> core::option::Option>> { diff --git a/pallets/storage-provider/src/proofs.rs b/pallets/storage-provider/src/proofs.rs index e34eae98a..931a9091a 100644 --- a/pallets/storage-provider/src/proofs.rs +++ b/pallets/storage-provider/src/proofs.rs @@ -14,7 +14,7 @@ use crate::partition::{PartitionNumber, MAX_PARTITIONS_PER_DEADLINE}; pub struct PoStProof { /// The proof type, currently only one type is supported. pub post_proof: RegisteredPoStProof, - /// The proof submission, to be checked in the storage provider pallet. + /// The proof submission, to be checked by [`ProofVerification::verify_post`], usually [`pallet_proofs`]. pub proof_bytes: BoundedVec>, } diff --git a/primitives/proofs/src/lib.rs b/primitives/proofs/src/lib.rs index d7aad6d85..0ce080ed3 100644 --- a/primitives/proofs/src/lib.rs +++ b/primitives/proofs/src/lib.rs @@ -25,17 +25,33 @@ where encoded } +/// Current deadline in a proving period of a Storage Provider. #[derive(Encode, Decode, TypeInfo)] pub struct CurrentDeadline { + /// Index of a deadline. + /// + /// If there are 10 deadlines if the proving period, values will be [0, 9]. + /// After proving period rolls over, it'll start from 0 again. pub deadline_index: u64, + /// Whether the deadline is open. + /// Only is false when `current_block < sp.proving_period_start`. pub open: bool, + /// [`pallet_storage_provider::DeadlineInfo::challenge`]. + /// + /// Block at which the randomness should be fetched to generate/verify Post. pub challenge_block: BlockNumber, + /// Block at which the deadline opens. pub start: BlockNumber, } sp_api::decl_runtime_apis! { pub trait StorageProviderApi where AccountId: Codec { + /// Gets the current deadline of the storage provider. + /// + /// If there is no Storage Provider of given AccountId returns [`Option::None`]. + /// May exceptionally return [`Option::None`] when + /// conversion between BlockNumbers fails, but technically should not ever happen. fn current_deadline(storage_provider: AccountId) -> core::option::Option< CurrentDeadline< <::Header as sp_runtime::traits::Header>::Number diff --git a/storagext/lib/Cargo.toml b/storagext/lib/Cargo.toml index 150d13550..fde9cea82 100644 --- a/storagext/lib/Cargo.toml +++ b/storagext/lib/Cargo.toml @@ -20,7 +20,7 @@ frame-support = { workspace = true, features = ["std"] } futures.workspace = true hex = { workspace = true, features = ["serde"] } itertools = { workspace = true } -primitives-proofs = { workspace = true, features = ["std", "serde"] } +primitives-proofs = { workspace = true, features = ["serde", "std"] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } sha2 = { workspace = true } From 0454cd4a94b9deec3ccd9249aedbbfd7dda6930b Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Tue, 26 Nov 2024 11:19:57 +0100 Subject: [PATCH 19/22] fix: randomness test --- primitives/proofs/src/randomness.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/proofs/src/randomness.rs b/primitives/proofs/src/randomness.rs index cb1bf5551..3e4a08e5e 100644 --- a/primitives/proofs/src/randomness.rs +++ b/primitives/proofs/src/randomness.rs @@ -62,7 +62,7 @@ mod tests { fn draw_randomness_test() { let expected_randomness = [ 16, 4, 148, 26, 85, 39, 23, 237, 122, 218, 235, 235, 69, 17, 177, 142, 200, 107, 127, - 84, 189, 40, 145, 187, 205, 159, 58, 161, 209, 57, 226, 68, + 84, 189, 40, 145, 187, 205, 159, 58, 161, 209, 57, 226, 4, ]; let digest = [ From 1650a405bb4afc21d7305756dbf97bac88fc6647 Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Tue, 26 Nov 2024 17:14:42 +0100 Subject: [PATCH 20/22] fix: coerce error in submit windowed post --- pallets/storage-provider/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index b655bc3e4..8feda32ae 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -755,7 +755,7 @@ pub mod pallet { comm_r: comm_r.raw(), }, ) - .map_err(|_| Error::::TooManyReplicas); + .map_err(|_| Error::::TooManyReplicas)?; } } From d32a25f5312dd6d10ff86ce169febe2ffcfc652f Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Tue, 26 Nov 2024 17:27:24 +0100 Subject: [PATCH 21/22] refactor: remove option qualifier --- pallets/storage-provider/src/lib.rs | 2 +- primitives/proofs/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 8feda32ae..306ca1dc8 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -1072,7 +1072,7 @@ pub mod pallet { /// conversion between BlockNumbers fails, but technically should not ever happen. pub fn current_deadline( storage_provider: &T::AccountId, - ) -> core::option::Option>> { + ) -> Option>> { let sp = StorageProviders::::try_get(storage_provider).ok()?; let current_block = >::block_number(); diff --git a/primitives/proofs/src/lib.rs b/primitives/proofs/src/lib.rs index 0ce080ed3..b9af60136 100644 --- a/primitives/proofs/src/lib.rs +++ b/primitives/proofs/src/lib.rs @@ -52,7 +52,7 @@ sp_api::decl_runtime_apis! { /// If there is no Storage Provider of given AccountId returns [`Option::None`]. /// May exceptionally return [`Option::None`] when /// conversion between BlockNumbers fails, but technically should not ever happen. - fn current_deadline(storage_provider: AccountId) -> core::option::Option< + fn current_deadline(storage_provider: AccountId) -> Option< CurrentDeadline< <::Header as sp_runtime::traits::Header>::Number > From e6498151aefe2aa3b6981a17af769715100c3ae2 Mon Sep 17 00:00:00 2001 From: Konrad Stepniak Date: Wed, 27 Nov 2024 11:23:29 +0100 Subject: [PATCH 22/22] chore: remove fixed todo --- pallets/storage-provider/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 306ca1dc8..9540a6691 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -685,9 +685,6 @@ pub mod pallet { } ); - // Ensure a valid proof size - // TODO(@jmg-duarte,#91,19/8/24): correctly check the length - // https://github.com/filecoin-project/builtin-actors/blob/17ede2b256bc819dc309edf38e031e246a516486/actors/miner/src/lib.rs#L565-L573 ensure!( windowed_post.proof.proof_bytes.len() <= primitives_proofs::MAX_POST_PROOF_BYTES as usize,