From 63c6687140ca6bcfff7ca4fafd34791bf6c66137 Mon Sep 17 00:00:00 2001 From: Gabriel Facco de Arruda Date: Sun, 3 Dec 2023 16:25:25 -0300 Subject: [PATCH] refactor: new derivation in pallet rings --- INV4/pallet-inv4/src/inv4_core.rs | 2 +- INV4/pallet-inv4/src/lib.rs | 13 ++-- INV4/pallet-inv4/src/multisig.rs | 2 +- INV4/pallet-inv4/src/origin.rs | 4 +- INV4/pallet-inv4/src/tests/mock.rs | 2 +- INV4/pallet-inv4/src/tests/mod.rs | 20 +++--- INV4/pallet-inv4/src/util.rs | 55 ---------------- OCIF/staking/src/lib.rs | 4 +- pallet-rings/src/lib.rs | 101 +++++++++++++++-------------- 9 files changed, 74 insertions(+), 129 deletions(-) delete mode 100644 INV4/pallet-inv4/src/util.rs diff --git a/INV4/pallet-inv4/src/inv4_core.rs b/INV4/pallet-inv4/src/inv4_core.rs index 046cd877..e5a6a557 100644 --- a/INV4/pallet-inv4/src/inv4_core.rs +++ b/INV4/pallet-inv4/src/inv4_core.rs @@ -1,8 +1,8 @@ use super::pallet::*; use crate::{ + account_derivation::CoreAccountDerivation, fee_handling::{FeeAsset, FeeAssetNegativeImbalance, MultisigFeeHandler}, origin::{ensure_multisig, INV4Origin}, - util::CoreAccountConversion, }; use frame_support::{ pallet_prelude::*, diff --git a/INV4/pallet-inv4/src/lib.rs b/INV4/pallet-inv4/src/lib.rs index a78f778c..e7e671de 100644 --- a/INV4/pallet-inv4/src/lib.rs +++ b/INV4/pallet-inv4/src/lib.rs @@ -31,19 +31,19 @@ mod tests; //pub mod migrations; +pub mod account_derivation; mod dispatch; pub mod fee_handling; pub mod inv4_core; mod lookup; pub mod multisig; pub mod origin; -pub mod util; pub mod voting; pub mod weights; +pub use account_derivation::CoreAccountDerivation; use fee_handling::FeeAsset; pub use lookup::INV4Lookup; -pub use util::CoreAccountConversion; pub use weights::WeightInfo; #[frame_support::pallet] @@ -152,14 +152,9 @@ pub mod pallet { type FeeCharger: MultisigFeeHandler; - #[pallet::constant] - type GenesisHash: Get<::Hash>; - - #[pallet::constant] - type GlobalNetworkId: Get; + const GLOBAL_NETWORK_ID: NetworkId; - #[pallet::constant] - type ParaId: Get; + const PARA_ID: u32; #[pallet::constant] type MaxCallSize: Get; diff --git a/INV4/pallet-inv4/src/multisig.rs b/INV4/pallet-inv4/src/multisig.rs index de26328a..ea4cd0aa 100644 --- a/INV4/pallet-inv4/src/multisig.rs +++ b/INV4/pallet-inv4/src/multisig.rs @@ -1,8 +1,8 @@ use super::pallet::{self, *}; use crate::{ + account_derivation::CoreAccountDerivation, fee_handling::FeeAsset, origin::{ensure_multisig, INV4Origin}, - util::CoreAccountConversion, voting::{Tally, Vote}, }; use core::{ diff --git a/INV4/pallet-inv4/src/origin.rs b/INV4/pallet-inv4/src/origin.rs index 7f080ac9..96a6d204 100644 --- a/INV4/pallet-inv4/src/origin.rs +++ b/INV4/pallet-inv4/src/origin.rs @@ -1,6 +1,6 @@ use crate::{ + account_derivation::CoreAccountDerivation, pallet::{self, Origin, Pallet}, - util::CoreAccountConversion, Config, }; use codec::{Decode, Encode, MaxEncodedLen}; @@ -26,7 +26,7 @@ where } pub fn to_account_id(&self) -> T::AccountId { - Pallet::::derive_core_account(self.id.clone()) + Pallet::::derive_core_account(self.id) } } diff --git a/INV4/pallet-inv4/src/tests/mock.rs b/INV4/pallet-inv4/src/tests/mock.rs index ba0952e8..64f516f4 100644 --- a/INV4/pallet-inv4/src/tests/mock.rs +++ b/INV4/pallet-inv4/src/tests/mock.rs @@ -355,7 +355,7 @@ impl ExtBuilder { (BOB, INITIAL_BALANCE), (CHARLIE, INITIAL_BALANCE), ( - util::derive_core_account::(0u32), + account_derivation::derive_core_account::(0u32), INITIAL_BALANCE, ), ], diff --git a/INV4/pallet-inv4/src/tests/mod.rs b/INV4/pallet-inv4/src/tests/mod.rs index e075c8ce..634727f4 100644 --- a/INV4/pallet-inv4/src/tests/mod.rs +++ b/INV4/pallet-inv4/src/tests/mod.rs @@ -45,7 +45,7 @@ fn create_core_works() { assert_eq!( INV4::core_storage(0u32), Some(CoreInfo { - account: util::derive_core_account::(0u32), + account: account_derivation::derive_core_account::(0u32), metadata: vec![].try_into().unwrap(), minimum_support: Perbill::from_percent(1), required_approval: Perbill::from_percent(1), @@ -77,7 +77,7 @@ fn create_core_works() { assert_eq!( INV4::core_storage(1u32), Some(CoreInfo { - account: util::derive_core_account::(1u32), + account: account_derivation::derive_core_account::(1u32), metadata: vec![1, 2, 3].try_into().unwrap(), minimum_support: Perbill::from_percent(100), required_approval: Perbill::from_percent(100), @@ -160,7 +160,7 @@ fn set_parameters_works() { assert_eq!( INV4::core_storage(0u32), Some(CoreInfo { - account: util::derive_core_account::(0u32), + account: account_derivation::derive_core_account::(0u32), metadata: vec![1, 2, 3].try_into().unwrap(), minimum_support: Perbill::from_percent(100), required_approval: Perbill::from_percent(100), @@ -438,7 +438,7 @@ fn operate_multisig_works() { System::assert_has_event( Event::MultisigExecuted { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: ALICE, call: call.clone(), call_hash: <::Hashing as Hash>::hash_of(&call), @@ -468,7 +468,7 @@ fn operate_multisig_works() { System::assert_has_event( Event::MultisigVoteStarted { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: ALICE, votes_added: Vote::Aye(CoreSeedBalance::get()), call_hash: <::Hashing as Hash>::hash_of(&call), @@ -815,7 +815,7 @@ fn vote_multisig_works() { System::assert_has_event( Event::MultisigVoteAdded { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: BOB, votes_added: Vote::Nay(CoreSeedBalance::get()), current_votes: Tally::from_parts( @@ -866,7 +866,7 @@ fn vote_multisig_works() { System::assert_has_event( Event::MultisigExecuted { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: BOB, call: call2.clone(), call_hash: <::Hashing as Hash>::hash_of(&call2), @@ -1043,7 +1043,7 @@ fn withdraw_vote_multisig_works() { System::assert_has_event( Event::MultisigVoteAdded { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: BOB, votes_added: Vote::Nay(CoreSeedBalance::get()), current_votes: Tally::from_parts( @@ -1093,7 +1093,7 @@ fn withdraw_vote_multisig_works() { System::assert_has_event( Event::MultisigVoteWithdrawn { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: BOB, votes_removed: Vote::Nay(CoreSeedBalance::get()), call_hash: <::Hashing as Hash>::hash_of(&call2), @@ -1134,7 +1134,7 @@ fn withdraw_vote_multisig_works() { System::assert_has_event( Event::MultisigVoteWithdrawn { core_id: 0u32, - executor_account: util::derive_core_account::(0u32), + executor_account: account_derivation::derive_core_account::(0u32), voter: ALICE, votes_removed: Vote::Aye(CoreSeedBalance::get()), call_hash: <::Hashing as Hash>::hash_of(&call2), diff --git a/INV4/pallet-inv4/src/util.rs b/INV4/pallet-inv4/src/util.rs deleted file mode 100644 index 58851024..00000000 --- a/INV4/pallet-inv4/src/util.rs +++ /dev/null @@ -1,55 +0,0 @@ -use crate::{Config, Pallet}; -use codec::{Compact, Decode, Encode}; -use frame_support::traits::Get; -use sp_io::hashing::blake2_256; -use sp_runtime::traits::TrailingZeroInput; -use xcm::latest::{BodyId, BodyPart, Junction, Junctions}; - -/// Generates an `AccountId` using an `IpId` as the seed + the PalletId as salt. -pub fn derive_core_account_old(core_id: CoreId) -> AccountId -where - (T::Hash, CoreId): Encode, -{ - let entropy = ( - //frame_system::Pallet::::block_hash(T::BlockNumber::zero()), - T::GenesisHash::get(), - core_id, - ) - .using_encoded(blake2_256); - - Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) - .expect("infinite length input; no invalid inputs for type; qed") -} - -pub trait CoreAccountConversion { - fn derive_core_account(core_id: T::CoreId) -> T::AccountId; - fn core_location(core_id: T::CoreId) -> Junctions; -} - -impl CoreAccountConversion for Pallet -where - T::AccountId: From<[u8; 32]>, -{ - fn derive_core_account(core_id: T::CoreId) -> T::AccountId { - ( - b"GlobalConsensus", - T::GlobalNetworkId::get(), - b"Parachain", - Compact::::from(T::ParaId::get()), - (b"Body", BodyId::Index(core_id.into()), BodyPart::Voice).encode(), - ) - .using_encoded(blake2_256) - .into() - } - - fn core_location(core_id: T::CoreId) -> Junctions { - Junctions::X3( - Junction::GlobalConsensus(T::GlobalNetworkId::get()), - Junction::Parachain(T::ParaId::get()), - Junction::Plurality { - id: BodyId::Index(core_id.into()), - part: BodyPart::Voice, - }, - ) - } -} diff --git a/OCIF/staking/src/lib.rs b/OCIF/staking/src/lib.rs index 34dfb7a3..4d72590b 100644 --- a/OCIF/staking/src/lib.rs +++ b/OCIF/staking/src/lib.rs @@ -93,7 +93,7 @@ pub use pallet::*; pub mod pallet { use pallet_inv4::{ origin::{ensure_multisig, INV4Origin}, - CoreAccountConversion, + CoreAccountDerivation, }; use super::*; @@ -758,7 +758,7 @@ pub mod pallet { )?; let core_account = - as CoreAccountConversion>::derive_core_account(core_id); + as CoreAccountDerivation>::derive_core_account(core_id); ::Currency::resolve_creating(&core_account, reward_imbalance); Self::deposit_event(Event::::CoreClaimed { diff --git a/pallet-rings/src/lib.rs b/pallet-rings/src/lib.rs index 1397eeca..8e6e820c 100644 --- a/pallet-rings/src/lib.rs +++ b/pallet-rings/src/lib.rs @@ -37,12 +37,6 @@ pub mod pallet { type Chains: ChainList; - #[pallet::constant] - type ParaId: Get; - - #[pallet::constant] - type INV4PalletIndex: Get; - #[pallet::constant] type MaxXCMCallLength: Get; @@ -149,19 +143,19 @@ pub mod pallet { Error::::ChainUnderMaintenance ); - let interior = Junctions::X2( - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), - ); + let descend_interior = Junction::Plurality { + id: BodyId::Index(core_id), + part: BodyPart::Voice, + }; let fee_asset_location = fee_asset.get_asset_location(); let beneficiary: MultiLocation = MultiLocation { - parents: 1, + parents: 0, interior: Junctions::X3( - Junction::Parachain(::ParaId::get()), - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), + Junction::GlobalConsensus(::GLOBAL_NETWORK_ID), + Junction::Parachain(::PARA_ID), + descend_interior, ), }; @@ -171,6 +165,10 @@ pub mod pallet { }; let message = Xcm(vec![ + Instruction::UniversalOrigin(Junction::GlobalConsensus( + ::GLOBAL_NETWORK_ID, + )), + Instruction::DescendOrigin(descend_interior.into()), Instruction::WithdrawAsset(fee_multiasset.clone().into()), Instruction::BuyExecution { fees: fee_multiasset, @@ -188,7 +186,7 @@ pub mod pallet { }, ]); - pallet_xcm::Pallet::::send_xcm(interior, dest, message) + pallet_xcm::Pallet::::send_xcm(Junctions::Here, dest, message) .map_err(|_| Error::::SendingFailed)?; Self::deposit_event(Event::CallSent { @@ -223,10 +221,10 @@ pub mod pallet { ensure!(chain == fee_asset.get_chain(), Error::::DifferentChains); - let interior = Junctions::X2( - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), - ); + let descend_interior = Junction::Plurality { + id: BodyId::Index(core_id), + part: BodyPart::Voice, + }; let asset_location = asset.get_asset_location(); @@ -244,11 +242,11 @@ pub mod pallet { }; let core_multilocation: MultiLocation = MultiLocation { - parents: 1, + parents: 0, interior: Junctions::X3( - Junction::Parachain(::ParaId::get()), - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), + Junction::GlobalConsensus(::GLOBAL_NETWORK_ID), + Junction::Parachain(::PARA_ID), + descend_interior, ), }; @@ -258,6 +256,10 @@ pub mod pallet { }; let message = Xcm(vec![ + Instruction::UniversalOrigin(Junction::GlobalConsensus( + ::GLOBAL_NETWORK_ID, + )), + Instruction::DescendOrigin(descend_interior.into()), // Pay execution fees Instruction::WithdrawAsset(fee_multiasset.clone().into()), Instruction::BuyExecution { @@ -277,7 +279,7 @@ pub mod pallet { }, ]); - pallet_xcm::Pallet::::send_xcm(interior, dest, message) + pallet_xcm::Pallet::::send_xcm(Junctions::Here, dest, message) .map_err(|_| Error::::SendingFailed)?; Self::deposit_event(Event::AssetsTransferred { @@ -317,10 +319,10 @@ pub mod pallet { Error::::ChainUnderMaintenance ); - let interior = Junctions::X2( - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), - ); + let descend_interior = Junction::Plurality { + id: BodyId::Index(core_id), + part: BodyPart::Voice, + }; let asset_location = asset.get_asset_location(); @@ -358,33 +360,32 @@ pub mod pallet { }) .map_err(|_| Error::::FailedToReanchorAsset)?; - let beneficiary: MultiLocation = MultiLocation { - parents: 0, - interior: if let Some(to_inner) = to.clone() { - Junctions::X1(Junction::AccountId32 { - network: None, - id: to_inner.into(), - }) - } else { - Junctions::X3( - Junction::Parachain(::ParaId::get()), - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), - ) - }, - }; - let core_multilocation: MultiLocation = MultiLocation { parents: 0, interior: Junctions::X3( - Junction::Parachain(::ParaId::get()), - Junction::PalletInstance(::INV4PalletIndex::get()), - Junction::GeneralIndex(core_id as u128), + Junction::GlobalConsensus(::GLOBAL_NETWORK_ID), + Junction::Parachain(::PARA_ID), + descend_interior, ), }; + let beneficiary: MultiLocation = match to.clone() { + Some(to_inner) => MultiLocation { + parents: 0, + interior: Junctions::X1(Junction::AccountId32 { + network: None, + id: to_inner.into(), + }), + }, + None => core_multilocation, + }; + let message = if asset_location.starts_with(&dest) { Xcm(vec![ + Instruction::UniversalOrigin(Junction::GlobalConsensus( + ::GLOBAL_NETWORK_ID, + )), + Instruction::DescendOrigin(descend_interior.into()), WithdrawAsset(vec![fee_multiasset.clone(), multiasset.clone()].into()), Instruction::BuyExecution { fees: fee_multiasset, @@ -417,6 +418,10 @@ pub mod pallet { ]) } else { Xcm(vec![ + Instruction::UniversalOrigin(Junction::GlobalConsensus( + ::GLOBAL_NETWORK_ID, + )), + Instruction::DescendOrigin(descend_interior.into()), // Pay execution fees Instruction::WithdrawAsset(fee_multiasset.clone().into()), Instruction::BuyExecution { @@ -447,7 +452,7 @@ pub mod pallet { ]) }; - pallet_xcm::Pallet::::send_xcm(interior, from_chain_location, message) + pallet_xcm::Pallet::::send_xcm(Junctions::Here, from_chain_location, message) .map_err(|_| Error::::SendingFailed)?; Self::deposit_event(Event::AssetsBridged {