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

Full Unbond in Staking #3811

Open
wants to merge 53 commits into
base: master
Choose a base branch
from

Conversation

dharjeezy
Copy link
Contributor

@dharjeezy dharjeezy commented Mar 24, 2024

closes #414

Polkadot address: 12GyGD3QhT4i2JJpNzvMf96sxxBLWymz4RdGCxRH5Rj5agKW

@dharjeezy dharjeezy changed the title Dami/full unbond Full Unbond in Staking Mar 24, 2024
@dharjeezy
Copy link
Contributor Author

@Ank4n according to @kianenigma we want to fully unbond everything after chilling. Therefore, the extrinsic don't need to take any value, also there will be no need to add a check for minimum active bond which you suggested earlier here. #3629 (comment)

Also, if we want to do a chill also when we expect a value, we can definitely do that in the unbond extrinsic. should i go ahead and include that?

@Ank4n
Copy link
Contributor

Ank4n commented Mar 27, 2024

@kianenigma wdyt about call::unbond doing chill implicitly if it is a full unbond (unbond value == ledger.total)?

@kianenigma
Copy link
Contributor

@kianenigma wdyt about call::unbond doing chill implicitly if it is a full unbond (unbond value == ledger.total)?

This is also fine with me, no strong opinion. I like your idea better as it improves UX without wallets needing to do much.

@dharjeezy
Copy link
Contributor Author

OK. So, should we still keep the full unbond extrinsic and also implement the implicit one that @Ank4n is suggesting?

# Conflicts:
#	polkadot/runtime/westend/src/weights/pallet_staking.rs
#	substrate/frame/staking/src/benchmarking.rs
#	substrate/frame/staking/src/pallet/impls.rs
#	substrate/frame/staking/src/pallet/mod.rs
#	substrate/frame/staking/src/weights.rs
@dharjeezy
Copy link
Contributor Author

dharjeezy commented Apr 10, 2024

@Ank4n @kianenigma
I have implicitly added the chill for unbonding

please help review again

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

Looks good, needs more tests (for unbond fully calling chill) and better prdoc and bench etc.

Copy link
Contributor

@Ank4n Ank4n left a comment

Choose a reason for hiding this comment

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

Looking good. I am happy to approve once you fix the default pallet::weight for fn unbond. Few other suggestions but those are just nits.

@@ -1094,86 +1094,21 @@ pub mod pallet {
#[pallet::compact] value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let controller = ensure_signed(origin)?;
let unlocking =
Copy link
Contributor

Choose a reason for hiding this comment

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

github doesn't allow me to add comment on the non changed line 1090 so adding here.

Add chill weight to the call so in worst case it always adds chill weight.

		#[pallet::call_index(2)]
		#[pallet::weight(
            T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS).saturating_add(T::WeightInfo::unbond())
			.saturating_add(T::WeightInfo::Chill()))
        ]

Additional info: Before any transaction this fund is reserved, and any difference in the PostInfo is refunded. So this should always contain the worst case weight.

Self::deposit_event(Event::<T>::Unbonded { stash, amount: value });
if value >= ledger.total {
Self::chill_stash(&ledger.stash);
total_weight = total_weight.saturating_add(T::WeightInfo::chill());
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
total_weight = total_weight.saturating_add(T::WeightInfo::chill());
total_weight.saturating_accrue(T::WeightInfo::chill());

Comment on lines 4170 to 4175
assert_eq!(*staking_events().last().unwrap(), Event::Unbonded { stash: 11, amount: 1000 });
assert_eq!(
*staking_events().get(staking_events().len() - 2).unwrap(),
Event::Chilled { stash: 11 }
);
})
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would be lot nicer to assert against all events (see this) or do a match (see this)

@Ank4n Ank4n requested a review from gpestana September 18, 2024 12:14
@dharjeezy dharjeezy requested a review from Ank4n October 3, 2024 17:49
@Ank4n
Copy link
Contributor

Ank4n commented Oct 8, 2024

bot fmt

@command-bot
Copy link

command-bot bot commented Oct 8, 2024

@Ank4n https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7537093 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh". 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 27-5680a312-e6a2-4dde-a768-20aeb25c933c to cancel this command or bot cancel to cancel all commands in this pull request.

@Ank4n
Copy link
Contributor

Ank4n commented Oct 8, 2024

cc: @gpestana

@command-bot
Copy link

command-bot bot commented Oct 8, 2024

@Ank4n Command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh" has finished. Result: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7537093 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7537093/artifacts/download.

@Ank4n Ank4n requested review from gpestana and removed request for andresilva and gpestana October 24, 2024 11:53
Copy link
Contributor

@gpestana gpestana left a comment

Choose a reason for hiding this comment

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

I left a few nits and suggestions which when included should wrap up this PR, thanks!

@@ -1087,93 +1087,28 @@ pub mod pallet {
/// See also [`Call::withdraw_unbonded`].
#[pallet::call_index(2)]
#[pallet::weight(
T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS).saturating_add(T::WeightInfo::unbond()))
T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS).saturating_add(T::WeightInfo::unbond()).saturating_add(T::WeightInfo::chill()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS).saturating_add(T::WeightInfo::unbond()).saturating_add(T::WeightInfo::chill()))
T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS)
.saturating_add(T::WeightInfo::unbond())
.saturating_add(T::WeightInfo::chill()))

Or something along these lines. Have you ran rustfmt against the current code? At least the clippy CI job seems to be failing.

Copy link
Contributor Author

@dharjeezy dharjeezy Oct 26, 2024

Choose a reason for hiding this comment

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

I actually ran cargo +nightly fmt before pushing.

Comment on lines 1102 to 1104
if value >= ledger.total {
Self::chill_stash(&ledger.stash);
total_weight.saturating_accrue(T::WeightInfo::chill());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if value >= ledger.total {
Self::chill_stash(&ledger.stash);
total_weight.saturating_accrue(T::WeightInfo::chill());
let mut total_weight = if value >= ledger.total {
Self::chill_stash(&ledger.stash);
T::WeightInfo::chill()
} else {
Zero::zero()
};

nit but how about this? then we can remove let mut total_weight instantiation from line 1100 (but do not forget to saturate_accrue the weight of unbound at the end (line 1111).

#[test]
fn unbond_with_chill_works() {
// Should test:
// * Given an account being bonded [and chosen as a validator](not mandatory)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// * Given an account being bonded [and chosen as a validator](not mandatory)
// * Given a abounded account,

@@ -1087,93 +1087,28 @@ pub mod pallet {
/// See also [`Call::withdraw_unbonded`].
#[pallet::call_index(2)]
#[pallet::weight(
T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS).saturating_add(T::WeightInfo::unbond()))
T::WeightInfo::withdraw_unbonded_kill(SPECULATIVE_NUM_SPANS).saturating_add(T::WeightInfo::unbond()).saturating_add(T::WeightInfo::chill()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also update the rustdoc of this callable and mention that the stash may be chilled if the ledger total amount falls to 0 after unbonding?

Comment on lines 4179 to 4180
let res = Staking::unbond(RuntimeOrigin::signed(11), 1000);
assert!(res.is_ok());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let res = Staking::unbond(RuntimeOrigin::signed(11), 1000);
assert!(res.is_ok());
assert_ok!(Staking::unbond(RuntimeOrigin::signed(11), 1000));

This is cleaner and common practice in frame.

Event::Unbonded { stash: 11, amount: 1000 }
]
));
})
Copy link
Contributor

Choose a reason for hiding this comment

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

One last check is to ensure that the stash is actually chilled instead of only relying on the events above. e.g.

assert!(Nominators::<Test>::get(11).is_none());
assert!(Validators::<Test>::get(11).is_none());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i have done this

Comment on lines 6 to 7
Modifies the `unbond` extrinsic by forcefully chilling and unbonding the full value
if the value to be unbonded is the total of what is bonded
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Modifies the `unbond` extrinsic by forcefully chilling and unbonding the full value
if the value to be unbonded is the total of what is bonded
Modifies the `unbond` extrinsic to forcefully chill stash when unbonding
if the value to be unbonded is the total of what is bonded.

@kianenigma kianenigma removed their request for review October 30, 2024 19:15
@dharjeezy
Copy link
Contributor Author

@gpestana can you please help with review so this can be closed out?

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
None yet
Development

Successfully merging this pull request may close these issues.

Staking force_unbond
5 participants