Skip to content

Commit

Permalink
Update loction to account id (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwangningbo committed May 7, 2024
1 parent 09db2d0 commit ab150aa
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ cumulus-relay-chain-inprocess-interface = { version = "0.7.0" }
cumulus-relay-chain-interface = { version = "0.7.0" }
cumulus-relay-chain-minimal-node = { version = "0.7.0" }
cumulus-relay-chain-rpc-interface = { version = "0.7.0" }
cumulus-test-relay-validation-worker-provider = { version = "0.1.0" }
frame-benchmarking-cli = { version = "32.0.0" }
node-inspect = { version = "0.12.0", package = "staging-node-inspect" }
pallet-transaction-payment-rpc = { version = "30.0.0" }
Expand Down Expand Up @@ -259,7 +258,6 @@ sp-rpc = { version = "26.0.0" }
substrate-build-script-utils = { version = "11.0.0" }
substrate-frame-rpc-system = { version = "28.0.0" }
substrate-prometheus-endpoint = { version = "0.17.0" }
substrate-test-utils = { version = "3.0.0" }
substrate-wasm-builder = { version = "17.0.0" }
try-runtime-cli = { version = "0.38.0" }
xcm-emulator = { version = "0.5.0" }
Expand Down
2 changes: 1 addition & 1 deletion pallets/slpx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use xcm::{latest::prelude::*, v3::MultiLocation};
use zenlink_protocol::AssetBalance;

pub mod migration;
mod types;
pub mod types;

pub mod weights;
pub use weights::WeightInfo;
Expand Down
5 changes: 4 additions & 1 deletion runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,10 @@ pub mod migrations {
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>;
pub type Unreleased = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
migration::slpx_migrates_whitelist::UpdateWhitelist,
);
}

/// Executive: handles dispatch to the various modules.
Expand Down
37 changes: 37 additions & 0 deletions runtime/bifrost-kusama/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,40 @@ pub mod v1 {
}
}
}

pub mod slpx_migrates_whitelist {
use super::*;
use bifrost_slpx::types::SupportChain;
use sp_core::crypto::Ss58Codec;

pub struct UpdateWhitelist;
impl OnRuntimeUpgrade for UpdateWhitelist {
fn on_runtime_upgrade() -> Weight {
let new_whitelist: BoundedVec<AccountId, ConstU32<10>> =
vec![AccountId::from_ss58check("eVjSno5E5Teibbu3zdaZbBKhsFWU4QjpQXif2YiZ1jFbmEB")
.unwrap()]
.try_into()
.unwrap();
bifrost_slpx::WhitelistAccountId::<Runtime>::insert(
SupportChain::Moonbeam,
new_whitelist,
);

Weight::from(<Runtime as frame_system::Config>::DbWeight::get().writes(1u64))
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
let whitelist =
bifrost_slpx::WhitelistAccountId::<Runtime>::get(SupportChain::Moonbeam);
let new_whitelist: BoundedVec<AccountId, ConstU32<10>> =
vec![AccountId::from_ss58check("eVjSno5E5Teibbu3zdaZbBKhsFWU4QjpQXif2YiZ1jFbmEB")
.unwrap()]
.try_into()
.unwrap();
assert_eq!(whitelist, new_whitelist);

Ok(())
}
}
}
5 changes: 1 addition & 4 deletions runtime/bifrost-kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling};
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
use sp_core::bounded::BoundedVec;
use xcm::v3::prelude::*;
use xcm_builder::{Account32Hash, FrameTransactionalProcessor, TrailingSetTopicAsId};
use xcm_builder::{FrameTransactionalProcessor, TrailingSetTopicAsId};
use xcm_executor::traits::Properties;

/// Bifrost Asset Matcher
Expand Down Expand Up @@ -260,9 +260,6 @@ pub type LocationToAccountId = (
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
AccountId32Aliases<RelayNetwork, AccountId>,
// TODO: Generate remote accounts according to polkadot standards
// Derives a private `Account32` by hashing `("multiloc", received multilocation)`
Account32Hash<RelayNetwork, AccountId>,
// Foreign locations alias into accounts according to a hash of their standard description.
HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
);
Expand Down
5 changes: 4 additions & 1 deletion runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,10 @@ pub mod migrations {
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>;
pub type Unreleased = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
migration::slpx_migrates_whitelist::UpdateWhitelist,
);
}

/// Executive: handles dispatch to the various modules.
Expand Down
64 changes: 64 additions & 0 deletions runtime/bifrost-polkadot/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,67 @@ pub mod v1 {
}
}
}

pub mod slpx_migrates_whitelist {
use super::*;
use bifrost_slpx::types::SupportChain;
use sp_core::crypto::Ss58Codec;

pub struct UpdateWhitelist;
impl OnRuntimeUpgrade for UpdateWhitelist {
fn on_runtime_upgrade() -> Weight {
let new_whitelist: BoundedVec<AccountId, ConstU32<10>> = vec![
AccountId::from_ss58check("dCCU6pkmwQEb29MSigvWjhvnWTtE3GaBqaAdxt4ppW7kUkw")
.unwrap(),
AccountId::from_ss58check("fV6ngGNKkM1BUymusAMcZECxNu3fqhSnS4Jhz2RBk4NtZrw")
.unwrap(),
]
.try_into()
.unwrap();
bifrost_slpx::WhitelistAccountId::<Runtime>::insert(
SupportChain::Moonbeam,
new_whitelist,
);

let new_whitelist: BoundedVec<AccountId, ConstU32<10>> = vec![
AccountId::from_ss58check("fErAtK3KrBPZyAtd26DMQAmuAvo8YswQQBhexibCcqh3D1c")
.unwrap(),
AccountId::from_ss58check("cG6stm2jXgbreRNGxZEvaUn3jT17fxdFXUa6mwE4bSL1v1L")
.unwrap(),
]
.try_into()
.unwrap();
bifrost_slpx::WhitelistAccountId::<Runtime>::insert(SupportChain::Astar, new_whitelist);

Weight::from(<Runtime as frame_system::Config>::DbWeight::get().writes(2u64))
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
let whitelist =
bifrost_slpx::WhitelistAccountId::<Runtime>::get(SupportChain::Moonbeam);
let new_whitelist: BoundedVec<AccountId, ConstU32<10>> = vec![
AccountId::from_ss58check("dCCU6pkmwQEb29MSigvWjhvnWTtE3GaBqaAdxt4ppW7kUkw")
.unwrap(),
AccountId::from_ss58check("fV6ngGNKkM1BUymusAMcZECxNu3fqhSnS4Jhz2RBk4NtZrw")
.unwrap(),
]
.try_into()
.unwrap();
assert_eq!(whitelist, new_whitelist);

let whitelist = bifrost_slpx::WhitelistAccountId::<Runtime>::get(SupportChain::Astar);
let new_whitelist: BoundedVec<AccountId, ConstU32<10>> = vec![
AccountId::from_ss58check("fErAtK3KrBPZyAtd26DMQAmuAvo8YswQQBhexibCcqh3D1c")
.unwrap(),
AccountId::from_ss58check("cG6stm2jXgbreRNGxZEvaUn3jT17fxdFXUa6mwE4bSL1v1L")
.unwrap(),
]
.try_into()
.unwrap();
assert_eq!(whitelist, new_whitelist);

Ok(())
}
}
}
139 changes: 131 additions & 8 deletions runtime/bifrost-polkadot/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ use frame_support::{
sp_runtime::traits::{CheckedConversion, Convert},
traits::{ContainsPair, Get, ProcessMessageError, TransformOrigin},
};
use orml_traits::location::Reserve;
pub use orml_traits::{location::AbsoluteReserveProvider, parameter_type_with_key, MultiCurrency};
use pallet_xcm::XcmPassthrough;
use parity_scale_codec::{Decode, Encode};
pub use polkadot_parachain_primitives::primitives::Sibling;
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
use sp_core::bounded::BoundedVec;
use sp_std::{convert::TryFrom, marker::PhantomData};
pub use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
Expand All @@ -39,20 +44,18 @@ pub use xcm_builder::{
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit,
};
use xcm_builder::{
DescribeAllTerminal, DescribeFamily, FrameTransactionalProcessor, HashedDescription,
TrailingSetTopicAsId,
};
use xcm_executor::traits::{MatchesFungible, ShouldExecute};

// orml imports
use bifrost_currencies::BasicCurrencyAdapter;
use bifrost_runtime_common::currency_adapter::{
BifrostDropAssets, DepositToAlternative, MultiCurrencyAdapter,
};
use orml_traits::location::Reserve;
pub use orml_traits::{location::AbsoluteReserveProvider, parameter_type_with_key, MultiCurrency};
use pallet_xcm::XcmPassthrough;
use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling};
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
use sp_core::bounded::BoundedVec;
use xcm_builder::{Account32Hash, FrameTransactionalProcessor, TrailingSetTopicAsId};

/// Bifrost Asset Matcher
pub struct BifrostAssetMatcher<CurrencyId, CurrencyIdConvert>(
Expand Down Expand Up @@ -211,8 +214,8 @@ pub type LocationToAccountId = (
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
AccountId32Aliases<RelayNetwork, AccountId>,
// Derives a private `Account32` by hashing `("multiloc", received multilocation)`
Account32Hash<RelayNetwork, AccountId>,
// Foreign locations alias into accounts according to a hash of their standard description.
HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
);

/// This is the type we use to convert an (incoming) XCM origin into a local `RuntimeOrigin`
Expand Down Expand Up @@ -765,3 +768,123 @@ impl bifrost_xcm_interface::Config for Runtime {
type CallBackTimeOut = ConstU32<10>;
type CurrencyIdConvert = AssetIdMaps<Runtime>;
}

#[cfg(test)]
mod tests {
use hex_literal::hex;
use sp_core::crypto::Ss58Codec;
use xcm::{
prelude::{AccountId32, AccountKey20, Parachain, X2},
v3::{MultiLocation, NetworkId},
};
use xcm_builder::{DescribeAllTerminal, DescribeFamily};
use xcm_executor::traits::ConvertLocation;

use crate::AccountId;

#[test]
fn test_location_to_account() {
let moonbeam_slpx_origin_location = MultiLocation::new(
0,
X2(
Parachain(2004),
AccountKey20 {
network: None,
key: hex!["F1d4797E51a4640a76769A50b57abE7479ADd3d8"].into(),
},
),
);
let moonbeam_slpx_derived = xcm_builder::HashedDescription::<
AccountId,
DescribeFamily<DescribeAllTerminal>,
>::convert_location(&moonbeam_slpx_origin_location)
.unwrap();

let moonbeam_receiver_origin_location = MultiLocation::new(
0,
X2(
Parachain(2004),
AccountKey20 {
network: None,
key: hex!["64DC1E8b5E9515dE37b58b4d8629Bf89BcD1F576"].into(),
},
),
);
let moonbeam_receiver_derived = xcm_builder::HashedDescription::<
AccountId,
DescribeFamily<DescribeAllTerminal>,
>::convert_location(&moonbeam_receiver_origin_location)
.unwrap();

let moonriver_slpx_origin_location = MultiLocation::new(
0,
X2(
Parachain(2023),
AccountKey20 {
network: None,
key: hex!["6b0A44c64190279f7034b77c13a566E914FE5Ec4"].into(),
},
),
);
let moonriver_slpx_derived = xcm_builder::HashedDescription::<
AccountId,
DescribeFamily<DescribeAllTerminal>,
>::convert_location(&moonriver_slpx_origin_location)
.unwrap();

let astar_slpx_origin_location = MultiLocation::new(
0,
X2(
Parachain(2006),
AccountId32 {
network: Some(NetworkId::Polkadot),
id: hex!["b3d19dad606c8f323460d7bb64a147af60923b85d3c853596954c71d2cfe20e3"]
.into(),
},
),
);
let astar_slpx_derived = xcm_builder::HashedDescription::<
AccountId,
DescribeFamily<DescribeAllTerminal>,
>::convert_location(&astar_slpx_origin_location)
.unwrap();

let astar_receiver_origin_location = MultiLocation::new(
0,
X2(
Parachain(2006),
AccountId32 {
network: Some(NetworkId::Polkadot),
id: hex!["576dee92eedbd7ffe0695569ac7a8db30edd204f2c42f53f14e0e863702c597e"]
.into(),
},
),
);
let astar_receiver_derived = xcm_builder::HashedDescription::<
AccountId,
DescribeFamily<DescribeAllTerminal>,
>::convert_location(&astar_receiver_origin_location)
.unwrap();

assert_eq!(
moonbeam_slpx_derived,
AccountId::from_ss58check("dCCU6pkmwQEb29MSigvWjhvnWTtE3GaBqaAdxt4ppW7kUkw").unwrap()
);
assert_eq!(
moonbeam_receiver_derived,
AccountId::from_ss58check("fV6ngGNKkM1BUymusAMcZECxNu3fqhSnS4Jhz2RBk4NtZrw").unwrap()
);
assert_eq!(
moonriver_slpx_derived,
AccountId::from_ss58check("eVjSno5E5Teibbu3zdaZbBKhsFWU4QjpQXif2YiZ1jFbmEB").unwrap()
);
assert_eq!(
astar_slpx_derived,
AccountId::from_ss58check("fErAtK3KrBPZyAtd26DMQAmuAvo8YswQQBhexibCcqh3D1c").unwrap()
);
assert_eq!(
astar_receiver_derived,
AccountId::from_ss58check("cG6stm2jXgbreRNGxZEvaUn3jT17fxdFXUa6mwE4bSL1v1L").unwrap()
);
}
}

0 comments on commit ab150aa

Please sign in to comment.