Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into ao-past-session-slashing-companion
Browse files Browse the repository at this point in the history
* master:
  Add Support for Foreign Assets (#2133)
  XCM V2 weight template (#2344)
  Companion for polkadot#6928 (#2356)
  move back to ci-linux@production image
  • Loading branch information
ordian committed Mar 23, 2023
2 parents 29be66e + 020a024 commit e9327cd
Show file tree
Hide file tree
Showing 44 changed files with 4,786 additions and 1,271 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ variables:
GIT_STRATEGY: fetch
GIT_DEPTH: 100
CARGO_INCREMENTAL: 0
CI_IMAGE: "paritytech/ci-linux@sha256:7c76c3f9639f919447abbf9db535588178fde4df583d6926444d44cc20c094e6" # staging 2023-03-20
CI_IMAGE: "paritytech/ci-linux:production"
DOCKER_OS: "debian:stretch"
ARCH: "x86_64"
ZOMBIENET_IMAGE: "docker.io/paritytech/zombienet:v1.3.40"
Expand Down
528 changes: 272 additions & 256 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion file_header.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// Copyright Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
Expand Down
2 changes: 2 additions & 0 deletions parachain-template/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use frame_support::{
traits::{ConstU32, Everything, Nothing},
weights::Weight,
};
use frame_system::EnsureRoot;
use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
use polkadot_runtime_common::impls::ToAuthor;
Expand Down Expand Up @@ -246,6 +247,7 @@ impl pallet_xcm::Config for Runtime {
type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest;
type AdminOrigin = EnsureRoot<AccountId>;
}

impl cumulus_pallet_xcm::Config for Runtime {
Expand Down
12 changes: 12 additions & 0 deletions parachains/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ where
}
}

/// Allow checking in assets that exists.
pub struct AssetExists<AccountId, Assets>(PhantomData<(AccountId, Assets)>);
impl<AccountId, Assets> Contains<<Assets as fungibles::Inspect<AccountId>>::AssetId>
for AssetExists<AccountId, Assets>
where
Assets: fungibles::Inspect<AccountId>,
{
fn contains(id: &<Assets as fungibles::Inspect<AccountId>>::AssetId) -> bool {
Assets::asset_exists(*id)
}
}

/// Asset filter that allows all assets from a certain location.
pub struct AssetsFrom<T>(PhantomData<T>);
impl<T: Get<MultiLocation>> ContainsPair<MultiAsset, MultiLocation> for AssetsFrom<T> {
Expand Down
12 changes: 12 additions & 0 deletions parachains/runtimes/assets/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ description = "Assets common utilities"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }

# Substrate
frame-support = { git = "https:/paritytech/substrate", default-features = false, branch = "master" }
sp-api = { git = "https:/paritytech/substrate", default-features = false, branch = "master" }
sp-std = { git = "https:/paritytech/substrate", default-features = false, branch = "master" }

# Polkadot
pallet-xcm = { git = "https:/paritytech/polkadot", default-features = false, branch = "master" }
xcm = { git = "https:/paritytech/polkadot", default-features = false, branch = "master" }
xcm-builder = { git = "https:/paritytech/polkadot", default-features = false, branch = "master" }
xcm-executor = { git = "https:/paritytech/polkadot", default-features = false, branch = "master" }

# Cumulus
parachains-common = { path = "../../../common", default-features = false }
cumulus-primitives-core = { path = "../../../../primitives/core", default-features = false }

[build-dependencies]
substrate-wasm-builder = { git = "https:/paritytech/substrate", branch = "master" }
Expand All @@ -28,11 +31,20 @@ substrate-wasm-builder = { git = "https:/paritytech/substrate", bran
default = [ "std" ]
std = [
"codec/std",
"log/std",
"frame-support/std",
"parachains-common/std",
"cumulus-primitives-core/std",
"sp-api/std",
"sp-std/std",
"pallet-xcm/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
]

runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
56 changes: 56 additions & 0 deletions parachains/runtimes/assets/common/src/foreign_creators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use frame_support::traits::{
ContainsPair, EnsureOrigin, EnsureOriginWithArg, Everything, OriginTrait,
};
use pallet_xcm::{EnsureXcm, Origin as XcmOrigin};
use xcm::latest::MultiLocation;
use xcm_executor::traits::Convert;

// `EnsureOriginWithArg` impl for `CreateOrigin` that allows only XCM origins that are locations
// containing the class location.
pub struct ForeignCreators<IsForeign, AccountOf, AccountId>(
sp_std::marker::PhantomData<(IsForeign, AccountOf, AccountId)>,
);
impl<
IsForeign: ContainsPair<MultiLocation, MultiLocation>,
AccountOf: Convert<MultiLocation, AccountId>,
AccountId: Clone,
RuntimeOrigin: From<XcmOrigin> + OriginTrait + Clone,
> EnsureOriginWithArg<RuntimeOrigin, MultiLocation>
for ForeignCreators<IsForeign, AccountOf, AccountId>
where
RuntimeOrigin::PalletsOrigin:
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
{
type Success = AccountId;

fn try_origin(
origin: RuntimeOrigin,
asset_location: &MultiLocation,
) -> sp_std::result::Result<Self::Success, RuntimeOrigin> {
let origin_location = EnsureXcm::<Everything>::try_origin(origin.clone())?;
if !IsForeign::contains(&asset_location, &origin_location) {
return Err(origin)
}
AccountOf::convert(origin_location).map_err(|_| origin)
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &MultiLocation) -> Result<RuntimeOrigin, ()> {
Ok(pallet_xcm::Origin::Xcm(a.clone()).into())
}
}
60 changes: 41 additions & 19 deletions parachains/runtimes/assets/common/src/fungible_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// This file is part of Substrate.

// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Runtime API definition for assets.

use crate::runtime_api::FungiblesAccessError;
use frame_support::traits::Contains;
use sp_std::{borrow::Borrow, vec::Vec};
use xcm::latest::{MultiAsset, MultiLocation};
use xcm_builder::ConvertedConcreteId;
use xcm_builder::{ConvertedConcreteId, MatchedConvertedConcreteId};
use xcm_executor::traits::{Convert, MatchesFungibles};

/// Converting any [`(AssetId, Balance)`] to [`MultiAsset`]
Expand Down Expand Up @@ -60,6 +58,29 @@ impl<
}
}

impl<
AssetId: Clone,
Balance: Clone,
MatchAssetId: Contains<MultiLocation>,
ConvertAssetId: Convert<MultiLocation, AssetId>,
ConvertBalance: Convert<u128, Balance>,
> MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance>
for MatchedConvertedConcreteId<AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertBalance>
{
fn convert_ref(
value: impl Borrow<(AssetId, Balance)>,
) -> Result<MultiAsset, FungiblesAccessError> {
let (asset_id, balance) = value.borrow();
match ConvertAssetId::reverse_ref(asset_id) {
Ok(asset_id_as_multilocation) => match ConvertBalance::reverse_ref(balance) {
Ok(amount) => Ok((asset_id_as_multilocation, amount).into()),
Err(_) => Err(FungiblesAccessError::AmountToBalanceConversionFailed),
},
Err(_) => Err(FungiblesAccessError::AssetIdConversionFailed),
}
}
}

/// Helper function to convert collections with [`(AssetId, Balance)`] to [`MultiAsset`]
pub fn convert<'a, AssetId, Balance, ConvertAssetId, ConvertBalance, Converter>(
items: impl Iterator<Item = &'a (AssetId, Balance)>,
Expand Down Expand Up @@ -90,11 +111,12 @@ pub fn convert_balance<
#[cfg(test)]
mod tests {
use super::*;
use frame_support::traits::Everything;

use xcm::latest::prelude::*;
use xcm_executor::traits::{Identity, JustTry};

type Converter = ConvertedConcreteId<MultiLocation, u64, Identity, JustTry>;
type Converter = MatchedConvertedConcreteId<MultiLocation, u64, Everything, Identity, JustTry>;

#[test]
fn converted_concrete_id_fungible_multi_asset_conversion_roundtrip_works() {
Expand Down
Loading

0 comments on commit e9327cd

Please sign in to comment.