Skip to content

Commit

Permalink
Apply the change in location conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cem Eliguzel committed Sep 14, 2023
1 parent eca25ff commit acf716a
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions polkadot/xcm/xcm-builder/src/location_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use frame_support::traits::Get;
use parity_scale_codec::{Compact, Decode, Encode};
use sp_io::hashing::blake2_256;
use sp_runtime::traits::{AccountIdConversion, Convert, TrailingZeroInput};
use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*};
use sp_std::{marker::PhantomData, prelude::*};
use xcm::latest::prelude::*;
use xcm_executor::traits::ConvertLocation;

Expand Down Expand Up @@ -170,43 +170,39 @@ impl DescribeLocation for LegacyDescribeForeignChainAccount {
/// HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>
pub struct HashedDescriptionDescribeFamilyAllTerminal<AccountId>(PhantomData<AccountId>);
impl<AccountId: From<[u8; 32]> + Clone> HashedDescriptionDescribeFamilyAllTerminal<AccountId> {
fn describe_location_suffix(l: &MultiLocation) -> Result<Vec<u8>, ()> {
fn describe_location_suffix(l: &MultiLocation) -> Option<Vec<u8>> {
match (l.parents, &l.interior) {
(0, Here) => Ok(Vec::new()),
(0, X1(PalletInstance(i))) => Ok((b"Pallet", Compact::<u32>::from(*i as u32)).encode()),
(0, X1(AccountId32 { id, .. })) => Ok((b"AccountId32", id).encode()),
(0, X1(AccountKey20 { key, .. })) => Ok((b"AccountKey20", key).encode()),
_ => return Err(()),
(0, Here) => Some(Vec::new()),
(0, X1(PalletInstance(i))) => Some((b"Pallet", Compact::<u32>::from(*i as u32)).encode()),
(0, X1(AccountId32 { id, .. })) => Some((b"AccountId32", id).encode()),
(0, X1(AccountKey20 { key, .. })) => Some((b"AccountKey20", key).encode()),
_ => return None,
}
}
}

impl<AccountId: From<[u8; 32]> + Clone> Convert<MultiLocation, AccountId>
impl<AccountId: From<[u8; 32]> + Clone> ConvertLocation<AccountId>
for HashedDescriptionDescribeFamilyAllTerminal<AccountId> {
fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> {
let l = location.borrow();
let to_hash = match (l.parents, l.interior.first()) {
fn convert_location(location: &MultiLocation) -> Option<AccountId> {
let to_hash = match (location.parents, location.interior.first()) {
(0, Some(Parachain(index))) => {
let tail = l.interior.split_first().0;
let tail = location.interior.split_first().0;
let interior = Self::describe_location_suffix(&tail.into())?;
(b"ChildChain", Compact::<u32>::from(*index), interior).encode()
},
(1, Some(Parachain(index))) => {
let tail = l.interior.split_first().0;
let tail = location.interior.split_first().0;
let interior = Self::describe_location_suffix(&tail.into())?;
(b"SiblingChain", Compact::<u32>::from(*index), interior).encode()
},
(1, _) => {
let tail = l.interior.into();
let tail = location.interior.into();
let interior = Self::describe_location_suffix(&tail)?;
(b"ParentChain", interior).encode()
},
_ => return Err(()),
_ => return None,
};
Ok(blake2_256(&to_hash).into())
}
fn reverse_ref(_: impl Borrow<AccountId>) -> Result<MultiLocation, ()> {
Err(())
Some(blake2_256(&to_hash).into())
}
}

Expand Down Expand Up @@ -979,7 +975,7 @@ mod tests {
129, 211, 14, 6, 146, 54, 225, 200, 135, 103, 248, 244, 125, 112, 53, 133,
91, 42, 215, 236, 154, 199, 191, 208, 110, 148, 223, 55, 92, 216, 250, 34
],
Converter::<[u8; 32]>::convert(MultiLocation {
Converter::<[u8; 32]>::convert_location(&MultiLocation {
parents: 0,
interior: X2(Parachain(1), AccountId32 { network: None, id: [0u8; 32] }),
}).unwrap()
Expand All @@ -989,7 +985,7 @@ mod tests {
17, 142, 105, 253, 199, 34, 43, 136, 155, 48, 12, 137, 155, 219, 155, 110,
93, 181, 93, 252, 124, 60, 250, 195, 229, 86, 31, 220, 121, 111, 254, 252
],
Converter::<[u8; 32]>::convert(MultiLocation {
Converter::<[u8; 32]>::convert_location(&MultiLocation {
parents: 1,
interior: X2(Parachain(1), AccountId32 { network: None, id: [0u8; 32] }),
}).unwrap()
Expand All @@ -999,7 +995,7 @@ mod tests {
237, 65, 190, 49, 53, 182, 196, 183, 151, 24, 214, 23, 72, 244, 235, 87,
187, 67, 52, 122, 195, 192, 10, 58, 253, 49, 0, 112, 175, 224, 125, 66
],
Converter::<[u8; 32]>::convert(MultiLocation {
Converter::<[u8; 32]>::convert_location(&MultiLocation {
parents: 0,
interior: X2(Parachain(1), AccountKey20 { network: None, key: [0u8; 20] }),
}).unwrap()
Expand All @@ -1009,7 +1005,7 @@ mod tests {
226, 225, 225, 162, 254, 156, 113, 95, 68, 155, 160, 118, 126, 18, 166, 132,
144, 19, 8, 204, 228, 112, 164, 189, 179, 124, 249, 1, 168, 110, 151, 50
],
Converter::<[u8; 32]>::convert(MultiLocation {
Converter::<[u8; 32]>::convert_location(&MultiLocation {
parents: 1,
interior: X2(Parachain(1), AccountKey20 { network: None, key: [0u8; 20] }),
}).unwrap()
Expand All @@ -1019,7 +1015,7 @@ mod tests {
254, 186, 179, 229, 13, 24, 84, 36, 84, 35, 64, 95, 114, 136, 62, 69, 247,
74, 215, 104, 121, 114, 53, 6, 124, 46, 42, 245, 121, 197, 12, 208
],
Converter::<[u8; 32]>::convert(MultiLocation {
Converter::<[u8; 32]>::convert_location(&MultiLocation {
parents: 1,
interior: X2(Parachain(2), PalletInstance(3)),
}).unwrap()
Expand All @@ -1029,7 +1025,7 @@ mod tests {
217, 56, 0, 36, 228, 154, 250, 26, 200, 156, 1, 39, 254, 162, 16, 187, 107,
67, 27, 16, 218, 254, 250, 184, 6, 27, 216, 138, 194, 93, 23, 165
],
Converter::<[u8; 32]>::convert(MultiLocation {
Converter::<[u8; 32]>::convert_location(&MultiLocation {
parents: 1,
interior: Here,
}).unwrap()
Expand Down

0 comments on commit acf716a

Please sign in to comment.