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

[Staking] Currency <> Fungible migration #5501

Open
wants to merge 70 commits into
base: ankan/staking-migrate-currency-to-fungible
Choose a base branch
from

Conversation

Ank4n
Copy link
Contributor

@Ank4n Ank4n commented Aug 27, 2024

Migrate staking currency from traits::LockableCurrency to traits::fungible::holds.

Summary of changes

  • Config: Currency becomes of type Fungible while OldCurrency is the LockableCurrency used before.
  • Lazy migration of accounts. Any ledger updates will create a new hold with no extra reads/writes. A permissionless extrinsic migrate_currency() releases the old lock along with some housekeeping.
  • Staking now adds a provider instead of a consumer. More details later.
  • If hold cannot be applied to all stake, the un-holdable part is force withdrawn from the ledger.
  • Reap stash might fail now if provider cannot be decremented.

Migration stats

Polkadot

Total accounts that can be migrated: 61752
Accounts failing to migrate: 1
Accounts with some stake force withdrawn: 35
Total force withdrawal: 16287.7 DOT

Kusama

Total accounts that can be migrated: 26835
Accounts failing to migrate: 0
Accounts with some stake force withdrawn: 59
Total force withdrawal: 14.5 KSM

Full logs here. Error in polkadot is caused due to the ledger corruption, will be fixed by this runtime update. It is possible some of the force withdraws are happening because of ledger corruption and the migration stats might change after those ledgers are fixed.

Note about locks (freeze) vs holds

With locks or freezes, staking could use total balance of an account. But with holds, the account needs to be left with at least Existential Deposit in free balance. This would also affect nomination pools which till now has been able to stake all funds contributed to it. An alternate version of this PR is #5658 where staking pallet does not add any provider, but means pools and delegated-staking pallet has to provide for these accounts and makes the end to end logic (of provider and consumer ref) lot less intuitive and prone to bug.

One way to handle this issue is add a provider to staking stash. This then replicates old behaviour of staking where accounts could stake their whole balance. The side effect of this is that, for stakers that have been slashed to zero, their stash may not be reaped until all consumers to this account is removed. This is because, dec_provider would fail until consumer == 0.

Note about providers and consumers

Before

pallet-staking added consumers for stash account. This meant the agent/pool account must need a provider (by holding ED or system inc provider). We did an explicit inc provider for agents in pallet-delegated staking to allow consumer inc.

Now

This is more simplified. pallet-staking adds provider for stash accounts, but no consumer. pallet-delegated-staking or pools does not need to add provider anymore (or consumer), and as stated before, helps retain the old behaviour of being able to stake all balance via both direct staking or pool.

TODO

  • Permissionless call to release lock.
  • Migration of consumer (dec) and provider (inc) for direct stakers.
  • force unstake if hold cannot be applied to all stake.
  • Fix try state checks (it thinks nothing is staked for unmigrated ledgers).
  • Bench migrate_currency.
  • Add migration to decrement agent provider.
  • Ensure total issuance is upto date when minting rewards.

Followup

balance hold checks both frozen and reserved

wip: around 25 tests failing

check Holds instead of locks

20 tests failing

fmt

11 fails

4 fails

2 failing

1 fail

all tests pass but pending a hygiene check of code

fix compile

minor refactor

remove T::Currency calls from asset mod
@Ank4n Ank4n changed the base branch from master to ankan/staking-migrate-currency-to-fungible August 27, 2024 12:20
Ank4n and others added 24 commits August 27, 2024 14:45
@Ank4n Ank4n added the T2-pallets This PR/Issue is related to a particular pallet. label Sep 16, 2024
@Ank4n Ank4n marked this pull request as ready for review September 17, 2024 11:22
@Ank4n Ank4n requested review from acatangiu and a team as code owners September 17, 2024 11:22
@Ank4n
Copy link
Contributor Author

Ank4n commented Sep 17, 2024

bot bench substrate-pallet --pallet=pallet_staking

@command-bot
Copy link

command-bot bot commented Sep 17, 2024

@Ank4n https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7361897 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/bench/bench.sh" --subcommand=pallet --runtime=dev --target_dir=substrate --pallet=pallet_staking. Check out https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 2-1fbd27ff-ba29-4ae6-8984-1739e2d7b51b to cancel this command or bot cancel to cancel all commands in this pull request.

@Ank4n Ank4n requested a review from gpestana September 17, 2024 12:00
@@ -131,19 +131,13 @@ impl<T: Config> AgentLedger<T> {
///
/// Increments provider count if this is a new agent.
pub(crate) fn update(self, key: &T::AccountId) {
if !<Agents<T>>::contains_key(key) {
// This is a new agent. Provide for this account.
frame_system::Pallet::<T>::inc_providers(key);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agents don't need provider anymore as staking account does not have consumers.
TODO: add migration for agents to dec provider.

…=dev --target_dir=substrate --pallet=pallet_staking
@command-bot
Copy link

command-bot bot commented Sep 17, 2024

@Ank4n Command "$PIPELINE_SCRIPTS_DIR/commands/bench/bench.sh" --subcommand=pallet --runtime=dev --target_dir=substrate --pallet=pallet_staking has finished. Result: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7361897 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7361897/artifacts/download.

@Ank4n
Copy link
Contributor Author

Ank4n commented Sep 17, 2024

bot bench polkadot-pallet --pallet=pallet_staking --runtime=westend

@command-bot
Copy link

command-bot bot commented Sep 17, 2024

@Ank4n https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7363513 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/bench/bench.sh" --subcommand=pallet --runtime=westend --target_dir=polkadot --pallet=pallet_staking. Check out https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 3-f9f236a6-cae9-4abe-ac2c-7575afc7a749 to cancel this command or bot cancel to cancel all commands in this pull request.

…=westend --target_dir=polkadot --pallet=pallet_staking
@command-bot
Copy link

command-bot bot commented Sep 17, 2024

@Ank4n Command "$PIPELINE_SCRIPTS_DIR/commands/bench/bench.sh" --subcommand=pallet --runtime=westend --target_dir=polkadot --pallet=pallet_staking has finished. Result: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7363513 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7363513/artifacts/download.

use frame_support::traits::fungible::Balanced;
let numeric_amount = credit.peek();

let _ = T::Currency::resolve(&Pallet::<T>::account_id(), credit);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-check if this is okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T2-pallets This PR/Issue is related to a particular pallet.
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

3 participants