Skip to content

Commit

Permalink
Add pallet-xcm precompile to Moonbeam and Moonriver (`transfer_assets…
Browse files Browse the repository at this point in the history
…_using_type_and_then` extrinsic support) (#2992)

* add pallet-xcm precompile to MB and MR with TS tests

* fmt and fix rust tests

* allow transfer_assets_using_type_and_then in filter

* use proper moonkit branch
  • Loading branch information
Agusrodri authored Oct 9, 2024
1 parent 168350b commit f9cde1f
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 51 deletions.
35 changes: 20 additions & 15 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions runtime/moonbeam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ nimbus-primitives = { workspace = true }
pallet-async-backing = { workspace = true }
pallet-author-inherent = { workspace = true }
pallet-author-slot-filter = { workspace = true }
pallet-evm-precompile-xcm = { workspace = true }
pallet-relay-storage-roots = { workspace = true }

# Benchmarking
Expand Down Expand Up @@ -257,6 +258,7 @@ std = [
"pallet-evm-precompile-referenda/std",
"pallet-evm-precompile-relay-encoder/std",
"pallet-evm-precompile-relay-verifier/std",
"pallet-evm-precompile-xcm/std",
"pallet-evm-precompile-xcm-transactor/std",
"pallet-evm-precompile-xcm-utils/std",
"pallet-evm-precompile-xtokens/std",
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ impl Contains<RuntimeCall> for NormalFilter {
// is populated at genesis
RuntimeCall::PolkadotXcm(method) => match method {
pallet_xcm::Call::force_default_xcm_version { .. } => true,
pallet_xcm::Call::transfer_assets_using_type_and_then { .. } => true,
_ => false,
},
// We filter anonymous proxy as they make "reserve" inconsistent
Expand Down
35 changes: 31 additions & 4 deletions runtime/moonbeam/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::{
asset_config::ForeignAssetInstance, xcm_config::XcmExecutorConfig, OpenTechCommitteeInstance,
Runtime, TreasuryCouncilInstance,
asset_config::ForeignAssetInstance,
xcm_config::{AssetType, XcmExecutorConfig},
AccountId, AssetId, AssetManager, Balances, Erc20XcmBridge, OpenTechCommitteeInstance, Runtime,
TreasuryCouncilInstance, H160,
};
use crate::{AssetId, H160};
use frame_support::parameter_types;
use moonbeam_runtime_common::weights as moonbeam_weights;
use moonkit_xcm_primitives::AccountIdAssetIdConversion;
use moonkit_xcm_primitives::{
location_matcher::{Erc20PalletMatcher, ForeignAssetMatcher, SingleAddressMatcher},
AccountIdAssetIdConversion,
};
use pallet_evm_precompile_author_mapping::AuthorMappingPrecompile;
use pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata};
use pallet_evm_precompile_batch::BatchPrecompile;
Expand All @@ -45,6 +49,7 @@ use pallet_evm_precompile_relay_encoder::RelayEncoderPrecompile;
use pallet_evm_precompile_relay_verifier::RelayDataVerifierPrecompile;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use pallet_evm_precompile_xcm::PalletXcmPrecompile;
use pallet_evm_precompile_xcm_transactor::{
v1::XcmTransactorPrecompileV1, v2::XcmTransactorPrecompileV2, v3::XcmTransactorPrecompileV3,
};
Expand All @@ -54,6 +59,7 @@ use pallet_evm_precompileset_assets_erc20::Erc20AssetsPrecompileSet;
use pallet_precompile_benchmarks::WeightInfo;
use precompile_utils::precompile_set::*;
use sp_std::prelude::*;
use xcm_primitives::AsAssetType;

parameter_types! {
pub P256VerifyWeight: frame_support::weights::Weight =
Expand Down Expand Up @@ -93,13 +99,29 @@ pub const FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
/// to Erc20AssetsPrecompileSet being marked as local
pub const LOCAL_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8, 255u8, 255u8, 254u8];

/// Const to identify ERC20_BALANCES_PRECOMPILE address
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;

parameter_types! {
pub ForeignAssetPrefix: &'static [u8] = FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX;
pub LocalAssetPrefix: &'static [u8] = LOCAL_ASSET_PRECOMPILE_ADDRESS_PREFIX;
}

type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);

// Pallet-xcm precompile types.
// Type that converts AssetId into Location
type AssetIdToLocationManager = AsAssetType<AssetId, AssetType, AssetManager>;

// The pallet-balances address is identified by ERC20_BALANCES_PRECOMPILE const
type SingleAddressMatch = SingleAddressMatcher<AccountId, ERC20_BALANCES_PRECOMPILE, Balances>;

// Type that matches an AccountId with a foreign asset address (if any)
type ForeignAssetMatch = ForeignAssetMatcher<AccountId, AssetId, Runtime, AssetIdToLocationManager>;

// Erc20XcmBridge pallet is used to match ERC20s
type Erc20Match = Erc20PalletMatcher<AccountId, Erc20XcmBridge>;

#[precompile_utils::precompile_name_from_address]
type MoonbeamPrecompilesAt<R> = (
// Ethereum precompiles:
Expand Down Expand Up @@ -249,6 +271,11 @@ type MoonbeamPrecompilesAt<R> = (
RelayDataVerifierPrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
PrecompileAt<
AddressU64<2074>,
PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
(CallableByContract, CallableByPrecompile),
>,
);

pub struct DisabledLocalAssets<Runtime>(sp_std::marker::PhantomData<Runtime>);
Expand Down
2 changes: 1 addition & 1 deletion runtime/moonbeam/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,7 +2455,7 @@ fn precompile_existence() {
let precompile_addresses: std::collections::BTreeSet<_> = vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 256, 1024, 1025, 1026, 2048, 2049, 2050, 2051, 2052, 2053,
2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067,
2068, 2069, 2070, 2071, 2072, 2073,
2068, 2069, 2070, 2071, 2072, 2073, 2074,
]
.into_iter()
.map(H160::from_low_u64_be)
Expand Down
2 changes: 2 additions & 0 deletions runtime/moonriver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ nimbus-primitives = { workspace = true }
pallet-async-backing = { workspace = true }
pallet-author-inherent = { workspace = true }
pallet-author-slot-filter = { workspace = true }
pallet-evm-precompile-xcm = { workspace = true }
pallet-relay-storage-roots = { workspace = true }

# Benchmarking
Expand Down Expand Up @@ -258,6 +259,7 @@ std = [
"pallet-evm-precompile-referenda/std",
"pallet-evm-precompile-relay-encoder/std",
"pallet-evm-precompile-relay-verifier/std",
"pallet-evm-precompile-xcm/std",
"pallet-evm-precompile-xcm-transactor/std",
"pallet-evm-precompile-xcm-utils/std",
"pallet-evm-precompile-xtokens/std",
Expand Down
1 change: 1 addition & 0 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ impl Contains<RuntimeCall> for NormalFilter {
// is populated at genesis
RuntimeCall::PolkadotXcm(method) => match method {
pallet_xcm::Call::force_default_xcm_version { .. } => true,
pallet_xcm::Call::transfer_assets_using_type_and_then { .. } => true,
_ => false,
},
// We filter anonymous proxy as they make "reserve" inconsistent
Expand Down
32 changes: 30 additions & 2 deletions runtime/moonriver/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::{
asset_config::ForeignAssetInstance, xcm_config::XcmExecutorConfig, OpenTechCommitteeInstance,
Runtime, TreasuryCouncilInstance,
asset_config::ForeignAssetInstance,
xcm_config::{AssetType, XcmExecutorConfig},
AccountId, AssetId, AssetManager, Balances, Erc20XcmBridge, OpenTechCommitteeInstance, Runtime,
TreasuryCouncilInstance,
};
use frame_support::parameter_types;
use moonbeam_runtime_common::weights as moonriver_weights;
use moonkit_xcm_primitives::location_matcher::{
Erc20PalletMatcher, ForeignAssetMatcher, SingleAddressMatcher,
};
use pallet_evm_precompile_author_mapping::AuthorMappingPrecompile;
use pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata};
use pallet_evm_precompile_batch::BatchPrecompile;
Expand All @@ -43,6 +48,7 @@ use pallet_evm_precompile_relay_encoder::RelayEncoderPrecompile;
use pallet_evm_precompile_relay_verifier::RelayDataVerifierPrecompile;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use pallet_evm_precompile_xcm::PalletXcmPrecompile;
use pallet_evm_precompile_xcm_transactor::{
v1::XcmTransactorPrecompileV1, v2::XcmTransactorPrecompileV2, v3::XcmTransactorPrecompileV3,
};
Expand All @@ -51,6 +57,7 @@ use pallet_evm_precompile_xtokens::XtokensPrecompile;
use pallet_evm_precompileset_assets_erc20::Erc20AssetsPrecompileSet;
use pallet_precompile_benchmarks::WeightInfo;
use precompile_utils::precompile_set::*;
use xcm_primitives::AsAssetType;

parameter_types! {
pub P256VerifyWeight: frame_support::weights::Weight =
Expand Down Expand Up @@ -87,12 +94,28 @@ impl Erc20Metadata for NativeErc20Metadata {
/// to Erc20AssetsPrecompileSet being marked as foreign
pub const FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];

/// Const to identify ERC20_BALANCES_PRECOMPILE address
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;

parameter_types! {
pub ForeignAssetPrefix: &'static [u8] = FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX;
}

type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);

// Pallet-xcm precompile types.
// Type that converts AssetId into Location
type AssetIdToLocationManager = AsAssetType<AssetId, AssetType, AssetManager>;

// The pallet-balances address is identified by ERC20_BALANCES_PRECOMPILE const
type SingleAddressMatch = SingleAddressMatcher<AccountId, ERC20_BALANCES_PRECOMPILE, Balances>;

// Type that matches an AccountId with a foreign asset address (if any)
type ForeignAssetMatch = ForeignAssetMatcher<AccountId, AssetId, Runtime, AssetIdToLocationManager>;

// Erc20XcmBridge pallet is used to match ERC20s
type Erc20Match = Erc20PalletMatcher<AccountId, Erc20XcmBridge>;

#[precompile_utils::precompile_name_from_address]
type MoonriverPrecompilesAt<R> = (
// Ethereum precompiles:
Expand Down Expand Up @@ -242,6 +265,11 @@ type MoonriverPrecompilesAt<R> = (
RelayDataVerifierPrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
PrecompileAt<
AddressU64<2074>,
PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
(CallableByContract, CallableByPrecompile),
>,
);

/// The PrecompileSet installed in the Moonriver runtime.
Expand Down
2 changes: 1 addition & 1 deletion runtime/moonriver/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2367,7 +2367,7 @@ fn precompile_existence() {
let precompile_addresses: std::collections::BTreeSet<_> = vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 256, 1024, 1025, 1026, 2048, 2049, 2050, 2051, 2052, 2053,
2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067,
2068, 2069, 2070, 2071, 2072, 2073,
2068, 2069, 2070, 2071, 2072, 2073, 2074,
]
.into_iter()
.map(H160::from_low_u64_be)
Expand Down
Loading

0 comments on commit f9cde1f

Please sign in to comment.