From 205b7c41dd7c7f5ba9927694397e49d5bcf10f91 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Rios <54085674+JuaniRios@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:44:48 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=A8=F0=9F=8F=BB=E2=80=8D=F0=9F=92=BB?= =?UTF-8?q?=20Allow=20polimec=20receiver=20pallet=20to=20have=20any=20inde?= =?UTF-8?q?x=20(#327)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What? - Allow parachains to have their polimec receiver pallet assigned to any pallet index ## Why? - We should not opinionate that ## How? - We no longer have the config item `PolimecReceiverInfo`. - We previously needed it because the `PalletInfo` items returned on the pallet query were set to private. Now we can just check the pallet name and save the index for later ## Testing? - Modify penpal so the receiver has a different index and run the integration ct_migration tests --- integration-tests/penpal/src/lib.rs | 4 ++-- .../funding/src/functions/7_ct_migration.rs | 24 ++++++++++++++----- pallets/funding/src/functions/misc.rs | 6 +++-- pallets/funding/src/lib.rs | 4 ---- pallets/funding/src/mock.rs | 1 - pallets/funding/src/tests/misc.rs | 1 + pallets/funding/src/types.rs | 6 +++-- runtimes/polimec/src/lib.rs | 1 - runtimes/politest/src/lib.rs | 1 - 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/integration-tests/penpal/src/lib.rs b/integration-tests/penpal/src/lib.rs index 970b66109..90a1a477f 100644 --- a/integration-tests/penpal/src/lib.rs +++ b/integration-tests/penpal/src/lib.rs @@ -672,8 +672,8 @@ construct_runtime!( // The main stage. Assets: pallet_assets::{Pallet, Call, Storage, Event} = 50, - PolimecReceiver: polimec_receiver = 51, - Vesting: pallet_vesting = 52, + Vesting: pallet_vesting = 51, + PolimecReceiver: polimec_receiver = 69, Sudo: pallet_sudo::{Pallet, Call, Storage, Event, Config} = 255, } diff --git a/pallets/funding/src/functions/7_ct_migration.rs b/pallets/funding/src/functions/7_ct_migration.rs index e814f4032..9c2134110 100644 --- a/pallets/funding/src/functions/7_ct_migration.rs +++ b/pallets/funding/src/functions/7_ct_migration.rs @@ -1,4 +1,5 @@ use super::*; +use xcm::v3::MaxPalletNameLen; impl Pallet { #[transactional] @@ -281,7 +282,7 @@ impl Pallet { id: Concrete(MultiLocation { parents: 1, interior: X1(Parachain(pid)) }), fun: Fungible(amount), } if amount >= ct_sold_as_u128 && pid == u32::from(para_id) => { - migration_check.holding_check.1 = CheckOutcome::Passed; + migration_check.holding_check.1 = CheckOutcome::Passed(None); Self::deposit_event(Event::::MigrationCheckResponseAccepted { project_id, query_id, @@ -302,14 +303,21 @@ impl Pallet { ( Response::PalletsInfo(pallets_info), MigrationReadinessCheck { pallet_check: (_, CheckOutcome::AwaitingResponse), .. }, - ) => - if pallets_info.len() == 1 && pallets_info[0] == T::PolimecReceiverInfo::get() { - migration_check.pallet_check.1 = CheckOutcome::Passed; + ) => { + let expected_module_name: BoundedVec = + BoundedVec::try_from("polimec_receiver".as_bytes().to_vec()).map_err(|_| Error::::NotAllowed)?; + let Some(PalletInfo { index, module_name, .. }) = pallets_info.first() else { + return Err(Error::::NotAllowed.into()); + }; + let u8_index: u8 = (*index).try_into().map_err(|_| Error::::NotAllowed)?; + if pallets_info.len() == 1 && module_name == &expected_module_name { + migration_check.pallet_check.1 = CheckOutcome::Passed(Some(u8_index)); Self::deposit_event(Event::::MigrationCheckResponseAccepted { project_id, query_id, response }); } else { migration_check.pallet_check.1 = CheckOutcome::Failed; Self::deposit_event(Event::::MigrationCheckResponseRejected { project_id, query_id, response }); - }, + } + }, _ => return Err(Error::::NotAllowed.into()), }; @@ -341,10 +349,14 @@ impl Pallet { let query_id = pallet_xcm::Pallet::::new_notify_query(project_multilocation, call.into(), now + 20u32.into(), Here); + let CheckOutcome::Passed(Some(pallet_index)) = migration_readiness_check.pallet_check.1 else { + return Err(Error::::NotAllowed.into()); + }; + Self::change_migration_status(project_id, participant.clone(), MigrationStatus::Sent(query_id))?; // * Process Data * - let xcm = Self::construct_migration_xcm_message(migrations.into(), query_id); + let xcm = Self::construct_migration_xcm_message(migrations.into(), query_id, pallet_index); >::send_xcm(Here, project_multilocation, xcm).map_err(|_| Error::::XcmFailed)?; ActiveMigrationQueue::::insert(query_id, (project_id, participant.clone())); diff --git a/pallets/funding/src/functions/misc.rs b/pallets/funding/src/functions/misc.rs index 41697239b..dcc8b86b2 100644 --- a/pallets/funding/src/functions/misc.rs +++ b/pallets/funding/src/functions/misc.rs @@ -573,15 +573,17 @@ impl Pallet { pub fn construct_migration_xcm_message( migrations: BoundedVec>, query_id: QueryId, + pallet_index: PalletIndex, ) -> Xcm<()> { // TODO: adjust this as benchmarks for polimec-receiver are written const MAX_WEIGHT: Weight = Weight::from_parts(10_000, 0); const MAX_RESPONSE_WEIGHT: Weight = Weight::from_parts(700_000_000, 10_000); // const MAX_WEIGHT: Weight = Weight::from_parts(100_003_000_000_000, 10_000_196_608); - let _polimec_receiver_info = T::PolimecReceiverInfo::get(); let migrations_item = Migrations::from(migrations.into()); - let mut encoded_call = vec![51u8, 0]; + // First byte is the pallet index, second byte is the call index + let mut encoded_call = vec![pallet_index, 0]; + // migrations_item can contain a Maximum of MaxParticipationsPerUser migrations which // is 48. So we know that there is an upper limit to this encoded call, namely 48 * // Migration encode size. diff --git a/pallets/funding/src/lib.rs b/pallets/funding/src/lib.rs index 75c4829cf..beb589496 100644 --- a/pallets/funding/src/lib.rs +++ b/pallets/funding/src/lib.rs @@ -361,10 +361,6 @@ pub mod pallet { #[pallet::constant] type PalletId: Get; - /// Pallet info of the polimec receiver pallet. Used for CT migrations - #[pallet::constant] - type PolimecReceiverInfo: Get; - /// The maximum size of a preimage allowed, expressed in bytes. #[pallet::constant] type PreImageLimit: Get; diff --git a/pallets/funding/src/mock.rs b/pallets/funding/src/mock.rs index c29ae3194..e6743df7d 100644 --- a/pallets/funding/src/mock.rs +++ b/pallets/funding/src/mock.rs @@ -424,7 +424,6 @@ impl Config for TestRuntime { type Multiplier = Multiplier; type NativeCurrency = Balances; type PalletId = FundingPalletId; - type PolimecReceiverInfo = PolimecReceiverInfo; type PreImageLimit = ConstU32<1024>; type Price = FixedU128; type PriceProvider = ConstPriceProvider; diff --git a/pallets/funding/src/tests/misc.rs b/pallets/funding/src/tests/misc.rs index 31845a100..7ce326cb1 100644 --- a/pallets/funding/src/tests/misc.rs +++ b/pallets/funding/src/tests/misc.rs @@ -310,6 +310,7 @@ mod helper_functions { // logic of small functions that extrinsics use to process data or interact with storage mod inner_functions { use super::*; + use parity_scale_codec::Encode; #[test] fn calculate_vesting_duration() { diff --git a/pallets/funding/src/types.rs b/pallets/funding/src/types.rs index a08672829..2fb3ebbf6 100644 --- a/pallets/funding/src/types.rs +++ b/pallets/funding/src/types.rs @@ -834,14 +834,16 @@ pub mod inner_types { impl MigrationReadinessCheck { pub fn is_ready(&self) -> bool { - self.holding_check.1 == CheckOutcome::Passed && self.pallet_check.1 == CheckOutcome::Passed + self.holding_check.1 == CheckOutcome::Passed(None) && + matches!(self.pallet_check.1, CheckOutcome::Passed(Some(_))) } } + pub type PalletIndex = u8; #[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum CheckOutcome { AwaitingResponse, - Passed, + Passed(Option), Failed, } diff --git a/runtimes/polimec/src/lib.rs b/runtimes/polimec/src/lib.rs index 94087c891..01dec0404 100644 --- a/runtimes/polimec/src/lib.rs +++ b/runtimes/polimec/src/lib.rs @@ -1045,7 +1045,6 @@ impl pallet_funding::Config for Runtime { type Multiplier = pallet_funding::types::Multiplier; type NativeCurrency = Balances; type PalletId = FundingPalletId; - type PolimecReceiverInfo = PolimecReceiverInfo; type PreImageLimit = ConstU32<1024>; type Price = Price; type PriceProvider = OraclePriceProvider; diff --git a/runtimes/politest/src/lib.rs b/runtimes/politest/src/lib.rs index 3a1b6e20b..78b483571 100644 --- a/runtimes/politest/src/lib.rs +++ b/runtimes/politest/src/lib.rs @@ -1047,7 +1047,6 @@ impl pallet_funding::Config for Runtime { type Multiplier = pallet_funding::types::Multiplier; type NativeCurrency = Balances; type PalletId = FundingPalletId; - type PolimecReceiverInfo = PolimecReceiverInfo; type PreImageLimit = ConstU32<1024>; type Price = Price; type PriceProvider = OraclePriceProvider;