Skip to content

Commit

Permalink
fix: Wrong core location when dest is relay
Browse files Browse the repository at this point in the history
  • Loading branch information
arrudagates committed Mar 21, 2024
1 parent b416567 commit 8b2fba2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
33 changes: 25 additions & 8 deletions pallet-rings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub mod pallet {
origin_chain_asset: <<T as pallet::Config>::Chains as ChainList>::ChainAssets,
amount: u128,
from: <T as pallet_inv4::Config>::CoreId,
to: <T as frame_system::Config>::AccountId,
to: Option<<T as frame_system::Config>::AccountId>,
},

/// A Chain's maintenance status changed.
Expand Down Expand Up @@ -201,14 +201,16 @@ pub mod pallet {

let fee_asset_location = fee_asset.get_asset_location();

let beneficiary: MultiLocation = MultiLocation {
let mut core_multilocation: MultiLocation = MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(<T as pallet_inv4::Config>::ParaId::get()),
descend_interior,
),
};

mutate_if_relay(&mut core_multilocation, &dest);

let fee_multiasset = MultiAsset {
id: AssetId::Concrete(fee_asset_location),
fun: Fungibility::Fungible(fee),
Expand All @@ -228,7 +230,7 @@ pub mod pallet {
Instruction::RefundSurplus,
Instruction::DepositAsset {
assets: MultiAssetFilter::Wild(WildMultiAsset::AllCounted(1)),
beneficiary,
beneficiary: core_multilocation,
},
]);

Expand Down Expand Up @@ -298,14 +300,16 @@ pub mod pallet {
}),
};

let core_multilocation: MultiLocation = MultiLocation {
let mut core_multilocation: MultiLocation = MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(<T as pallet_inv4::Config>::ParaId::get()),
descend_interior,
),
};

mutate_if_relay(&mut core_multilocation, &dest);

let fee_multiasset = MultiAsset {
id: AssetId::Concrete(fee_asset.get_asset_location()),
fun: Fungibility::Fungible(fee),
Expand Down Expand Up @@ -367,7 +371,6 @@ pub mod pallet {
let core = ensure_multisig::<T, OriginFor<T>>(origin)?;

let core_id = core.id.into();
let core_account = core.to_account_id();

let from_chain = asset.get_chain();
let from_chain_location = from_chain.get_location();
Expand Down Expand Up @@ -421,7 +424,7 @@ pub mod pallet {
})
.map_err(|_| Error::<T>::FailedToReanchorAsset)?;

let core_multilocation: MultiLocation = MultiLocation {
let mut core_multilocation: MultiLocation = MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(<T as pallet_inv4::Config>::ParaId::get()),
Expand All @@ -437,9 +440,17 @@ pub mod pallet {
id: to_inner.into(),
}),
},
None => core_multilocation,
None => {
let mut dest_core_multilocation = core_multilocation.clone();

mutate_if_relay(&mut dest_core_multilocation, &dest);

dest_core_multilocation
}
};

mutate_if_relay(&mut core_multilocation, &dest);

// If the asset originates from the destination chain, we need to reverse the reserve-transfer.
let message = if asset_location.starts_with(&dest) {
Xcm(vec![
Expand Down Expand Up @@ -516,10 +527,16 @@ pub mod pallet {
origin_chain_asset: asset,
from: core.id,
amount,
to: to.unwrap_or(core_account),
to,
});

Ok(())
}
}

pub fn mutate_if_relay(origin: &mut MultiLocation, dest: &MultiLocation) {
if dest.contains_parents_only(1) {
origin.dec_parent();
}
}
}
9 changes: 9 additions & 0 deletions pallet-rings/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ impl pallet::Config for Test {

#[derive(Encode, Decode, Clone, Eq, PartialEq, MaxEncodedLen, Debug, TypeInfo)]
pub enum Chains {
Relay,
ChainA,
ChainB,
}
Expand All @@ -488,6 +489,7 @@ pub enum Assets {

#[derive(Encode, Decode, Clone, Eq, PartialEq, MaxEncodedLen, Debug, TypeInfo)]
pub enum ChainAssets {
Relay(Assets),
ChainA(Assets),
ChainB(Assets),
}
Expand All @@ -499,6 +501,7 @@ impl ChainAssetsList for ChainAssets {
match self {
Self::ChainA(_) => Chains::ChainA,
Self::ChainB(_) => Chains::ChainB,
Self::Relay(_) => Chains::Relay,
}
}

Expand All @@ -507,6 +510,7 @@ impl ChainAssetsList for ChainAssets {
match self {
Self::ChainA(asset) => asset,
Self::ChainB(asset) => asset,
Self::Relay(asset) => asset,
}
} {
Assets::AssetA => MultiLocation {
Expand Down Expand Up @@ -536,13 +540,18 @@ impl ChainList for Chains {
parents: 1,
interior: Junctions::X1(Junction::Parachain(2345)),
},
Self::Relay => MultiLocation {
parents: 1,
interior: Junctions::Here,
},
}
}

fn get_main_asset(&self) -> Self::ChainAssets {
match self {
Self::ChainA => ChainAssets::ChainA(Assets::AssetA),
Self::ChainB => ChainAssets::ChainB(Assets::AssetB),
Self::Relay => ChainAssets::Relay(Assets::AssetA),
}
}
}
Expand Down
51 changes: 50 additions & 1 deletion pallet-rings/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use frame_system::RawOrigin;
use mock::*;
use pallet_inv4::{origin::MultisigInternalOrigin, Origin};
use sp_std::vec;
use xcm::latest::Weight;
use xcm::latest::{BodyId, BodyPart, Junction, Junctions, MultiLocation, Weight};

#[test]
fn set_maintenance_status() {
Expand Down Expand Up @@ -239,3 +239,52 @@ fn bridge_assets_fails() {
);
})
}

#[test]
fn mutate_location_if_dest_is_relay() {
let relay_dest = Chains::Relay.get_location();
let para_dest = Chains::ChainA.get_location();

let mut core_multilocation = MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(2125),
Junction::Plurality {
id: BodyId::Index(0),
part: BodyPart::Voice,
},
),
};

crate::pallet::mutate_if_relay(&mut core_multilocation, &para_dest);

assert_eq!(
core_multilocation,
MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(2125),
Junction::Plurality {
id: BodyId::Index(0),
part: BodyPart::Voice,
}
)
}
);

crate::pallet::mutate_if_relay(&mut core_multilocation, &relay_dest);

assert_eq!(
core_multilocation,
MultiLocation {
parents: 0,
interior: Junctions::X2(
Junction::Parachain(2125),
Junction::Plurality {
id: BodyId::Index(0),
part: BodyPart::Voice,
}
)
}
);
}

0 comments on commit 8b2fba2

Please sign in to comment.