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 transferrable balance to currency trait to fix balance inconsistency #2038

Closed

Conversation

dnjscksdn98
Copy link

@dnjscksdn98 dnjscksdn98 commented Oct 26, 2023

Description

This PR will resolve the balance inconsistency between Substrate's runtime and Frontier's pallet_evm that occurred after this prior PR was merged. In the mentioned PR, a new concept has been introduced in the balance system such as holds and frozen. However, the update can be disabled by setting some config types to empty tuples in the runtime as below.

type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type MaxHolds = ();

Furthermore, the new interface has been moved to the fungible trait and the old interface is left in the currency trait. From this point it affected the issue to pallet_evm. Currently, pallet_evm's Config trait contains a Currency type, that will mostly be set to pallet_balances. The Currency type has a trait bound specified as below.

type Currency: Currency<Self::AccountId> + Inspect<Self::AccountId>;

The problem is that it contains the Inspect trait that is from fungible, which is the target interface that has been updated to the new balance system. Previously, Inspect was added in order to use the reducible_balance() method to fetch the current transferrable balance of an account. But now this method has been modified to follow the new balance system, which means that even though the new balance system is disabled for a moment, pallet_evm will fetch transferrable balance through the modified logic. As a result, this will lead to balance inconsistency (In the case when an account has locked and reserved balance both).

  • locked < reserved
    • evm balance = substrate balance + locked
  • locked > reserved
    • evm balance = substrate balance + reserved
  • locked == reserved
    • evm balance = substrate balance + locked (or reserved)

So, I added a new method to the currency trait named as transferrable_balance() that has the same functionality as the old version of reducible_balance(). I know that the currency trait is deprecated and will be removed further some day, but for a short term until the balance system is fully migrated, I think this will be a good to go for chains that implements Frontier for now. (This issue affects every chain that has implemented Frontier's pallet_evm such as Moonbeam.)

And for more information, there is another side effect occurred by this issue. Since the evm balance are shown higher than expected, even though the actual balance is insufficient to pay, evm transactions are still capable to be sent. However, the transaction will be stuck in the mempool infinitely until the balance is filled enough to be payed.

Perhaps if this PR has been merged in any good way, the pallet_evm must be updated as below.

  • Remove the Inspect trait from the Config
  • Replace reducible_balance() to transferrable_balance()
    • This will be good to be rollbacked, when it is fully migrated to the new balance system.

@dnjscksdn98 dnjscksdn98 requested review from a team October 26, 2023 05:39
@cla-bot-2021
Copy link

cla-bot-2021 bot commented Oct 26, 2023

User @dnjscksdn98, please sign the CLA here.

@dnjscksdn98 dnjscksdn98 changed the title feature: add transferrable balance to currency trait to fix balance inconsistency Add transferrable balance to currency trait to fix balance inconsistency Oct 26, 2023
@paritytech-review-bot paritytech-review-bot bot requested a review from a team November 2, 2023 01:29
@bkchr bkchr added the T1-FRAME This PR/Issue is related to core FRAME, the framework. label Nov 2, 2023
@@ -487,6 +492,24 @@ where
)
.unwrap_or_else(|_| SignedImbalance::Positive(Self::PositiveImbalance::zero()))
}

fn transferrable_balance(who: &T::AccountId, preservation: Preservation) -> Self::Balance {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to factor this one function out into a LegacyCurrencyTransferrableBalance trait or something?
Than you can require that in pallet_evm like type Currency: Currency<Balance> + LegacyCurrencyTransferrableBalance<Balance>; or so.
It seems like a better solution than editing the existing trait. Do you think it could work?

@paritytech-ci paritytech-ci requested a review from a team November 23, 2023 18:23
@boundless-forest
Copy link
Contributor

I think this is duplicated as #1833 and has been fixed in #2292. It can be closed.

@dnjscksdn98 dnjscksdn98 deleted the alex/transferrable-balance branch July 29, 2024 05:28
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.

5 participants