From 7364f63adf220e7f17228528c2a15a822a386392 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Sun, 15 Oct 2023 18:44:58 +0530 Subject: [PATCH 01/15] feat: charge storage fee for mappings --- pallets/unified-accounts/src/lib.rs | 27 ++++++++++++++++--- pallets/unified-accounts/src/mock.rs | 3 ++- pallets/unified-accounts/src/tests.rs | 37 +++++++++++++++++++-------- runtime/shibuya/src/lib.rs | 6 +++++ tests/integration/src/xvm.rs | 8 ++++++ 5 files changed, 65 insertions(+), 16 deletions(-) diff --git a/pallets/unified-accounts/src/lib.rs b/pallets/unified-accounts/src/lib.rs index b0ac498832..93585f5f5b 100644 --- a/pallets/unified-accounts/src/lib.rs +++ b/pallets/unified-accounts/src/lib.rs @@ -68,8 +68,8 @@ use astar_primitives::{ use frame_support::{ pallet_prelude::*, traits::{ - fungible::{Inspect, Mutate}, - tokens::{Fortitude::*, Preservation::*}, + fungible::{Inspect as FungibleInspect, Mutate as FungibleMutate}, + tokens::{Fortitude::*, Precision::*, Preservation::*}, IsType, OnKilledAccount, }, }; @@ -97,6 +97,9 @@ mod tests; /// ECDSA Signature type, with last bit for recovering address type EvmSignature = [u8; 65]; +type BalanceOf = + <::Currency as FungibleInspect<::AccountId>>::Balance; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -109,13 +112,18 @@ pub mod pallet { /// The overarching event type type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The Currency for managing evm address assets - type Currency: Mutate; + type Currency: FungibleMutate; /// Default evm address to account id conversion type DefaultEvmToNative: AddressMapping; /// Default account id to evm address conversion type DefaultNativeToEvm: AccountMapping; /// EVM chain id + #[pallet::constant] type ChainId: Get; + /// The amount of currency needed per mapping to be added. + /// TODO: calculate total bytes key + value in storage for mappings + #[pallet::constant] + type AccountMappingStorageFee: Get>; /// Weight information for the extrinsics in this module type WeightInfo: WeightInfo; } @@ -190,6 +198,9 @@ pub mod pallet { ensure!(evm_address == address, Error::::InvalidSignature); + // charge the storage fee + Self::charge_storage_fee(&who)?; + // Check if the default account id already exists for this evm address let default_account_id = T::DefaultEvmToNative::into_account_id(evm_address.clone()); if frame_system::Pallet::::account_exists(&default_account_id) { @@ -246,6 +257,9 @@ impl Pallet { !EvmToNative::::contains_key(&evm_address), Error::::AlreadyMapped ); + + Self::charge_storage_fee(&account_id)?; + // create double mappings for the pair with default evm address EvmToNative::::insert(&evm_address, &account_id); NativeToEvm::::insert(&account_id, &evm_address); @@ -256,6 +270,11 @@ impl Pallet { }); Ok(evm_address) } + + /// Charge the (exact) storage fee (polietly) from the user and burn it + fn charge_storage_fee(who: &T::AccountId) -> Result, DispatchError> { + T::Currency::burn_from(who, T::AccountMappingStorageFee::get(), Exact, Polite) + } } /// EIP-712 compatible signature scheme for verifying ownership of EVM Address @@ -375,7 +394,7 @@ impl AddressMapping for Pallet { pub struct KillAccountMapping(PhantomData); impl OnKilledAccount for KillAccountMapping { fn on_killed_account(who: &T::AccountId) { - // remove mapping created by `claim_account` or `get_or_create_evm_address` + // remove mapping of account reaped and burn the deposit if let Some(evm_addr) = NativeToEvm::::take(who) { EvmToNative::::remove(evm_addr); NativeToEvm::::remove(who); diff --git a/pallets/unified-accounts/src/mock.rs b/pallets/unified-accounts/src/mock.rs index 1b0f4a4851..74e60d54b4 100644 --- a/pallets/unified-accounts/src/mock.rs +++ b/pallets/unified-accounts/src/mock.rs @@ -149,7 +149,7 @@ impl pallet_ethereum::Config for TestRuntime { } parameter_types! { - pub TxWeightLimit: Weight = Weight::from_parts(u64::max_value(), 0); + pub const AccountMappingStorageFee: u128 = 100_000_000; } impl pallet_unified_accounts::Config for TestRuntime { @@ -158,6 +158,7 @@ impl pallet_unified_accounts::Config for TestRuntime { type DefaultEvmToNative = HashedAddressMapping; type DefaultNativeToEvm = HashedAccountMapping; type ChainId = ChainId; + type AccountMappingStorageFee = AccountMappingStorageFee; type WeightInfo = (); } diff --git a/pallets/unified-accounts/src/tests.rs b/pallets/unified-accounts/src/tests.rs index be8f9cbe34..d625c1d78e 100644 --- a/pallets/unified-accounts/src/tests.rs +++ b/pallets/unified-accounts/src/tests.rs @@ -144,7 +144,8 @@ fn account_claim_should_work() { ExtBuilder::default().build().execute_with(|| { let alice_eth = UnifiedAccounts::eth_address(&alice_secret()); // default ss58 account associated with eth address - let alice_eth_old_account = ::DefaultEvmToNative::into_account_id(alice_eth.clone()); + let alice_eth_old_account = + ::DefaultEvmToNative::into_account_id(alice_eth.clone()); let signature = get_evm_signature(&ALICE, &alice_secret()); // transfer some funds to alice_eth (H160) @@ -165,21 +166,28 @@ fn account_claim_should_work() { // old account (alice_eth_old_account) assert!(System::events().iter().any(|r| matches!( &r.event, - RuntimeEvent::System(frame_system::Event::KilledAccount { account }) if account == &alice_eth_old_account + RuntimeEvent::System(frame_system::Event::KilledAccount { account }) + if account == &alice_eth_old_account + ))); + + // check if storage fee is charged and burned + assert!(System::events().iter().any(|r| matches!( + &r.event, + RuntimeEvent::Balances(pallet_balances::Event::Burned { who, amount }) + if who == &ALICE && amount == &AccountMappingStorageFee::get() ))); // check for claim account event - System::assert_last_event( - RuntimeEvent::UnifiedAccounts(crate::Event::AccountClaimed { account_id: ALICE.clone(), evm_address: alice_eth.clone()}) - ); + System::assert_last_event(RuntimeEvent::UnifiedAccounts( + crate::Event::AccountClaimed { + account_id: ALICE.clone(), + evm_address: alice_eth.clone(), + }, + )); // make sure mappings are in place - assert_eq!( - EvmToNative::::get(alice_eth).unwrap(), ALICE - ); - assert_eq!( - NativeToEvm::::get(ALICE).unwrap(), alice_eth - ) + assert_eq!(EvmToNative::::get(alice_eth).unwrap(), ALICE); + assert_eq!(NativeToEvm::::get(ALICE).unwrap(), alice_eth) }); } @@ -200,6 +208,13 @@ fn account_default_claim_works() { }, )); + // check if storage fee is charged and burned + assert!(System::events().iter().any(|r| matches!( + &r.event, + RuntimeEvent::Balances(pallet_balances::Event::Burned { who, amount }) + if who == &ALICE && amount == &AccountMappingStorageFee::get() + ))); + // check UnifiedAddressMapper's mapping works assert_eq!( >::to_h160(&ALICE), diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index f89fa911ba..16aa3b0209 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -1203,12 +1203,18 @@ impl pallet_xc_asset_config::Config for Runtime { type WeightInfo = pallet_xc_asset_config::weights::SubstrateWeight; } +parameter_types! { + /// + pub const AccountMappingStorageFee: u128 = deposit(2, 84); +} + impl pallet_unified_accounts::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; type DefaultEvmToNative = pallet_evm::HashedAddressMapping; type DefaultNativeToEvm = HashedAccountMapping; type ChainId = EVMChainId; + type AccountMappingStorageFee = AccountMappingStorageFee; type WeightInfo = pallet_unified_accounts::weights::SubstrateWeight; } diff --git a/tests/integration/src/xvm.rs b/tests/integration/src/xvm.rs index bf179f5ab5..335165ce9a 100644 --- a/tests/integration/src/xvm.rs +++ b/tests/integration/src/xvm.rs @@ -331,6 +331,14 @@ fn calling_evm_payable_from_wasm_works() { let value = UNIT; + // fund the wasm contract addres for paying storage fees for + // AU mappings. + assert_ok!(Balances::transfer_allow_death( + RuntimeOrigin::signed(ALICE), + wasm_caller_addr.clone().into(), + AccountMappingStorageFee::get() + )); + // claim the default mappings for wasm contract claim_default_accounts(wasm_caller_addr.clone()); From 47b75efcbd99a01026178437a841c6dbb73bdf5c Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Sun, 15 Oct 2023 22:48:46 +0530 Subject: [PATCH 02/15] feat: add unified account chain extension --- Cargo.lock | 33 ++++++++ Cargo.toml | 3 + .../types/unified-accounts/Cargo.toml | 27 ++++++ .../types/unified-accounts/src/lib.rs | 35 ++++++++ chain-extensions/unified-accounts/Cargo.toml | 44 ++++++++++ chain-extensions/unified-accounts/src/lib.rs | 84 +++++++++++++++++++ runtime/local/Cargo.toml | 2 + runtime/local/src/chain_extensions.rs | 7 +- runtime/local/src/lib.rs | 6 ++ runtime/shibuya/Cargo.toml | 2 + runtime/shibuya/src/chain_extensions.rs | 7 +- runtime/shibuya/src/lib.rs | 1 - 12 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 chain-extensions/types/unified-accounts/Cargo.toml create mode 100644 chain-extensions/types/unified-accounts/src/lib.rs create mode 100644 chain-extensions/unified-accounts/Cargo.toml create mode 100644 chain-extensions/unified-accounts/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 2f81d10f4c..b1b0802922 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5994,6 +5994,7 @@ dependencies = [ "pallet-block-reward", "pallet-chain-extension-assets", "pallet-chain-extension-dapps-staking", + "pallet-chain-extension-unified-accounts", "pallet-chain-extension-xvm", "pallet-collective", "pallet-contracts", @@ -7461,6 +7462,26 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-chain-extension-unified-accounts" +version = "0.1.0" +dependencies = [ + "astar-primitives", + "frame-support", + "frame-system", + "log", + "num-traits", + "pallet-contracts", + "pallet-contracts-primitives", + "pallet-unified-accounts", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-runtime", + "sp-std", + "unified-accounts-chain-extension-types", +] + [[package]] name = "pallet-chain-extension-xvm" version = "0.1.1" @@ -13069,6 +13090,7 @@ dependencies = [ "pallet-block-reward", "pallet-chain-extension-assets", "pallet-chain-extension-dapps-staking", + "pallet-chain-extension-unified-accounts", "pallet-chain-extension-xvm", "pallet-collator-selection", "pallet-collective", @@ -15247,6 +15269,17 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "unified-accounts-chain-extension-types" +version = "0.1.0" +dependencies = [ + "num_enum 0.5.11", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-runtime", +] + [[package]] name = "universal-hash" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index c5828cc529..6536eb17f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [ "chain-extensions/dapps-staking", "chain-extensions/pallet-assets", "chain-extensions/xvm", + "chain-extensions/unified-accounts", "chain-extensions/types/*", "vendor/evm-tracing", @@ -294,10 +295,12 @@ pallet-evm-precompile-batch = { path = "./precompiles/batch", default-features = pallet-chain-extension-dapps-staking = { path = "./chain-extensions/dapps-staking", default-features = false } pallet-chain-extension-xvm = { path = "./chain-extensions/xvm", default-features = false } pallet-chain-extension-assets = { path = "./chain-extensions/pallet-assets", default-features = false } +pallet-chain-extension-unified-accounts = { path = "./chain-extensions/unified-accounts", default-features = false } dapps-staking-chain-extension-types = { path = "./chain-extensions/types/dapps-staking", default-features = false } xvm-chain-extension-types = { path = "./chain-extensions/types/xvm", default-features = false } assets-chain-extension-types = { path = "./chain-extensions/types/assets", default-features = false } +unified-accounts-chain-extension-types = { path = "./chain-extensions/types/unified-accounts", default-features = false } precompile-utils = { path = "./precompiles/utils", default-features = false } diff --git a/chain-extensions/types/unified-accounts/Cargo.toml b/chain-extensions/types/unified-accounts/Cargo.toml new file mode 100644 index 0000000000..340f7195ee --- /dev/null +++ b/chain-extensions/types/unified-accounts/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "unified-accounts-chain-extension-types" +version = "0.1.0" +description = "Types definitions for contracts using Unified Accounts chain-extension." +authors.workspace = true +edition.workspace = true +homepage.workspace = true +repository.workspace = true + +[dependencies] +num_enum = { workspace = true } +parity-scale-codec = { workspace = true } +scale-info = { workspace = true } + +#substarte +sp-core = { workspace = true } +sp-runtime = { workspace = true } + +[features] +default = ["std"] +std = [ + "num_enum/std", + "parity-scale-codec/std", + "scale-info/std", + "sp-core/std", + "sp-runtime/std", +] diff --git a/chain-extensions/types/unified-accounts/src/lib.rs b/chain-extensions/types/unified-accounts/src/lib.rs new file mode 100644 index 0000000000..a629a53708 --- /dev/null +++ b/chain-extensions/types/unified-accounts/src/lib.rs @@ -0,0 +1,35 @@ +// This file is part of Astar. + +// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later + +// Astar is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Astar is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Astar. If not, see . + +#![cfg_attr(not(feature = "std"), no_std)] + +use num_enum::{IntoPrimitive, TryFromPrimitive}; +use parity_scale_codec::{Decode, Encode}; + +#[repr(u16)] +#[derive(TryFromPrimitive, IntoPrimitive, Decode, Encode)] +pub enum Command { + /// Get the mapped Evm address if any + GetEvmAddress = 0, + /// Get the mapped Evm address if any otheriwse default associated Evm address + GetEvmAddressOrDefault = 1, + /// Get the mapped Native address if any + GetNativeAddress = 2, + /// Get the mapped Native address if any otheriwse default associated Native address + GetNativeAddressOrDefault = 3, +} diff --git a/chain-extensions/unified-accounts/Cargo.toml b/chain-extensions/unified-accounts/Cargo.toml new file mode 100644 index 0000000000..f2ae7ff9e7 --- /dev/null +++ b/chain-extensions/unified-accounts/Cargo.toml @@ -0,0 +1,44 @@ +[package] +name = "pallet-chain-extension-unified-accounts" +version = "0.1.0" +description = "Chain extension for AU" +authors.workspace = true +edition.workspace = true +homepage.workspace = true +repository.workspace = true + +[dependencies] +frame-support = { workspace = true } +frame-system = { workspace = true } +log = { workspace = true } +num-traits = { workspace = true } +pallet-contracts = { workspace = true } +pallet-contracts-primitives = { workspace = true } +parity-scale-codec = { workspace = true } +scale-info = { workspace = true } +sp-core = { workspace = true } +sp-runtime = { workspace = true } +sp-std = { workspace = true } + +# Astar +astar-primitives = { workspace = true } +pallet-unified-accounts = { workspace = true } +unified-accounts-chain-extension-types = { workspace = true } + +[features] +default = ["std"] +std = [ + "parity-scale-codec/std", + "frame-support/std", + "frame-system/std", + "num-traits/std", + "pallet-contracts/std", + "pallet-contracts-primitives/std", + "scale-info/std", + "sp-std/std", + "sp-core/std", + "sp-runtime/std", + # Astar + "astar-primitives/std", + "pallet-unified-accounts/std", +] diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs new file mode 100644 index 0000000000..4e79cfd179 --- /dev/null +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -0,0 +1,84 @@ +// This file is part of Astar. + +// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later + +// Astar is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Astar is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Astar. If not, see . + +#![cfg_attr(not(feature = "std"), no_std)] + +use astar_primitives::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 parity_scale_codec::Encode; +pub use unified_accounts_chain_extension_types::Command::{self, *}; + +#[derive(DefaultNoBound)] +pub struct UnifiedAccountsExtension(PhantomData<(T, UA)>); + +impl ChainExtension for UnifiedAccountsExtension +where + T: pallet_contracts::Config + pallet_unified_accounts::Config, + UA: UnifiedAddressMapper, +{ + fn call(&mut self, env: Environment) -> DispatchResult + where + E: Ext, + { + let mut env = env.buf_in_buf_out(); + match env.func_id().try_into().map_err(|_| { + DispatchError::Other("Unsupported func id in Unified Accounts Chain Extension") + })? { + GetEvmAddress => { + let account_id: T::AccountId = env.read_as()?; + + let base_weight = ::DbWeight::get().reads(1); + env.charge_weight(base_weight)?; + // write to buffer + UA::to_h160(&account_id).using_encoded(|r| env.write(r, false, None))?; + } + GetEvmAddressOrDefault => { + let account_id: T::AccountId = env.read_as()?; + + let base_weight = ::DbWeight::get().reads(1); + env.charge_weight(base_weight)?; + // write to buffer + UA::to_h160_or_default(&account_id).using_encoded(|r| env.write(r, false, None))?; + } + GetNativeAddress => { + let evm_address: EvmAddress = env.read_as()?; + + let base_weight = ::DbWeight::get().reads(1); + env.charge_weight(base_weight)?; + // write to buffer + UA::to_account_id(&evm_address).using_encoded(|r| env.write(r, false, None))?; + } + GetNativeAddressOrDefault => { + let evm_address: EvmAddress = env.read_as()?; + + let base_weight = ::DbWeight::get().reads(1); + env.charge_weight(base_weight)?; + // write to buffer + UA::to_account_id_or_default(&evm_address) + .using_encoded(|r| env.write(r, false, None))?; + } + }; + Ok(RetVal::Converging(0)) + } +} diff --git a/runtime/local/Cargo.toml b/runtime/local/Cargo.toml index 2dc340bcc6..df5e1a3a7e 100644 --- a/runtime/local/Cargo.toml +++ b/runtime/local/Cargo.toml @@ -68,6 +68,7 @@ pallet-transaction-payment-rpc-runtime-api = { workspace = true } astar-primitives = { workspace = true } pallet-block-reward = { workspace = true } pallet-chain-extension-dapps-staking = { workspace = true } +pallet-chain-extension-unified-accounts = { workspace = true } pallet-chain-extension-xvm = { workspace = true } pallet-dapps-staking = { workspace = true } pallet-dynamic-evm-base-fee = { workspace = true } @@ -116,6 +117,7 @@ std = [ "pallet-contracts-primitives/std", "pallet-chain-extension-dapps-staking/std", "pallet-chain-extension-xvm/std", + "pallet-chain-extension-unified-accounts/std", "pallet-dapps-staking/std", "pallet-dynamic-evm-base-fee/std", "pallet-ethereum/std", diff --git a/runtime/local/src/chain_extensions.rs b/runtime/local/src/chain_extensions.rs index 9bd7609c5f..917baff1e2 100644 --- a/runtime/local/src/chain_extensions.rs +++ b/runtime/local/src/chain_extensions.rs @@ -16,13 +16,14 @@ // You should have received a copy of the GNU General Public License // along with Astar. If not, see . -use super::{Runtime, Xvm}; +use super::{Runtime, UnifiedAccounts, Xvm}; /// Registered WASM contracts chain extensions. pub use pallet_chain_extension_assets::AssetsExtension; use pallet_contracts::chain_extension::RegisteredChainExtension; pub use pallet_chain_extension_dapps_staking::DappsStakingExtension; +pub use pallet_chain_extension_unified_accounts::UnifiedAccountsExtension; pub use pallet_chain_extension_xvm::XvmExtension; // Following impls defines chain extension IDs. @@ -40,3 +41,7 @@ impl RegisteredChainExten { const ID: u16 = 02; } + +impl RegisteredChainExtension for UnifiedAccountsExtension { + const ID: u16 = 03; +} diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index b372b6bdbf..37020c8644 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -507,12 +507,18 @@ impl pallet_utility::Config for Runtime { type WeightInfo = pallet_utility::weights::SubstrateWeight; } +parameter_types! { + pub const AccountMappingStorageFee: u128 = deposit(2, 84); +} + impl pallet_unified_accounts::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; type DefaultEvmToNative = pallet_evm::HashedAddressMapping; type DefaultNativeToEvm = HashedAccountMapping; + type AccountMappingStorageFee = AccountMappingStorageFee; type ChainId = ChainId; + type WeightInfo = pallet_unified_accounts::weights::SubstrateWeight; } diff --git a/runtime/shibuya/Cargo.toml b/runtime/shibuya/Cargo.toml index 86fbdc5a57..5330385f16 100644 --- a/runtime/shibuya/Cargo.toml +++ b/runtime/shibuya/Cargo.toml @@ -97,6 +97,7 @@ orml-xtokens = { workspace = true } astar-primitives = { workspace = true } pallet-block-reward = { workspace = true } pallet-chain-extension-dapps-staking = { workspace = true } +pallet-chain-extension-unified-accounts = { workspace = true } pallet-chain-extension-xvm = { workspace = true } pallet-collator-selection = { workspace = true } pallet-dapps-staking = { workspace = true } @@ -166,6 +167,7 @@ std = [ "pallet-contracts-primitives/std", "pallet-chain-extension-dapps-staking/std", "pallet-chain-extension-xvm/std", + "pallet-chain-extension-unified-accounts/std", "pallet-dynamic-evm-base-fee/std", "pallet-ethereum/std", "pallet-preimage/std", diff --git a/runtime/shibuya/src/chain_extensions.rs b/runtime/shibuya/src/chain_extensions.rs index 9bd7609c5f..917baff1e2 100644 --- a/runtime/shibuya/src/chain_extensions.rs +++ b/runtime/shibuya/src/chain_extensions.rs @@ -16,13 +16,14 @@ // You should have received a copy of the GNU General Public License // along with Astar. If not, see . -use super::{Runtime, Xvm}; +use super::{Runtime, UnifiedAccounts, Xvm}; /// Registered WASM contracts chain extensions. pub use pallet_chain_extension_assets::AssetsExtension; use pallet_contracts::chain_extension::RegisteredChainExtension; pub use pallet_chain_extension_dapps_staking::DappsStakingExtension; +pub use pallet_chain_extension_unified_accounts::UnifiedAccountsExtension; pub use pallet_chain_extension_xvm::XvmExtension; // Following impls defines chain extension IDs. @@ -40,3 +41,7 @@ impl RegisteredChainExten { const ID: u16 = 02; } + +impl RegisteredChainExtension for UnifiedAccountsExtension { + const ID: u16 = 03; +} diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index 16aa3b0209..2cc06c9c6f 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -1204,7 +1204,6 @@ impl pallet_xc_asset_config::Config for Runtime { } parameter_types! { - /// pub const AccountMappingStorageFee: u128 = deposit(2, 84); } From 283f11662565dae727d2dd447175206cf4fb88a5 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 16 Oct 2023 15:39:15 +0530 Subject: [PATCH 03/15] feat: add AU CE in Shibuya --- runtime/local/src/lib.rs | 1 + runtime/shibuya/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index 37020c8644..6568ae9817 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -852,6 +852,7 @@ impl pallet_contracts::Config for Runtime { DappsStakingExtension, XvmExtension, AssetsExtension>, + UnifiedAccountsExtension, ); type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index 2cc06c9c6f..98ae1030ee 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -668,6 +668,7 @@ impl pallet_contracts::Config for Runtime { DappsStakingExtension, XvmExtension, AssetsExtension>, + UnifiedAccountsExtension, ); type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; From cb89df448eacdb5d60642ca9057db436f461c6c3 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 16 Oct 2023 15:39:40 +0530 Subject: [PATCH 04/15] feat: add AU CE integration tests --- Cargo.lock | 15 + Cargo.toml | 2 + .../{integration => }/ink-contracts/README.md | 0 .../async-xcm-call-no-ce.json | 0 .../async-xcm-call-no-ce.wasm | Bin tests/ink-contracts/au_ce_getters.json | 641 ++++++++++++++++++ tests/ink-contracts/au_ce_getters.wasm | Bin 0 -> 19287 bytes .../ink-contracts/call_xvm_payable.json | 0 .../ink-contracts/call_xvm_payable.wasm | Bin .../ink-contracts/dummy_error.json | 0 .../ink-contracts/dummy_error.wasm | Bin .../fixtures => ink-contracts}/flipper.json | 0 .../fixtures => ink-contracts}/flipper.wasm | Bin .../ink-contracts/payable.json | 0 .../ink-contracts/payable.wasm | Bin .../ink-contracts/simple_storage.json | 0 .../ink-contracts/simple_storage.wasm | Bin tests/integration/Cargo.toml | 3 +- tests/integration/src/account.rs | 41 -- tests/integration/src/lib.rs | 4 +- tests/integration/src/setup.rs | 32 +- tests/integration/src/unified_accounts.rs | 112 +++ tests/integration/src/xvm.rs | 2 +- tests/utils/Cargo.toml | 29 + tests/utils/src/lib.rs | 117 ++++ tests/xcm-simulator/Cargo.toml | 1 + tests/xcm-simulator/fixtures/README.md | 7 - tests/xcm-simulator/src/mocks/mod.rs | 100 +-- tests/xcm-simulator/src/tests/experimental.rs | 13 +- 29 files changed, 949 insertions(+), 170 deletions(-) rename tests/{integration => }/ink-contracts/README.md (100%) rename tests/{xcm-simulator/fixtures => ink-contracts}/async-xcm-call-no-ce.json (100%) rename tests/{xcm-simulator/fixtures => ink-contracts}/async-xcm-call-no-ce.wasm (100%) create mode 100644 tests/ink-contracts/au_ce_getters.json create mode 100644 tests/ink-contracts/au_ce_getters.wasm rename tests/{integration => }/ink-contracts/call_xvm_payable.json (100%) rename tests/{integration => }/ink-contracts/call_xvm_payable.wasm (100%) rename tests/{integration => }/ink-contracts/dummy_error.json (100%) rename tests/{integration => }/ink-contracts/dummy_error.wasm (100%) rename tests/{xcm-simulator/fixtures => ink-contracts}/flipper.json (100%) rename tests/{xcm-simulator/fixtures => ink-contracts}/flipper.wasm (100%) rename tests/{integration => }/ink-contracts/payable.json (100%) rename tests/{integration => }/ink-contracts/payable.wasm (100%) rename tests/{integration => }/ink-contracts/simple_storage.json (100%) rename tests/{integration => }/ink-contracts/simple_storage.wasm (100%) delete mode 100644 tests/integration/src/account.rs create mode 100644 tests/integration/src/unified_accounts.rs create mode 100644 tests/utils/Cargo.toml create mode 100644 tests/utils/src/lib.rs delete mode 100644 tests/xcm-simulator/fixtures/README.md diff --git a/Cargo.lock b/Cargo.lock index b1b0802922..6e5e9db1f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -636,6 +636,19 @@ dependencies = [ "xcm-executor", ] +[[package]] +name = "astar-test-utils" +version = "0.1.0" +dependencies = [ + "frame-support", + "frame-system", + "pallet-contracts", + "pallet-contracts-primitives", + "parity-scale-codec", + "scale-info", + "sp-runtime", +] + [[package]] name = "async-channel" version = "1.9.0" @@ -4923,6 +4936,7 @@ version = "0.1.0" dependencies = [ "astar-primitives", "astar-runtime", + "astar-test-utils", "env_logger 0.10.0", "fp-evm", "frame-support", @@ -16641,6 +16655,7 @@ name = "xcm-simulator-tests" version = "0.1.0" dependencies = [ "astar-primitives", + "astar-test-utils", "cumulus-pallet-xcm", "frame-support", "frame-system", diff --git a/Cargo.toml b/Cargo.toml index 6536eb17f9..6a2c0a31e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "runtime/shibuya", "tests/xcm-simulator", "tests/integration", + "tests/utils", "pallets/*", "precompiles/*", @@ -283,6 +284,7 @@ pallet-dynamic-evm-base-fee = { path = "./pallets/dynamic-evm-base-fee", default pallet-unified-accounts = { path = "./pallets/unified-accounts", default-features = false } astar-primitives = { path = "./primitives", default-features = false } +astar-test-utils = { path = "./tests/utils", default-features = false } pallet-evm-precompile-assets-erc20 = { path = "./precompiles/assets-erc20", default-features = false } pallet-evm-precompile-sr25519 = { path = "./precompiles/sr25519", default-features = false } diff --git a/tests/integration/ink-contracts/README.md b/tests/ink-contracts/README.md similarity index 100% rename from tests/integration/ink-contracts/README.md rename to tests/ink-contracts/README.md diff --git a/tests/xcm-simulator/fixtures/async-xcm-call-no-ce.json b/tests/ink-contracts/async-xcm-call-no-ce.json similarity index 100% rename from tests/xcm-simulator/fixtures/async-xcm-call-no-ce.json rename to tests/ink-contracts/async-xcm-call-no-ce.json diff --git a/tests/xcm-simulator/fixtures/async-xcm-call-no-ce.wasm b/tests/ink-contracts/async-xcm-call-no-ce.wasm similarity index 100% rename from tests/xcm-simulator/fixtures/async-xcm-call-no-ce.wasm rename to tests/ink-contracts/async-xcm-call-no-ce.wasm diff --git a/tests/ink-contracts/au_ce_getters.json b/tests/ink-contracts/au_ce_getters.json new file mode 100644 index 0000000000..abffbdfe7e --- /dev/null +++ b/tests/ink-contracts/au_ce_getters.json @@ -0,0 +1,641 @@ +{ + "source": { + "hash": "0x5eb72ad6cdb6869bb8419ae07ff533bca787346360e58713c679f14679e188f7", + "language": "ink! 4.3.0", + "compiler": "rustc 1.72.0", + "build_info": { + "build_mode": "Debug", + "cargo_contract_version": "3.2.0", + "rust_toolchain": "stable-aarch64-apple-darwin", + "wasm_opt_settings": { + "keep_debug_symbols": true, + "optimization_passes": "Z" + } + } + }, + "contract": { + "name": "au-ce-getters", + "version": "0.1.0", + "authors": [ + "Stake Technologies " + ], + "repository": "https://github.com/AstarNetwork/ink-test-contracts", + "homepage": "https://astar.network/" + }, + "spec": { + "constructors": [ + { + "args": [], + "default": false, + "docs": [], + "label": "new", + "payable": false, + "returnType": { + "displayName": [ + "ink_primitives", + "ConstructorResult" + ], + "type": 0 + }, + "selector": "0x9bae9d5e" + } + ], + "docs": [], + "environment": { + "accountId": { + "displayName": [ + "AccountId" + ], + "type": 3 + }, + "balance": { + "displayName": [ + "Balance" + ], + "type": 14 + }, + "blockNumber": { + "displayName": [ + "BlockNumber" + ], + "type": 17 + }, + "chainExtension": { + "displayName": [ + "ChainExtension" + ], + "type": 18 + }, + "hash": { + "displayName": [ + "Hash" + ], + "type": 15 + }, + "maxEventTopics": 4, + "timestamp": { + "displayName": [ + "Timestamp" + ], + "type": 16 + } + }, + "events": [], + "lang_error": { + "displayName": [ + "ink", + "LangError" + ], + "type": 2 + }, + "messages": [ + { + "args": [ + { + "label": "account_id", + "type": { + "displayName": [ + "AccountId" + ], + "type": 3 + } + } + ], + "default": false, + "docs": [], + "label": "to_h160", + "mutates": false, + "payable": true, + "returnType": { + "displayName": [ + "ink", + "MessageResult" + ], + "type": 6 + }, + "selector": "0x0000002a" + }, + { + "args": [ + { + "label": "account_id", + "type": { + "displayName": [ + "AccountId" + ], + "type": 3 + } + } + ], + "default": false, + "docs": [], + "label": "to_h160_or_default", + "mutates": false, + "payable": true, + "returnType": { + "displayName": [ + "ink", + "MessageResult" + ], + "type": 10 + }, + "selector": "0x0000002b" + }, + { + "args": [ + { + "label": "evm_address", + "type": { + "displayName": [ + "H160" + ], + "type": 8 + } + } + ], + "default": false, + "docs": [], + "label": "to_account_id", + "mutates": false, + "payable": true, + "returnType": { + "displayName": [ + "ink", + "MessageResult" + ], + "type": 11 + }, + "selector": "0x0000002c" + }, + { + "args": [ + { + "label": "evm_address", + "type": { + "displayName": [ + "H160" + ], + "type": 8 + } + } + ], + "default": false, + "docs": [], + "label": "to_account_id_or_default", + "mutates": false, + "payable": true, + "returnType": { + "displayName": [ + "ink", + "MessageResult" + ], + "type": 13 + }, + "selector": "0x0000002d" + } + ] + }, + "storage": { + "root": { + "layout": { + "struct": { + "fields": [], + "name": "UAMappingGetter" + } + }, + "root_key": "0x00000000" + } + }, + "types": [ + { + "id": 0, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 1 + } + ], + "index": 0, + "name": "Ok" + }, + { + "fields": [ + { + "type": 2 + } + ], + "index": 1, + "name": "Err" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 1 + }, + { + "name": "E", + "type": 2 + } + ], + "path": [ + "Result" + ] + } + }, + { + "id": 1, + "type": { + "def": { + "tuple": [] + } + } + }, + { + "id": 2, + "type": { + "def": { + "variant": { + "variants": [ + { + "index": 1, + "name": "CouldNotReadInput" + } + ] + } + }, + "path": [ + "ink_primitives", + "LangError" + ] + } + }, + { + "id": 3, + "type": { + "def": { + "composite": { + "fields": [ + { + "type": 4, + "typeName": "[u8; 32]" + } + ] + } + }, + "path": [ + "ink_primitives", + "types", + "AccountId" + ] + } + }, + { + "id": 4, + "type": { + "def": { + "array": { + "len": 32, + "type": 5 + } + } + } + }, + { + "id": 5, + "type": { + "def": { + "primitive": "u8" + } + } + }, + { + "id": 6, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 7 + } + ], + "index": 0, + "name": "Ok" + }, + { + "fields": [ + { + "type": 2 + } + ], + "index": 1, + "name": "Err" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 7 + }, + { + "name": "E", + "type": 2 + } + ], + "path": [ + "Result" + ] + } + }, + { + "id": 7, + "type": { + "def": { + "variant": { + "variants": [ + { + "index": 0, + "name": "None" + }, + { + "fields": [ + { + "type": 8 + } + ], + "index": 1, + "name": "Some" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 8 + } + ], + "path": [ + "Option" + ] + } + }, + { + "id": 8, + "type": { + "def": { + "composite": { + "fields": [ + { + "type": 9, + "typeName": "[u8; 20]" + } + ] + } + }, + "path": [ + "primitive_types", + "H160" + ] + } + }, + { + "id": 9, + "type": { + "def": { + "array": { + "len": 20, + "type": 5 + } + } + } + }, + { + "id": 10, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 8 + } + ], + "index": 0, + "name": "Ok" + }, + { + "fields": [ + { + "type": 2 + } + ], + "index": 1, + "name": "Err" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 8 + }, + { + "name": "E", + "type": 2 + } + ], + "path": [ + "Result" + ] + } + }, + { + "id": 11, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 12 + } + ], + "index": 0, + "name": "Ok" + }, + { + "fields": [ + { + "type": 2 + } + ], + "index": 1, + "name": "Err" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 12 + }, + { + "name": "E", + "type": 2 + } + ], + "path": [ + "Result" + ] + } + }, + { + "id": 12, + "type": { + "def": { + "variant": { + "variants": [ + { + "index": 0, + "name": "None" + }, + { + "fields": [ + { + "type": 3 + } + ], + "index": 1, + "name": "Some" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 3 + } + ], + "path": [ + "Option" + ] + } + }, + { + "id": 13, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 3 + } + ], + "index": 0, + "name": "Ok" + }, + { + "fields": [ + { + "type": 2 + } + ], + "index": 1, + "name": "Err" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 3 + }, + { + "name": "E", + "type": 2 + } + ], + "path": [ + "Result" + ] + } + }, + { + "id": 14, + "type": { + "def": { + "primitive": "u128" + } + } + }, + { + "id": 15, + "type": { + "def": { + "composite": { + "fields": [ + { + "type": 4, + "typeName": "[u8; 32]" + } + ] + } + }, + "path": [ + "ink_primitives", + "types", + "Hash" + ] + } + }, + { + "id": 16, + "type": { + "def": { + "primitive": "u64" + } + } + }, + { + "id": 17, + "type": { + "def": { + "primitive": "u32" + } + } + }, + { + "id": 18, + "type": { + "def": { + "variant": {} + }, + "path": [ + "ink_env", + "types", + "NoChainExtension" + ] + } + } + ], + "version": "4" +} \ No newline at end of file diff --git a/tests/ink-contracts/au_ce_getters.wasm b/tests/ink-contracts/au_ce_getters.wasm new file mode 100644 index 0000000000000000000000000000000000000000..482e199b56008317a41583cab693937648026d79 GIT binary patch literal 19287 zcmch9e~cX0ecziOdq22$84x0kd0P{qy8YhkO@;>b8gyAH+Qnt z(ID*&yV+onb%d_&9BrqYo2%`M=~{0!du){ThHL$vIHYct)_U7JqhjiOCu{9oSl!Bo zLxL7^j#nNY+z!{$&DB9R+8OkOVPw7CsjY0QKe(c6x@AP6fuo`0pJ5nNGk~w_rIOI5 zHDQP;eY&L22r;b-F>6klvp5=L6ll@5HOrcs0Rxh2*Ua+tO}a_+#cflaY=d|UaqHO;(whe+&=s=gp>Jvp{sHO`30;JLM4HH-6^^=c_UR|lbA1yDV) zt=Xozf=%?3hKPh_i{#dI+mHi+!AE;~SJy*blx?$oh$9eUmCL`O8CP|nw5ZQiP0Rp1 z^FaVJ6!g*u=P?sNZ4-#v<_5te&wMop9_lkk%E_NSV-r&`Y;uYBgR6;>s?XjnfNyY?kJ^ph~pI|gl16Q!*9h4mXBg!D|pl)|^}AkSv_HD@izITZsNP^41Tb!(bR zfYsmbsplJCh$|8wHXzm`}2FRloLB9Ls0liDMZ5fjQM=FA&_ zhHILe+IJny)j zK|tg6^Dvlstx%X;8%t0aUDv>HEzvd(8-`hu%>R*CM~Wit1-eXBUwHzWN>rbxtv`wr zYUjUwLfQaE2Uq;~b2nP?6F53p4OaZMRD@%aZ&b7dI)QI{@50OS!d+ul+vaHj6v!1T z5}&~27!Ewq1{EVWKn)&78yioRxftaHv1o8`@D0fKSuxQmB6jhpAA%A8;XhjS6PX+uD+tNwiDzvy>l4vsL}D zu8A@Q2*mPbcnUcc!)U~NP37_mBFM_u4X(D zjKIjO*3*r+Pifl??l+^0H_`##lyxNo33khC)7Ty^dxa~R_w zDJ8^}jXlj-iHt?^o_Y!C->=lhRB9uX+AJ1obBad2f~c`yZ3uZ$U)iTP_W&pqhgu1* zN3F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yH-1c?}SRriwE^i&ac%j$jxdswW*v>JzbY1Mf!O8x!_iC5@=a8h7g6gzF4r*XgwU48h-r@ARARJw4t7 z0U++~^x7;@!?V2ydu^n+z5~*D_TmEp4$tqS=}eVcGF=7GZuW~ta=WR4P0*6aU3?I;L=3&_a81b+uGbE~_d4j@TE8}uz|f{#ah zKm|W_`ZOkQh&HpvC)aZ7(f}kzk}z>iV59sK-Ip44KjT%3NO) z6#s*w2_wxs6qixM^_&(`5ac(}$%4Ikn8ecOlyAJ72iVl8^mV1^#r#kKC>9@?ts1>>GHY&D8 z2*=*)G~f4@L8ZQrY@r)6xO+hwH1B!KpuBqAid~ckbtJhd62j@5HF0DW|7TU&MzP. - -use crate::setup::*; -pub use sp_io::hashing::keccak_256; - -#[test] -fn transfer_to_h160_via_lookup() { - new_test_ext().execute_with(|| { - let eth_address = H160::from_slice(&keccak_256(b"Alice")[0..20]); - - // make sure account is empty - assert!(EVM::is_account_empty(ð_address)); - - // tranfer to evm account - assert_ok!(Balances::transfer( - RuntimeOrigin::signed(ALICE), - MultiAddress::Address20(eth_address.clone().into()), - UNIT, - )); - - // evm account should have recieved the funds - let (account, _) = EVM::account_basic(ð_address); - assert_eq!(account.balance, (UNIT - ExistentialDeposit::get()).into()); - }); -} diff --git a/tests/integration/src/lib.rs b/tests/integration/src/lib.rs index d43b845417..012ee39d07 100644 --- a/tests/integration/src/lib.rs +++ b/tests/integration/src/lib.rs @@ -32,7 +32,7 @@ mod assets; #[cfg(feature = "shibuya")] mod xvm; -#[cfg(feature = "shibuya")] -mod account; #[cfg(any(feature = "shibuya", feature = "shiden", feature = "astar"))] mod dispatch_precompile_filter; +#[cfg(feature = "shibuya")] +mod unified_accounts; diff --git a/tests/integration/src/setup.rs b/tests/integration/src/setup.rs index d0de6fb599..05989ae199 100644 --- a/tests/integration/src/setup.rs +++ b/tests/integration/src/setup.rs @@ -35,6 +35,7 @@ pub use shibuya::*; #[cfg(feature = "shibuya")] mod shibuya { use super::*; + use parity_scale_codec::Decode; pub use shibuya_runtime::*; /// 1 SBY. @@ -79,32 +80,37 @@ mod shibuya { } } - /// Deploy a WASM contract with its name. (The code is in `resource/`.) + /// Deploy a WASM contract via ALICE as origin. (The code is in `../ink-contracts/`.) + /// Assumption: Contract constructor is called "new" and take no arguments pub fn deploy_wasm_contract(name: &str) -> AccountId32 { - let path = format!("ink-contracts/{}.wasm", name); - let code = std::fs::read(path).expect("invalid path"); - let instantiate_result = Contracts::bare_instantiate( + let (address, _) = astar_test_utils::deploy_wasm_contract::( + name, ALICE, 0, Weight::from_parts(10_000_000_000, 1024 * 1024), None, - pallet_contracts_primitives::Code::Upload(code), - // `new` constructor hex::decode("9bae9d5e").expect("invalid data hex"), - vec![], - pallet_contracts::DebugInfo::Skip, - pallet_contracts::CollectEvents::Skip, ); - let address = instantiate_result - .result - .expect("instantiation failed") - .account_id; // On instantiation, the contract got existential deposit. assert_eq!(Balances::free_balance(&address), ExistentialDeposit::get(),); address } + /// Call a wasm smart contract method with ALICE as origin + pub fn call_wasm_contract_method(contract_id: AccountId, data: Vec) -> V { + let (value, _, _) = astar_test_utils::call_wasm_contract_method::( + ALICE, + contract_id, + 0, + Weight::from_parts(10_000_000_000, 1024 * 1024), + None, + data, + true, + ); + value + } + /// Build the signature payload for given native account and eth private key fn get_evm_signature(who: &AccountId32, secret: &libsecp256k1::SecretKey) -> [u8; 65] { // sign the payload diff --git a/tests/integration/src/unified_accounts.rs b/tests/integration/src/unified_accounts.rs new file mode 100644 index 0000000000..5a4540889d --- /dev/null +++ b/tests/integration/src/unified_accounts.rs @@ -0,0 +1,112 @@ +// This file is part of Astar. + +// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later + +// Astar is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Astar is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Astar. If not, see . + +use crate::setup::*; +use parity_scale_codec::Encode; +pub use sp_io::hashing::keccak_256; + +const AU_CE_GETTER: &'static str = "au_ce_getters"; + +#[test] +fn transfer_to_h160_via_lookup() { + new_test_ext().execute_with(|| { + let eth_address = H160::from_slice(&keccak_256(b"Alice")[0..20]); + + // make sure account is empty + assert!(EVM::is_account_empty(ð_address)); + + // tranfer to evm account + assert_ok!(Balances::transfer( + RuntimeOrigin::signed(ALICE), + MultiAddress::Address20(eth_address.clone().into()), + UNIT, + )); + + // evm account should have recieved the funds + let (account, _) = EVM::account_basic(ð_address); + assert_eq!(account.balance, (UNIT - ExistentialDeposit::get()).into()); + }); +} + +#[test] +fn unified_accounts_chain_extension_works() { + const GET_H160: [u8; 4] = [0x00, 0x00, 0x00, 0x2a]; + const GET_H160_OR_DEFAULT: [u8; 4] = [0x00, 0x00, 0x00, 0x2b]; + const GET_NATIVE: [u8; 4] = [0x00, 0x00, 0x00, 0x2c]; + const GET_NATIVE_OR_DEFAULT: [u8; 4] = [0x00, 0x00, 0x00, 0x2d]; + + new_test_ext().execute_with(|| { + let contract_id = deploy_wasm_contract(AU_CE_GETTER); + + // mapped h160 address should None + assert_eq!( + call_wasm_contract_method::>( + contract_id.clone(), + [GET_H160.to_vec(), ALICE.encode()].concat() + ), + None + ); + // default h160 address should match + assert_eq!( + call_wasm_contract_method::( + contract_id.clone(), + [GET_H160_OR_DEFAULT.to_vec(), ALICE.encode()].concat() + ), + UnifiedAccounts::to_h160_or_default(&ALICE) + ); + // mapped native address should be None + assert_eq!( + call_wasm_contract_method::>( + contract_id.clone(), + [GET_NATIVE.to_vec(), alith().encode()].concat() + ), + None + ); + // default native address should match + assert_eq!( + call_wasm_contract_method::( + contract_id.clone(), + [GET_NATIVE_OR_DEFAULT.to_vec(), alith().encode()].concat() + ), + UnifiedAccounts::to_account_id_or_default(&alith()) + ); + + // + // Create account mappings + // + connect_accounts(&ALICE, &alith_secret_key()); + + // ALICE mapped h160 address should alith + assert_eq!( + call_wasm_contract_method::>( + contract_id.clone(), + [GET_H160.to_vec(), ALICE.encode()].concat() + ), + Some(alith()) + ); + + // alith mapped native address should ALICE + assert_eq!( + call_wasm_contract_method::>( + contract_id.clone(), + [GET_NATIVE.to_vec(), alith().encode()].concat() + ), + Some(ALICE) + ); + }); +} diff --git a/tests/integration/src/xvm.rs b/tests/integration/src/xvm.rs index 335165ce9a..f617bb266f 100644 --- a/tests/integration/src/xvm.rs +++ b/tests/integration/src/xvm.rs @@ -331,7 +331,7 @@ fn calling_evm_payable_from_wasm_works() { let value = UNIT; - // fund the wasm contract addres for paying storage fees for + // fund the wasm contract address for paying storage fees for // AU mappings. assert_ok!(Balances::transfer_allow_death( RuntimeOrigin::signed(ALICE), diff --git a/tests/utils/Cargo.toml b/tests/utils/Cargo.toml new file mode 100644 index 0000000000..0421e99221 --- /dev/null +++ b/tests/utils/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "astar-test-utils" +version = "0.1.0" +description = "Astar test utils" +authors.workspace = true +edition.workspace = true +homepage.workspace = true +repository.workspace = true + +[dependencies] +parity-scale-codec = { workspace = true } +scale-info = { workspace = true } + +# Base functionality +frame-support = { workspace = true } +frame-system = { workspace = true } +pallet-contracts = { workspace = true } +pallet-contracts-primitives = { workspace = true } +sp-runtime = { workspace = true } + +[features] +default = ["std"] +std = [ + "parity-scale-codec/std", + "frame-support/std", + "frame-system/std", + "pallet-contracts/std", + "pallet-contracts-primitives/std", +] diff --git a/tests/utils/src/lib.rs b/tests/utils/src/lib.rs new file mode 100644 index 0000000000..ad84cc9167 --- /dev/null +++ b/tests/utils/src/lib.rs @@ -0,0 +1,117 @@ +// This file is part of Astar. + +// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later + +// Astar is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Astar is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Astar. If not, see . + +use frame_support::{traits::Currency, weights::Weight}; +use pallet_contracts_primitives::{Code, ReturnFlags}; +use parity_scale_codec::Decode; +use sp_runtime::traits::Hash; + +type ContractBalanceOf = <::Currency as Currency< + ::AccountId, +>>::Balance; + +/// Load a given wasm module from wasm binary contents along +/// with it's hash. +/// +/// The fixture files are located under the `../ink-contracts/` directory. +pub fn load_wasm_module( + fixture_name: &str, +) -> std::io::Result<(Vec, ::Output)> +where + T: frame_system::Config, +{ + let fixture_path = ["../ink-contracts/", fixture_name, ".wasm"].concat(); + let wasm_binary = std::fs::read(fixture_path)?; + let code_hash = T::Hashing::hash(&wasm_binary); + Ok((wasm_binary, code_hash)) +} + +// Load and deploy the contract from wasm binary +/// and check for successful deploy +pub fn deploy_wasm_contract( + contract_name: &str, + origin: T::AccountId, + value: ContractBalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option>, + data: Vec, +) -> (T::AccountId, ::Output) { + let (code, hash) = load_wasm_module::(contract_name).unwrap(); + let outcome = pallet_contracts::Pallet::::bare_instantiate( + origin, + value, + gas_limit, + storage_deposit_limit, + Code::Upload(code), + data, + vec![], + pallet_contracts::DebugInfo::Skip, + pallet_contracts::CollectEvents::Skip, + ); + + // make sure it does not revert + let result = outcome.result.unwrap(); + assert!( + !result.result.did_revert(), + "deploy_contract: reverted - {:?}", + result + ); + (result.account_id, hash) +} + +/// Call the wasm contract method and returns the decoded return +/// values along with return flags and consumed weight +pub fn call_wasm_contract_method( + origin: T::AccountId, + dest: T::AccountId, + value: ContractBalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option>, + data: Vec, + debug: bool, +) -> (V, ReturnFlags, Weight) { + let outcome = pallet_contracts::Pallet::::bare_call( + origin, + dest, + value, + gas_limit, + storage_deposit_limit, + data, + pallet_contracts::DebugInfo::Skip, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if debug { + println!( + "Contract debug buffer - {:?}", + String::from_utf8(outcome.debug_message.clone()) + ); + println!("Contract outcome - {outcome:?}"); + } + + let res = outcome.result.unwrap(); + // check for revert + assert!(!res.did_revert(), "Contract reverted!"); + + let value = Result::::decode(&mut res.data.as_ref()) + .expect("decoding failed") + .expect("ink! lang error"); + + (value, res.flags, outcome.gas_consumed) +} diff --git a/tests/xcm-simulator/Cargo.toml b/tests/xcm-simulator/Cargo.toml index 3acbe7fbf2..0b784de7d4 100644 --- a/tests/xcm-simulator/Cargo.toml +++ b/tests/xcm-simulator/Cargo.toml @@ -32,6 +32,7 @@ sp-tracing = { workspace = true } # Custom Astar inclusions astar-primitives = { workspace = true } +astar-test-utils = { workspace = true } pallet-dapps-staking = { workspace = true } pallet-xc-asset-config = { workspace = true } diff --git a/tests/xcm-simulator/fixtures/README.md b/tests/xcm-simulator/fixtures/README.md deleted file mode 100644 index 5c71d97488..0000000000 --- a/tests/xcm-simulator/fixtures/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## fixtures -This directory contains contracts which are used in experimental scenarios. - -Files in this directory are used by experimental scenarios in `src/experimental.rs`. The json -files are for informational purposes only and are not consumed by the tests. - -The source code for the contracts can be found at https://github.com/AstarNetwork/ink-test-contracts diff --git a/tests/xcm-simulator/src/mocks/mod.rs b/tests/xcm-simulator/src/mocks/mod.rs index 211789d55d..e8ea59983c 100644 --- a/tests/xcm-simulator/src/mocks/mod.rs +++ b/tests/xcm-simulator/src/mocks/mod.rs @@ -21,19 +21,12 @@ pub(crate) mod parachain; pub(crate) mod relay_chain; use frame_support::traits::{Currency, IsType, OnFinalize, OnInitialize}; -use frame_support::weights::Weight; -use pallet_contracts_primitives::{Code, ReturnFlags}; -use parity_scale_codec::Decode; -use sp_runtime::traits::{Bounded, Hash, StaticLookup}; +use sp_runtime::traits::{Bounded, StaticLookup}; use sp_runtime::DispatchResult; use xcm::latest::prelude::*; use xcm_executor::traits::Convert; use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt}; -type ContractBalanceOf = <::Currency as Currency< - ::AccountId, ->>::Balance; - pub const ALICE: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0xFAu8; 32]); pub const BOB: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0xFBu8; 32]); pub const INITIAL_BALANCE: u128 = 1_000_000_000_000_000_000_000_000; @@ -260,94 +253,3 @@ where units_per_second.unwrap_or(1_000_000_000_000), ) } - -/// Load a given wasm module from wasm binary contents along -/// with it's hash. -/// -/// The fixture files are located under the `fixtures/` directory. -pub fn load_module( - fixture_name: &str, -) -> std::io::Result<(Vec, ::Output)> -where - T: frame_system::Config, -{ - let fixture_path = ["fixtures/", fixture_name, ".wasm"].concat(); - let wasm_binary = std::fs::read(fixture_path)?; - let code_hash = T::Hashing::hash(&wasm_binary); - Ok((wasm_binary, code_hash)) -} - -/// Load and deploy the contract from wasm binary -/// and check for successful deploy -pub fn deploy_contract( - contract_name: &str, - origin: T::AccountId, - value: ContractBalanceOf, - gas_limit: Weight, - storage_deposit_limit: Option>, - data: Vec, -) -> (T::AccountId, ::Output) { - let (code, hash) = load_module::(contract_name).unwrap(); - let outcome = pallet_contracts::Pallet::::bare_instantiate( - origin, - value, - gas_limit, - storage_deposit_limit, - Code::Upload(code), - data, - vec![], - pallet_contracts::DebugInfo::Skip, - pallet_contracts::CollectEvents::Skip, - ); - - // make sure it does not revert - let result = outcome.result.unwrap(); - assert!( - !result.result.did_revert(), - "deploy_contract: reverted - {:?}", - result - ); - (result.account_id, hash) -} - -/// Call the wasm contract method and returns the decoded return -/// values along with return flags and consumed weight -pub fn call_contract_method( - origin: T::AccountId, - dest: T::AccountId, - value: ContractBalanceOf, - gas_limit: Weight, - storage_deposit_limit: Option>, - data: Vec, - debug: bool, -) -> (V, ReturnFlags, Weight) { - let outcome = pallet_contracts::Pallet::::bare_call( - origin, - dest, - value, - gas_limit, - storage_deposit_limit, - data, - pallet_contracts::DebugInfo::Skip, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if debug { - println!( - "Contract debug buffer - {:?}", - String::from_utf8(outcome.debug_message.clone()) - ); - println!("Contract outcome - {outcome:?}"); - } - - let res = outcome.result.unwrap(); - // check for revert - assert!(!res.did_revert(), "Contract reverted!"); - - ( - V::decode(&mut res.data.as_ref()).unwrap(), - res.flags, - outcome.gas_consumed, - ) -} diff --git a/tests/xcm-simulator/src/tests/experimental.rs b/tests/xcm-simulator/src/tests/experimental.rs index 6665d17efc..c139315274 100644 --- a/tests/xcm-simulator/src/tests/experimental.rs +++ b/tests/xcm-simulator/src/tests/experimental.rs @@ -22,6 +22,7 @@ use crate::mocks::{ *, }; +use astar_test_utils::{call_wasm_contract_method, deploy_wasm_contract}; use frame_support::{assert_ok, weights::Weight}; use parity_scale_codec::Encode; use sp_runtime::traits::Bounded; @@ -171,7 +172,7 @@ fn xcm_remote_transact_contract() { // deploy and initialize flipper contract with `true` in ParaA let mut contract_id = [0u8; 32].into(); ParaA::execute_with(|| { - (contract_id, _) = deploy_contract::( + (contract_id, _) = deploy_wasm_contract::( "flipper", ALICE.into(), 0, @@ -182,7 +183,7 @@ fn xcm_remote_transact_contract() { ); // check for flip status - let (res, _, _) = call_contract_method::>( + let (res, _, _) = call_wasm_contract_method::>( ALICE.into(), contract_id.clone(), 0, @@ -228,7 +229,7 @@ fn xcm_remote_transact_contract() { // check for flip status, it should be false ParaA::execute_with(|| { - let (res, _, _) = call_contract_method::>( + let (res, _, _) = call_wasm_contract_method::>( ALICE.into(), contract_id.clone(), 0, @@ -273,7 +274,7 @@ fn test_async_xcm_contract_call_no_ce() { // let contract_id = ParaA::execute_with(|| { // deploy contract - let (contract_id, _) = deploy_contract::( + let (contract_id, _) = deploy_wasm_contract::( "async-xcm-call-no-ce", ALICE.into(), 0, @@ -309,7 +310,7 @@ fn test_async_xcm_contract_call_no_ce() { // ParaA::execute_with(|| { assert_eq!( - call_contract_method::>( + call_wasm_contract_method::>( ALICE.into(), contract_id.clone(), 0, @@ -359,7 +360,7 @@ fn test_async_xcm_contract_call_no_ce() { // Check for contract method called ParaA::execute_with(|| { assert_eq!( - call_contract_method::, ()>>( + call_wasm_contract_method::, ()>>( ALICE.into(), contract_id.clone(), 0, From dde42c150353ce2b8e488a173df7de24a058eb14 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 16 Oct 2023 15:39:53 +0530 Subject: [PATCH 05/15] fix: warnings --- tests/integration/src/dispatch_precompile_filter.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/src/dispatch_precompile_filter.rs b/tests/integration/src/dispatch_precompile_filter.rs index 1099d592f0..7992b39c0a 100644 --- a/tests/integration/src/dispatch_precompile_filter.rs +++ b/tests/integration/src/dispatch_precompile_filter.rs @@ -187,7 +187,6 @@ fn test_incorrect_dispatch_info_fails() { struct Filter; struct AccountId; enum RuntimeCall { - System, DappsStaking, } impl GetDispatchInfo for RuntimeCall { @@ -204,7 +203,6 @@ fn test_incorrect_dispatch_info_fails() { fn contains(t: &RuntimeCall) -> bool { match t { RuntimeCall::DappsStaking => true, - _ => false, } } } From a3787cdfca2b5ae8f9165d6f54962c26c97653eb Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 16 Oct 2023 18:32:08 +0530 Subject: [PATCH 06/15] fix: storage deposit values --- pallets/unified-accounts/src/lib.rs | 11 ++++++++--- runtime/local/src/lib.rs | 6 +++--- runtime/shibuya/src/lib.rs | 3 ++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pallets/unified-accounts/src/lib.rs b/pallets/unified-accounts/src/lib.rs index 93585f5f5b..3089418738 100644 --- a/pallets/unified-accounts/src/lib.rs +++ b/pallets/unified-accounts/src/lib.rs @@ -39,6 +39,10 @@ //! * `claim_default_evm_address`: Creates the double mapping with default evm address given that //! no prior mapping exists. //! +//! ## Storage Fee +//! User is also charged a storage fee [`AccountMappingStorageFee`](`crate::Config::AccountMappingStorageFee`) +//! before mappings are created to prevent storage abuse. +//! //! WARNINGS: //! * This pallet only handles transfer of native balance only, for the rest of native assets //! hold by evm address like XC20, DAppStaking unclaimed rewards, etc should be transferred @@ -120,8 +124,9 @@ pub mod pallet { /// EVM chain id #[pallet::constant] type ChainId: Get; - /// The amount of currency needed per mapping to be added. - /// TODO: calculate total bytes key + value in storage for mappings + /// The amount of currency needed for mappings to be added. + /// Two storage items with values sizes, sizeof(AccountId) and sizeof(H160) + /// respectively #[pallet::constant] type AccountMappingStorageFee: Get>; /// Weight information for the extrinsics in this module @@ -394,7 +399,7 @@ impl AddressMapping for Pallet { pub struct KillAccountMapping(PhantomData); impl OnKilledAccount for KillAccountMapping { fn on_killed_account(who: &T::AccountId) { - // remove mapping of account reaped and burn the deposit + // remove mappings of account reaped if let Some(evm_addr) = NativeToEvm::::take(who) { EvmToNative::::remove(evm_addr); NativeToEvm::::remove(who); diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index f67d1cce5d..2df0db9035 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -508,7 +508,8 @@ impl pallet_utility::Config for Runtime { } parameter_types! { - pub const AccountMappingStorageFee: u128 = deposit(2, 84); + // 2 storage items with value size 20 and 32 + pub const AccountMappingStorageFee: u128 = deposit(2, 32 + 20); } impl pallet_unified_accounts::Config for Runtime { @@ -516,9 +517,8 @@ impl pallet_unified_accounts::Config for Runtime { type Currency = Balances; type DefaultEvmToNative = pallet_evm::HashedAddressMapping; type DefaultNativeToEvm = HashedAccountMapping; - type AccountMappingStorageFee = AccountMappingStorageFee; type ChainId = ChainId; - + type AccountMappingStorageFee = AccountMappingStorageFee; type WeightInfo = pallet_unified_accounts::weights::SubstrateWeight; } diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index 1316031ef8..86b0048531 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -1205,7 +1205,8 @@ impl pallet_xc_asset_config::Config for Runtime { } parameter_types! { - pub const AccountMappingStorageFee: u128 = deposit(2, 84); + // 2 storage items with values 20 and 32 + pub const AccountMappingStorageFee: u128 = deposit(2, 32 + 20); } impl pallet_unified_accounts::Config for Runtime { From 8e6c1483ac5a49b97702fa2786139c5641b11e09 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 16 Oct 2023 18:33:14 +0530 Subject: [PATCH 07/15] fix: integration tests --- tests/integration/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/Cargo.toml b/tests/integration/Cargo.toml index 2232b2407b..8ba11a57e3 100644 --- a/tests/integration/Cargo.toml +++ b/tests/integration/Cargo.toml @@ -46,7 +46,7 @@ shibuya-runtime = { workspace = true, features = ["std"], optional = true } shiden-runtime = { workspace = true, features = ["std"], optional = true } [features] -default = ["std", "shibuya"] +default = ["std"] std = [] shibuya = ["shibuya-runtime"] shiden = ["shiden-runtime"] From c5b1f078d03bba5cd0bc51d8c349b833d73657d8 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 16 Oct 2023 21:08:44 +0530 Subject: [PATCH 08/15] fix: xcm simulator tests --- tests/xcm-simulator/src/tests/experimental.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/xcm-simulator/src/tests/experimental.rs b/tests/xcm-simulator/src/tests/experimental.rs index c139315274..bf0d706864 100644 --- a/tests/xcm-simulator/src/tests/experimental.rs +++ b/tests/xcm-simulator/src/tests/experimental.rs @@ -183,7 +183,7 @@ fn xcm_remote_transact_contract() { ); // check for flip status - let (res, _, _) = call_wasm_contract_method::>( + let (res, _, _) = call_wasm_contract_method::( ALICE.into(), contract_id.clone(), 0, @@ -192,7 +192,7 @@ fn xcm_remote_transact_contract() { SELECTOR_GET.to_vec(), true, ); - assert_eq!(res, Ok(true)); + assert_eq!(res, true); }); ParaB::execute_with(|| { @@ -229,7 +229,7 @@ fn xcm_remote_transact_contract() { // check for flip status, it should be false ParaA::execute_with(|| { - let (res, _, _) = call_wasm_contract_method::>( + let (res, _, _) = call_wasm_contract_method::( ALICE.into(), contract_id.clone(), 0, @@ -238,7 +238,7 @@ fn xcm_remote_transact_contract() { SELECTOR_GET.to_vec(), true, ); - assert_eq!(res, Ok(false)); + assert_eq!(res, false); }); } @@ -310,7 +310,7 @@ fn test_async_xcm_contract_call_no_ce() { // ParaA::execute_with(|| { assert_eq!( - call_wasm_contract_method::>( + call_wasm_contract_method::( ALICE.into(), contract_id.clone(), 0, @@ -343,7 +343,7 @@ fn test_async_xcm_contract_call_no_ce() { true, ) .0, - Ok(true) + true ); }); @@ -360,7 +360,7 @@ fn test_async_xcm_contract_call_no_ce() { // Check for contract method called ParaA::execute_with(|| { assert_eq!( - call_wasm_contract_method::, ()>>( + call_wasm_contract_method::>( ALICE.into(), contract_id.clone(), 0, @@ -370,7 +370,7 @@ fn test_async_xcm_contract_call_no_ce() { true, ) .0, - Ok(Some(true)) + Some(true) ); }); } From 6744d8465225031c10dc6f0e0141648b508dcfc1 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Thu, 19 Oct 2023 11:21:36 +0530 Subject: [PATCH 09/15] feat: apply code suggestions --- chain-extensions/unified-accounts/Cargo.toml | 1 + pallets/unified-accounts/src/lib.rs | 8 +++----- tests/integration/src/unified_accounts.rs | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/chain-extensions/unified-accounts/Cargo.toml b/chain-extensions/unified-accounts/Cargo.toml index f2ae7ff9e7..f347773274 100644 --- a/chain-extensions/unified-accounts/Cargo.toml +++ b/chain-extensions/unified-accounts/Cargo.toml @@ -41,4 +41,5 @@ std = [ # Astar "astar-primitives/std", "pallet-unified-accounts/std", + "unified-accounts-chain-extension-types/std" ] diff --git a/pallets/unified-accounts/src/lib.rs b/pallets/unified-accounts/src/lib.rs index 3089418738..f8f080560a 100644 --- a/pallets/unified-accounts/src/lib.rs +++ b/pallets/unified-accounts/src/lib.rs @@ -68,6 +68,7 @@ use astar_primitives::{ ethereum_checked::AccountMapping, evm::{EvmAddress, UnifiedAddressMapper}, + Balance, }; use frame_support::{ pallet_prelude::*, @@ -101,9 +102,6 @@ mod tests; /// ECDSA Signature type, with last bit for recovering address type EvmSignature = [u8; 65]; -type BalanceOf = - <::Currency as FungibleInspect<::AccountId>>::Balance; - #[frame_support::pallet] pub mod pallet { use super::*; @@ -128,7 +126,7 @@ pub mod pallet { /// Two storage items with values sizes, sizeof(AccountId) and sizeof(H160) /// respectively #[pallet::constant] - type AccountMappingStorageFee: Get>; + type AccountMappingStorageFee: Get; /// Weight information for the extrinsics in this module type WeightInfo: WeightInfo; } @@ -277,7 +275,7 @@ impl Pallet { } /// Charge the (exact) storage fee (polietly) from the user and burn it - fn charge_storage_fee(who: &T::AccountId) -> Result, DispatchError> { + fn charge_storage_fee(who: &T::AccountId) -> Result { T::Currency::burn_from(who, T::AccountMappingStorageFee::get(), Exact, Polite) } } diff --git a/tests/integration/src/unified_accounts.rs b/tests/integration/src/unified_accounts.rs index 5a4540889d..de89dbdc85 100644 --- a/tests/integration/src/unified_accounts.rs +++ b/tests/integration/src/unified_accounts.rs @@ -91,7 +91,7 @@ fn unified_accounts_chain_extension_works() { // connect_accounts(&ALICE, &alith_secret_key()); - // ALICE mapped h160 address should alith + // ALICE mapped h160 address should be alith assert_eq!( call_wasm_contract_method::>( contract_id.clone(), From 09b30d892fc126885e0ba7a9dc8bf3be223b07ec Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Thu, 19 Oct 2023 12:49:51 +0530 Subject: [PATCH 10/15] fix: benchmarks --- chain-extensions/unified-accounts/Cargo.toml | 2 +- pallets/unified-accounts/src/benchmarking.rs | 10 ++++++++++ pallets/unified-accounts/src/lib.rs | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/chain-extensions/unified-accounts/Cargo.toml b/chain-extensions/unified-accounts/Cargo.toml index f347773274..dcd680c486 100644 --- a/chain-extensions/unified-accounts/Cargo.toml +++ b/chain-extensions/unified-accounts/Cargo.toml @@ -41,5 +41,5 @@ std = [ # Astar "astar-primitives/std", "pallet-unified-accounts/std", - "unified-accounts-chain-extension-types/std" + "unified-accounts-chain-extension-types/std", ] diff --git a/pallets/unified-accounts/src/benchmarking.rs b/pallets/unified-accounts/src/benchmarking.rs index a4291e63c3..e1a88290e3 100644 --- a/pallets/unified-accounts/src/benchmarking.rs +++ b/pallets/unified-accounts/src/benchmarking.rs @@ -20,6 +20,7 @@ use super::*; use frame_benchmarking::v2::*; +use frame_support::assert_ok; use frame_system::RawOrigin; /// Assert that the last event equals the provided one. @@ -42,6 +43,10 @@ mod benchmarks { ) .into(); + assert_ok!(T::Currency::mint_into( + &caller, + T::AccountMappingStorageFee::get() + )); let caller_clone = caller.clone(); #[extrinsic_call] @@ -62,6 +67,11 @@ mod benchmarks { let caller_clone = caller.clone(); let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); + assert_ok!(T::Currency::mint_into( + &caller, + T::AccountMappingStorageFee::get() + )); + #[extrinsic_call] _(RawOrigin::Signed(caller)); diff --git a/pallets/unified-accounts/src/lib.rs b/pallets/unified-accounts/src/lib.rs index f8f080560a..cb804d2f3b 100644 --- a/pallets/unified-accounts/src/lib.rs +++ b/pallets/unified-accounts/src/lib.rs @@ -114,7 +114,7 @@ pub mod pallet { /// The overarching event type type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The Currency for managing evm address assets - type Currency: FungibleMutate; + type Currency: FungibleMutate; /// Default evm address to account id conversion type DefaultEvmToNative: AddressMapping; /// Default account id to evm address conversion From 66c6235dba8580c89a1fa8dece451b879f7dab1a Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Thu, 19 Oct 2023 13:12:25 +0530 Subject: [PATCH 11/15] feat: apply code suggestions --- tests/integration/src/setup.rs | 14 +++++++++----- tests/integration/src/unified_accounts.rs | 6 ++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/integration/src/setup.rs b/tests/integration/src/setup.rs index 3c09af82fa..7d59f3ac04 100644 --- a/tests/integration/src/setup.rs +++ b/tests/integration/src/setup.rs @@ -55,7 +55,7 @@ mod shibuya { ::AddressMapping::into_account_id(address) } - /// Deploy an EVM contract with code. + /// Deploy an EVM contract with code via ALICE as origin. pub fn deploy_evm_contract(code: &str) -> H160 { assert_ok!(EVM::create2( RuntimeOrigin::root(), @@ -97,16 +97,20 @@ mod shibuya { address } - /// Call a wasm smart contract method with ALICE as origin - pub fn call_wasm_contract_method(contract_id: AccountId, data: Vec) -> V { + /// Call a wasm smart contract method + pub fn call_wasm_contract_method( + origin: AccountId, + contract_id: AccountId, + data: Vec, + ) -> V { let (value, _, _) = astar_test_utils::call_wasm_contract_method::( - ALICE, + origin, contract_id, 0, Weight::from_parts(10_000_000_000, 1024 * 1024), None, data, - true, + false, ); value } diff --git a/tests/integration/src/unified_accounts.rs b/tests/integration/src/unified_accounts.rs index de89dbdc85..ded4e36805 100644 --- a/tests/integration/src/unified_accounts.rs +++ b/tests/integration/src/unified_accounts.rs @@ -56,6 +56,7 @@ fn unified_accounts_chain_extension_works() { // mapped h160 address should None assert_eq!( call_wasm_contract_method::>( + ALICE, contract_id.clone(), [GET_H160.to_vec(), ALICE.encode()].concat() ), @@ -64,6 +65,7 @@ fn unified_accounts_chain_extension_works() { // default h160 address should match assert_eq!( call_wasm_contract_method::( + ALICE, contract_id.clone(), [GET_H160_OR_DEFAULT.to_vec(), ALICE.encode()].concat() ), @@ -72,6 +74,7 @@ fn unified_accounts_chain_extension_works() { // mapped native address should be None assert_eq!( call_wasm_contract_method::>( + ALICE, contract_id.clone(), [GET_NATIVE.to_vec(), alith().encode()].concat() ), @@ -80,6 +83,7 @@ fn unified_accounts_chain_extension_works() { // default native address should match assert_eq!( call_wasm_contract_method::( + ALICE, contract_id.clone(), [GET_NATIVE_OR_DEFAULT.to_vec(), alith().encode()].concat() ), @@ -94,6 +98,7 @@ fn unified_accounts_chain_extension_works() { // ALICE mapped h160 address should be alith assert_eq!( call_wasm_contract_method::>( + ALICE, contract_id.clone(), [GET_H160.to_vec(), ALICE.encode()].concat() ), @@ -103,6 +108,7 @@ fn unified_accounts_chain_extension_works() { // alith mapped native address should ALICE assert_eq!( call_wasm_contract_method::>( + ALICE, contract_id.clone(), [GET_NATIVE.to_vec(), alith().encode()].concat() ), From 7a46c49fecdc91d031576733171ab058b90c3056 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 23 Oct 2023 09:21:57 +0530 Subject: [PATCH 12/15] feat: update CE interface with `is_mapped` value --- Cargo.lock | 1 + chain-extensions/unified-accounts/Cargo.toml | 2 ++ chain-extensions/unified-accounts/src/lib.rs | 31 ++++++++++++++++--- tests/ink-contracts/au_ce_getters.wasm | Bin 19287 -> 19868 bytes tests/integration/src/unified_accounts.rs | 8 ++--- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 30ecad1a05..6339ed8363 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7484,6 +7484,7 @@ dependencies = [ "num-traits", "pallet-contracts", "pallet-contracts-primitives", + "pallet-evm", "pallet-unified-accounts", "parity-scale-codec", "scale-info", diff --git a/chain-extensions/unified-accounts/Cargo.toml b/chain-extensions/unified-accounts/Cargo.toml index dcd680c486..5f6e4e4b8f 100644 --- a/chain-extensions/unified-accounts/Cargo.toml +++ b/chain-extensions/unified-accounts/Cargo.toml @@ -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 } @@ -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", diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs index 4e79cfd179..6d0b3f9c3d 100644 --- a/chain-extensions/unified-accounts/src/lib.rs +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -18,7 +18,10 @@ #![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; @@ -26,6 +29,8 @@ 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, *}; @@ -58,8 +63,17 @@ where let base_weight = ::DbWeight::get().reads(1); env.charge_weight(base_weight)?; + + // read the storage item + let mapped = NativeToEvm::::get(account_id.clone()); + + 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()?; @@ -74,9 +88,18 @@ where let base_weight = ::DbWeight::get().reads(1); env.charge_weight(base_weight)?; + + // read the storage item + let mapped = EvmToNative::::get(evm_address.clone()); + + 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)) diff --git a/tests/ink-contracts/au_ce_getters.wasm b/tests/ink-contracts/au_ce_getters.wasm index 482e199b56008317a41583cab693937648026d79..c69fc289de096f6b2728373db5f4494671b3f72b 100644 GIT binary patch delta 3314 zcmai0YiyI(89wj%zR$5^n|!%Am^hbX=VB+sU?+|*0g{~D>~L9WX*)`*1!EhC4NkbV zMOATnQ)#;&WiMh|fmV~It@}}6%A_)hsp`g$_Qy6cZR(g-#wJagG-++6b?YCrz2|ch z3fnYWI@j;Km*;uU`<{=^!G-hSWZ7effiVU%t}{-5H&obO#`Z!5Dtq+`{n%bZb*nwC zvoky1iEbL!IB<=tpXStRG<%J~xQ%n0je!A7V|H%XcnxC)XUw77F$mq=?Y4Yj;n4g^ z*5U8~li;iyPOS@%Z5SC1bawRwwnS>*d1M%mz%5ANPX0t7A(+tO++OFDSH7joR{6!Z zb+el}g(eJn`VHOm;4k>qjvjzYCzEC=z`K~>f(^4e0fNiAq6E{4r?fUO@q*@qdc3Ml z!g{=;b=LHe958z^sE6G_p@+C#H7+xRSv@?fv@ijrTas#TMr}^ zq`fGZ=@t-X4nNJq%uWKpvO#hQQb~hppCna2@)3^s@XvY?T5zXvirQZq-dc|`-kBj- z@J{0fPn>&6El6)sxdZaoFY0Cs{?&K|HscA~G;G9cww{JICD&||WHe!6t~Fcn@3!x4 zZ(+_iyx>qKE`sGbAG`ZE8|1&>h%~14F*?4uNPz8@ix+g7{FW6>P089F(B*TB@PHfe zW=$|kYhkwAZFP@TyFG4=SMCZ`A3QGmN^5jRG3LOiy$S3%>KMhB?Cns4pV8cgKelhj ztM-i;b9@Yq*if4Uj@xQ?5d?45Myb6~dyCeuIQIo>m1qml0AIs_xrliA7gnV912WR3 zwhp?Tm~xd#&)cqn!8W0X8BAGDF|*CZ2~PzM#R6CK)VW|~#aef|9O!m`4MG@odMctw zyqLvaMggK2^{TmjbuPHIW6}g;}^HpC--TSXlR@mPS9EJEHT?4m~4gkkcQDiR@{2^fO!ApbxZ zaXv|%Ev_(9fKWSDs-lu{lPEQ7Q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FPb(w6u|QTh36@ueZE9;F%#eadq{)_bs4qd8_w6vAS4ZX}rkN z%c=eJ_TN*xNj9}!g2dX@Rse{sU26*i?Y-N661L)$8z*_#U5#Xt)?Vsr?r{y-uoemE zKD5&#RG#Vdzo8L#b?gc?Iu4~x_Xj!IhNnBu(f2{D^P40ucP_)=T3y#iq0r5gxwP=a zA-Is*#imh>wBgwyYppQUVd0hGFt03NXuKUi93~qm@&z8AB&7!YX}%k(a7>LrHNK<< zpay@go`qUm-czLB`0JiNsKd}mKSVJL#cC2Cj8!L`2qkRcXMIGmyrg6dr>Pj7;ok>I^@1;^{BXJ$mTy(WSW)OV1o#dc13F?xD{OWam;d z58%pl4ZbkpQV0HU;y83;(`26ToSU4cKK#LCfjaF!lLJ)N_NlFW;0riEnZebmRs!O+ zsjt8`T%OsE^V4BFTOgBL`|(#ZN!&M6iLXwFA%{Pk?x1S_V|p8G$N0( + 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!( @@ -82,12 +82,12 @@ fn unified_accounts_chain_extension_works() { ); // default native address should match assert_eq!( - call_wasm_contract_method::( + 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) ); // From 880d595cc4ae1924e831d197ae88f59d19cc171c Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Mon, 23 Oct 2023 12:47:44 +0530 Subject: [PATCH 13/15] feat: apply code suggestions --- chain-extensions/unified-accounts/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs index 6d0b3f9c3d..b9a491fe08 100644 --- a/chain-extensions/unified-accounts/src/lib.rs +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -30,7 +30,6 @@ 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, *}; @@ -65,7 +64,7 @@ where env.charge_weight(base_weight)?; // read the storage item - let mapped = NativeToEvm::::get(account_id.clone()); + let mapped = UA::to_h160(&account_id); let is_mapped = mapped.is_some(); let evm_address = mapped.unwrap_or_else(|| { @@ -90,7 +89,7 @@ where env.charge_weight(base_weight)?; // read the storage item - let mapped = EvmToNative::::get(evm_address.clone()); + let mapped = UA::to_account_id(&evm_address); let is_mapped = mapped.is_some(); let native_address = mapped.unwrap_or_else(|| { From 26937433a0860cbfae76f73ad73559b7d6fdd2c5 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Tue, 24 Oct 2023 10:41:08 +0530 Subject: [PATCH 14/15] feat: add enum type and update tests --- Cargo.lock | 1 + .../types/unified-accounts/src/lib.rs | 9 +- chain-extensions/unified-accounts/src/lib.rs | 34 +++-- tests/ink-contracts/au_ce_getters.json | 132 ++++++++++++++---- tests/ink-contracts/au_ce_getters.wasm | Bin 19868 -> 19694 bytes tests/integration/Cargo.toml | 1 + tests/integration/src/unified_accounts.rs | 11 +- 7 files changed, 139 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6339ed8363..7f1416f3a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4961,6 +4961,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "unified-accounts-chain-extension-types", ] [[package]] diff --git a/chain-extensions/types/unified-accounts/src/lib.rs b/chain-extensions/types/unified-accounts/src/lib.rs index a629a53708..e4de735925 100644 --- a/chain-extensions/types/unified-accounts/src/lib.rs +++ b/chain-extensions/types/unified-accounts/src/lib.rs @@ -19,7 +19,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use num_enum::{IntoPrimitive, TryFromPrimitive}; -use parity_scale_codec::{Decode, Encode}; +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; #[repr(u16)] #[derive(TryFromPrimitive, IntoPrimitive, Decode, Encode)] @@ -33,3 +33,10 @@ pub enum Command { /// Get the mapped Native address if any otheriwse default associated Native address GetNativeAddressOrDefault = 3, } + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum UnifiedAddress { + Mapped(T), + Default(T), +} diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs index b9a491fe08..17ab43f4e4 100644 --- a/chain-extensions/unified-accounts/src/lib.rs +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -31,7 +31,10 @@ use pallet_contracts::chain_extension::{ }; use pallet_evm::AddressMapping; use parity_scale_codec::Encode; -pub use unified_accounts_chain_extension_types::Command::{self, *}; +pub use unified_accounts_chain_extension_types::{ + Command::{self, *}, + UnifiedAddress, +}; #[derive(DefaultNoBound)] pub struct UnifiedAccountsExtension(PhantomData<(T, UA)>); @@ -63,16 +66,13 @@ where let base_weight = ::DbWeight::get().reads(1); env.charge_weight(base_weight)?; - // read the storage item - let mapped = UA::to_h160(&account_id); - - let is_mapped = mapped.is_some(); - let evm_address = mapped.unwrap_or_else(|| { - // fallback to default account_id - T::DefaultNativeToEvm::into_h160(account_id) - }); + let evm_address = if let Some(h160) = UA::to_h160(&account_id) { + UnifiedAddress::Mapped(h160) + } else { + UnifiedAddress::Default(T::DefaultNativeToEvm::into_h160(account_id)) + }; // write to buffer - (evm_address, is_mapped).using_encoded(|r| env.write(r, false, None))?; + evm_address.using_encoded(|r| env.write(r, false, None))?; } GetNativeAddress => { let evm_address: EvmAddress = env.read_as()?; @@ -89,16 +89,14 @@ where env.charge_weight(base_weight)?; // read the storage item - let mapped = UA::to_account_id(&evm_address); - - 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) - }); + let native_address = if let Some(native) = UA::to_account_id(&evm_address) { + UnifiedAddress::Mapped(native) + } else { + UnifiedAddress::Default(T::DefaultEvmToNative::into_account_id(evm_address)) + }; // write to buffer - (native_address, is_mapped).using_encoded(|r| env.write(r, false, None))?; + native_address.using_encoded(|r| env.write(r, false, None))?; } }; Ok(RetVal::Converging(0)) diff --git a/tests/ink-contracts/au_ce_getters.json b/tests/ink-contracts/au_ce_getters.json index abffbdfe7e..d3448d5c79 100644 --- a/tests/ink-contracts/au_ce_getters.json +++ b/tests/ink-contracts/au_ce_getters.json @@ -1,6 +1,6 @@ { "source": { - "hash": "0x5eb72ad6cdb6869bb8419ae07ff533bca787346360e58713c679f14679e188f7", + "hash": "0x0cfe2b4d2b98a121c0ce38b07d826f1e4d0c7d9870ff84239cd554b32904617d", "language": "ink! 4.3.0", "compiler": "rustc 1.72.0", "build_info": { @@ -52,32 +52,32 @@ "displayName": [ "Balance" ], - "type": 14 + "type": 16 }, "blockNumber": { "displayName": [ "BlockNumber" ], - "type": 17 + "type": 19 }, "chainExtension": { "displayName": [ "ChainExtension" ], - "type": 18 + "type": 20 }, "hash": { "displayName": [ "Hash" ], - "type": 15 + "type": 17 }, "maxEventTopics": 4, "timestamp": { "displayName": [ "Timestamp" ], - "type": 16 + "type": 18 } }, "events": [], @@ -105,7 +105,7 @@ "docs": [], "label": "to_h160", "mutates": false, - "payable": true, + "payable": false, "returnType": { "displayName": [ "ink", @@ -131,7 +131,7 @@ "docs": [], "label": "to_h160_or_default", "mutates": false, - "payable": true, + "payable": false, "returnType": { "displayName": [ "ink", @@ -157,13 +157,13 @@ "docs": [], "label": "to_account_id", "mutates": false, - "payable": true, + "payable": false, "returnType": { "displayName": [ "ink", "MessageResult" ], - "type": 11 + "type": 12 }, "selector": "0x0000002c" }, @@ -183,13 +183,13 @@ "docs": [], "label": "to_account_id_or_default", "mutates": false, - "payable": true, + "payable": false, "returnType": { "displayName": [ "ink", "MessageResult" ], - "type": 13 + "type": 14 }, "selector": "0x0000002d" } @@ -429,7 +429,7 @@ { "fields": [ { - "type": 8 + "type": 11 } ], "index": 0, @@ -450,7 +450,7 @@ "params": [ { "name": "T", - "type": 8 + "type": 11 }, { "name": "E", @@ -471,7 +471,48 @@ { "fields": [ { - "type": 12 + "type": 8, + "typeName": "T" + } + ], + "index": 0, + "name": "Mapped" + }, + { + "fields": [ + { + "type": 8, + "typeName": "T" + } + ], + "index": 1, + "name": "Default" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 8 + } + ], + "path": [ + "unified_accounts_chain_extension_types", + "UnifiedAddress" + ] + } + }, + { + "id": 12, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 13 } ], "index": 0, @@ -492,7 +533,7 @@ "params": [ { "name": "T", - "type": 12 + "type": 13 }, { "name": "E", @@ -505,7 +546,7 @@ } }, { - "id": 12, + "id": 13, "type": { "def": { "variant": { @@ -538,7 +579,7 @@ } }, { - "id": 13, + "id": 14, "type": { "def": { "variant": { @@ -546,7 +587,7 @@ { "fields": [ { - "type": 3 + "type": 15 } ], "index": 0, @@ -567,7 +608,7 @@ "params": [ { "name": "T", - "type": 3 + "type": 15 }, { "name": "E", @@ -580,7 +621,48 @@ } }, { - "id": 14, + "id": 15, + "type": { + "def": { + "variant": { + "variants": [ + { + "fields": [ + { + "type": 3, + "typeName": "T" + } + ], + "index": 0, + "name": "Mapped" + }, + { + "fields": [ + { + "type": 3, + "typeName": "T" + } + ], + "index": 1, + "name": "Default" + } + ] + } + }, + "params": [ + { + "name": "T", + "type": 3 + } + ], + "path": [ + "unified_accounts_chain_extension_types", + "UnifiedAddress" + ] + } + }, + { + "id": 16, "type": { "def": { "primitive": "u128" @@ -588,7 +670,7 @@ } }, { - "id": 15, + "id": 17, "type": { "def": { "composite": { @@ -608,7 +690,7 @@ } }, { - "id": 16, + "id": 18, "type": { "def": { "primitive": "u64" @@ -616,7 +698,7 @@ } }, { - "id": 17, + "id": 19, "type": { "def": { "primitive": "u32" @@ -624,7 +706,7 @@ } }, { - "id": 18, + "id": 20, "type": { "def": { "variant": {} diff --git a/tests/ink-contracts/au_ce_getters.wasm b/tests/ink-contracts/au_ce_getters.wasm index c69fc289de096f6b2728373db5f4494671b3f72b..e93128bb916b660f1177837eb25278f5892ae910 100644 GIT binary patch delta 4602 zcmc&&YiwLc6`q-U_m$m^uiwwuch`2jcH(%w?{#8(C-(XgJEScHIcbrzUfXfjA&CQ} zN`YMGAtiN+l4>F~E$NR+C8B~K1q-QyP!bhH5kJ}=s6wIwiV%fTl~fHooeTv2%gzw?}`H50e4=r(@-c~vVHZ?jj$ zkCaMUB7UvZQ%$_54AOS7Rc&?zphvYf@kO=M+p4Pp=9G;`+bk}rowQlJrap-_Y3mTz zY=>x#(CjDCUbNTHa`9dJ7@lw2pQ1hDq+`g@N!)Qt4N+27U#;WAwR)@NR3|X3RcdwO z6UQ~9hPdz4P?yn6_#&P6(-y)~tAAQ4{|{Z@wjnj8Q@*%JV{C#iUR5#pEy-z$P6KSh zZ{w#H=^iiDscwt0h}68Q##j&!1QXB&bJh&5YA!Ekb&?6IGxk~iey_re)pe!Dqr78& zqe>7l7u+J(GYx4@YdSifnn&{|l53|Vt`I!GVJf{}Bze=6d;|$0aRMEkw$uG_B_XZs z^BC$nw$onbaoi>MSZmtXTl1IZ3V$QH03osB6*%)@J-G^7&zMabGuH0l)2%55YgAf$ zkgao?H|6#j@+;b;XW^)Vi?}WY@MXJ__iK$5q~;4__)tm26%9fJIHl8_fJU4>hOrrW z8FIXk06^IM5-!0UVvi4(QzuOKb0>@%Jb^aA4rpbv^Y~0iIeqYSfF*nmIU&Gy_?g6! z9a-eiRgL&mok{tGUm5kXRoKWUT?J?u65TBUKfh5?RGaDlKa<=}5@!er2aM&n{`;(I zSon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R$ zZ;RRm^=LUu=|V9xxR_szr{nR3g#|0Ob4(b#N&Gy!k2Z@xWd~`CsN1{fH#XuFsj3pW$7Gm9ziIb;E`pB(C?5%JogS=M#CI5+Ny z(VR~#jknOKSRJ1M3*H^UZI~%@DPIc*4p%5>Godm`)XQ zLzBQE2}U1@h!2jK^neIW_p{-ovUJn*9(qumoW7Ng$l2mw)Ae)|Pe*80wJ#n0`u_mN C+Hdgy diff --git a/tests/integration/Cargo.toml b/tests/integration/Cargo.toml index 8ba11a57e3..2295c836a1 100644 --- a/tests/integration/Cargo.toml +++ b/tests/integration/Cargo.toml @@ -38,6 +38,7 @@ pallet-evm-precompile-assets-erc20 = { workspace = true } pallet-evm-precompile-dispatch = { workspace = true } pallet-unified-accounts = { workspace = true } precompile-utils = { workspace = true } +unified-accounts-chain-extension-types = { workspace = true } astar-primitives = { workspace = true } astar-runtime = { workspace = true, features = ["std"], optional = true } diff --git a/tests/integration/src/unified_accounts.rs b/tests/integration/src/unified_accounts.rs index c0b86d7249..3029f4dbb5 100644 --- a/tests/integration/src/unified_accounts.rs +++ b/tests/integration/src/unified_accounts.rs @@ -18,7 +18,8 @@ use crate::setup::*; use parity_scale_codec::Encode; -pub use sp_io::hashing::keccak_256; +use sp_io::hashing::keccak_256; +use unified_accounts_chain_extension_types::UnifiedAddress; const AU_CE_GETTER: &'static str = "au_ce_getters"; @@ -64,12 +65,12 @@ fn unified_accounts_chain_extension_works() { ); // default h160 address should match assert_eq!( - call_wasm_contract_method::<(H160, bool)>( + call_wasm_contract_method::>( ALICE, contract_id.clone(), [GET_H160_OR_DEFAULT.to_vec(), ALICE.encode()].concat() ), - (UnifiedAccounts::to_h160_or_default(&ALICE), false) + UnifiedAddress::Default(UnifiedAccounts::to_h160_or_default(&ALICE)) ); // mapped native address should be None assert_eq!( @@ -82,12 +83,12 @@ fn unified_accounts_chain_extension_works() { ); // default native address should match assert_eq!( - call_wasm_contract_method::<(AccountId, bool)>( + call_wasm_contract_method::>( ALICE, contract_id.clone(), [GET_NATIVE_OR_DEFAULT.to_vec(), alith().encode()].concat() ), - (UnifiedAccounts::to_account_id_or_default(&alith()), false) + UnifiedAddress::Default(UnifiedAccounts::to_account_id_or_default(&alith())) ); // From 942c318529f6ffcd5e27e83a595aa11a23b68200 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Tue, 24 Oct 2023 12:31:08 +0530 Subject: [PATCH 15/15] feat: add docstring in type --- chain-extensions/types/unified-accounts/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chain-extensions/types/unified-accounts/src/lib.rs b/chain-extensions/types/unified-accounts/src/lib.rs index e4de735925..7371e3a9e0 100644 --- a/chain-extensions/types/unified-accounts/src/lib.rs +++ b/chain-extensions/types/unified-accounts/src/lib.rs @@ -37,6 +37,10 @@ pub enum Command { #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum UnifiedAddress { + /// The address fetched from the mappings and the account + /// is unified Mapped(T), + /// The default address associated with account as there + /// is no mapping found and accounts are not unified Default(T), }