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

Migrate Currency trait to Fungible in Multisig pallet #3136

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = MaxSignatories;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = MaxSignatories;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = pallet_multisig::weights::SubstrateWeight<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
3 changes: 2 additions & 1 deletion cumulus/parachains/runtimes/people/people-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
3 changes: 2 additions & 1 deletion cumulus/parachains/runtimes/people/people-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_utility::Config for Runtime {
Expand Down
3 changes: 2 additions & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = MaxSignatories;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = MaxSignatories;
type WeightInfo = weights::pallet_multisig::WeightInfo<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,12 @@ parameter_types! {
impl pallet_multisig::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = DepositBase;
type DepositFactor = DepositFactor;
type MaxSignatories = ConstU32<100>;
type WeightInfo = pallet_multisig::weights::SubstrateWeight<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion substrate/frame/multisig/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::*;
use frame_benchmarking::v1::{account, benchmarks};
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
use frame_support::traits::fungible::Unbalanced;

use crate::Pallet as Multisig;

Expand All @@ -37,7 +38,7 @@ fn setup_multi<T: Config>(
let signatory = account("signatory", i, SEED);
// Give them some balance for a possible deposit
let balance = BalanceOf::<T>::max_value();
T::Currency::make_free_balance_be(&signatory, balance);
let _ = T::NativeBalance::write_balance(&signatory, balance);
signatories.push(signatory);
}
signatories.sort();
Expand Down
44 changes: 34 additions & 10 deletions substrate/frame/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ use frame_support::{
PostDispatchInfo,
},
ensure,
traits::{Currency, Get, ReservableCurrency},
traits::{
fungible::{Inspect, MutateHold},
tokens::Precision::BestEffort,
Get,
},
weights::Weight,
BoundedVec,
};
use frame_system::{self as system, pallet_prelude::BlockNumberFor, RawOrigin};
use scale_info::TypeInfo;
use sp_io::hashing::blake2_256;
use sp_runtime::{
traits::{Dispatchable, TrailingZeroInput, Zero},
traits::{Dispatchable, TrailingZeroInput},
DispatchError, RuntimeDebug,
};
use sp_std::prelude::*;
Expand All @@ -85,8 +89,8 @@ macro_rules! log {
};
}

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type BalanceOf<T> =
<<T as Config>::NativeBalance as Inspect<<T as frame_system::Config>::AccountId>>::Balance;

/// A global extrinsic index, formed as the extrinsic index within a block, together with that
/// block's height. This allows a transaction in which a multisig operation of a particular
Expand Down Expand Up @@ -142,8 +146,9 @@ pub mod pallet {
+ GetDispatchInfo
+ From<frame_system::Call<Self>>;

/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
/// The fungible in which multisig balances are held.
type NativeBalance: Inspect<Self::AccountId>
+ MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>;

/// The base amount of currency needed to reserve for creating a multisig execution or to
/// store a dispatch call for later.
Expand All @@ -166,6 +171,9 @@ pub mod pallet {

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;

/// The hold reason when holding funds for starting a multisig operation.
type RuntimeHoldReason: From<HoldReason>;
}

/// The current storage version.
Expand Down Expand Up @@ -247,6 +255,13 @@ pub mod pallet {
},
}

/// A reason for this pallet placing a hold on funds.
#[pallet::composite_enum]
pub enum HoldReason {
/// The funds are held as deposit for the multisig
MultisigHold,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

Expand Down Expand Up @@ -481,8 +496,12 @@ pub mod pallet {
ensure!(m.when == timepoint, Error::<T>::WrongTimepoint);
ensure!(m.depositor == who, Error::<T>::NotOwner);

let err_amount = T::Currency::unreserve(&m.depositor, m.deposit);
debug_assert!(err_amount.is_zero());
T::NativeBalance::release(
&HoldReason::MultisigHold.into(),
&m.depositor,
m.deposit,
BestEffort,
)?;
<Multisigs<T>>::remove(&id, &call_hash);

Self::deposit_event(Event::MultisigCancelled {
Expand Down Expand Up @@ -559,7 +578,12 @@ impl<T: Config> Pallet<T> {
// Clean up storage before executing call to avoid an possibility of reentrancy
// attack.
<Multisigs<T>>::remove(&id, call_hash);
T::Currency::unreserve(&m.depositor, m.deposit);
T::NativeBalance::release(
&HoldReason::MultisigHold.into(),
&m.depositor,
m.deposit,
BestEffort,
)?;

let result = call.dispatch(RawOrigin::Signed(id.clone()).into());
Self::deposit_event(Event::MultisigExecuted {
Expand Down Expand Up @@ -612,7 +636,7 @@ impl<T: Config> Pallet<T> {
// Just start the operation by recording it in storage.
let deposit = T::DepositBase::get() + T::DepositFactor::get() * threshold.into();

T::Currency::reserve(&who, deposit)?;
T::NativeBalance::hold(&HoldReason::MultisigHold.into(), &who, deposit)?;

let initial_approvals =
vec![who.clone()].try_into().map_err(|_| Error::<T>::TooManySignatories)?;
Expand Down
9 changes: 7 additions & 2 deletions substrate/frame/multisig/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ pub mod v1 {

if onchain > 0 {
log!(info, "MigrateToV1 should be removed");
return T::DbWeight::get().reads(1)
return T::DbWeight::get().reads(1);
}

let mut call_count = 0u64;
Calls::<T>::drain().for_each(|(_call_hash, (_data, caller, deposit))| {
T::Currency::unreserve(&caller, deposit);
let _ = T::NativeBalance::release(
&HoldReason::MultisigHold.into(),
&caller,
deposit,
BestEffort,
);
call_count.saturating_inc();
});

Expand Down
3 changes: 2 additions & 1 deletion substrate/frame/multisig/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ impl Contains<RuntimeCall> for TestBaseCallFilter {
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type NativeBalance = Balances;
type DepositBase = ConstU64<1>;
type DepositFactor = ConstU64<1>;
type MaxSignatories = ConstU32<3>;
type WeightInfo = ();
type RuntimeHoldReason = RuntimeHoldReason;
}

use pallet_balances::Call as BalancesCall;
Expand Down
Loading