Skip to content

Commit

Permalink
fix: SectorPreCommitInfoBuilder default impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cernicc committed Jul 15, 2024
1 parent 5557b0a commit 18eab0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 86 deletions.
54 changes: 28 additions & 26 deletions pallets/storage-provider/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,29 +211,35 @@ fn register_storage_provider(account: AccountIdOf<Test>) {

struct SectorPreCommitInfoBuilder {
seal_proof: RegisteredSealProof,
sector_number: Option<SectorNumber>,
sealed_cid: Option<SectorId>,
deal_ids: Option<BoundedVec<DealId, ConstU32<MAX_DEALS_PER_SECTOR>>>,
sector_number: SectorNumber,
sealed_cid: SectorId,
deal_ids: BoundedVec<DealId, ConstU32<MAX_DEALS_PER_SECTOR>>,
expiration: u64,
unsealed_cid: Option<SectorId>,
unsealed_cid: SectorId,
}

impl Default for SectorPreCommitInfoBuilder {
fn default() -> Self {
Self {
seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1,
sector_number: None,
sealed_cid: None,
deal_ids: None,
sector_number: 1,
sealed_cid: cid_of("sealed_cid")
.to_bytes()
.try_into()
.expect("hash is always 32 bytes"),
deal_ids: bounded_vec![0, 1],
expiration: YEARS,
unsealed_cid: None,
unsealed_cid: cid_of("unsealed_cid")
.to_bytes()
.try_into()
.expect("hash is always 32 bytes"),
}
}
}

impl SectorPreCommitInfoBuilder {
pub fn sector_number(mut self, sector_number: u64) -> Self {
self.sector_number = Some(sector_number);
self.sector_number = sector_number;
self
}

Expand All @@ -242,27 +248,23 @@ impl SectorPreCommitInfoBuilder {
I: IntoIterator<Item = DealId>,
{
let deal_ids_vec = deal_ids.into_iter().collect::<Vec<_>>();
self.deal_ids = Some(BoundedVec::try_from(deal_ids_vec).unwrap());
self.deal_ids = BoundedVec::try_from(deal_ids_vec).unwrap();
self
}

pub fn sealed_cid(mut self, data: &str) -> Self {
self.sealed_cid = Some(
cid_of(data)
.to_bytes()
.try_into()
.expect("hash is always 32 bytes"),
);
self.sealed_cid = cid_of(data)
.to_bytes()
.try_into()
.expect("hash is always 32 bytes");
self
}

pub fn unsealed_cid(mut self, data: &str) -> Self {
self.unsealed_cid = Some(
cid_of(data)
.to_bytes()
.try_into()
.expect("hash is always 32 bytes"),
);
self.unsealed_cid = cid_of(data)
.to_bytes()
.try_into()
.expect("hash is always 32 bytes");
self
}

Expand All @@ -274,11 +276,11 @@ impl SectorPreCommitInfoBuilder {
pub fn build(self) -> SectorPreCommitInfo<u64> {
SectorPreCommitInfo {
seal_proof: self.seal_proof,
sector_number: self.sector_number.expect("sector number is required"),
sealed_cid: self.sealed_cid.expect("sealed cid is required"),
deal_ids: self.deal_ids.expect("deal ids are required"),
sector_number: self.sector_number,
sealed_cid: self.sealed_cid,
deal_ids: self.deal_ids,
expiration: self.expiration,
unsealed_cid: self.unsealed_cid.expect("unsealed cid is required"),
unsealed_cid: self.unsealed_cid,
}
}
}
Expand Down
56 changes: 6 additions & 50 deletions pallets/storage-provider/src/tests/pre_commit_sector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ fn successfully_precommited() {
register_storage_provider(account(storage_provider));

// Sector to be pre-committed.
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();
let sector = SectorPreCommitInfoBuilder::default().build();

// Check starting balance
assert_eq!(Balances::free_balance(account(storage_provider)), 100);
Expand Down Expand Up @@ -66,12 +61,7 @@ fn successfully_precommited() {
fn fails_should_be_signed() {
new_test_ext().execute_with(|| {
// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();
let sector = SectorPreCommitInfoBuilder::default().build();

// Run pre commit extrinsic
assert_noop!(
Expand All @@ -85,12 +75,7 @@ fn fails_should_be_signed() {
fn fails_storage_provider_not_found() {
new_test_ext().execute_with(|| {
// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();
let sector = SectorPreCommitInfoBuilder::default().build();

// Run pre commit extrinsic
assert_noop!(
Expand All @@ -111,12 +96,7 @@ fn fails_sector_number_already_used() {
register_storage_provider(account(storage_provider));

// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();
let sector = SectorPreCommitInfoBuilder::default().build();

// Run pre commit extrinsic
assert_ok!(StorageProvider::pre_commit_sector(
Expand All @@ -127,7 +107,7 @@ fn fails_sector_number_already_used() {
assert_noop!(
StorageProvider::pre_commit_sector(
RuntimeOrigin::signed(account(storage_provider)),
sector.clone()
sector
),
Error::<Test>::SectorNumberAlreadyUsed,
);
Expand All @@ -144,9 +124,6 @@ fn fails_invalid_sector() {
// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(SECTORS_MAX as u64 + 1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();

// Run pre commit extrinsic
Expand All @@ -168,12 +145,7 @@ fn fails_invalid_cid() {
register_storage_provider(account(storage_provider));

// Sector to be pre-committed
let mut sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();
let mut sector = SectorPreCommitInfoBuilder::default().build();

// Setting the wrong unseal cid on the sector
sector.unsealed_cid = BoundedVec::new();
Expand All @@ -200,10 +172,6 @@ fn fails_expiration_before_activation() {

// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.expiration(1000)
.build();

Expand Down Expand Up @@ -231,10 +199,6 @@ fn fails_expiration_too_soon() {

// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
// Set expiration to be in the next block after the maximum
// allowed activation.
.expiration(current_height + MaxProveCommitDuration::get() + 1)
Expand Down Expand Up @@ -263,10 +227,6 @@ fn fails_expiration_too_long() {

// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
// Set expiration to be in the next block after the maximum
// allowed
.expiration(current_height + MaxSectorExpirationExtension::get() + 1)
Expand Down Expand Up @@ -299,10 +259,6 @@ fn fails_expiration_too_long() {

// // Sector to be pre-committed
// let sector = SectorPreCommitInfoBuilder::default()
// .sector_number(1)
// .deals([0, 1])
// .sealed_cid("sealed_cid")
// .unsealed_cid("unsealed_cid")
// .expiration(current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get())
// .build();

Expand Down
11 changes: 1 addition & 10 deletions pallets/storage-provider/src/tests/prove_commit_sector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ fn successfully_prove_sector() {
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(sector_number)
.deals([0])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();

// Run pre commit extrinsic
Expand Down Expand Up @@ -105,12 +103,7 @@ fn successfully_prove_sector() {
fn fails_should_be_signed() {
new_test_ext().execute_with(|| {
// Sector to be pre-committed
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(1)
.deals([0, 1])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();
let sector = SectorPreCommitInfoBuilder::default().build();

// Run pre commit extrinsic
assert_noop!(
Expand Down Expand Up @@ -204,8 +197,6 @@ fn fails_prove_commit_after_deadline() {
let sector = SectorPreCommitInfoBuilder::default()
.sector_number(sector_number)
.deals([0])
.sealed_cid("sealed_cid")
.unsealed_cid("unsealed_cid")
.build();

// Run pre commit extrinsic
Expand Down

0 comments on commit 18eab0b

Please sign in to comment.