Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Validate in extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Oct 21, 2024
1 parent 3b9f514 commit 0808537
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 4 additions & 1 deletion bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,10 @@ pub mod pallet {
metadata: AssetMetadata,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
ensure!(metadata.validate(), Error::<T>::InvalidMetadata);
ensure!(
metadata.decimals > 0 && metadata.name.len() > 0 && metadata.symbol.len() > 0,
Error::<T>::InvalidMetadata
);

let location: Location =
(*location).try_into().map_err(|_| Error::<T>::UnsupportedLocationVersion)?;
Expand Down
22 changes: 22 additions & 0 deletions bridges/snowbridge/pallets/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,25 @@ fn register_ethereum_native_token_fails() {
);
});
}

#[test]
fn register_token_with_invalid_meta_data_fails() {
new_test_ext(true).execute_with(|| {
let origin = RuntimeOrigin::root();
let location = Location::new(
2,
[
GlobalConsensus(Ethereum { chain_id: 11155111 }),
AccountKey20 {
network: None,
key: hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d"),
},
],
);
let versioned_location: Box<VersionedLocation> = Box::new(location.clone().into());
assert_noop!(
EthereumSystem::register_token(origin, versioned_location, AssetMetadata::default()),
Error::<Test>::InvalidMetadata
);
});
}
6 changes: 0 additions & 6 deletions bridges/snowbridge/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,6 @@ 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 {
Expand Down

0 comments on commit 0808537

Please sign in to comment.