From 26937433a0860cbfae76f73ad73559b7d6fdd2c5 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Tue, 24 Oct 2023 10:41:08 +0530 Subject: [PATCH] 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=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>#FMOvRdrz)ZN(Z{Ca^hH ztN|6*NQ1qADd|(@5FX@q)|Ca_l?CF;f?1V?sFZ~?e_#t^Tv6U#@NSTa^cI!)BD`H` zsD(6yzfc+!XWWf|B(Kp5>Cx2&{`vc{fkz9z@XE8%jy8&FwY{!f@00#Dr{p1_vaKh9 zw;9#thCaTwpEnxBD^2G+r76h75HX2uM8j9}%_nH{0l2Nna)k|u5@8PxMhC<@El<#TabN2{d+MRRuDI$D zS6u>tu%lXTJ~utOYaA!0w{}h76w}o!*Xtj9UNsN7VWi zZ{FI}y5hDZps-Yq-h$b$M@Miqx}TukSD)&pl(t=cx~HC^@8#aJG%bc>9k|ZM9&ENE zo*R*LvieBBOwTe{krJQ94k1_v`#z#!vA_RdC>#hVCdx%J;%xsVoIwLOJdEq*8&1>V zt2KK+4Fw+R)lcT{D$-|r_|@RhbF3~ipGp^!g9*pVTOxopH!i_Su_xHxDP6ZxW(5Fd;T8uN>GqF_1maVufZJE;Y+e>B-( zEvBr3wGhu+$wE5m6moUK8cj4NGO|BiC|JdO(Q*EkRVYR$ 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())) ); //