Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"runtime/shibuya",
"tests/xcm-simulator",
"tests/integration",
"tests/utils",

"pallets/*",
"precompiles/*",
Expand All @@ -17,6 +18,7 @@ members = [
"chain-extensions/dapps-staking",
"chain-extensions/pallet-assets",
"chain-extensions/xvm",
"chain-extensions/unified-accounts",
"chain-extensions/types/*",

"vendor/evm-tracing",
Expand Down Expand Up @@ -279,6 +281,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 }
Expand All @@ -290,10 +293,12 @@ pallet-evm-precompile-dapps-staking = { path = "./precompiles/dapps-staking", de
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 }

Expand Down
27 changes: 27 additions & 0 deletions chain-extensions/types/unified-accounts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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",
]
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

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(&eth_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(&eth_address);
assert_eq!(account.balance, (UNIT - ExistentialDeposit::get()).into());
});
#![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,
}
47 changes: 47 additions & 0 deletions chain-extensions/unified-accounts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[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 }
pallet-evm = { 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-evm/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",
"unified-accounts-chain-extension-types/std",
]
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved
106 changes: 106 additions & 0 deletions chain-extensions/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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 <http://www.gnu.org/licenses/>.

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

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

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

#[derive(DefaultNoBound)]
pub struct UnifiedAccountsExtension<T, UA>(PhantomData<(T, UA)>);

impl<T, UA> ChainExtension<T> for UnifiedAccountsExtension<T, UA>
where
T: pallet_contracts::Config + pallet_unified_accounts::Config,
UA: UnifiedAddressMapper<T::AccountId>,
{
fn call<E>(&mut self, env: Environment<E, InitState>) -> DispatchResult<RetVal>
where
E: Ext<T = T>,
{
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 = <T as frame_system::Config>::DbWeight::get().reads(1);
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved
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 = <T as frame_system::Config>::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)
});
// write to buffer
(evm_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look really Rust-like to me.
Returning an enum would be more appropriate: (Mapped(address), Default(address)).
That also makes it more extensible for the future (not sure how we'd need it, but in general).

Anyhow, if others are ok with the proposed solution, you can ignore this comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the rust way too.
I'll update the PR tomorrow with changes.

cc: @PierreOssun @shaunxw

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, the enum one looks better.

@PierreOssun the CE interface in ink! can get the decoded enum type without extra work in SDK right? Pls correct me if I'm wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the CE with enum, please have a look

the CE interface in ink! can get the decoded enum type without extra work in SDK right?

Yes, as long as it supports scale decode it'll be fine, I've updated the test ink! contracts here.

}
GetNativeAddress => {
let evm_address: EvmAddress = env.read_as()?;

let base_weight = <T as frame_system::Config>::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 = <T as frame_system::Config>::DbWeight::get().reads(1);
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)
});

// write to buffer
(native_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
}
};
Ok(RetVal::Converging(0))
}
}
10 changes: 10 additions & 0 deletions pallets/unified-accounts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -42,6 +43,10 @@ mod benchmarks {
)
.into();

assert_ok!(T::Currency::mint_into(
&caller,
T::AccountMappingStorageFee::get()
));
let caller_clone = caller.clone();

#[extrinsic_call]
Expand All @@ -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));

Expand Down
Loading
Loading