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

Polkadot v1.1.0: Account converter changes #1806

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use cfg_types::{
use cfg_utils::vec_to_fixed_array;
use cumulus_primitives_core::ParaId;
use hex_literal::hex;
use runtime_common::{account_conversion::convert_evm_address, evm::precompile::H160Addresses};
use runtime_common::{account_conversion::AccountConverter, evm::precompile::H160Addresses};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::{ChainType, Properties};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -557,7 +557,7 @@ fn centrifuge_genesis(

endowed_accounts.extend(endowed_evm_accounts.into_iter().map(|(addr, id)| {
let chain_id = id.unwrap_or_else(|| chain_id.into());
convert_evm_address(chain_id, addr)
AccountConverter::convert_evm_address(chain_id, addr)
}));

let num_endowed_accounts = endowed_accounts.len();
Expand Down Expand Up @@ -700,7 +700,7 @@ fn altair_genesis(

endowed_accounts.extend(endowed_evm_accounts.into_iter().map(|(addr, id)| {
let chain_id = id.unwrap_or_else(|| chain_id.into());
convert_evm_address(chain_id, addr)
AccountConverter::convert_evm_address(chain_id, addr)
}));

let num_endowed_accounts = endowed_accounts.len();
Expand Down Expand Up @@ -829,7 +829,7 @@ fn development_genesis(

endowed_accounts.extend(endowed_evm_accounts.into_iter().map(|(addr, id)| {
let chain_id = id.unwrap_or_else(|| chain_id.into());
convert_evm_address(chain_id, addr)
AccountConverter::convert_evm_address(chain_id, addr)
}));

let num_endowed_accounts = endowed_accounts.len();
Expand Down
6 changes: 3 additions & 3 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use pallet_transaction_payment_rpc_runtime_api::{FeeDetails, RuntimeDispatchInfo
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use polkadot_runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate};
use runtime_common::{
account_conversion::{self, AccountConverter, AccountMapping},
account_conversion::{AccountConverter, RuntimeAccountConverter},
asset_registry,
evm::{
precompile::Precompiles, BaseFeeThreshold, FindAuthorTruncated, GAS_LIMIT_POV_SIZE_RATIO,
Expand Down Expand Up @@ -1849,7 +1849,7 @@ parameter_types! {
}

impl pallet_evm::Config for Runtime {
type AddressMapping = AccountMapping<Runtime>;
type AddressMapping = RuntimeAccountConverter<Runtime>;
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = EnsureAddressRoot<AccountId>;
Expand Down Expand Up @@ -2374,7 +2374,7 @@ impl_runtime_apis! {
// AccountConversionApi
impl runtime_common::apis::AccountConversionApi<Block, AccountId> for Runtime {
fn conversion_of(location: MultiLocation) -> Option<AccountId> {
account_conversion::location_to_account::<LocationToAccountId>(location)
AccountConverter::location_to_account::<LocationToAccountId>(location)
}
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use pallet_transaction_payment_rpc_runtime_api::{FeeDetails, RuntimeDispatchInfo
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use polkadot_runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate};
use runtime_common::{
account_conversion::{self, AccountConverter, AccountMapping},
account_conversion::{AccountConverter, RuntimeAccountConverter},
asset_registry,
evm::{
precompile::Precompiles, BaseFeeThreshold, FindAuthorTruncated, GAS_LIMIT_POV_SIZE_RATIO,
Expand Down Expand Up @@ -1963,7 +1963,7 @@ parameter_types! {
}

impl pallet_evm::Config for Runtime {
type AddressMapping = AccountMapping<Runtime>;
type AddressMapping = RuntimeAccountConverter<Runtime>;
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = EnsureAddressRoot<AccountId>;
Expand Down Expand Up @@ -2421,7 +2421,7 @@ impl_runtime_apis! {
// AccountConversionApi
impl runtime_common::apis::AccountConversionApi<Block, AccountId> for Runtime {
fn conversion_of(location: MultiLocation) -> Option<AccountId> {
account_conversion::location_to_account::<LocationToAccountId>(location)
AccountConverter::location_to_account::<LocationToAccountId>(location)
}
}

Expand Down
101 changes: 54 additions & 47 deletions runtime/common/src/account_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,61 @@ use sp_runtime::traits::Convert;
use staging_xcm::v3;
use staging_xcm_executor::traits::ConvertLocation;

/// Converts an EVM address from a given chain into a local AccountId
pub fn convert_evm_address(chain_id: u64, address: [u8; 20]) -> AccountId {
// We use a custom encoding here rather than relying on
// `AccountIdConversion` for a couple of reasons:
// 1. We have very few bytes to spare, so choosing our own fields is nice
// 2. AccountIdConversion puts the tag first, which can unbalance the storage
// trees if users create many H160-derived accounts. We put the tag last
// here.
let tag = b"EVM";
let mut bytes = [0; 32];
bytes[0..20].copy_from_slice(&address);
bytes[20..28].copy_from_slice(&chain_id.to_be_bytes());
bytes[28..31].copy_from_slice(tag);
AccountId::new(bytes)
}
/// Common converter code for translating accounts across different
/// domains and chains.
pub struct AccountConverter;

/// Common converter code for translating from eth accounts
pub struct AccountMapping<R>(sp_std::marker::PhantomData<R>);
impl AccountConverter {
/// Converts an EVM address from a given chain into a local AccountId
pub fn convert_evm_address(chain_id: u64, address: [u8; 20]) -> AccountId {
// We use a custom encoding here rather than relying on
// `AccountIdConversion` for a couple of reasons:
// 1. We have very few bytes to spare, so choosing our own fields is nice
// 2. AccountIdConversion puts the tag first, which can unbalance the storage
// trees if users create many H160-derived accounts. We put the tag last
// here.
let tag = b"EVM";
let mut bytes = [0; 32];
bytes[0..20].copy_from_slice(&address);
bytes[20..28].copy_from_slice(&chain_id.to_be_bytes());
bytes[28..31].copy_from_slice(tag);
AccountId::new(bytes)
}

// Implement EVM account conversion using our shared conversion code
impl<R: pallet_evm_chain_id::Config> AddressMapping<AccountId> for AccountMapping<R> {
fn into_account_id(address: H160) -> AccountId {
pub fn location_to_account<XcmConverter: ConvertLocation<AccountId>>(
location: v3::MultiLocation,
) -> Option<AccountId> {
// Try xcm logic first
match XcmConverter::convert_location(&location) {
Some(acc) => Some(acc),
None => {
// match EVM logic
match location {
v3::MultiLocation {
parents: 0,
interior:
v3::Junctions::X1(v3::Junction::AccountKey20 {
network: Some(v3::NetworkId::Ethereum { chain_id }),
key,
}),
} => Some(Self::convert_evm_address(chain_id, key)),
_ => None,
}
}
}
}

pub fn into_account_id<R: pallet_evm_chain_id::Config>(address: H160) -> AccountId {
let chain_id = pallet_evm_chain_id::Pallet::<R>::get();
convert_evm_address(chain_id, address.0)
Self::convert_evm_address(chain_id, address.0)
}
}

/// Common converter code for translating accounts across different
/// domains and chains.
pub struct AccountConverter;

impl Convert<DomainAddress, AccountId> for AccountConverter {
fn convert(domain_address: DomainAddress) -> AccountId {
match domain_address {
DomainAddress::Centrifuge(addr) => AccountId::new(addr),
DomainAddress::EVM(chain_id, addr) => convert_evm_address(chain_id, addr),
DomainAddress::EVM(chain_id, addr) => Self::convert_evm_address(chain_id, addr),
}
}
}
Expand All @@ -66,32 +85,20 @@ impl Convert<(Domain, [u8; 32]), AccountId> for AccountConverter {
Domain::EVM(chain_id) => {
let mut bytes20 = [0; 20];
bytes20.copy_from_slice(&account[..20]);
convert_evm_address(chain_id, bytes20)
Self::convert_evm_address(chain_id, bytes20)
}
}
}
}

pub fn location_to_account<XcmConverter: ConvertLocation<AccountId>>(
location: v3::MultiLocation,
) -> Option<AccountId> {
// Try xcm logic first
match XcmConverter::convert_location(&location) {
Some(acc) => Some(acc),
None => {
// match EVM logic
match location {
v3::MultiLocation {
parents: 0,
interior:
v3::Junctions::X1(v3::Junction::AccountKey20 {
network: Some(v3::NetworkId::Ethereum { chain_id }),
key,
}),
} => Some(convert_evm_address(chain_id, key)),
_ => None,
}
}
// A type that use AccountConverter to carry along with it the Runtime type and
// offer an `AddressMapping` implementation.
// Required by `pallet_evm`
pub struct RuntimeAccountConverter<R>(sp_std::marker::PhantomData<R>);

impl<R: pallet_evm_chain_id::Config> AddressMapping<AccountId> for RuntimeAccountConverter<R> {
fn into_account_id(address: H160) -> AccountId {
AccountConverter::into_account_id::<R>(address)
}
}

Expand Down
5 changes: 2 additions & 3 deletions runtime/common/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
// GNU General Public License for more details.

use cfg_primitives::AccountId;
use pallet_evm::AddressMapping;
use polkadot_parachain_primitives::primitives::Sibling;
use sp_core::{crypto::AccountId32, Get, H160};
use sp_runtime::traits::AccountIdConversion;

use crate::account_conversion::AccountMapping;
use crate::account_conversion::AccountConverter;

pub fn get_gateway_account<T: pallet_evm_chain_id::Config + parachain_info::Config>() -> AccountId {
let sender_account: AccountId =
Expand All @@ -25,5 +24,5 @@ pub fn get_gateway_account<T: pallet_evm_chain_id::Config + parachain_info::Conf
let truncated_sender_account =
H160::from_slice(&<AccountId32 as AsRef<[u8; 32]>>::as_ref(&sender_account)[0..20]);

AccountMapping::<T>::into_account_id(truncated_sender_account)
AccountConverter::into_account_id::<T>(truncated_sender_account)
}
6 changes: 3 additions & 3 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use pallet_transaction_payment_rpc_runtime_api::{FeeDetails, RuntimeDispatchInfo
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use runtime_common::{
account_conversion::{self, AccountConverter, AccountMapping},
account_conversion::{AccountConverter, RuntimeAccountConverter},
asset_registry,
changes::FastDelay,
evm::{
Expand Down Expand Up @@ -1941,7 +1941,7 @@ parameter_types! {
}

impl pallet_evm::Config for Runtime {
type AddressMapping = AccountMapping<Runtime>;
type AddressMapping = RuntimeAccountConverter<Runtime>;
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = EnsureAddressTruncated;
Expand Down Expand Up @@ -2460,7 +2460,7 @@ impl_runtime_apis! {
// AccountConversionApi
impl runtime_common::apis::AccountConversionApi<Block, AccountId> for Runtime {
fn conversion_of(location: MultiLocation) -> Option<AccountId> {
account_conversion::location_to_account::<LocationToAccountId>(location)
AccountConverter::location_to_account::<LocationToAccountId>(location)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use polkadot_runtime_parachains::{
paras::{ParaGenesisArgs, ParaKind},
};
use runtime_common::{
account_conversion::{convert_evm_address, AccountConverter},
account_conversion::AccountConverter,
foreign_investments::IdentityPoolCurrencyConverter,
xcm::general_key,
xcm_fees::{default_per_second, ksm_per_second},
Expand Down Expand Up @@ -4773,7 +4773,8 @@ mod development {
) {
let chain_id = env.parachain_state(|| pallet_evm_chain_id::Pallet::<T>::get());

let derived_account = convert_evm_address(chain_id, address.to_fixed_bytes());
let derived_account =
AccountConverter::convert_evm_address(chain_id, address.to_fixed_bytes());

env.parachain_state_mut(|| {
pallet_balances::Pallet::<T>::mint_into(&derived_account.into(), balance)
Expand Down
6 changes: 3 additions & 3 deletions runtime/integration-tests/src/utils/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use frame_support::{dispatch::RawOrigin, traits::fungible::Mutate};
use fudge::primitives::Chain;
use pallet_evm::FeeCalculator;
use runtime_common::account_conversion::{convert_evm_address, AccountConverter};
use runtime_common::account_conversion::AccountConverter;
use sp_core::{Get, H160, U256};

use crate::{
Expand All @@ -28,7 +28,7 @@ pub fn mint_balance_into_derived_account(env: &mut TestEnv, address: H160, balan
})
.unwrap();

let derived_account = convert_evm_address(chain_id, address.to_fixed_bytes());
let derived_account = AccountConverter::convert_evm_address(chain_id, address.to_fixed_bytes());

env.with_mut_state(Chain::Para(PARA_ID), || {
Balances::mint_into(&derived_account.into(), balance).unwrap()
Expand All @@ -43,7 +43,7 @@ pub fn deploy_contract(env: &mut TestEnv, address: H160, code: Vec<u8>) {
})
.unwrap();

let derived_address = convert_evm_address(chain_id, address.to_fixed_bytes());
let derived_address = AccountConverter::convert_evm_address(chain_id, address.to_fixed_bytes());

let transaction_create_cost = env
.with_state(Chain::Para(PARA_ID), || {
Expand Down
Loading