Skip to content

Commit

Permalink
test: Add test-case for succes PoSt submission
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan46 committed Jul 11, 2024
1 parent 64e5fbb commit ee342d2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
53 changes: 41 additions & 12 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub mod pallet {
owner: T::AccountId,
sector_number: SectorNumber,
},
/// Emitted when an SP submits a valid PoSt
ValidPoStSubmitted { owner: T::AccountId },
}

#[pallet::error]
Expand Down Expand Up @@ -169,6 +171,8 @@ pub mod pallet {
/// Emitted when a prove commit is sent after the dealine
/// These precommits will be cleaned up in the hook
ProveCommitAfterDeadline,
/// Emitted when a PoSt supplied by by the SP is invalid
PoStProofInvalid,
}

#[pallet::call]
Expand Down Expand Up @@ -319,24 +323,26 @@ pub mod pallet {
/// Proof is valid when proof.len() > 0
pub fn submit_windowed_post(
origin: OriginFor<T>,
params: SubmitWindowedPoStParams<BlockNumberFor<T>>,
) -> DispatchResultWithPostInfo {
windowed_post: SubmitWindowedPoStParams<BlockNumberFor<T>>,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let current_block = <frame_system::Pallet<T>>::block_number();
let sp = StorageProviders::<T>::try_get(&owner)
.map_err(|_| Error::<T>::StorageProviderNotFound)?;
ensure!(
params.proofs.post_proof == sp.info.window_post_proof_type,
Error::<T>::InvalidProofType
);
validate_windowed_post(params.proofs.proof_bytes);
Ok(().into())
if let Err(e) = Self::validate_windowed_post(
current_block,
&windowed_post,
sp.info.window_post_proof_type,
) {
log::error!(target: LOG_TARGET, "submit_window_post: PoSt submission is invalid {e:?}");
return Err(e.into());
}
// Set new deadline for PoSt submission
Self::deposit_event(Event::ValidPoStSubmitted { owner });
Ok(())
}
}

fn validate_windowed_post(proof_bytes: BoundedVec<u8, ConstU32<256>>) -> bool {
proof_bytes.len() == 0 // TODO(@aidan46, no-ref, 2024-07-03): Actually validate proof.
}

impl<T: Config> Pallet<T> {
fn validate_expiration(
curr_block: BlockNumberFor<T>,
Expand Down Expand Up @@ -365,6 +371,29 @@ pub mod pallet {
);
Ok(())
}

/// Validates the SPs submitted PoSt
fn validate_windowed_post(
current_block: BlockNumberFor<T>,
windowed_post: &SubmitWindowedPoStParams<BlockNumberFor<T>>,
expected_proof: RegisteredPoStProof,
) -> Result<(), Error<T>> {
ensure!(
windowed_post.proof.post_proof == expected_proof,
Error::<T>::InvalidProofType
);
// TODO(@aidan46, no-ref, 2024-07-03): Actually validate proof.
ensure!(
windowed_post.proof.proof_bytes.len() > 0,
Error::<T>::PoStProofInvalid
);
// chain commit block must be less than the current epoch
ensure!(
windowed_post.chain_commit_block < current_block,
Error::<T>::PoStProofInvalid
);
Ok(())
}
}

// Adapted from filecoin reference here: https://github.com/filecoin-project/builtin-actors/blob/54236ae89880bf4aa89b0dba6d9060c3fd2aacee/actors/miner/src/commd.rs#L51-L56
Expand Down
5 changes: 2 additions & 3 deletions pallets/storage-provider/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ pub struct SubmitWindowedPoStParams<BlockNumber> {
pub deadline: u64,
/// The partition being proven.
pub index: u64,
/// Array of proofs, one per distinct registered proof type present in the sectors being proven.
/// In the usual case of a single proof type, this array will always have a single element (independent of number of partitions).
pub proofs: PoStProof,
/// Proof submission
pub proof: PoStProof,
/// The block at which these proofs is being committed.
pub chain_commit_block: BlockNumber,
}
Expand Down
42 changes: 42 additions & 0 deletions pallets/storage-provider/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use sp_runtime::MultiSignature;
use crate::{
mock::*,
pallet::{Error, Event, StorageProviders},
proofs::{PoStProof, SubmitWindowedPoStParams},
sector::{ProveCommitSector, SectorPreCommitInfo},
storage_provider::StorageProviderInfo,
};
Expand Down Expand Up @@ -299,6 +300,47 @@ fn prove_commit_sector() {
});
}

#[test]
fn submit_windowed_post() {
new_test_ext().execute_with(|| {
let peer_id = "storage_provider_1".as_bytes().to_vec();
let peer_id = BoundedVec::try_from(peer_id).unwrap();
let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1;
// Register ALICE as a storage provider.
assert_ok!(StorageProvider::register_storage_provider(
RuntimeOrigin::signed(account(ALICE)),
peer_id.clone(),
window_post_type,
));
// Flush events
events();
// Build window post proof
let windowed_post = SubmitWindowedPoStParams {
deadline: 1,
index: 1,
proof: PoStProof {
post_proof: window_post_type,
proof_bytes: bounded_vec![0x1, 0x2, 0x3],
},
chain_commit_block: System::block_number() - 1,
};
// Run extrinsic and assert that the result is `Ok`
assert_ok!(StorageProvider::submit_windowed_post(
RuntimeOrigin::signed(account(ALICE)),
windowed_post,
));
// Check that expected events were emitted
assert_eq!(
events(),
[RuntimeEvent::StorageProvider(
Event::<Test>::ValidPoStSubmitted {
owner: account(ALICE)
}
)]
);
});
}

/// Builder to simplify writing complex tests of [`DealProposal`].
/// Exclusively uses [`Test`] for simplification purposes.
struct DealProposalBuilder {
Expand Down

0 comments on commit ee342d2

Please sign in to comment.