Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AU): charge storage fee and add initial CE #1058

Merged
merged 16 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions chain-extensions/unified-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ log = { workspace = true }
num-traits = { workspace = true }
pallet-contracts = { workspace = true }
pallet-contracts-primitives = { workspace = true }
pallet-evm = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-core = { workspace = true }
Expand All @@ -33,6 +34,7 @@ std = [
"frame-system/std",
"num-traits/std",
"pallet-contracts/std",
"pallet-evm/std",
"pallet-contracts-primitives/std",
"scale-info/std",
"sp-std/std",
Expand Down
31 changes: 27 additions & 4 deletions chain-extensions/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

#![cfg_attr(not(feature = "std"), no_std)]

use astar_primitives::evm::{EvmAddress, UnifiedAddressMapper};
use astar_primitives::{
ethereum_checked::AccountMapping,
evm::{EvmAddress, UnifiedAddressMapper},
};
use core::marker::PhantomData;
use sp_runtime::DispatchError;

use frame_support::{traits::Get, DefaultNoBound};
use pallet_contracts::chain_extension::{
ChainExtension, Environment, Ext, InitState, Result as DispatchResult, RetVal,
};
use pallet_evm::AddressMapping;
use pallet_unified_accounts::{EvmToNative, NativeToEvm};
use parity_scale_codec::Encode;
pub use unified_accounts_chain_extension_types::Command::{self, *};

Expand Down Expand Up @@ -58,8 +63,17 @@ where

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;

// read the storage item
let mapped = NativeToEvm::<T>::get(account_id.clone());
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved

let is_mapped = mapped.is_some();
let evm_address = mapped.unwrap_or_else(|| {
// fallback to default account_id
T::DefaultNativeToEvm::into_h160(account_id)
});
// write to buffer
UA::to_h160_or_default(&account_id).using_encoded(|r| env.write(r, false, None))?;
(evm_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
}
GetNativeAddress => {
let evm_address: EvmAddress = env.read_as()?;
Expand All @@ -74,9 +88,18 @@ where

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;

// read the storage item
let mapped = EvmToNative::<T>::get(evm_address.clone());
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved

let is_mapped = mapped.is_some();
let native_address = mapped.unwrap_or_else(|| {
// fallback to default evm_address
T::DefaultEvmToNative::into_account_id(evm_address)
});

// write to buffer
UA::to_account_id_or_default(&evm_address)
.using_encoded(|r| env.write(r, false, None))?;
(native_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
}
};
Ok(RetVal::Converging(0))
Expand Down
Binary file modified tests/ink-contracts/au_ce_getters.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/integration/src/unified_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ fn unified_accounts_chain_extension_works() {
);
// default h160 address should match
assert_eq!(
call_wasm_contract_method::<H160>(
call_wasm_contract_method::<(H160, bool)>(
ALICE,
contract_id.clone(),
[GET_H160_OR_DEFAULT.to_vec(), ALICE.encode()].concat()
),
UnifiedAccounts::to_h160_or_default(&ALICE)
(UnifiedAccounts::to_h160_or_default(&ALICE), false)
);
// mapped native address should be None
assert_eq!(
Expand All @@ -82,12 +82,12 @@ fn unified_accounts_chain_extension_works() {
);
// default native address should match
assert_eq!(
call_wasm_contract_method::<AccountId>(
call_wasm_contract_method::<(AccountId, bool)>(
ALICE,
contract_id.clone(),
[GET_NATIVE_OR_DEFAULT.to_vec(), alith().encode()].concat()
),
UnifiedAccounts::to_account_id_or_default(&alith())
(UnifiedAccounts::to_account_id_or_default(&alith()), false)
);

//
Expand Down
Loading