-
Notifications
You must be signed in to change notification settings - Fork 803
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
[Pools] fix derivation of pool account #4999
Merged
Merged
Changes from 52 commits
Commits
Show all changes
78 commits
Select commit
Hold shift + click to select a range
11a0c44
migration test
Ank4n efd3324
fmt
Ank4n 3f8d2fc
undo pool changes
Ank4n 7388573
fixes all NotEnoughFunds error
Ank4n 6634d25
return false if pool does not exist
Ank4n a3901a9
fix transfer on hold amount should still be held in dst acc
Ank4n 462be1f
fmt
Ank4n c7e9f6c
unexpected errors check
Ank4n 9a68720
all non staking accounts succeed when given minimum balance
Ank4n 268d0a5
assert migrate tests
Ank4n 000762f
remove clone
Ank4n a904fc8
uncomment
Ank4n 0c776b8
incrementing provider fixes users migrating with zero balance
Ank4n 699a2ec
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n c3fa887
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 3d2379e
test for clean up accounts
Ank4n ec9035c
providers are incremented correctly when migration agent/delegators
Ank4n a9d4595
migrated agents/delegators are also cleaned up correctly
Ank4n 8a27ae0
improve remote tests
Ank4n 8f77ac4
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 7fd484a
add runtime api for total member balance
Ank4n 9925a9f
ensure member can't bond extra if they are in unmigrated state
Ank4n 4a9f35f
gate all calls if non migrated state as defensive measure
Ank4n bc3ce11
ensure all calls are gated
Ank4n 5b91644
add runtime api for pool balance
Ank4n 6ceb1ba
remove unused imports
Ank4n de48083
fix import
Ank4n fd8a451
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n ab2317b
fix test
Ank4n cfdaf90
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n d5634c4
add prdoc
Ank4n 0da9807
fix prdoc bump
Ank4n 00f7e6f
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 1514c2b
add missing crate to prdoc
Ank4n 3dee9c4
remove unintended file
Ank4n a52f819
fix derivation of pool account
Ank4n 41c01e6
fix import
Ank4n 85498be
migration code
Ank4n 172b14d
remove released migrations
Ank4n 28fb070
fix clippy
Ank4n 8c96add
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 89c1964
fix agent count
Ank4n 2440303
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 3c24530
Merge branch 'ankan/delegate-stake-remote-tests' into ankan/delegated…
Ank4n 66b5458
remove unnecessary conversion
Ank4n 9ac94ee
fix import
Ank4n 0bef5a9
minor fixes
Ank4n 0c57ab7
missing import
Ank4n 95bf173
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 0115e7c
rustdoc fix
Ank4n c6eafa5
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n b585901
Merge branch 'ankan/delegate-stake-remote-tests' into ankan/delegated…
Ank4n 2e486db
remove condition for minimum balance when migrating delegation
Ank4n c209d9e
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 3e945c4
Merge branch 'ankan/delegate-stake-remote-tests' into ankan/delegated…
Ank4n cbfbf6f
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n acbb674
refactor based on pr comments
Ank4n 65cecb5
allow migration lower than ED
Ank4n eab6f35
Merge branch 'ankan/delegate-stake-remote-tests' into ankan/delegated…
Ank4n 94b2e8b
add prdoc
Ank4n 4081f12
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n 17bccae
Merge branch 'ankan/delegate-stake-remote-tests' into ankan/delegated…
Ank4n 00ab010
move remote tests to another file
Ank4n 7326b10
fix below ed test
Ank4n 8cdde30
move remote tests to another file
Ank4n 99698d6
fix below ed test
Ank4n 5e465f2
revert comment
Ank4n c38f83c
ensure there is atleast ED to transfer
Ank4n b3a3263
fix imports
Ank4n dfcdc42
Update substrate/frame/delegated-staking/src/lib.rs
Ank4n 5082542
Merge branch 'master' into ankan/delegate-stake-remote-tests
Ank4n bce9803
refactor inc dec of providers
Ank4n ae9a370
gate extrinsics on top level function
Ank4n 251ba4c
fix clippy
Ank4n 4ba0c97
remove empty line
Ank4n 56c1075
Merge branch 'ankan/delegate-stake-remote-tests' into ankan/delegated…
Ank4n 6d08f19
Merge branch 'master' into ankan/delegated-staking-fix-derivation
Ank4n a886fa9
fix rust doc
Ank4n 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 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 |
---|---|---|
|
@@ -99,7 +99,7 @@ use sp_runtime::{ | |
IdentityLookup, Keccak256, OpaqueKeys, SaturatedConversion, Verify, | ||
}, | ||
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity}, | ||
ApplyExtrinsicResult, FixedU128, KeyTypeId, Perbill, Percent, Permill, | ||
ApplyExtrinsicResult, FixedU128, KeyTypeId, Percent, Permill, | ||
}; | ||
use sp_staking::SessionIndex; | ||
#[cfg(any(feature = "std", test))] | ||
|
@@ -1759,11 +1759,8 @@ pub type SignedExtra = ( | |
); | ||
|
||
parameter_types! { | ||
// This is the max pools that will be migrated in the runtime upgrade. Westend has more pools | ||
// than this, but we want to emulate some non migrated pools. In prod runtimes, if weight is not | ||
// a concern, it is recommended to set to (existing pools + 10) to also account for any new | ||
// pools getting created before the migration is actually executed. | ||
pub const MaxPoolsToMigrate: u32 = 250; | ||
/// Bounding number of agent pot accounts to be migrated in a single block. | ||
pub const MaxAgentsToMigrate: u32 = 300; | ||
} | ||
|
||
/// All migrations that will run on the next runtime upgrade. | ||
|
@@ -1797,13 +1794,11 @@ pub mod migrations { | |
|
||
/// Unreleased migrations. Add new ones here: | ||
pub type Unreleased = ( | ||
// Migrate NominationPools to `DelegateStake` adapter. This is unversioned upgrade and | ||
// should not be applied yet in Kusama/Polkadot. | ||
pallet_nomination_pools::migration::unversioned::DelegationStakeMigration< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removed migrations are already released. |
||
// This is only needed for Westend. | ||
pallet_delegated_staking::migration::unversioned::ProxyDelegatorMigration< | ||
Runtime, | ||
MaxPoolsToMigrate, | ||
MaxAgentsToMigrate, | ||
>, | ||
pallet_staking::migrations::v15::MigrateV14ToV15<Runtime>, | ||
); | ||
} | ||
|
||
|
@@ -2474,6 +2469,14 @@ sp_api::impl_runtime_apis! { | |
fn member_needs_delegate_migration(member: AccountId) -> bool { | ||
NominationPools::api_member_needs_delegate_migration(member) | ||
} | ||
|
||
fn member_total_balance(member: AccountId) -> Balance { | ||
NominationPools::api_member_total_balance(member) | ||
} | ||
|
||
fn pool_balance(pool_id: pallet_nomination_pools::PoolId) -> Balance { | ||
NominationPools::api_pool_balance(pool_id) | ||
} | ||
} | ||
|
||
impl pallet_staking_runtime_api::StakingApi<Block, Balance, AccountId> for Runtime { | ||
|
@@ -2811,6 +2814,114 @@ mod remote_tests { | |
.unwrap(); | ||
ext.execute_with(|| Runtime::on_runtime_upgrade(UpgradeCheckSelect::PreAndPost)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn delegate_stake_migration() { | ||
// Intended to be run only manually. | ||
if var("RUN_MIGRATION_TESTS").is_err() { | ||
return; | ||
} | ||
use frame_support::assert_ok; | ||
sp_tracing::try_init_simple(); | ||
|
||
let transport: Transport = var("WS").unwrap_or("ws://127.0.0.1:9900".to_string()).into(); | ||
let maybe_state_snapshot: Option<SnapshotConfig> = var("SNAP").map(|s| s.into()).ok(); | ||
let mut ext = Builder::<Block>::default() | ||
.mode(if let Some(state_snapshot) = maybe_state_snapshot { | ||
Mode::OfflineOrElseOnline( | ||
OfflineConfig { state_snapshot: state_snapshot.clone() }, | ||
OnlineConfig { | ||
transport, | ||
state_snapshot: Some(state_snapshot), | ||
pallets: vec![ | ||
"staking".into(), | ||
"system".into(), | ||
"balances".into(), | ||
"nomination-pools".into(), | ||
"delegated-staking".into(), | ||
], | ||
..Default::default() | ||
}, | ||
) | ||
} else { | ||
Mode::Online(OnlineConfig { transport, ..Default::default() }) | ||
}) | ||
.build() | ||
.await | ||
.unwrap(); | ||
ext.execute_with(|| { | ||
// create an account with some balance | ||
let alice = AccountId::from([1u8; 32]); | ||
use frame_support::traits::Currency; | ||
let _ = Balances::deposit_creating(&alice, 100_000 * UNITS); | ||
|
||
// iterate over all pools | ||
pallet_nomination_pools::BondedPools::<Runtime>::iter_keys().for_each(|k| { | ||
if pallet_nomination_pools::Pallet::<Runtime>::api_pool_needs_delegate_migration(k) | ||
{ | ||
assert_ok!( | ||
pallet_nomination_pools::Pallet::<Runtime>::migrate_pool_to_delegate_stake( | ||
RuntimeOrigin::signed(alice.clone()).into(), | ||
k, | ||
) | ||
); | ||
} | ||
}); | ||
|
||
// member migration stats | ||
let mut success = 0; | ||
let mut members_below_ed = 0; | ||
let mut direct_stakers = 0; | ||
let mut unexpected_errors = 0; | ||
|
||
// iterate over all pool members | ||
pallet_nomination_pools::PoolMembers::<Runtime>::iter().for_each(|(k, member)| { | ||
if pallet_nomination_pools::Pallet::<Runtime>::api_member_needs_delegate_migration( | ||
k.clone(), | ||
) { | ||
// reasons migrations can fail: | ||
let is_direct_staker = pallet_staking::Bonded::<Runtime>::contains_key(&k); | ||
let member_balance_below_ed = member.total_balance() < ExistentialDeposit::get(); | ||
|
||
let migration = pallet_nomination_pools::Pallet::<Runtime>::migrate_delegation( | ||
RuntimeOrigin::signed(alice.clone()).into(), | ||
sp_runtime::MultiAddress::Id(k.clone()), | ||
); | ||
|
||
if member_balance_below_ed { | ||
// if the member has balance below ED, the migration should fail. | ||
members_below_ed += 1; | ||
assert_eq!( | ||
migration.unwrap_err(), | ||
pallet_nomination_pools::Error::<Runtime>::MinimumBondNotMet.into() | ||
); | ||
} else if is_direct_staker { | ||
// if the member is a direct staker, the migration should fail until pool | ||
// member unstakes all funds from pallet-staking. | ||
direct_stakers += 1; | ||
assert_eq!( | ||
migration.unwrap_err(), | ||
pallet_delegated_staking::Error::<Runtime>::AlreadyStaking.into() | ||
); | ||
} else if migration.is_err() { | ||
unexpected_errors += 1; | ||
log::error!(target: "remote_test", "Unexpected error {:?} while migrating {:?}", migration.unwrap_err(), k); | ||
} else { | ||
success += 1; | ||
} | ||
} | ||
}); | ||
|
||
log::info!( | ||
target: "remote_test", | ||
"Migration stats: success: {}, members_below_ed: {}, direct_stakers: {}, unexpected_errors: {}", | ||
success, | ||
members_below_ed, | ||
direct_stakers, | ||
unexpected_errors | ||
); | ||
}); | ||
} | ||
} | ||
|
||
mod clean_state_migration { | ||
|
This file contains 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,25 @@ | ||
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 | ||
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json | ||
|
||
title: Ensure as many as possible pool members can migrate to `DelegateStake` | ||
|
||
doc: | ||
- audience: Runtime Dev | ||
description: | | ||
1. Allows pool members to use their total balance while joining pool with `DelegateStake`. | ||
2. Gates call mutating pool or member in unmigrated state. | ||
3. Runtime apis for reading pool and member balance. | ||
|
||
crates: | ||
- name: westend-runtime | ||
bump: minor | ||
- name: kitchensink-runtime | ||
bump: patch | ||
- name: pallet-delegated-staking | ||
bump: patch | ||
- name: pallet-nomination-pools | ||
bump: minor | ||
- name: sp-staking | ||
bump: patch | ||
- name: pallet-nomination-pools-runtime-api | ||
bump: minor |
This file contains 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 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 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
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.
Current count of agents (same as pools) in Westend is 269.
300
should be enough to cover all.