XCM remote lock consumers (#6947)

* xcm remote lock consumers

* update xcm pallet config setups

* fix import

* update xcm pallet config setups

* rename consumers to users

* rename

* rename users to consumers, more docs

* correct doc

---------

Co-authored-by: parity-processbot <>
This commit is contained in:
Muharem Ismailov
2023-05-05 16:11:35 +02:00
committed by GitHub
parent 9e50f18250
commit 245305be4e
12 changed files with 58 additions and 10 deletions
@@ -422,6 +422,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = SovereignAccountOf; type SovereignAccountOf = SovereignAccountOf;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>; type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -417,6 +417,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = SovereignAccountOf; type SovereignAccountOf = SovereignAccountOf;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>; type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -384,6 +384,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = LocationConverter; type SovereignAccountOf = LocationConverter;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>; type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -144,6 +144,8 @@ impl pallet_xcm::Config for crate::Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = (); type SovereignAccountOf = ();
type MaxLockers = frame_support::traits::ConstU32<8>; type MaxLockers = frame_support::traits::ConstU32<8>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo; type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -300,6 +300,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = LocationConverter; type SovereignAccountOf = LocationConverter;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>; type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
+36 -10
View File
@@ -250,6 +250,12 @@ pub mod pallet {
/// The maximum number of local XCM locks that a single account may have. /// The maximum number of local XCM locks that a single account may have.
type MaxLockers: Get<u32>; type MaxLockers: Get<u32>;
/// The maximum number of consumers a single remote lock may have.
type MaxRemoteLockConsumers: Get<u32>;
/// The ID type for local consumers of remote locks.
type RemoteLockConsumerIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
/// Weight information for extrinsics in this pallet. /// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo; type WeightInfo: WeightInfo;
@@ -445,7 +451,7 @@ pub mod pallet {
FeesNotMet, FeesNotMet,
/// A remote lock with the corresponding data could not be found. /// A remote lock with the corresponding data could not be found.
LockNotFound, LockNotFound,
/// The unlock operation cannot succeed because there are still users of the lock. /// The unlock operation cannot succeed because there are still consumers of the lock.
InUse, InUse,
} }
@@ -588,11 +594,26 @@ pub mod pallet {
StorageValue<_, VersionMigrationStage, OptionQuery>; StorageValue<_, VersionMigrationStage, OptionQuery>;
#[derive(Clone, Encode, Decode, Eq, PartialEq, Ord, PartialOrd, TypeInfo, MaxEncodedLen)] #[derive(Clone, Encode, Decode, Eq, PartialEq, Ord, PartialOrd, TypeInfo, MaxEncodedLen)]
pub struct RemoteLockedFungibleRecord { #[scale_info(skip_type_params(MaxConsumers))]
pub struct RemoteLockedFungibleRecord<ConsumerIdentifier, MaxConsumers: Get<u32>> {
/// Total amount of the asset held by the remote lock.
pub amount: u128, pub amount: u128,
/// The owner of the locked asset.
pub owner: VersionedMultiLocation, pub owner: VersionedMultiLocation,
/// The location which holds the original lock.
pub locker: VersionedMultiLocation, pub locker: VersionedMultiLocation,
pub users: u32, /// Local consumers of the remote lock with a consumer identifier and the amount
/// of fungible asset every consumer holds.
/// Every consumer can hold up to total amount of the remote lock.
pub consumers: BoundedVec<(ConsumerIdentifier, u128), MaxConsumers>,
}
impl<LockId, MaxConsumers: Get<u32>> RemoteLockedFungibleRecord<LockId, MaxConsumers> {
/// Amount of the remote lock in use by consumers.
/// Returns `None` if the remote lock has no consumers.
pub fn amount_held(&self) -> Option<u128> {
self.consumers.iter().max_by(|x, y| x.1.cmp(&y.1)).map(|max| max.1)
}
} }
/// Fungible assets which we know are locked on a remote chain. /// Fungible assets which we know are locked on a remote chain.
@@ -604,7 +625,7 @@ pub mod pallet {
NMapKey<Blake2_128Concat, T::AccountId>, NMapKey<Blake2_128Concat, T::AccountId>,
NMapKey<Blake2_128Concat, VersionedAssetId>, NMapKey<Blake2_128Concat, VersionedAssetId>,
), ),
RemoteLockedFungibleRecord, RemoteLockedFungibleRecord<T::RemoteLockConsumerIdentifier, T::MaxRemoteLockConsumers>,
OptionQuery, OptionQuery,
>; >;
@@ -1693,11 +1714,12 @@ impl<T: Config> xcm_executor::traits::Enact for ReduceTicket<T> {
use xcm_executor::traits::LockError::UnexpectedState; use xcm_executor::traits::LockError::UnexpectedState;
let mut record = RemoteLockedFungibles::<T>::get(&self.key).ok_or(UnexpectedState)?; let mut record = RemoteLockedFungibles::<T>::get(&self.key).ok_or(UnexpectedState)?;
ensure!(self.locker == record.locker && self.owner == record.owner, UnexpectedState); ensure!(self.locker == record.locker && self.owner == record.owner, UnexpectedState);
ensure!(record.users == 0, UnexpectedState); let new_amount = record.amount.checked_sub(self.amount).ok_or(UnexpectedState)?;
record.amount = record.amount.checked_sub(self.amount).ok_or(UnexpectedState)?; ensure!(record.amount_held().map_or(true, |h| new_amount >= h), UnexpectedState);
if record.amount == 0 { if new_amount == 0 {
RemoteLockedFungibles::<T>::remove(&self.key); RemoteLockedFungibles::<T>::remove(&self.key);
} else { } else {
record.amount = new_amount;
RemoteLockedFungibles::<T>::insert(&self.key, &record); RemoteLockedFungibles::<T>::insert(&self.key, &record);
} }
Ok(()) Ok(())
@@ -1757,11 +1779,12 @@ impl<T: Config> xcm_executor::traits::AssetLock for Pallet<T> {
let owner = owner.into(); let owner = owner.into();
let id: VersionedAssetId = asset.id.into(); let id: VersionedAssetId = asset.id.into();
let key = (XCM_VERSION, account, id); let key = (XCM_VERSION, account, id);
let mut record = RemoteLockedFungibleRecord { amount, owner, locker, users: 0 }; let mut record =
RemoteLockedFungibleRecord { amount, owner, locker, consumers: BoundedVec::default() };
if let Some(old) = RemoteLockedFungibles::<T>::get(&key) { if let Some(old) = RemoteLockedFungibles::<T>::get(&key) {
// Make sure that the new record wouldn't clobber any old data. // Make sure that the new record wouldn't clobber any old data.
ensure!(old.locker == record.locker && old.owner == record.owner, WouldClobber); ensure!(old.locker == record.locker && old.owner == record.owner, WouldClobber);
record.users = old.users; record.consumers = old.consumers;
record.amount = record.amount.max(old.amount); record.amount = record.amount.max(old.amount);
} }
RemoteLockedFungibles::<T>::insert(&key, record); RemoteLockedFungibles::<T>::insert(&key, record);
@@ -1788,8 +1811,11 @@ impl<T: Config> xcm_executor::traits::AssetLock for Pallet<T> {
let record = RemoteLockedFungibles::<T>::get(&key).ok_or(NotLocked)?; let record = RemoteLockedFungibles::<T>::get(&key).ok_or(NotLocked)?;
// Make sure that the record contains what we expect and there's enough to unlock. // Make sure that the record contains what we expect and there's enough to unlock.
ensure!(locker == record.locker && owner == record.owner, WouldClobber); ensure!(locker == record.locker && owner == record.owner, WouldClobber);
ensure!(record.users == 0, InUse);
ensure!(record.amount >= amount, NotEnoughLocked); ensure!(record.amount >= amount, NotEnoughLocked);
ensure!(
record.amount_held().map_or(true, |h| record.amount.saturating_sub(amount) >= h),
InUse
);
Ok(ReduceTicket { key, amount, locker, owner }) Ok(ReduceTicket { key, amount, locker, owner })
} }
} }
+2
View File
@@ -342,6 +342,8 @@ impl pallet_xcm::Config for Test {
type Currency = Balances; type Currency = Balances;
type CurrencyMatcher = IsConcrete<RelayLocation>; type CurrencyMatcher = IsConcrete<RelayLocation>;
type MaxLockers = frame_support::traits::ConstU32<8>; type MaxLockers = frame_support::traits::ConstU32<8>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = TestWeightInfo; type WeightInfo = TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -233,6 +233,8 @@ impl pallet_xcm::Config for Runtime {
type Currency = Balances; type Currency = Balances;
type CurrencyMatcher = IsConcrete<KsmLocation>; type CurrencyMatcher = IsConcrete<KsmLocation>;
type MaxLockers = frame_support::traits::ConstU32<8>; type MaxLockers = frame_support::traits::ConstU32<8>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo; type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -423,6 +423,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = LocationToAccountId; type SovereignAccountOf = LocationToAccountId;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo; type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -220,6 +220,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = LocationToAccountId; type SovereignAccountOf = LocationToAccountId;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo; type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -338,6 +338,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = LocationToAccountId; type SovereignAccountOf = LocationToAccountId;
type MaxLockers = frame_support::traits::ConstU32<8>; type MaxLockers = frame_support::traits::ConstU32<8>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo; type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;
@@ -184,6 +184,8 @@ impl pallet_xcm::Config for Runtime {
type TrustedLockers = (); type TrustedLockers = ();
type SovereignAccountOf = SovereignAccountOf; type SovereignAccountOf = SovereignAccountOf;
type MaxLockers = ConstU32<8>; type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo; type WeightInfo = pallet_xcm::TestWeightInfo;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest; type ReachableDest = ReachableDest;