From 3b9f5149497a5a3a61db0408bd7b9675910b5d3d Mon Sep 17 00:00:00 2001 From: ron Date: Mon, 14 Oct 2024 22:18:51 +0800 Subject: [PATCH] Validate asset meta data --- bridges/snowbridge/pallets/system/src/lib.rs | 2 ++ bridges/snowbridge/pallets/system/src/mock.rs | 12 ++++++++++-- bridges/snowbridge/pallets/system/src/tests.rs | 4 ++-- bridges/snowbridge/primitives/core/src/lib.rs | 6 ++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/bridges/snowbridge/pallets/system/src/lib.rs b/bridges/snowbridge/pallets/system/src/lib.rs index 1e8a788b7a5a..b777edfe4ea6 100644 --- a/bridges/snowbridge/pallets/system/src/lib.rs +++ b/bridges/snowbridge/pallets/system/src/lib.rs @@ -249,6 +249,7 @@ pub mod pallet { InvalidTokenTransferFees, InvalidPricingParameters, InvalidUpgradeParameters, + InvalidMetadata, } /// The set of registered agents @@ -624,6 +625,7 @@ pub mod pallet { metadata: AssetMetadata, ) -> DispatchResultWithPostInfo { ensure_root(origin)?; + ensure!(metadata.validate(), Error::::InvalidMetadata); let location: Location = (*location).try_into().map_err(|_| Error::::UnsupportedLocationVersion)?; diff --git a/bridges/snowbridge/pallets/system/src/mock.rs b/bridges/snowbridge/pallets/system/src/mock.rs index 47b089866a53..3fd38ab4ede6 100644 --- a/bridges/snowbridge/pallets/system/src/mock.rs +++ b/bridges/snowbridge/pallets/system/src/mock.rs @@ -5,14 +5,14 @@ use frame_support::{ derive_impl, parameter_types, traits::{tokens::fungible::Mutate, ConstU128, ConstU8}, weights::IdentityFee, - PalletId, + BoundedVec, PalletId, }; use sp_core::H256; use xcm_executor::traits::ConvertLocation; use snowbridge_core::{ gwei, meth, outbound::ConstantGasMeter, sibling_sovereign_account, AgentId, AllowSiblingsOnly, - ParaId, PricingParameters, Rewards, + AssetMetadata, ParaId, PricingParameters, Rewards, }; use sp_runtime::{ traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, Keccak256}, @@ -253,3 +253,11 @@ pub fn make_agent_id(location: Location) -> AgentId { ::AgentIdOf::convert_location(&location) .expect("convert location") } + +pub fn mock_asset_meta() -> AssetMetadata { + AssetMetadata { + name: BoundedVec::try_from("SDOT".as_bytes().to_vec()).unwrap(), + symbol: BoundedVec::try_from("SDOT".as_bytes().to_vec()).unwrap(), + decimals: 10, + } +} diff --git a/bridges/snowbridge/pallets/system/src/tests.rs b/bridges/snowbridge/pallets/system/src/tests.rs index d0286e04abdf..1734f3148465 100644 --- a/bridges/snowbridge/pallets/system/src/tests.rs +++ b/bridges/snowbridge/pallets/system/src/tests.rs @@ -709,7 +709,7 @@ fn register_all_tokens_succeeds() { assert_ok!(EthereumSystem::register_token( origin, Box::new(versioned_location), - Default::default() + mock_asset_meta() )); assert_eq!(NativeToForeignId::::get(tc.reanchored.clone()), Some(tc.foreign)); @@ -739,7 +739,7 @@ fn register_ethereum_native_token_fails() { ); let versioned_location: Box = Box::new(location.clone().into()); assert_noop!( - EthereumSystem::register_token(origin, versioned_location, Default::default()), + EthereumSystem::register_token(origin, versioned_location, mock_asset_meta()), Error::::LocationConversionFailed ); }); diff --git a/bridges/snowbridge/primitives/core/src/lib.rs b/bridges/snowbridge/primitives/core/src/lib.rs index 7ad129a52542..ab194e2ec599 100644 --- a/bridges/snowbridge/primitives/core/src/lib.rs +++ b/bridges/snowbridge/primitives/core/src/lib.rs @@ -159,6 +159,12 @@ pub struct AssetMetadata { pub decimals: u8, } +impl AssetMetadata { + pub fn validate(&self) -> bool { + self.decimals > 0 && self.name.len() > 0 && self.symbol.len() > 0 + } +} + #[cfg(any(test, feature = "std", feature = "runtime-benchmarks"))] impl Default for AssetMetadata { fn default() -> Self {