-
Notifications
You must be signed in to change notification settings - Fork 288
Crowdloan max contributors limit to avoid unbounded work #1591
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
487c231
added log crate to crowdloan
l0r1s 39cea2a
keep track of contributors count and allow a maximum of contributors,…
l0r1s 203a8a3
fix tests
l0r1s ea8700e
added MaxContributors to runtime
l0r1s 6ec5d63
fix migration name
l0r1s d49c04e
added migration test
l0r1s 6b37119
cargo clippy
l0r1s 96cdf64
cargo fmt
l0r1s ae2d1f0
Merge branch 'devnet-ready' into l0r1s/crowdloan-contributors-limit
l0r1s 016e7f3
propagate log/std in Cargo.toml
l0r1s 08dd35e
move contributors count to Crowdloan struct and update migration
l0r1s e79e783
fix benchmark
l0r1s 3f565fb
fix arithmetic
l0r1s 79490d3
added freeze_struct to OldCrowdloanInfo
l0r1s 32b124f
Merge branch 'devnet-ready' into l0r1s/crowdloan-contributors-limit
l0r1s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
pallets/crowdloan/src/migrations/migrate_add_contributors_count.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| use alloc::string::String; | ||
| use frame_support::{BoundedVec, migration::storage_key_iter, traits::Get, weights::Weight}; | ||
| use subtensor_macros::freeze_struct; | ||
|
|
||
| use crate::*; | ||
|
|
||
| mod old_storage { | ||
| use super::*; | ||
|
|
||
| #[freeze_struct("84bcbf9b8d3f0ddf")] | ||
| #[derive(Encode, Decode, Debug)] | ||
| pub struct OldCrowdloanInfo<AccountId, Balance, BlockNumber, Call> { | ||
| pub creator: AccountId, | ||
| pub deposit: Balance, | ||
| pub min_contribution: Balance, | ||
| pub end: BlockNumber, | ||
| pub cap: Balance, | ||
| pub funds_account: AccountId, | ||
| pub raised: Balance, | ||
| pub target_address: Option<AccountId>, | ||
| pub call: Option<Call>, | ||
| pub finalized: bool, | ||
| } | ||
| } | ||
|
|
||
| pub fn migrate_add_contributors_count<T: Config>() -> Weight { | ||
| let migration_name = BoundedVec::truncate_from(b"migrate_add_contributors_count".to_vec()); | ||
| let mut weight = T::DbWeight::get().reads(1); | ||
|
|
||
| if HasMigrationRun::<T>::get(&migration_name) { | ||
| log::info!( | ||
| "Migration '{:?}' has already run. Skipping.", | ||
| migration_name | ||
| ); | ||
| return weight; | ||
| } | ||
|
|
||
| log::info!( | ||
| "Running migration '{}'", | ||
| String::from_utf8_lossy(&migration_name) | ||
| ); | ||
|
|
||
| let pallet_name = b"Crowdloan"; | ||
| let item_name = b"Crowdloans"; | ||
| let crowdloans = storage_key_iter::< | ||
| CrowdloanId, | ||
| old_storage::OldCrowdloanInfo< | ||
| T::AccountId, | ||
| BalanceOf<T>, | ||
| BlockNumberFor<T>, | ||
| BoundedCallOf<T>, | ||
| >, | ||
| Twox64Concat, | ||
| >(pallet_name, item_name) | ||
| .collect::<Vec<_>>(); | ||
| weight = weight.saturating_add(T::DbWeight::get().reads(crowdloans.len() as u64)); | ||
|
|
||
| for (id, crowdloan) in crowdloans { | ||
| let contributions = Contributions::<T>::iter_key_prefix(id) | ||
| .collect::<Vec<_>>() | ||
| .len(); | ||
| weight = weight.saturating_add(T::DbWeight::get().reads(contributions as u64)); | ||
|
|
||
| Crowdloans::<T>::insert( | ||
| id, | ||
| CrowdloanInfo { | ||
| creator: crowdloan.creator, | ||
| deposit: crowdloan.deposit, | ||
| min_contribution: crowdloan.min_contribution, | ||
| end: crowdloan.end, | ||
| cap: crowdloan.cap, | ||
| funds_account: crowdloan.funds_account, | ||
| raised: crowdloan.raised, | ||
| target_address: crowdloan.target_address, | ||
| call: crowdloan.call, | ||
| finalized: crowdloan.finalized, | ||
| contributors_count: contributions as u32, | ||
| }, | ||
| ); | ||
| weight = weight.saturating_add(T::DbWeight::get().writes(1)); | ||
| } | ||
|
|
||
| HasMigrationRun::<T>::insert(&migration_name, true); | ||
| weight = weight.saturating_add(T::DbWeight::get().writes(1)); | ||
|
|
||
| log::info!( | ||
| "Migration '{:?}' completed successfully.", | ||
| String::from_utf8_lossy(&migration_name) | ||
| ); | ||
|
|
||
| weight | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use frame_support::{Hashable, storage::unhashed::put_raw}; | ||
| use sp_core::U256; | ||
| use sp_io::hashing::twox_128; | ||
|
|
||
| use super::*; | ||
| use crate::mock::{Test, TestState}; | ||
|
|
||
| #[test] | ||
| fn test_migrate_add_contributors_count_works() { | ||
| TestState::default().build_and_execute(|| { | ||
| let pallet_name = twox_128(b"Crowdloan"); | ||
| let storage_name = twox_128(b"Crowdloans"); | ||
| let prefix = [pallet_name, storage_name].concat(); | ||
|
|
||
| let items = vec![ | ||
| ( | ||
| old_storage::OldCrowdloanInfo { | ||
| creator: U256::from(1), | ||
| deposit: 100u64, | ||
| min_contribution: 10u64, | ||
| end: 100u64, | ||
| cap: 1000u64, | ||
| funds_account: U256::from(2), | ||
| raised: 0u64, | ||
| target_address: None, | ||
| call: None::<BoundedCallOf<Test>>, | ||
| finalized: false, | ||
| }, | ||
| vec![(U256::from(1), 100)], | ||
| ), | ||
| ( | ||
| old_storage::OldCrowdloanInfo { | ||
| creator: U256::from(1), | ||
| deposit: 100u64, | ||
| min_contribution: 10u64, | ||
| end: 100u64, | ||
| cap: 1000u64, | ||
| funds_account: U256::from(2), | ||
| raised: 0u64, | ||
| target_address: None, | ||
| call: None::<BoundedCallOf<Test>>, | ||
| finalized: false, | ||
| }, | ||
| vec![ | ||
| (U256::from(1), 100), | ||
| (U256::from(2), 100), | ||
| (U256::from(3), 100), | ||
| ], | ||
| ), | ||
| ( | ||
| old_storage::OldCrowdloanInfo { | ||
| creator: U256::from(1), | ||
| deposit: 100u64, | ||
| min_contribution: 10u64, | ||
| end: 100u64, | ||
| cap: 1000u64, | ||
| funds_account: U256::from(2), | ||
| raised: 0u64, | ||
| target_address: None, | ||
| call: None::<BoundedCallOf<Test>>, | ||
| finalized: false, | ||
| }, | ||
| vec![ | ||
| (U256::from(1), 100), | ||
| (U256::from(2), 100), | ||
| (U256::from(3), 100), | ||
| (U256::from(4), 100), | ||
| (U256::from(5), 100), | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| for (id, (crowdloan, contributions)) in items.into_iter().enumerate() { | ||
| let key = [prefix.clone(), (id as u32).twox_64_concat()].concat(); | ||
| put_raw(&key, &crowdloan.encode()); | ||
|
|
||
| for (contributor, amount) in contributions { | ||
| Contributions::<Test>::insert(id as u32, contributor, amount); | ||
| } | ||
| } | ||
|
|
||
| migrate_add_contributors_count::<Test>(); | ||
|
|
||
| assert!(Crowdloans::<Test>::get(0).is_some_and(|c| c.contributors_count == 1)); | ||
| assert!(Crowdloans::<Test>::get(1).is_some_and(|c| c.contributors_count == 3)); | ||
| assert!(Crowdloans::<Test>::get(2).is_some_and(|c| c.contributors_count == 5)); | ||
|
|
||
| assert!(HasMigrationRun::<Test>::get(BoundedVec::truncate_from( | ||
| b"migrate_add_contributors_count".to_vec() | ||
| ))); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| mod migrate_add_contributors_count; | ||
| pub use migrate_add_contributors_count::*; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we get away without the migration since the crowdloan code didn't get to mainnet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, there is only 1 crowdloan on the testnet at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's likely dependent on the next release date.