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

test(storage provider pallet): add setup for multiple partitions #194

Merged
merged 17 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 17 additions & 7 deletions pallets/storage-provider/src/deadline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,40 @@ where
let mut partitions = core::mem::take(&mut self.partitions).into_inner();
let initial_partitions = partitions.len();

// Needs to start at 1 because if we take the length of `self.partitions`
// it will always be `MAX_PARTITIONS_PER_DEADLINE` because the partitions are pre-initialized.
let mut partition_idx = 0;
// We are starting at the last partition. That is because we know that
// all partitions before last are already full.
cernicc marked this conversation as resolved.
Show resolved Hide resolved
let mut partition_idx = partitions.len().saturating_sub(1);
cernicc marked this conversation as resolved.
Show resolved Hide resolved
loop {
// Get/create partition to update.
// Get partition to which we want to add sectors. If the partition
cernicc marked this conversation as resolved.
Show resolved Hide resolved
// does not exist, create a new one. The new partition is created
// when it's our first time adding sectors to it.
let partition = partitions
.entry(partition_idx as u32)
.or_insert(Partition::new());

// Figure out which (if any) sectors we want to add to this partition.
// Get the current partition's sector count.
// If the current partition is full, move to the next one.
let sector_count = partition.sectors.len() as u64;
if sector_count >= partition_size {
// Create a new partition.
partition_idx += 1;
continue;
}

// Calculate how many sectors we can add to current partition.
let size = cmp::min(partition_size - sector_count, sectors.len() as u64) as usize;

// Split the sectors into two parts: one to add to the current
// partition and the rest which will be added to the next one.
let (partition_new_sectors, sectors) = sectors.split_at(size);

// Extract the sector numbers from the new sectors.
let new_partition_sectors: Vec<SectorNumber> = partition_new_sectors
.into_iter()
.map(|sector| sector.sector_number)
.collect();

// Add sectors to partition.
// Add new sector numbers to the current partition.
partition
.add_sectors(&new_partition_sectors)
.map_err(|_| DeadlineError::CouldNotAddSectors)?;
Expand All @@ -184,10 +193,10 @@ where
.map(|s| (s.expiration, partition_idx as PartitionNumber)),
);

// No more sectors to add
if sectors.is_empty() {
break;
}
partition_idx += 1;
}

let partitions = BoundedBTreeMap::try_from(partitions).map_err(|_| {
Expand Down Expand Up @@ -226,6 +235,7 @@ where
if !partition_sectors.0.contains_key(&partition_number) {
continue;
}

partition.record_faults(
sectors,
partition_sectors
Expand Down
12 changes: 12 additions & 0 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,29 @@ pub mod pallet {
Error::<T>::InvalidProofType,
);

// Sector that will be activated and required to be periodically
// proven
let new_sector =
SectorOnChainInfo::from_pre_commit(precommit.info.clone(), current_block);

// Mutate the storage provider state
StorageProviders::<T>::try_mutate(&owner, |maybe_sp| -> DispatchResult {
let sp = maybe_sp
.as_mut()
.ok_or(Error::<T>::StorageProviderNotFound)?;

// Activate the sector
sp.activate_sector(sector_number, new_sector.clone())
.map_err(|e| Error::<T>::StorageProviderError(e))?;

// Sectors which will be assigned to the deadlines
let mut new_sectors = BoundedVec::new();
new_sectors
.try_push(new_sector)
.expect("Infallible since only 1 element is inserted");

// Assign sectors to deadlines which specify when sectors needs
// to be proven
sp.assign_sectors_to_deadlines(
current_block,
new_sectors,
Expand All @@ -489,6 +499,8 @@ pub mod pallet {
T::WPoStChallengeLookBack::get(),
)
.map_err(|e| Error::<T>::StorageProviderError(e))?;

// Remove sector from the pre-committed map
sp.remove_pre_committed_sector(sector_number)
.map_err(|e| Error::<T>::StorageProviderError(e))?;
Ok(())
Expand Down
7 changes: 1 addition & 6 deletions pallets/storage-provider/src/sector_map.rs
jmg-duarte marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ mod test {
use sp_core::bounded_btree_map;

use super::*;
use crate::tests::create_set;

#[test]
fn partition_map_add_sectors() {
Expand Down Expand Up @@ -278,12 +279,6 @@ mod test {
expect_deadline_sectors_exact(&map, deadline, partition, &sectors);
}

/// This is a helper function to easily create a set of sectors.
fn create_set<const T: u32>(sectors: &[u64]) -> BoundedBTreeSet<SectorNumber, ConstU32<T>> {
let sectors = sectors.iter().copied().collect::<BTreeSet<_>>();
BoundedBTreeSet::try_from(sectors).unwrap()
}

/// Checks that items in `expected_sectors` are in the actual partition. Any
/// extra items that are not in the `expected_sectors` are ignored.
fn expect_sectors_partial(
Expand Down
Loading
Loading