Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XCM coretime region transfers #3455

Merged
merged 46 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5581f42
Fix XCM benchmarks
Szegoo Feb 23, 2024
89b8829
nonfungible implementation
Szegoo Feb 23, 2024
55844cf
progress
Szegoo Feb 23, 2024
c398930
works
Szegoo Feb 23, 2024
9200399
prdoc
Szegoo Feb 23, 2024
7f9eb12
Merge branch 'master' into xcm-bench
Szegoo Feb 23, 2024
99a447e
Merge branch 'master' into xcm-bench
Szegoo Feb 27, 2024
507eb47
conflict readjustements
Szegoo Feb 27, 2024
4b0ee5d
fix
Szegoo Feb 27, 2024
eb7a61b
..
Szegoo Feb 27, 2024
e1f99dd
Region reserve transfers
Szegoo Mar 2, 2024
01ffbe0
tests & migration
Szegoo Mar 4, 2024
6307dd3
fix benchmarks
Szegoo Mar 4, 2024
c9c2504
try runtmie
Szegoo Mar 4, 2024
2ebe29d
fix
Szegoo Mar 4, 2024
beee619
Merge branch 'master' into xcm-bench
bkontur Mar 5, 2024
5448324
fix
Szegoo Mar 7, 2024
911c8c2
Update cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs
Szegoo Mar 7, 2024
30a9133
Update cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs
Szegoo Mar 7, 2024
8ec636b
fix
Szegoo Mar 7, 2024
7809eba
check
Szegoo Mar 7, 2024
663dc0b
...
Szegoo Mar 8, 2024
799a1d9
remove duplicate
Szegoo Mar 11, 2024
52f7d4b
Update polkadot/xcm/pallet-xcm/src/benchmarking.rs
Szegoo Mar 11, 2024
5b5f8de
log error
Szegoo Mar 11, 2024
b414856
improve docs & tests
Szegoo Mar 14, 2024
fd70daa
prdoc
Szegoo Mar 14, 2024
d8eb54b
Update cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs
Szegoo Mar 27, 2024
fdd21e4
Update cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs
Szegoo Mar 27, 2024
128b665
Merge branch 'region-reserve-transfers' into xcm-bench
Szegoo Mar 29, 2024
9b2575b
region reserve transfer
Szegoo Mar 29, 2024
09789f9
fix delivery helper
Szegoo Mar 30, 2024
afc3193
apply same fixes in westend
Szegoo Mar 30, 2024
c75b486
update prdoc
Szegoo Apr 2, 2024
3c714a5
move RegionRecordV0 to v1
Szegoo Apr 2, 2024
962b589
Update prdoc/pr_3455.prdoc
Szegoo Apr 5, 2024
94fc92f
Update prdoc/pr_3455.prdoc
Szegoo Apr 5, 2024
883ded4
Update substrate/frame/broker/src/nonfungible_impl.rs
Szegoo Apr 5, 2024
0f510c8
revert contracts-rococo xcm config
Szegoo Apr 5, 2024
e6cc759
update prdoc
Szegoo Apr 5, 2024
73e9c7f
bump version
Szegoo Apr 5, 2024
a04285e
Merge branch 'master' into xcm-bench
Szegoo Apr 8, 2024
e312286
Update substrate/frame/broker/src/lib.rs
Szegoo Apr 8, 2024
948aafa
Merge branch 'master' into xcm-bench
Szegoo Apr 16, 2024
1374042
add missing migration in rococo & westend
Szegoo Apr 16, 2024
8d1d338
fmt
Szegoo Apr 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 44 additions & 12 deletions cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,20 @@ impl_runtime_apis! {

use pallet_xcm::benchmarking::Pallet as PalletXcmExtrinsicsBenchmark;
impl pallet_xcm::benchmarking::Config for Runtime {
type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>;
type DeliveryHelper = (
cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>,
polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
PriceForSiblingParachainDelivery,
RandomParaId,
ParachainSystem,
>
);

fn reachable_dest() -> Option<Location> {
Some(Parent.into())
Expand All @@ -741,8 +750,21 @@ impl_runtime_apis! {
}

fn reserve_transferable_asset_and_dest() -> Option<(Asset, Location)> {
// Reserve transfers are disabled
None
// Coretime chain can reserve transfer regions to some random parachain.

// Properties of a mock region:
let core = 0;
let begin = 0;
let end = 42;

let region_id = pallet_broker::Pallet::<Runtime>::issue(core, begin, end, None, None);
Some((
Asset {
fun: NonFungible(Index(region_id.into())),
id: AssetId(xcm_config::BrokerPalletLocation::get())
},
ParentThen(Parachain(RandomParaId::get().into()).into()).into(),
))
}

fn get_asset() -> Asset {
Expand All @@ -758,15 +780,25 @@ impl_runtime_apis! {
RocRelayLocation::get(),
ExistentialDeposit::get()
).into());
pub const RandomParaId: ParaId = ParaId::new(43211234);
}

impl pallet_xcm_benchmarks::Config for Runtime {
type XcmConfig = xcm_config::XcmConfig;
type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>;
type DeliveryHelper = (
cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>,
polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
PriceForSiblingParachainDelivery,
RandomParaId,
ParachainSystem,
>
);
type AccountIdConverter = xcm_config::LocationToAccountId;
fn valid_destination() -> Result<Location, BenchmarkError> {
Ok(RocRelayLocation::get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl pallet_xcm::Config for Runtime {
type XcmExecuteFilter = Everything;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Nothing; // This parachain is not meant as a reserve location.
type XcmReserveTransferFilter = Everything;
type Weigher = WeightInfoBounds<
crate::weights::xcm::CoretimeRococoXcmWeight<RuntimeCall>,
RuntimeCall,
Expand Down
58 changes: 46 additions & 12 deletions cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl pallet_authorship::Config for Runtime {

parameter_types! {
pub const ExistentialDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const RandomParaId: ParaId = ParaId::new(43211234);
}

impl pallet_balances::Config for Runtime {
Expand Down Expand Up @@ -710,11 +711,20 @@ impl_runtime_apis! {

use pallet_xcm::benchmarking::Pallet as PalletXcmExtrinsicsBenchmark;
impl pallet_xcm::benchmarking::Config for Runtime {
type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>;
type DeliveryHelper = (
cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>,
polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
PriceForSiblingParachainDelivery,
RandomParaId,
ParachainSystem,
>
);

fn reachable_dest() -> Option<Location> {
Some(Parent.into())
Expand All @@ -732,8 +742,21 @@ impl_runtime_apis! {
}

fn reserve_transferable_asset_and_dest() -> Option<(Asset, Location)> {
// Reserve transfers are disabled
None
// Coretime chain can reserve transfer regions to some random parachain.
Szegoo marked this conversation as resolved.
Show resolved Hide resolved

// Properties of a mock region:
let core = 0;
let begin = 0;
let end = 42;

let region_id = pallet_broker::Pallet::<Runtime>::issue(core, begin, end, None, None);
Some((
Asset {
fun: NonFungible(Index(region_id.into())),
id: AssetId(xcm_config::BrokerPalletLocation::get())
},
ParentThen(Parachain(RandomParaId::get().into()).into()).into(),
))
}

fn get_asset() -> Asset {
Expand All @@ -753,11 +776,22 @@ impl_runtime_apis! {

impl pallet_xcm_benchmarks::Config for Runtime {
type XcmConfig = xcm_config::XcmConfig;
type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>;

type DeliveryHelper = (
cumulus_primitives_utility::ToParentDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
xcm_config::PriceForParentDelivery,
>,
polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper<
xcm_config::XcmConfig,
ExistentialDepositAsset,
PriceForSiblingParachainDelivery,
RandomParaId,
ParachainSystem,
>
);

type AccountIdConverter = xcm_config::LocationToAccountId;
fn valid_destination() -> Result<Location, BenchmarkError> {
Ok(TokenRelayLocation::get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl pallet_xcm::Config for Runtime {
type XcmExecuteFilter = Everything;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Nothing; // This parachain is not meant as a reserve location.
type XcmReserveTransferFilter = Everything;
type Weigher = WeightInfoBounds<
crate::weights::xcm::CoretimeWestendXcmWeight<RuntimeCall>,
RuntimeCall,
Expand Down
100 changes: 60 additions & 40 deletions polkadot/xcm/pallet-xcm/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use super::*;
use bounded_collections::{ConstU32, WeakBoundedVec};
use codec::Encode;
use frame_benchmarking::{benchmarks, whitelisted_caller, BenchmarkError, BenchmarkResult};
use frame_support::{
traits::fungible::{Inspect, Mutate},
weights::Weight,
};
use frame_support::{assert_ok, weights::Weight};
use frame_system::RawOrigin;
use sp_std::prelude::*;
use xcm::{latest::prelude::*, v2};
Expand Down Expand Up @@ -90,11 +87,6 @@ pub trait Config: crate::Config {
}

benchmarks! {
where_clause {
where
T: pallet_balances::Config,
<T as pallet_balances::Config>::Balance: From<u128> + Into<u128>,
}
bkontur marked this conversation as resolved.
Show resolved Hide resolved
send {
let send_origin =
T::SendXcmOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
Expand Down Expand Up @@ -129,11 +121,7 @@ benchmarks! {
BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)),
)?;

let transferred_amount = match &asset.fun {
Fungible(amount) => *amount,
_ => return Err(BenchmarkError::Stop("Benchmark asset not fungible")),
}.into();
let assets: Assets = asset.into();
let assets: Assets = asset.clone().into();

let caller: T::AccountId = whitelisted_caller();
let send_origin = RawOrigin::Signed(caller.clone());
Expand All @@ -150,35 +138,40 @@ benchmarks! {
FeeReason::ChargeFees,
);

// Actual balance (e.g. `ensure_successful_delivery` could drip delivery fees, ...)
let balance = <pallet_balances::Pallet<T> as Inspect<_>>::balance(&caller);
// Add transferred_amount to origin
<pallet_balances::Pallet<T> as Mutate<_>>::mint_into(&caller, transferred_amount)?;
// verify initial balance
let balance = balance + transferred_amount;
assert_eq!(<pallet_balances::Pallet<T> as Inspect<_>>::balance(&caller), balance);
match &asset.fun {
Fungible(amount) => {
// Add transferred_amount to origin
<T::XcmExecutor as XcmAssetTransfers>::AssetTransactor::deposit_asset(
&Asset { fun: Fungible(*amount), id: asset.id },
&origin_location,
None,
).map_err(|error| {
log::error!("Fungible asset couldn't be deposited, error: {:?}", error);
BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX))
})?;
},
NonFungible(instance) => {
<T::XcmExecutor as XcmAssetTransfers>::AssetTransactor::deposit_asset(&asset, &origin_location, None)
.map_err(|error| {
log::error!("Nonfungible asset couldn't be deposited, error: {:?}", error);
BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX))
})?;
}
};

let recipient = [0u8; 32];
let versioned_dest: VersionedLocation = destination.into();
let versioned_beneficiary: VersionedLocation =
AccountId32 { network: None, id: recipient.into() }.into();
let versioned_assets: VersionedAssets = assets.into();
}: _<RuntimeOrigin<T>>(send_origin.into(), Box::new(versioned_dest), Box::new(versioned_beneficiary), Box::new(versioned_assets), 0)
seadanda marked this conversation as resolved.
Show resolved Hide resolved
verify {
// verify balance after transfer, decreased by transferred amount (+ maybe XCM delivery fees)
assert!(<pallet_balances::Pallet<T> as Inspect<_>>::balance(&caller) <= balance - transferred_amount);
}
bkontur marked this conversation as resolved.
Show resolved Hide resolved

reserve_transfer_assets {
let (asset, destination) = T::reserve_transferable_asset_and_dest().ok_or(
BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)),
)?;

let transferred_amount = match &asset.fun {
Fungible(amount) => *amount,
_ => return Err(BenchmarkError::Stop("Benchmark asset not fungible")),
}.into();
let assets: Assets = asset.into();
let assets: Assets = asset.clone().into();

let caller: T::AccountId = whitelisted_caller();
let send_origin = RawOrigin::Signed(caller.clone());
Expand All @@ -195,23 +188,50 @@ benchmarks! {
FeeReason::ChargeFees,
);

// Actual balance (e.g. `ensure_successful_delivery` could drip delivery fees, ...)
let balance = <pallet_balances::Pallet<T> as Inspect<_>>::balance(&caller);
// Add transferred_amount to origin
<pallet_balances::Pallet<T> as Mutate<_>>::mint_into(&caller, transferred_amount)?;
// verify initial balance
let balance = balance + transferred_amount;
assert_eq!(<pallet_balances::Pallet<T> as Inspect<_>>::balance(&caller), balance);
match &asset.fun {
Fungible(amount) => {
// Add transferred_amount to origin
<T::XcmExecutor as XcmAssetTransfers>::AssetTransactor::deposit_asset(
&Asset { fun: Fungible(*amount), id: asset.id.clone() },
&origin_location,
None,
).map_err(|error| {
log::error!("Fungible asset couldn't be deposited, error: {:?}", error);
BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX))
})?;
},
NonFungible(instance) => {
<T::XcmExecutor as XcmAssetTransfers>::AssetTransactor::deposit_asset(&asset, &origin_location, None)
.map_err(|error| {
log::error!("Nonfungible asset couldn't be deposited, error: {:?}", error);
BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX))
})?;
}
};

let recipient = [0u8; 32];
let versioned_dest: VersionedLocation = destination.into();
let versioned_dest: VersionedLocation = destination.clone().into();
let versioned_beneficiary: VersionedLocation =
AccountId32 { network: None, id: recipient.into() }.into();
let versioned_assets: VersionedAssets = assets.into();
}: _<RuntimeOrigin<T>>(send_origin.into(), Box::new(versioned_dest), Box::new(versioned_beneficiary), Box::new(versioned_assets), 0)
verify {
// verify balance after transfer, decreased by transferred amount (+ maybe XCM delivery fees)
assert!(<pallet_balances::Pallet<T> as Inspect<_>>::balance(&caller) <= balance - transferred_amount);
match &asset.fun {
Fungible(amount) => {
assert_ok!(<T::XcmExecutor as XcmAssetTransfers>::AssetTransactor::withdraw_asset(
&Asset { fun: Fungible(*amount), id: asset.id },
&destination,
None,
));
},
NonFungible(instance) => {
assert_ok!(<T::XcmExecutor as XcmAssetTransfers>::AssetTransactor::withdraw_asset(
&asset,
&destination,
None,
));
}
};
}

transfer_assets {
Expand Down
28 changes: 28 additions & 0 deletions prdoc/pr_3455.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Region reserve transfers fix

doc:
- audience: Runtime User
description: |
This PR introduces changes enabling the transfer of coretime regions via XCM.
There are two primary issues that are resolved in this PR:
1. The mint and burn functions were not implemented for coretime regions. These operations
are essential for moving assets to and from the XCM holding register.
2. The transfer of non-fungible assets through XCM was previously disallowed. This was due
to incorrectly benchmarking non-fungible asset transfers via XCM, which led to assigning
it a weight of Weight::Max, effectively preventing its execution.

migrations:
db: []
runtime:
- reference: pallet-broker
description: |
The region owner is optional.

crates:
- name: pallet-broker
- name: pallet-xcm
- name: coretime-rococo-runtime
- name: coretime-westend-runtime
2 changes: 2 additions & 0 deletions substrate/frame/broker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
log = { workspace = true }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.11.1", default-features = false, features = ["derive"] }
bitvec = { version = "1.0.0", default-features = false }
Expand All @@ -40,6 +41,7 @@ std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"scale-info/std",
"sp-api/std",
"sp-arithmetic/std",
Expand Down
Loading
Loading