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

Add ConsiderationFromLegacy traits #2274

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

Conversation

NachoPal
Copy link
Contributor

This PR addresses a potential issue that may arise when migrating pallets from using Currency to Consideration. It specifically addresses scenarios where a pallet's migrations involve a Deposit that hasn't been constant over time (e.g. increased by a runtime upgrade).

During the migration, the new HoldConsideration results in a fixed Balance based on a constant Footprint and a Convert function. For instance, if the new HoldConsideration is expected to have a Balance of 10, what happens when, for a particular account, the stored Deposit is 5? After calling Currency::unreserve(account, 5), attempting to create a new ticket to hold a Balance of 10 might fail due to insufficient balance. Currently, HoldConsideration::new() returns either Ok(()) or Err. What should be done if it's not possible to hold the balance?

Following a similar approach to #1813 (comment), I introduced two new traits:

  • FreezeConsiderationFromLegacy
  • HoldConsiderationFromLegacy

These traits extend Consideration with a new method new_from_exact() which proves useful when a new ticket with a precise balance is needed (as illustrated in the migration example) rather than deriving it from a footprint.

@NachoPal NachoPal requested review from a team November 10, 2023 16:28
@NachoPal NachoPal added the T1-FRAME This PR/Issue is related to core FRAME, the framework. label Nov 10, 2023
@NachoPal NachoPal enabled auto-merge (squash) November 10, 2023 16:39
Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

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

Now it looks clean, thanks!

(test could still be good though)

substrate/frame/support/src/traits/tokens/fungible/mod.rs Outdated Show resolved Hide resolved
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Comment on lines 90 to 93
Self: Sized,
{
Ok(None)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This default impl feels like a footgun to me

Suggested change
Self: Sized,
{
Ok(None)
}
Self: Sized;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replied why of the default impl in the next comment

Comment on lines 109 to 112
Self: Sized,
{
Ok(None)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

same, not sure why anyone would want to use this default impl

Suggested change
Self: Sized,
{
Ok(None)
}
Self: Sized;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The FRAME pallet that makes use of it on its Config would look something like this:

  type Consideration: Consideration<Self::AccountId> + HoldConsiderationFromLegacy<Self::AccountId, BalanceOf<T>>

The default impl is for 3rd parties that make use of the pallet, so then they do not have to manually implement it.

{
/// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately
/// be consumed through `update` or `drop` once a footprint changes or is removed.
fn new_from_exact(_who: &A, _new: F::Balance) -> Result<Option<Self>, DispatchError>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do these return Result<Option<Self>, DispatchError> instead of Result<Self, DispatchError>?

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 believe I implemented that to return None as the default impl. As mentioned earlier, ConsiderationFromLegacy will be required for those pallets that need it during the migration.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I'm still not convinced

  1. this feels like a hack to pass a type indirectly to a migration.. As discussed in dm, I'd like to check if it's possible to pass this type directly to the migration if indeed it is only needed in the migration, not the pallet
  2. there are genuine use cases for when you do want to use this directly in the pallet, e.g. @gupnik is working on a lazy migration right now where we're discussing potentially using it

Copy link
Member

Choose a reason for hiding this comment

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

I guess the default impl would never be used and is only meant to make it compile once all currency related crates were removed from the runtime config.
But then it should indeed return an Error and not None.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah would feel much better returning an error to me instead of the None 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

I still would like to see if possible to avoid the default impl (and polluting the pallet config) by passing this type directly to the migration it is used in tho.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liamaharon @ggwpez I replaced None with Err. It should be now good to go.

For reference, here an example of the new method being used: https://github.com/paritytech/polkadot-sdk/pull/1789/files#diff-489bbb1c7c1b1b8803ab344f63079bdc1b7d67db3d2d8809d6a3a755443619f0R77-R107

@NachoPal NachoPal added the R0-silent Changes should not be mentioned in any release notes label Jan 26, 2024
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez ggwpez removed the R0-silent Changes should not be mentioned in any release notes label Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants