Skip to content

Commit

Permalink
Add implementation of codec::MaxEncodedLen for all types persisted to…
Browse files Browse the repository at this point in the history
… storage in assets-registry
  • Loading branch information
andor0 committed Feb 1, 2022
1 parent 0dd32dc commit 9815dfb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
29 changes: 7 additions & 22 deletions frame/assets-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ pub mod pallet {
type ForeignAdminOrigin: EnsureOrigin<Self::Origin>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, TypeInfo)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum CandidateStatus {
LocalAdminApproved,
ForeignAdminApproved,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, TypeInfo)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct ForeignMetadata {
pub decimals: u8,
}
Expand All @@ -84,17 +84,12 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn local_admin)]
/// Local admin account
#[allow(clippy::disallowed_type)] // LocalAdminOnEmpty provides a default value, so ValueQuery is ok here.
pub type LocalAdmin<T: Config> =
StorageValue<_, T::AccountId, ValueQuery, LocalAdminOnEmpty<T>>;
pub type LocalAdmin<T: Config> = StorageValue<_, T::AccountId>;

#[pallet::storage]
#[pallet::getter(fn foreign_admin)]
/// Foreign admin account
#[allow(clippy::disallowed_type)] // ForeignAdminOnEmpty provides a default value, so ValueQuery is ok here.
pub type ForeignAdmin<T: Config> =
StorageValue<_, T::AccountId, ValueQuery, ForeignAdminOnEmpty<T>>;
pub type ForeignAdmin<T: Config> = StorageValue<_, T::AccountId>;

#[pallet::storage]
#[pallet::getter(fn from_local_asset)]
Expand Down Expand Up @@ -125,16 +120,6 @@ pub mod pallet {
pub type ForeignAssetMetadata<T: Config> =
StorageMap<_, Blake2_128Concat, T::LocalAssetId, ForeignMetadata, OptionQuery>;

#[pallet::type_value]
pub fn LocalAdminOnEmpty<T: Config>() -> T::AccountId {
T::AccountId::default()
}

#[pallet::type_value]
pub fn ForeignAdminOnEmpty<T: Config>() -> T::AccountId {
T::AccountId::default()
}

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
local_admin: Option<T::AccountId>,
Expand Down Expand Up @@ -305,7 +290,7 @@ pub mod pallet {
let foreign_admin = <ForeignAdmin<T>>::get();
match current_candidate_status {
None =>
if who == local_admin {
if Some(who) == local_admin {
<AssetsMappingCandidates<T>>::insert(
(local_asset_id, foreign_asset_id),
CandidateStatus::LocalAdminApproved,
Expand All @@ -317,12 +302,12 @@ pub mod pallet {
);
},
Some(CandidateStatus::LocalAdminApproved) =>
if who == foreign_admin {
if Some(who) == foreign_admin {
Self::set_location(local_asset_id, foreign_asset_id.clone())?;
<AssetsMappingCandidates<T>>::remove((local_asset_id, foreign_asset_id));
},
Some(CandidateStatus::ForeignAdminApproved) =>
if who == local_admin {
if Some(who) == local_admin {
Self::set_location(local_asset_id, foreign_asset_id.clone())?;
<AssetsMappingCandidates<T>>::remove((local_asset_id, foreign_asset_id));
},
Expand Down
8 changes: 6 additions & 2 deletions frame/assets-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ use sp_runtime::traits::BadOrigin;
#[test]
fn set_local_admin_tests() {
new_test_ext().execute_with(|| {
assert_noop!(AssetsRegistry::set_local_admin(Origin::signed(ALICE), ALICE), BadOrigin,);
assert_eq!(AssetsRegistry::local_admin(), None);
assert_noop!(AssetsRegistry::set_local_admin(Origin::signed(ALICE), ALICE), BadOrigin);

assert_ok!(AssetsRegistry::set_local_admin(Origin::signed(ROOT), ALICE));
assert_eq!(AssetsRegistry::local_admin(), Some(ALICE));
})
}

#[test]
fn set_foreign_admin_tests() {
new_test_ext().execute_with(|| {
assert_noop!(AssetsRegistry::set_foreign_admin(Origin::signed(BOB), BOB), BadOrigin,);
assert_eq!(AssetsRegistry::foreign_admin(), None);
assert_noop!(AssetsRegistry::set_foreign_admin(Origin::signed(BOB), BOB), BadOrigin);

assert_ok!(AssetsRegistry::set_foreign_admin(Origin::signed(ROOT), BOB));
assert_eq!(AssetsRegistry::foreign_admin(), Some(BOB));
})
}

Expand Down

0 comments on commit 9815dfb

Please sign in to comment.