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

CodeSee Demo Review Fork #3

Closed
wants to merge 28 commits into from
Closed

Conversation

mattheworris
Copy link
Owner

@mattheworris mattheworris commented Dec 4, 2023

Goal

The goal of this PR is to replace the Currency trait with the fungible trait.

Closes frequency-chain#942
Closes frequency-chain#1532

Discussion

The following Parity issues/PRs were used as references for changes:
Deprecate Currency - PR 12951 (Explanation of necessary changes.)
FRAME: Move pallets over to use fungible traits (Issue to track Parity's efforts to update their pallets.)
pallet vesting / update to use fungible (Example of some necessary changes.)

Changes

  • .cargo-deny.toml Added "multiformats" for cid crate to fix warning.
  • vscode/settings.json Added script that sets up the source map for the rust std library files for debugging.
  • Cargo.lock cargo updated.
  • e2e/package-lock.json npm updated.
  • Replaced traits as needed: Use tokens::fungible::
    • InspectFungible for balance() and reducible_balance()
    • InspectFreeze for balance_frozen()
    • Mutate for set_balance(), mint_into()
    • MutateFreeze for set_freeze(), and thaw()
  • Added pub enum FreezeReason to support freezes
  • Updated error handling as set_freeze() and thaw() can fail, so errors needed to be propagated.
  • Updated mocks to set MaxLocks, MaxReserves, MaxFreezes, and MaxHolds to 50, to match Balances pallet config.
  • Updated runtime pallet configs to use BalancesMaxXXXXXs
  • Updated tests with .expect() where set_freeze() or thaw() can fail.
  • FreezeIdentifier and RuntimeFreezeReason configured with defaults.

How to Review

  • Read through Deprecate Currency - PR 12951 to understand context and check that Currency traits were properly replaced with fungible traits.
  • Check impact of changing to set_freeze() and thaw() which can now fail and make sure all error states are propagated correctly without possibility for panic
  • Check if balance() is used correctly, or should be changed to reducible_balance(). The calculations evaluating the Existential Deposit (ED) have been updated and Parity comments indicate that reducible_balance() is most likely the value needed.

Checklist

  • Chain spec updated
  • Custom RPC OR Runtime API added/changed? Updated js/api-augment.
  • Design doc(s) updated
  • Tests added
  • Benchmarks added
  • Weights updated

Matthew Orris and others added 25 commits November 17, 2023 16:21
…942-capacity-replace-currency-trait-with-fungible-trait
@mattheworris mattheworris self-assigned this Dec 4, 2023
@ghost
Copy link

ghost commented Dec 4, 2023

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@@ -100,6 +101,16 @@ pub mod pallet {
};
use sp_runtime::traits::{AtLeast32BitUnsigned, MaybeDisplay};

/// A reason for freezing funds.
#[pallet::composite_enum]
pub enum FreezeReason {
Copy link
Owner Author

Choose a reason for hiding this comment

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

FreezeReason is required for the fungible traits. The enum is defined and used by capacity and time-release. Comments in the Parity deprecation pr indicated that there should only be one enum:

Both Holds and Freezes require an identifier which is configurable and is expected to be an enum aggregated across all pallet instances of the runtime.

This enum is scoped to each pallet, but we may want to move it to a common location within the runtime, if possible.

/// Function that allows a balance to be locked.
type Currency: LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>;
/// Functions that allow a fungible balance to be changed or frozen.
type Currency: MutateFreeze<Self::AccountId, Id = Self::RuntimeFreezeReason>
Copy link
Owner Author

Choose a reason for hiding this comment

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

Updating Frequency's type Currency with the traits from frame_support::traits::tokens::fungible

@@ -562,7 +579,7 @@ impl<T: Config> Pallet<T> {
staker: &T::AccountId,
proposed_amount: BalanceOf<T>,
) -> BalanceOf<T> {
let account_balance = T::Currency::free_balance(&staker);
let account_balance = T::Currency::balance(&staker);
Copy link
Owner Author

Choose a reason for hiding this comment

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

Some fungible methods require name changes: free_balance -> balance

@@ -589,9 +606,9 @@ impl<T: Config> Pallet<T> {
let staking_account = Self::get_staking_account_for(staker).unwrap_or_default();
let total_locked = staking_account.active.saturating_add(total_unlocking);
if total_locked.is_zero() {
T::Currency::remove_lock(STAKING_ID, &staker);
T::Currency::thaw(&FreezeReason::Staked.into(), staker)?;
Copy link
Owner Author

Choose a reason for hiding this comment

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

remove_lock was infallible and did not return anything. thaw returns a result, so do_withdraw_unstaked needs to propagate any errors.

type RuntimeFreezeReason: From<FreezeReason>;

/// We need MaybeSerializeDeserialize because of the genesis config.
type Balance: Balance + MaybeSerializeDeserialize;
Copy link
Owner Author

Choose a reason for hiding this comment

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

In time-release, the Frequency type Balance needed to be updated to include the serialize/deserialize traits.

@@ -467,6 +468,7 @@ pub type MaxReleaseSchedules = ConstU32<{ MAX_RELEASE_SCHEDULES }>;
// the descriptions of these configs.
impl pallet_time_release::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
Copy link
Owner Author

Choose a reason for hiding this comment

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

Here we update type Balance because time-release adds the MaybeSerializeDeserialize trait.

#[codec(index = 0)]
Staked,
/// Funds are currently locked and are not yet liquid.
NotYetVested,
Copy link
Owner Author

Choose a reason for hiding this comment

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

Remove this value if enum stays in this pallet.

assert_eq!(14u64, Balances::locks(&staker)[0].amount);
assert_eq!(
14u64,
<Test as Config>::Currency::balance_frozen(&FreezeReason::Staked.into(), &staker)
Copy link
Owner Author

Choose a reason for hiding this comment

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

Check to see if line can be shorter by removing Test as Config.

"Account do not have enough balance"
);

T::Currency::set_lock(
RELEASE_LOCK_ID,
let _ = T::Currency::set_freeze(
Copy link
Owner Author

Choose a reason for hiding this comment

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

Handle Result.

type MaxHolds = ConstU32<0>;
type MaxFreezes = ConstU32<0>;
type MaxHolds = BalancesMaxHolds;
type MaxFreezes = BalancesMaxFreezes;
Copy link
Owner Author

Choose a reason for hiding this comment

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

Need more research on Locks/Freezes and how they affect time-release pallet, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant