From 51c721d33df37b277a5f733f726a2450046e2e3c Mon Sep 17 00:00:00 2001 From: Michael Eberhardt Date: Fri, 8 Mar 2024 14:37:07 +0100 Subject: [PATCH] feat: unique usage of balance type in interfaces --- pallet/src/balance.rs | 20 +++---------- pallet/src/lib.rs | 33 +++++++++++++++------- pallet/src/mock.rs | 1 + pallet/src/signer.rs | 65 ++++++++++++++++++------------------------- 4 files changed, 55 insertions(+), 64 deletions(-) diff --git a/pallet/src/balance.rs b/pallet/src/balance.rs index 2d2916a..5d33114 100644 --- a/pallet/src/balance.rs +++ b/pallet/src/balance.rs @@ -51,10 +51,7 @@ impl From> for StatusCode { /// Balance adapter for providing basic access to balance cheques within the MoveVM. #[derive(Clone)] -pub struct BalanceAdapter -where - BalanceOf: From + Into, -{ +pub struct BalanceAdapter { _pd_config: PhantomData, /// Virtual cheques record of involved users. cheques: Rc>>>, @@ -62,10 +59,7 @@ where initial_state: HashMap>, } -impl BalanceAdapter -where - BalanceOf: From + Into, -{ +impl BalanceAdapter { /// Create a new [`BalanceAdapter`]. pub fn new() -> Self { BalanceAdapter { @@ -180,19 +174,13 @@ where } } -impl Default for BalanceAdapter -where - BalanceOf: From + Into, -{ +impl Default for BalanceAdapter { fn default() -> Self { Self::new() } } -impl BalanceHandler for BalanceAdapter -where - BalanceOf: From + Into, -{ +impl BalanceHandler for BalanceAdapter { type Error = StatusCode; fn transfer( diff --git a/pallet/src/lib.rs b/pallet/src/lib.rs index 622a90a..d4697d3 100644 --- a/pallet/src/lib.rs +++ b/pallet/src/lib.rs @@ -93,9 +93,22 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config { /// The currency mechanism. - type Currency: Currency - + ReservableCurrency - + LockableCurrency; + type Currency: Currency + + ReservableCurrency + + LockableCurrency; + + /// Just the `Currency::Balance` type; we have this item to allow us to + /// constrain it to `From` and `Into`. + type CurrencyBalance: sp_runtime::traits::AtLeast32BitUnsigned + + FullCodec + + Copy + + MaybeSerializeDeserialize + + core::fmt::Debug + + Default + + From + + Into + + TypeInfo + + MaxEncodedLen; /// Maximum life time for requests. #[pallet::constant] @@ -171,8 +184,8 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet - where - BalanceOf: From + Into, + // where + // BalanceOf: From + Into + Default, { fn on_idle(block_height: BlockNumberFor, mut remaining_weight: Weight) -> Weight { let len = MultisigStorage::::iter_keys().count() as u64; @@ -235,8 +248,8 @@ pub mod pallet { // Dispatchable functions must be annotated with a weight and must return a DispatchResult. #[pallet::call] impl Pallet - where - BalanceOf: From + Into, + // where + // BalanceOf: From + Into + Default, { /// Execute Move script transaction sent by the user. // TODO(eiger) in M3: ensure the weight depends on basic extrinsic cost + gas_limit + size of the @@ -288,7 +301,7 @@ pub mod pallet { }; if signer_count > 0 { let lock_id = Self::multi_signer_lock_id(&who, &transaction_bc[..], &cheque_limit); - signature_handler.sign_script(&who, cheque_limit.into(), lock_id)?; + signature_handler.sign_script(&who, &cheque_limit, lock_id)?; } // If the script is signed correctly, we can execute it in MoveVM and update the @@ -413,8 +426,8 @@ pub mod pallet { } impl Pallet - where - BalanceOf: From + Into, + // where + // BalanceOf: From + Into + Default, { // Internal helper for creating new MoveVM instance with StorageAdapter. fn move_vm() -> MvmResult { diff --git a/pallet/src/mock.rs b/pallet/src/mock.rs index 478e516..c7d92b0 100644 --- a/pallet/src/mock.rs +++ b/pallet/src/mock.rs @@ -92,6 +92,7 @@ parameter_types! { impl pallet_move::Config for Test { type Currency = Balances; + type CurrencyBalance = Balance; type MaxLifetimeRequests = MaxLifetimeRequests; type MaxScriptSigners = MaxScriptSigners; type RuntimeEvent = RuntimeEvent; diff --git a/pallet/src/signer.rs b/pallet/src/signer.rs index 121964a..2ba4d42 100644 --- a/pallet/src/signer.rs +++ b/pallet/src/signer.rs @@ -24,7 +24,7 @@ use crate::{ // Some alias definition to make life easier. pub type MaxSignersOf = ::MaxScriptSigners; -pub type MultisigOf = Multisig, BlockNumberFor, MaxSignersOf>; +pub type MultisigOf = Multisig, BalanceOf, BlockNumberFor, MaxSignersOf>; /// This definition stores the hash value of a script transaction. pub type CallHash = [u8; 32]; @@ -59,11 +59,11 @@ impl From for Error { } #[derive(Clone, Eq, PartialEq, Encode, Decode, Default, RuntimeDebug, TypeInfo, MaxEncodedLen)] -pub struct SignerData { +pub struct SignerData { /// State of the user's signature. pub signature: Signature, /// Individual cheque-limit. - pub cheque_limit: u128, + pub cheque_limit: Balance, /// Lock ID for locked currency. pub lock_id: LockIdentifier, } @@ -71,21 +71,23 @@ pub struct SignerData { /// Storage struct definition for a multi-signer request. #[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(Size))] -pub struct Multisig +pub struct Multisig where AccountId: Parameter + Ord + MaybeSerializeDeserialize, + Balance: From + Into + Default, BlockNumber: Parameter + Ord + MaybeSerializeDeserialize + Default, Size: Get, { /// The signers of a script transaction. - signers: BoundedBTreeMap, + signers: BoundedBTreeMap, Size>, /// The block number when this `Multisig` was created and stored. block_height: BlockNumber, } -impl Multisig +impl Multisig where AccountId: Parameter + Ord + MaybeSerializeDeserialize, + Balance: From + Into + Default, BlockNumber: Parameter + Ord + MaybeSerializeDeserialize + Default, Size: Get, { @@ -95,7 +97,7 @@ where return Err(MultisigError::MaxSignersExceeded); } - let mut multisig_info = Multisig:: { + let mut multisig_info = Multisig:: { block_height, ..Default::default() }; @@ -113,22 +115,26 @@ where } } -impl core::ops::Deref for Multisig +impl core::ops::Deref + for Multisig where AccountId: Parameter + Ord + MaybeSerializeDeserialize, + Balance: From + Into + Default, BlockNumber: Parameter + Ord + MaybeSerializeDeserialize + Default, Size: Get, { - type Target = BoundedBTreeMap; + type Target = BoundedBTreeMap, Size>; fn deref(&self) -> &Self::Target { &self.signers } } -impl core::ops::DerefMut for Multisig +impl core::ops::DerefMut + for Multisig where AccountId: Parameter + Ord + MaybeSerializeDeserialize, + Balance: From + Into + Default, BlockNumber: Parameter + Ord + MaybeSerializeDeserialize + Default, Size: Get, { @@ -138,40 +144,30 @@ where } // Because in substrate_move::AccountAddress Default impl is missing. -impl Default for Multisig +impl Default + for Multisig where AccountId: Parameter + Ord + MaybeSerializeDeserialize, + Balance: From + Into + Default, BlockNumber: Parameter + Ord + MaybeSerializeDeserialize + Default, Size: Get, { fn default() -> Self { Multisig { - signers: BoundedBTreeMap::::new(), + signers: BoundedBTreeMap::, Size>::new(), block_height: BlockNumber::default(), } } } /// Script signature handler tracks required signatures for the provided script. -pub(crate) struct ScriptSignatureHandler -where - T: Config + SysConfig, - T::AccountId: Parameter + Ord + MaybeSerializeDeserialize, - BlockNumberFor: Parameter + Ord + MaybeSerializeDeserialize, - BalanceOf: From + Into, -{ +pub(crate) struct ScriptSignatureHandler { _pd_config: PhantomData, /// All required multisig_info. multisig_info: MultisigOf, } -impl ScriptSignatureHandler -where - T: Config + SysConfig, - T::AccountId: Parameter + Ord + MaybeSerializeDeserialize, - BlockNumberFor: Parameter + Ord + MaybeSerializeDeserialize, - BalanceOf: From + Into, -{ +impl ScriptSignatureHandler { /// Creates a new [`ScriptSignatureHandler`] with all blank signatures for the provided script. pub(crate) fn new( accounts: Vec, @@ -194,7 +190,7 @@ where pub(crate) fn sign_script( &mut self, account: &T::AccountId, - cheque_limit: u128, + cheque_limit: &BalanceOf, lock_id: LockIdentifier, ) -> Result<(), Error> { if let Some(ms_data) = self.multisig_info.get_mut(account) { @@ -202,10 +198,9 @@ where Err(Error::::UserHasAlreadySigned) } else { ms_data.signature = Signature::Approved; - ms_data.cheque_limit = cheque_limit; + ms_data.cheque_limit = *cheque_limit; ms_data.lock_id = lock_id; - let amount = BalanceOf::::from(cheque_limit); - T::Currency::set_lock(lock_id, account, amount, WithdrawReasons::all()); + T::Currency::set_lock(lock_id, account, *cheque_limit, WithdrawReasons::all()); Ok(()) } } else { @@ -227,7 +222,7 @@ where for (account, ms_data) in self.multisig_info.iter() { T::Currency::remove_lock(ms_data.lock_id, account); balances - .write_cheque(account, &BalanceOf::::from(ms_data.cheque_limit)) + .write_cheque(account, &ms_data.cheque_limit) .map_err(|_| Error::::InsufficientBalance)?; } @@ -246,13 +241,7 @@ where } } -impl From> for ScriptSignatureHandler -where - T: Config + SysConfig, - T::AccountId: Parameter + Ord + MaybeSerializeDeserialize, - BlockNumberFor: Parameter + Ord + MaybeSerializeDeserialize, - BalanceOf: From + Into, -{ +impl From> for ScriptSignatureHandler { fn from(multisig_info: MultisigOf) -> Self { Self { _pd_config: PhantomData,