Skip to content

Commit

Permalink
feat: inline add_mappings() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Sep 26, 2023
1 parent 2609aec commit 037028a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
27 changes: 14 additions & 13 deletions pallets/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ pub mod pallet {
}

// create double mappings for the pair
Self::add_mappings(who, evm_address);
NativeToEvm::<T>::insert(&evm_address, &who);
EvmToNative::<T>::insert(&who, &evm_address);

Self::deposit_event(Event::AccountClaimed {
account_id: who,
evm_address,
});
Ok(())
}

Expand All @@ -226,17 +232,6 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
/// Add the given pair to create double mappings
fn add_mappings(account_id: T::AccountId, evm_address: EvmAddress) {
NativeToEvm::<T>::insert(&evm_address, &account_id);
EvmToNative::<T>::insert(&account_id, &evm_address);

Self::deposit_event(Event::AccountClaimed {
account_id,
evm_address,
});
}

/// Claim the default evm address
fn do_claim_default_evm_address(account_id: T::AccountId) -> Result<EvmAddress, DispatchError> {
ensure!(
Expand All @@ -252,7 +247,13 @@ impl<T: Config> Pallet<T> {
Error::<T>::AlreadyMapped
);
// create double mappings for the pair with default evm address
Self::add_mappings(account_id, evm_address.clone());
NativeToEvm::<T>::insert(&evm_address, &account_id);
EvmToNative::<T>::insert(&account_id, &evm_address);

Self::deposit_event(Event::AccountClaimed {
account_id,
evm_address,
});
Ok(evm_address)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pallets/unified-accounts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ fn account_default_claim_should_not_work_if_collision() {
ExtBuilder::default().build().execute_with(|| {
let bob_default_h160 = <UnifiedAccounts as UnifiedAddressMapper<_>>::to_default_h160(&BOB);

// connect alice native with bob's default address
// create mapping of alice native with bob's default address
// in real world possibilty of this happening is minuscule
UnifiedAccounts::add_mappings(ALICE, bob_default_h160);
NativeToEvm::<TestRuntime>::insert(&bob_default_h160, &ALICE);
EvmToNative::<TestRuntime>::insert(&ALICE, &bob_default_h160);

// bob try claiming default h160 address, it should fail since alice already
// has mapping in place with it.
Expand Down

0 comments on commit 037028a

Please sign in to comment.