Skip to content

Commit

Permalink
πŸ‘¨πŸ»β€πŸ’» Allow polimec receiver pallet to have any index (#327)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
JuaniRios committed Jun 20, 2024
1 parent 2aa7db7 commit 205b7c4
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions integration-tests/penpal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ construct_runtime!(

// The main stage.
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>} = 50,
PolimecReceiver: polimec_receiver = 51,
Vesting: pallet_vesting = 52,
Vesting: pallet_vesting = 51,
PolimecReceiver: polimec_receiver = 69,

Sudo: pallet_sudo::{Pallet, Call, Storage, Event<T>, Config<T>} = 255,
}
Expand Down
24 changes: 18 additions & 6 deletions pallets/funding/src/functions/7_ct_migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use xcm::v3::MaxPalletNameLen;

impl<T: Config> Pallet<T> {
#[transactional]
Expand Down Expand Up @@ -281,7 +282,7 @@ impl<T: Config> Pallet<T> {
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::<T>::MigrationCheckResponseAccepted {
project_id,
query_id,
Expand All @@ -302,14 +303,21 @@ impl<T: Config> Pallet<T> {
(
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<u8, MaxPalletNameLen> =
BoundedVec::try_from("polimec_receiver".as_bytes().to_vec()).map_err(|_| Error::<T>::NotAllowed)?;
let Some(PalletInfo { index, module_name, .. }) = pallets_info.first() else {
return Err(Error::<T>::NotAllowed.into());
};
let u8_index: u8 = (*index).try_into().map_err(|_| Error::<T>::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::<T>::MigrationCheckResponseAccepted { project_id, query_id, response });
} else {
migration_check.pallet_check.1 = CheckOutcome::Failed;
Self::deposit_event(Event::<T>::MigrationCheckResponseRejected { project_id, query_id, response });
},
}
},
_ => return Err(Error::<T>::NotAllowed.into()),
};

Expand Down Expand Up @@ -341,10 +349,14 @@ impl<T: Config> Pallet<T> {
let query_id =
pallet_xcm::Pallet::<T>::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::<T>::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);

<pallet_xcm::Pallet<T>>::send_xcm(Here, project_multilocation, xcm).map_err(|_| Error::<T>::XcmFailed)?;
ActiveMigrationQueue::<T>::insert(query_id, (project_id, participant.clone()));
Expand Down
6 changes: 4 additions & 2 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,17 @@ impl<T: Config> Pallet<T> {
pub fn construct_migration_xcm_message(
migrations: BoundedVec<Migration, MaxParticipationsPerUser<T>>,
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.
Expand Down
4 changes: 0 additions & 4 deletions pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,6 @@ pub mod pallet {
#[pallet::constant]
type PalletId: Get<PalletId>;

/// Pallet info of the polimec receiver pallet. Used for CT migrations
#[pallet::constant]
type PolimecReceiverInfo: Get<PalletInfo>;

/// The maximum size of a preimage allowed, expressed in bytes.
#[pallet::constant]
type PreImageLimit: Get<u32>;
Expand Down
1 change: 0 additions & 1 deletion pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions pallets/funding/src/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions pallets/funding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PalletIndex>),
Failed,
}

Expand Down
1 change: 0 additions & 1 deletion runtimes/polimec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetId, Price, Oracle>;
Expand Down
1 change: 0 additions & 1 deletion runtimes/politest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetId, FixedU128, Oracle>;
Expand Down

0 comments on commit 205b7c4

Please sign in to comment.