Skip to content

Commit

Permalink
Try to remove duplicated test code (#3135)
Browse files Browse the repository at this point in the history
* Try to remove duplicated test code

* remove storage
  • Loading branch information
Kailai-Wang authored Oct 18, 2024
1 parent cbbb20d commit 2fd6307
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 274 deletions.
18 changes: 0 additions & 18 deletions common/primitives/core/src/omni_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ impl From<Identity> for MemberAccount {
}
}

pub trait GetAccountStoreHash {
fn hash(&self) -> Hash;
}

pub trait OmniAccountConverter {
type OmniAccount;
fn convert(identity: &Identity) -> Self::OmniAccount;
Expand All @@ -62,17 +58,3 @@ impl OmniAccountConverter for DefaultOmniAccountConverter {
identity.to_omni_account()
}
}

impl<T> GetAccountStoreHash for BoundedVec<MemberAccount, T> {
fn hash(&self) -> Hash {
let hashes: Vec<Hash> = self.iter().map(|member| member.hash()).collect();
hashes.using_encoded(blake2_256).into()
}
}

impl GetAccountStoreHash for Vec<MemberAccount> {
fn hash(&self) -> Hash {
let hashes: Vec<Hash> = self.iter().map(|member| member.hash()).collect();
hashes.using_encoded(blake2_256).into()
}
}
21 changes: 4 additions & 17 deletions parachain/pallets/omni-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ mod mock;
#[cfg(test)]
mod tests;

pub use core_primitives::{
GetAccountStoreHash, Identity, Intention, MemberAccount, OmniAccountConverter,
};
pub use core_primitives::{Identity, Intention, MemberAccount, OmniAccountConverter};
pub use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;

Expand Down Expand Up @@ -118,17 +116,11 @@ pub mod pallet {
pub type MemberAccountHash<T: Config> =
StorageMap<Hasher = Blake2_128Concat, Key = H256, Value = T::AccountId>;

/// A map between OmniAccount and hash of its AccountStore
#[pallet::storage]
#[pallet::getter(fn account_store_hash)]
pub type AccountStoreHash<T: Config> =
StorageMap<Hasher = Blake2_128Concat, Key = T::AccountId, Value = H256>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// An account store is created
AccountStoreCreated { who: T::AccountId, account_store_hash: H256 },
AccountStoreCreated { who: T::AccountId },
/// Some member account is added
AccountAdded { who: T::AccountId, member_account_hash: H256 },
/// Some member accounts are removed
Expand Down Expand Up @@ -213,13 +205,9 @@ pub mod pallet {
.map_err(|_| Error::<T>::AccountStoreLenLimitReached)?;

MemberAccountHash::<T>::insert(hash, omni_account.clone());
AccountStore::<T>::insert(omni_account.clone(), member_accounts.clone());
AccountStoreHash::<T>::insert(omni_account.clone(), member_accounts.hash());
AccountStore::<T>::insert(omni_account.clone(), member_accounts);

Self::deposit_event(Event::AccountStoreCreated {
who: omni_account,
account_store_hash: member_accounts.hash(),
});
Self::deposit_event(Event::AccountStoreCreated { who: omni_account });

Ok(())
}
Expand Down Expand Up @@ -247,7 +235,6 @@ pub mod pallet {

MemberAccountHash::<T>::insert(hash, who.clone());
AccountStore::<T>::insert(who.clone(), member_accounts.clone());
AccountStoreHash::<T>::insert(who.clone(), member_accounts.hash());

Self::deposit_event(Event::AccountAdded { who, member_account_hash: hash });

Expand Down
38 changes: 29 additions & 9 deletions parachain/pallets/omni-account/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

use crate::{self as pallet_omni_account, EnsureOmniAccount};
use core_primitives::DefaultOmniAccountConverter;
use crate::{self as pallet_omni_account, Encode, EnsureOmniAccount};
use core_primitives::{DefaultOmniAccountConverter, Identity, MemberAccount};
use frame_support::{
assert_ok,
pallet_prelude::EnsureOrigin,
Expand Down Expand Up @@ -70,16 +70,36 @@ where
}
}

pub fn alice() -> AccountId {
AccountKeyring::Alice.to_account_id()
pub struct Accounts {
pub native_account: AccountId,
pub omni_account: AccountId,
pub identity: Identity,
}

pub fn bob() -> AccountId {
AccountKeyring::Bob.to_account_id()
fn create_accounts(keyring: AccountKeyring) -> Accounts {
let native_account = keyring.to_account_id();
let identity = Identity::from(native_account.clone());
Accounts { native_account, omni_account: identity.to_omni_account(), identity }
}

pub fn charlie() -> AccountId {
AccountKeyring::Charlie.to_account_id()
pub fn alice() -> Accounts {
create_accounts(AccountKeyring::Alice)
}

pub fn bob() -> Accounts {
create_accounts(AccountKeyring::Bob)
}

pub fn charlie() -> Accounts {
create_accounts(AccountKeyring::Charlie)
}

pub fn public_member_account(accounts: Accounts) -> MemberAccount {
MemberAccount::Public(accounts.identity)
}

pub fn private_member_account(accounts: Accounts) -> MemberAccount {
MemberAccount::Private(accounts.identity.encode(), accounts.identity.hash())
}

frame_support::construct_runtime!(
Expand Down Expand Up @@ -175,7 +195,7 @@ pub fn get_tee_signer() -> SystemAccountId {

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<TestRuntime> { balances: vec![(alice(), 10)] }
pallet_balances::GenesisConfig::<TestRuntime> { balances: vec![(alice().native_account, 10)] }
.assimilate_storage(&mut t)
.unwrap();

Expand Down
Loading

0 comments on commit 2fd6307

Please sign in to comment.