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

feat: add force_reset_foreign_asset_location #1098

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions pallets/foreign-assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,30 @@ mod benchmarks {

Ok(())
}

#[benchmark]
fn force_reset_foreign_asset_location() -> Result<(), BenchmarkError> {
let old_asset_id: AssetId = (Parachain(1000), PalletInstance(42), GeneralIndex(1)).into();
let new_asset_id: AssetId = (Parachain(2000), PalletInstance(42), GeneralIndex(1)).into();
let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();
let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();
let mode = ForeignCollectionMode::NFT;

<Pallet<T>>::force_register_foreign_asset(
RawOrigin::Root.into(),
Box::new(old_asset_id.clone().into()),
name,
token_prefix,
mode,
)?;

#[extrinsic_call]
_(
RawOrigin::Root,
Box::new(old_asset_id.into()),
Box::new(new_asset_id.into()),
);

Ok(())
}
}
46 changes: 46 additions & 0 deletions pallets/foreign-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ pub mod module {

/// The given asset ID could not be converted into the current XCM version.
BadForeignAssetId,

/// The specified foreign asset is not found.
ForeignAssetNotFound,
}

#[pallet::event]
Expand All @@ -131,6 +134,11 @@ pub mod module {

/// The migration status.
MigrationStatus(Box<MigrationStatus>),

ForeignAssetMoved {
old_asset_id: Box<VersionedAssetId>,
new_asset_id: Box<VersionedAssetId>,
}
}

/// The corresponding collections of foreign assets.
Expand Down Expand Up @@ -233,6 +241,44 @@ pub mod module {

Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::force_reset_foreign_asset_location())]
pub fn force_reset_foreign_asset_location(
origin: OriginFor<T>,
existing_versioned_asset_id: Box<VersionedAssetId>,
new_versioned_asset_id: Box<VersionedAssetId>,
) -> DispatchResult {
T::ManagerOrigin::ensure_origin(origin.clone())?;

let existing_asset_id: AssetId = existing_versioned_asset_id
.as_ref()
.clone()
.try_into()
.map_err(|()| Error::<T>::BadForeignAssetId)?;

let new_asset_id: AssetId = new_versioned_asset_id
.as_ref()
.clone()
.try_into()
.map_err(|()| Error::<T>::BadForeignAssetId)?;

let collection_id = <ForeignAssetToCollection<T>>::get(&existing_asset_id)
.ok_or(Error::<T>::ForeignAssetNotFound)?;

<ForeignAssetToCollection<T>>::remove(&existing_asset_id);
<CollectionToForeignAsset<T>>::remove(collection_id);

<ForeignAssetToCollection<T>>::insert(&new_asset_id, collection_id);
<CollectionToForeignAsset<T>>::insert(collection_id, new_asset_id);

Self::deposit_event(Event::<T>::ForeignAssetMoved {
old_asset_id: existing_versioned_asset_id,
new_asset_id: new_versioned_asset_id,
});

Ok(())
}
}

#[pallet::genesis_config]
Expand Down
24 changes: 24 additions & 0 deletions pallets/foreign-assets/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_foreign_assets.
pub trait WeightInfo {
fn force_register_foreign_asset() -> Weight;

fn force_reset_foreign_asset_location() -> Weight;
}

/// Weights for pallet_foreign_assets using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -64,6 +66,17 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(7_u64))
}

// FIXME run the benchmarks
fn force_reset_foreign_asset_location() -> Weight {
// Proof Size summary in bytes:
// Measured: `146`
// Estimated: `4080`
// Minimum execution time: 26_678_000 picoseconds.
Weight::from_parts(27_177_000, 4080)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(7_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -93,5 +106,16 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(7_u64))
}

// FIXME run the benchmarks
fn force_reset_foreign_asset_location() -> Weight {
// Proof Size summary in bytes:
// Measured: `146`
// Estimated: `4080`
// Minimum execution time: 26_678_000 picoseconds.
Weight::from_parts(27_177_000, 4080)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(7_u64))
}
}