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

fix: undecodable ForeignInvestmentInfo on Demo #1819

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pallets/foreign-investments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub mod pallet {
/// NOTE: The storage is killed once the investment is fully collected, or
/// decreased.
#[pallet::storage]
pub(super) type ForeignInvestmentInfo<T: Config> = StorageDoubleMap<
pub type ForeignInvestmentInfo<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Expand Down
55 changes: 55 additions & 0 deletions runtime/common/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,58 @@
pub mod increase_storage_version;
pub mod nuke;
pub mod precompile_account_codes;

pub mod utils {
use frame_support::storage::unhashed;
use sp_arithmetic::traits::Saturating;

/// Iterates keys of storage and removes undecodable keys
///
/// WARNING: USE WITH EXTREME CAUTION! Ensure the cleanup can be performed
/// beforehand!
pub fn remove_undecodable_storage_keys<T: parity_scale_codec::Decode + Sized>(
prefix: [u8; 32],
) -> (u64, u64) {
let mut previous_key = prefix.clone().to_vec();
let mut reads: u64 = 1;
let mut writes: u64 = 0;
while let Some(next) =
sp_io::storage::next_key(&previous_key).filter(|n| n.starts_with(&prefix))
{
reads.saturating_accrue(1);
previous_key = next;
let maybe_value = unhashed::get::<T>(&previous_key);
match maybe_value {
Some(_) => continue,
None => {
log::debug!(
"Removing key which could not be decoded at {:?}",
previous_key
);
unhashed::kill(&previous_key);
writes.saturating_accrue(1);
continue;
}
}
}

(reads, writes)
}

/// Iterates all keys of a storage and returns the count.
///
/// NOTE: Necessary because `Storage::<T>::iter().count()` does not account
/// for keys which cannot be decoded.
pub fn count_storage_keys(prefix: [u8; 32]) -> u32 {
let mut previous_key = prefix.clone().to_vec();
let mut n: u32 = 0;
while let Some(next) =
sp_io::storage::next_key(&previous_key).filter(|n| n.starts_with(&prefix))
{
n.saturating_accrue(1);
previous_key = next;
}

n
}
}
Loading
Loading