Skip to content

Commit

Permalink
refactor(sponsorship): use ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
aliXsed committed Dec 8, 2023
1 parent b69ae42 commit 5f3e32d
Showing 1 changed file with 74 additions and 62 deletions.
136 changes: 74 additions & 62 deletions pallets/sponsorship/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ pub(crate) mod v0 {

/// Return the minimum overhead of attempting to migrate the storage.
pub fn min_weight<T: Config>() -> Weight {
T::DbWeight::get().reads(2)
// 2 reads: PotMigrationCursor, UserMigrationCursor
// Fixed: 40_000_000 as a pessimistic estimation for non benchmarked logic with trivial cost
// during each loop of migrate_limited
T::DbWeight::get()
.reads(2)
.saturating_add(Weight::from_parts(40_000_000_u64, 0))
}

Check warning on line 231 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L224-L231

Added lines #L224 - L231 were not covered by tests

/// Return the maximum overhead of attempting to migrate the storage.
Expand Down Expand Up @@ -268,14 +273,16 @@ type StorageDoubleMapKey = Vec<u8>;

#[cfg(feature = "try-runtime")]
pub(crate) fn pre_upgrade<T: Config>() -> Result<Vec<u8>, TryRuntimeError> {
if StorageVersion::get::<Pallet<T>>() > 1 {
return Err(TryRuntimeError::Other("Storage version is not either 0 or 1"));
}
ensure!(
StorageVersion::get::<Pallet<T>>() == 0,
TryRuntimeError::Other("Storage version is not 0")

Check warning on line 278 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L275-L278

Added lines #L275 - L278 were not covered by tests
);

let block_usage = v0::max_weight::<T>();
if block_usage.any_lte(v0::min_weight::<T>()) {
return Err(TryRuntimeError::Other("Block usage is set too low"));
}
ensure!(
block_usage.all_gt(v0::min_weight::<T>()),
TryRuntimeError::Other("Block usage is set too low")

Check warning on line 284 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L281-L284

Added lines #L281 - L284 were not covered by tests
);
log::info!(target: "sponsorship", "pre_upgrade: block_usage = ({ref_time}, {proof_size})", ref_time=block_usage.ref_time(), proof_size=block_usage.proof_size());

Check warning on line 286 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L286

Added line #L286 was not covered by tests

let max_pots_per_block = block_usage
Expand All @@ -288,9 +295,10 @@ pub(crate) fn pre_upgrade<T: Config>() -> Result<Vec<u8>, TryRuntimeError> {
.ref_time()
.checked_div(T::WeightInfo::migrate_users(1).ref_time())
.unwrap_or(1) as usize;
if max_pots_per_block == 0 || max_users_per_block == 0 {
return Err(TryRuntimeError::Other("Migration allowed weight is too low"));
}
ensure!(
max_pots_per_block > 0 && max_users_per_block > 0,
TryRuntimeError::Other("Migration allowed weight is too low")

Check warning on line 300 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L288-L300

Added lines #L288 - L300 were not covered by tests
);
log::info!(target: "sponsorship", "pre_upgrade: max_pots_per_block = {max_pots_per_block}, max_users_per_block = {max_users_per_block}");

Check warning on line 302 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L302

Added line #L302 was not covered by tests

let pot_details = frame_support::migration::storage_key_iter::<
Expand Down Expand Up @@ -322,46 +330,51 @@ pub(crate) fn pre_upgrade<T: Config>() -> Result<Vec<u8>, TryRuntimeError> {

#[cfg(feature = "try-runtime")]
pub(crate) fn post_upgrade<T: Config>(state: Vec<u8>) -> Result<(), TryRuntimeError> {
if StorageVersion::get::<Pallet<T>>() != 1 {
return Err(TryRuntimeError::Other("Storage version is not 1"));
}
ensure!(
StorageVersion::get::<Pallet<T>>() == 1,
TryRuntimeError::Other("Storage version not fixed")

Check warning on line 335 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L332-L335

Added lines #L332 - L335 were not covered by tests
);

let (pre_pot_details, pre_user_details): (
Vec<(T::PotId, v0::PotDetailsOf<T>)>,
Vec<(StorageDoubleMapKey, v0::UserDetailsOf<T>)>,
) = Decode::decode(&mut state.as_slice()).map_err(|_| "Unable to decode previous collection details")?;
let pot_details = Pot::<T>::iter().collect::<Vec<_>>();

Check warning on line 342 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L338-L342

Added lines #L338 - L342 were not covered by tests

if pre_pot_details.len() != pot_details.len() {
return Err(TryRuntimeError::Other("Pot count mismatch"));
}
ensure!(
pre_pot_details.len() == pot_details.len(),
TryRuntimeError::Other("Pot count mismatch")

Check warning on line 346 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L344-L346

Added lines #L344 - L346 were not covered by tests
);

for (pre, post) in pre_pot_details.iter().zip(pot_details.iter()) {
if pre.0 != post.0 {
return Err(TryRuntimeError::Other("Pot id mismatch"));
}
if pre.1.sponsor != post.1.sponsor {
return Err(TryRuntimeError::Other("Pot sponsor mismatch"));
}
if pre.1.sponsorship_type != post.1.sponsorship_type {
return Err(TryRuntimeError::Other("Pot sponsorship type mismatch"));
}
if pre.1.fee_quota != post.1.fee_quota {
return Err(TryRuntimeError::Other("Pot fee quota mismatch"));
}
if pre.1.reserve_quota != post.1.reserve_quota {
return Err(TryRuntimeError::Other("Pot reserve quota mismatch"));
}
if post.1.deposit != Default::default() {
return Err(TryRuntimeError::Other("Pot deposit is not default"));
}
ensure!(pre.0 == post.0, TryRuntimeError::Other("Pot id mismatch"));
ensure!(
pre.1.sponsor == post.1.sponsor,
TryRuntimeError::Other("Pot sponsor mismatch")

Check warning on line 353 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L349-L353

Added lines #L349 - L353 were not covered by tests
);
ensure!(
pre.1.sponsorship_type == post.1.sponsorship_type,
TryRuntimeError::Other("Pot sponsorship type mismatch")

Check warning on line 357 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L355-L357

Added lines #L355 - L357 were not covered by tests
);
ensure!(
pre.1.fee_quota == post.1.fee_quota,
TryRuntimeError::Other("Pot fee quota mismatch")

Check warning on line 361 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L359-L361

Added lines #L359 - L361 were not covered by tests
);
ensure!(
pre.1.reserve_quota == post.1.reserve_quota,
TryRuntimeError::Other("Pot reserve quota mismatch")

Check warning on line 365 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L363-L365

Added lines #L363 - L365 were not covered by tests
);
ensure!(
post.1.deposit == Default::default(),
TryRuntimeError::Other("Pot deposit is not default")

Check warning on line 369 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L367-L369

Added lines #L367 - L369 were not covered by tests
);
}

let user_details = User::<T>::iter().collect::<Vec<_>>();

if pre_user_details.len() != user_details.len() {
return Err(TryRuntimeError::Other("User count mismatch"));
}
ensure!(
pre_user_details.len() == user_details.len(),
TryRuntimeError::Other("User count mismatch")

Check warning on line 376 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L373-L376

Added lines #L373 - L376 were not covered by tests
);

for (pre, post) in pre_user_details.iter().zip(user_details.iter()) {
let key1_hashed = post.0.borrow().using_encoded(Blake2_128Concat::hash);
Expand All @@ -370,33 +383,32 @@ pub(crate) fn post_upgrade<T: Config>(state: Vec<u8>) -> Result<(), TryRuntimeEr
final_key.extend_from_slice(key1_hashed.as_ref());
final_key.extend_from_slice(key2_hashed.as_ref());

Check warning on line 384 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L379-L384

Added lines #L379 - L384 were not covered by tests

if pre.0 != final_key {
return Err(TryRuntimeError::Other("User hashed key mismatch"));
}
if pre.1.proxy != post.2.proxy {
return Err(TryRuntimeError::Other("User proxy mismatch"));
}
if pre.1.fee_quota != post.2.fee_quota {
return Err(TryRuntimeError::Other("User fee quota mismatch"));
}
if pre.1.reserve_quota != post.2.reserve_quota {
return Err(TryRuntimeError::Other("User reserve quota mismatch"));
}
if post.2.deposit != Default::default() {
return Err(TryRuntimeError::Other("User deposit is not default"));
}
ensure!(pre.0 == final_key, TryRuntimeError::Other("User key mismatch"));
ensure!(
pre.1.proxy == post.2.proxy,
TryRuntimeError::Other("User proxy mismatch")

Check warning on line 389 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L386-L389

Added lines #L386 - L389 were not covered by tests
);
ensure!(
pre.1.fee_quota == post.2.fee_quota,
TryRuntimeError::Other("User fee quota mismatch")

Check warning on line 393 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L391-L393

Added lines #L391 - L393 were not covered by tests
);
ensure!(
pre.1.reserve_quota == post.2.reserve_quota,
TryRuntimeError::Other("User reserve quota mismatch")

Check warning on line 397 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L395-L397

Added lines #L395 - L397 were not covered by tests
);
ensure!(
post.2.deposit == Default::default(),
TryRuntimeError::Other("User deposit is not default")

Check warning on line 401 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L399-L401

Added lines #L399 - L401 were not covered by tests
);
}

UserRegistrationCount::<T>::iter().try_for_each(|(_user, count)| {
if count == 0 {
return Err(TryRuntimeError::Other("User registration count is 0"));
}
if count > pot_details.len() as u32 {
return Err(TryRuntimeError::Other(
"User registration count is greater than number of pots",
));
}
Ok(())
ensure!(count > 0, TryRuntimeError::Other("User registration count is 0"));
ensure!(
count <= pot_details.len() as u32,
TryRuntimeError::Other("User registration count is greater than number of pots")

Check warning on line 409 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L405-L409

Added lines #L405 - L409 were not covered by tests
);
Ok::<(), TryRuntimeError>(())
})?;

Check warning on line 412 in pallets/sponsorship/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/sponsorship/src/migration.rs#L411-L412

Added lines #L411 - L412 were not covered by tests

log::info!(target: "sponsorship", "post_upgrade: pots = {}, pot_user_count = {}, users = {}", pot_details.len(), user_details.len(), UserRegistrationCount::<T>::iter().count());
Expand Down

0 comments on commit 5f3e32d

Please sign in to comment.