From 28afa48006dff2084cfd69b7c8c719b6b56c8005 Mon Sep 17 00:00:00 2001 From: Squirrel <gilescope@gmail.com> Date: Fri, 2 Jun 2023 13:54:01 +0100 Subject: [PATCH] asset-conversion pallet: Generalise integrity test (#14289) * integrity test was only working for u32 asset ids. * cargo fmt * Update frame/asset-conversion/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> --------- Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> --- substrate/frame/asset-conversion/src/lib.rs | 38 +++++++++---------- substrate/frame/asset-conversion/src/mock.rs | 2 +- substrate/frame/asset-conversion/src/types.rs | 4 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/substrate/frame/asset-conversion/src/lib.rs b/substrate/frame/asset-conversion/src/lib.rs index 3db694a88e2..266c4eb7744 100644 --- a/substrate/frame/asset-conversion/src/lib.rs +++ b/substrate/frame/asset-conversion/src/lib.rs @@ -201,7 +201,7 @@ pub mod pallet { type WeightInfo: WeightInfo; /// The benchmarks need a way to create asset ids from u32s. - #[cfg(feature = "runtime-benchmarks")] + #[cfg(any(test, feature = "runtime-benchmarks"))] type BenchmarkHelper: BenchmarkHelper<Self::AssetId>; } @@ -352,24 +352,24 @@ pub mod pallet { #[pallet::hooks] impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> { fn integrity_test() { - sp_std::if_std! { - sp_io::TestExternalities::new_empty().execute_with(|| { - // ensure that the `AccountId` is set properly and doesn't generate the same - // pool account for different pool ids. - let native = T::MultiAssetIdConverter::get_native(); - // Decode the asset ids from bytes. - let asset_1 = T::MultiAssetIdConverter::into_multiasset_id(&T::AssetId::decode(&mut vec![0u8, 0, 0, 1].as_slice()).unwrap()); - let asset_2 = T::MultiAssetIdConverter::into_multiasset_id(&T::AssetId::decode(&mut vec![255u8, 255, 255, 255].as_slice()).unwrap()); - assert!(asset_1 != asset_2, "unfortunatly decoded to be the same asset."); - let pool_account_1 = Self::get_pool_account(&(native.clone(), asset_1)); - let pool_account_2 = Self::get_pool_account(&(native, asset_2)); - assert!(sp_std::mem::size_of::<T::AccountId>() >= sp_std::mem::size_of::<u128>()); - assert!( - pool_account_1 != pool_account_2, - "AccountId should be set at least to u128" - ); - }); - } + #[cfg(test)] + sp_io::TestExternalities::new_empty().execute_with(|| { + // ensure that the `AccountId` is set properly and doesn't generate the same + // pool account for different pool ids. + let native = T::MultiAssetIdConverter::get_native(); + let asset_1 = + T::MultiAssetIdConverter::into_multiasset_id(&T::BenchmarkHelper::asset_id(1)); + let asset_2 = + T::MultiAssetIdConverter::into_multiasset_id(&T::BenchmarkHelper::asset_id(2)); + assert!(asset_1 != asset_2, "must return different assets for different ids."); + let pool_account_1 = Self::get_pool_account(&(native.clone(), asset_1)); + let pool_account_2 = Self::get_pool_account(&(native, asset_2)); + assert!(sp_std::mem::size_of::<T::AccountId>() >= sp_std::mem::size_of::<u128>()); + assert!( + pool_account_1 != pool_account_2, + "AccountId should be set at least to u128" + ); + }); } } diff --git a/substrate/frame/asset-conversion/src/mock.rs b/substrate/frame/asset-conversion/src/mock.rs index 34d2eeb273c..9483cfca25c 100644 --- a/substrate/frame/asset-conversion/src/mock.rs +++ b/substrate/frame/asset-conversion/src/mock.rs @@ -178,7 +178,7 @@ impl Config for Test { type MultiAssetId = NativeOrAssetId<u32>; type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>; - #[cfg(feature = "runtime-benchmarks")] + #[cfg(any(test, feature = "runtime-benchmarks"))] type BenchmarkHelper = (); } diff --git a/substrate/frame/asset-conversion/src/types.rs b/substrate/frame/asset-conversion/src/types.rs index 837b14be283..b370d410f82 100644 --- a/substrate/frame/asset-conversion/src/types.rs +++ b/substrate/frame/asset-conversion/src/types.rs @@ -50,13 +50,13 @@ pub trait MultiAssetIdConverter<MultiAssetId, AssetId> { } /// Benchmark Helper -#[cfg(feature = "runtime-benchmarks")] +#[cfg(any(test, feature = "runtime-benchmarks"))] pub trait BenchmarkHelper<AssetId> { /// Returns an asset id from a given integer. fn asset_id(asset_id: u32) -> AssetId; } -#[cfg(feature = "runtime-benchmarks")] +#[cfg(any(test, feature = "runtime-benchmarks"))] impl<AssetId> BenchmarkHelper<AssetId> for () where AssetId: From<u32>, -- GitLab