XCM coretime region transfers (#3455)

This PR introduces changes enabling the transfer of coretime regions via
XCM.

TL;DR: 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.

### `mint_into` and `burn` implementation

This PR addresses the issue with cross-chain transferring regions back
to the Coretime chain. Remote reserve transfers are performed by
withdrawing and depositing the asset to and from the holding registry.
This requires the asset to support burning and minting functionality.

This PR adds burning and minting; however, they work a bit differently
than usual so that the associated region record is not lost when
burning. Instead of removing all the data, burning will set the owner of
the region to `None`, and when minting it back, it will set it to an
actual value. So, when cross-chain transferring, withdrawing into the
registry will remove the region from its original owner, and when
depositing it from the registry, it will set its owner to another
account

This was originally implemented in this PR: #3455, however we decided to
move all of it to this single PR
(https://github.com/paritytech/polkadot-sdk/pull/3455#discussion_r1547324892)

### Fixes made in this PR

- Update the `XcmReserveTransferFilter` on coretime chain since it is
meant as a reserve chain for coretime regions.
- Update the XCM benchmark to use `AssetTransactor` instead of assuming
`pallet-balances` for fungible transfers.
- Update the XCM benchmark to properly measure weight consumption for
nonfungible reserve asset transfers. ATM reserve transfers via the
extrinsic do not work since the weight for it is set to `Weight::max()`.

Closes: https://github.com/paritytech/polkadot-sdk/issues/865

---------

Co-authored-by: Branislav Kontur <bkontur@gmail.com>
Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: Dónal Murray <donalm@seadanda.dev>
This commit is contained in:
Sergej Sakac
2024-04-17 11:25:33 +02:00
committed by GitHub
parent 4be9f93cd7
commit e6f3106d89
16 changed files with 368 additions and 94 deletions
Generated
+1
View File
@@ -9971,6 +9971,7 @@ dependencies = [
"frame-benchmarking",
"frame-support",
"frame-system",
"log",
"parity-scale-codec",
"pretty_assertions",
"scale-info",
@@ -109,6 +109,7 @@ pub type UncheckedExtrinsic =
/// Migrations to apply on runtime upgrade.
pub type Migrations = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
pallet_broker::migration::MigrateV0ToV1<Runtime>,
// permanent
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
);
@@ -719,11 +720,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())
@@ -741,8 +751,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 {
@@ -758,15 +781,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())
@@ -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,
@@ -109,6 +109,7 @@ pub type UncheckedExtrinsic =
/// Migrations to apply on runtime upgrade.
pub type Migrations = (
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
pallet_broker::migration::MigrateV0ToV1<Runtime>,
// permanent
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
);
@@ -218,6 +219,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 {
@@ -710,11 +712,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())
@@ -732,8 +743,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 {
@@ -753,11 +777,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())
@@ -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,
+60 -40
View File
@@ -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};
@@ -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>,
}
send {
let send_origin =
T::SendXcmOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
@@ -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());
@@ -150,13 +138,26 @@ 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();
@@ -164,21 +165,13 @@ benchmarks! {
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);
}
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());
@@ -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 {
+28
View File
@@ -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
View File
@@ -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 }
@@ -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",
+2 -2
View File
@@ -313,8 +313,8 @@ mod benches {
assert_last_event::<T>(
Event::Transferred {
region_id: region,
old_owner: caller,
owner: recipient,
old_owner: Some(caller),
owner: Some(recipient),
duration: 3u32.into(),
}
.into(),
@@ -120,7 +120,8 @@ impl<T: Config> Pallet<T> {
sale.sellout_price = Some(price);
}
SaleInfo::<T>::put(&sale);
let id = Self::issue(core, sale.region_begin, sale.region_end, who.clone(), Some(price));
let id =
Self::issue(core, sale.region_begin, sale.region_end, Some(who.clone()), Some(price));
let duration = sale.region_end.saturating_sub(sale.region_begin);
Self::deposit_event(Event::Purchased { who, region_id: id, price, duration });
Ok(id)
@@ -178,11 +179,11 @@ impl<T: Config> Pallet<T> {
let mut region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(check_owner == region.owner, Error::<T>::NotOwner);
ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
}
let old_owner = region.owner;
region.owner = new_owner;
region.owner = Some(new_owner);
Regions::<T>::insert(&region_id, &region);
let duration = region.end.saturating_sub(region_id.begin);
Self::deposit_event(Event::Transferred {
@@ -203,7 +204,7 @@ impl<T: Config> Pallet<T> {
let mut region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(check_owner == region.owner, Error::<T>::NotOwner);
ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
}
let pivot = region_id.begin.saturating_add(pivot_offset);
ensure!(pivot < region.end, Error::<T>::PivotTooLate);
@@ -227,7 +228,7 @@ impl<T: Config> Pallet<T> {
let region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(check_owner == region.owner, Error::<T>::NotOwner);
ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
}
ensure!((pivot & !region_id.mask).is_void(), Error::<T>::ExteriorPivot);
+9 -2
View File
@@ -36,6 +36,7 @@ mod tick_impls;
mod types;
mod utility_impls;
pub mod migration;
pub mod runtime_api;
pub mod weights;
@@ -46,6 +47,9 @@ pub use core_mask::*;
pub use coretime_interface::*;
pub use types::*;
/// The log target for this pallet.
const LOG_TARGET: &str = "runtime::broker";
#[frame_support::pallet]
pub mod pallet {
use super::*;
@@ -61,7 +65,10 @@ pub mod pallet {
use sp_runtime::traits::{Convert, ConvertBack};
use sp_std::vec::Vec;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::config]
@@ -216,9 +223,9 @@ pub mod pallet {
/// The duration of the Region.
duration: Timeslice,
/// The old owner of the Region.
old_owner: T::AccountId,
old_owner: Option<T::AccountId>,
/// The new owner of the Region.
owner: T::AccountId,
owner: Option<T::AccountId>,
},
/// A Region has been split into two non-overlapping Regions.
Partitioned {
+87
View File
@@ -0,0 +1,87 @@
// This file is part of Substrate.
// Copyright (C) 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 super::*;
use crate::types::RegionRecord;
use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_support::traits::{Get, UncheckedOnRuntimeUpgrade};
use sp_runtime::Saturating;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;
mod v1 {
use super::*;
/// V0 region record.
#[derive(Encode, Decode)]
struct RegionRecordV0<AccountId, Balance> {
/// The end of the Region.
pub end: Timeslice,
/// The owner of the Region.
pub owner: AccountId,
/// The amount paid to Polkadot for this Region, or `None` if renewal is not allowed.
pub paid: Option<Balance>,
}
pub struct MigrateToV1Impl<T>(PhantomData<T>);
impl<T: Config> UncheckedOnRuntimeUpgrade for MigrateToV1Impl<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let mut count: u64 = 0;
<Regions<T>>::translate::<RegionRecordV0<T::AccountId, BalanceOf<T>>, _>(|_, v0| {
count.saturating_inc();
Some(RegionRecord { end: v0.end, owner: Some(v0.owner), paid: v0.paid })
});
log::info!(
target: LOG_TARGET,
"Storage migration v1 for pallet-broker finished.",
);
// calculate and return migration weights
T::DbWeight::get().reads_writes(count as u64 + 1, count as u64 + 1)
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
Ok((Regions::<T>::iter_keys().count() as u32).encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
let old_count = u32::decode(&mut &state[..]).expect("Known good");
let new_count = Regions::<T>::iter_values().count() as u32;
ensure!(old_count == new_count, "Regions count should not change");
Ok(())
}
}
}
/// Migrate the pallet storage from `0` to `1`.
pub type MigrateV0ToV1<T> = frame_support::migrations::VersionedMigration<
0,
1,
v1::MigrateToV1Impl<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
+48 -9
View File
@@ -25,12 +25,13 @@ use sp_std::vec::Vec;
impl<T: Config> Inspect<T::AccountId> for Pallet<T> {
type ItemId = u128;
fn owner(index: &Self::ItemId) -> Option<T::AccountId> {
Regions::<T>::get(RegionId::from(*index)).map(|r| r.owner)
fn owner(item: &Self::ItemId) -> Option<T::AccountId> {
let record = Regions::<T>::get(RegionId::from(*item))?;
record.owner
}
fn attribute(index: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
let id = RegionId::from(*index);
fn attribute(item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
let id = RegionId::from(*item);
let item = Regions::<T>::get(id)?;
match key {
b"begin" => Some(id.begin.encode()),
@@ -46,11 +47,49 @@ impl<T: Config> Inspect<T::AccountId> for Pallet<T> {
}
impl<T: Config> Transfer<T::AccountId> for Pallet<T> {
fn transfer(index: &Self::ItemId, dest: &T::AccountId) -> DispatchResult {
Self::do_transfer((*index).into(), None, dest.clone()).map_err(Into::into)
fn transfer(item: &Self::ItemId, dest: &T::AccountId) -> DispatchResult {
Self::do_transfer((*item).into(), None, dest.clone()).map_err(Into::into)
}
}
// We don't allow any of the mutate operations, so the default implementation is used, which will
// return `TokenError::Unsupported` in case any of the operations is called.
impl<T: Config> Mutate<T::AccountId> for Pallet<T> {}
/// We don't really support burning and minting.
///
/// We only need this to allow the region to be reserve transferable.
///
/// For reserve transfers that are not 'local', the asset must first be withdrawn to the holding
/// register and then deposited into the designated account. This process necessitates that the
/// asset is capable of being 'burned' and 'minted'.
///
/// Since each region is associated with specific record data, we will not actually burn the asset.
/// If we did, we wouldn't know what record to assign to the newly minted region. Therefore, instead
/// of burning, we set the asset's owner to `None`. In essence, 'burning' a region involves setting
/// its owner to `None`, whereas 'minting' the region assigns its owner to an actual account. This
/// way we never lose track of the associated record data.
impl<T: Config> Mutate<T::AccountId> for Pallet<T> {
/// Deposit a region into an account.
fn mint_into(item: &Self::ItemId, who: &T::AccountId) -> DispatchResult {
let region_id: RegionId = (*item).into();
let record = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
// 'Minting' can only occur if the asset has previously been burned (i.e. moved to the
// holding register)
ensure!(record.owner.is_none(), Error::<T>::NotAllowed);
Self::issue(region_id.core, region_id.begin, record.end, Some(who.clone()), record.paid);
Ok(())
}
/// Withdraw a region from account.
fn burn(item: &Self::ItemId, maybe_check_owner: Option<&T::AccountId>) -> DispatchResult {
let region_id: RegionId = (*item).into();
let mut record = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
if let Some(owner) = maybe_check_owner {
ensure!(Some(owner.clone()) == record.owner, Error::<T>::NotOwner);
}
record.owner = None;
Regions::<T>::insert(region_id, record);
Ok(())
}
}
+27 -6
View File
@@ -200,14 +200,35 @@ fn transfer_works() {
}
#[test]
fn mutate_operations_unsupported_for_regions() {
TestExt::new().execute_with(|| {
fn mutate_operations_work() {
TestExt::new().endow(1, 1000).execute_with(|| {
let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
assert_noop!(
<Broker as Mutate<_>>::mint_into(&region_id.into(), &2),
TokenError::Unsupported
Error::<Test>::UnknownRegion
);
assert_noop!(<Broker as Mutate<_>>::burn(&region_id.into(), None), TokenError::Unsupported);
assert_ok!(Broker::do_start_sales(100, 1));
advance_to(2);
let region_id = Broker::do_purchase(1, u64::max_value()).unwrap();
assert_noop!(
<Broker as Mutate<_>>::mint_into(&region_id.into(), &2),
Error::<Test>::NotAllowed
);
assert_noop!(
<Broker as Mutate<_>>::burn(&region_id.into(), Some(&2)),
Error::<Test>::NotOwner
);
// 'withdraw' the region from user 1:
assert_ok!(<Broker as Mutate<_>>::burn(&region_id.into(), Some(&1)));
assert_eq!(Regions::<Test>::get(region_id).unwrap().owner, None);
// `mint_into` works after burning:
assert_ok!(<Broker as Mutate<_>>::mint_into(&region_id.into(), &2));
assert_eq!(Regions::<Test>::get(region_id).unwrap().owner, Some(2));
// Unsupported operations:
assert_noop!(
<Broker as Mutate<_>>::set_attribute(&region_id.into(), &[], &[]),
TokenError::Unsupported
@@ -285,7 +306,7 @@ fn nft_metadata_works() {
assert_eq!(attribute::<Timeslice>(region, b"begin"), 4);
assert_eq!(attribute::<Timeslice>(region, b"length"), 3);
assert_eq!(attribute::<Timeslice>(region, b"end"), 7);
assert_eq!(attribute::<u64>(region, b"owner"), 1);
assert_eq!(attribute::<Option<u64>>(region, b"owner"), Some(1));
assert_eq!(attribute::<CoreMask>(region, b"part"), 0xfffff_fffff_fffff_fffff.into());
assert_eq!(attribute::<CoreIndex>(region, b"core"), 0);
assert_eq!(attribute::<Option<u64>>(region, b"paid"), Some(100));
@@ -297,7 +318,7 @@ fn nft_metadata_works() {
assert_eq!(attribute::<Timeslice>(region, b"begin"), 6);
assert_eq!(attribute::<Timeslice>(region, b"length"), 1);
assert_eq!(attribute::<Timeslice>(region, b"end"), 7);
assert_eq!(attribute::<u64>(region, b"owner"), 42);
assert_eq!(attribute::<Option<u64>>(region, b"owner"), Some(42));
assert_eq!(attribute::<CoreMask>(region, b"part"), 0x00000_fffff_fffff_00000.into());
assert_eq!(attribute::<CoreIndex>(region, b"core"), 0);
assert_eq!(attribute::<Option<u64>>(region, b"paid"), None);
+1 -1
View File
@@ -84,7 +84,7 @@ pub struct RegionRecord<AccountId, Balance> {
/// The end of the Region.
pub end: Timeslice,
/// The owner of the Region.
pub owner: AccountId,
pub owner: Option<AccountId>,
/// The amount paid to Polkadot for this Region, or `None` if renewal is not allowed.
pub paid: Option<Balance>,
}
+3 -3
View File
@@ -72,11 +72,11 @@ impl<T: Config> Pallet<T> {
Ok(())
}
pub(crate) fn issue(
pub fn issue(
core: CoreIndex,
begin: Timeslice,
end: Timeslice,
owner: T::AccountId,
owner: Option<T::AccountId>,
paid: Option<BalanceOf<T>>,
) -> RegionId {
let id = RegionId { begin, core, mask: CoreMask::complete() };
@@ -94,7 +94,7 @@ impl<T: Config> Pallet<T> {
let region = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(check_owner == region.owner, Error::<T>::NotOwner);
ensure!(Some(check_owner) == region.owner, Error::<T>::NotOwner);
}
Regions::<T>::remove(&region_id);