Skip to content

Commit

Permalink
Added XCMP into Astar runtime (#636)
Browse files Browse the repository at this point in the history
* Added XCMP into Astar runtime

* Fix runtime-benchmarking build
  • Loading branch information
akru authored May 10, 2022
1 parent 4ce03ee commit ecdca1c
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 23 deletions.
20 changes: 15 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/collator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astar-collator"
version = "3.24.2"
version = "3.25.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
description = "Astar collator implementation in Rust."
build = "build.rs"
Expand Down
28 changes: 25 additions & 3 deletions runtime/astar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astar-runtime"
version = "3.24.2"
version = "3.25.0"
authors = ["Stake Technologies <devops@stake.co.jp>"]
edition = "2021"
build = "build.rs"
Expand Down Expand Up @@ -30,6 +30,7 @@ sp-version = { git = "https://github.com/paritytech/substrate", branch = "polkad

# frame dependencies
frame-executive = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.19", default-features = false }
pallet-assets = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.19", default-features = false }
pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.19", default-features = false }
pallet-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.19", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.19", default-features = false }
Expand Down Expand Up @@ -61,11 +62,19 @@ cumulus-primitives-utility = { git = "https://github.com/paritytech/cumulus", br
cumulus-primitives-timestamp = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }
cumulus-pallet-parachain-system = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }
cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }
cumulus-pallet-dmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }
cumulus-pallet-xcmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }
cumulus-pallet-xcm = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }
parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.19", default-features = false }

# polkadot dependencies
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.19" }
polkadot-runtime-common = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.19" }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }
polkadot-runtime-common = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.19" }

# benchmarking
hex-literal = { version = '0.3.1', optional = true }
Expand All @@ -85,6 +94,7 @@ pallet-vesting = { git = "https://github.com/AstarNetwork/astar-frame", branch =
pallet-dapps-staking = { git = "https://github.com/AstarNetwork/astar-frame", branch = "pallet-dapps-staking-3.2.3/polkadot-v0.9.19", default-features = false }
pallet-precompile-dapps-staking = { git = "https://github.com/AstarNetwork/astar-frame", branch = "pallet-dapps-staking-3.2.3/polkadot-v0.9.19", default-features = false }
pallet-evm-precompile-sr25519 = { git = "https://github.com/AstarNetwork/astar-frame", branch = "polkadot-v0.9.19", default-features = false }
pallet-evm-precompile-assets-erc20 = { git = "https://github.com/AstarNetwork/astar-frame", branch = "polkadot-v0.9.19", default-features = false }

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.19" }
Expand All @@ -110,6 +120,7 @@ std = [
"frame-executive/std",
"frame-system/std",
"frame-system-rpc-runtime-api/std",
"pallet-assets/std",
"pallet-authorship/std",
"pallet-aura/std",
"pallet-balances/std",
Expand All @@ -118,6 +129,7 @@ std = [
"pallet-dapps-staking/std",
"pallet-precompile-dapps-staking/std",
"pallet-evm-precompile-sr25519/std",
"pallet-evm-precompile-assets-erc20/std",
"pallet-base-fee/std",
"pallet-ethereum/std",
"pallet-evm/std",
Expand All @@ -139,16 +151,24 @@ std = [
"pallet-sudo/std",
"pallet-transaction-payment/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-xcm/std",
"parachain-info/std",
"polkadot-parachain/std",
"polkadot-primitives/std",
"polkadot-runtime-common/std",
"cumulus-primitives-core/std",
"cumulus-primitives-utility/std",
"cumulus-primitives-timestamp/std",
"cumulus-pallet-parachain-system/std",
"cumulus-pallet-aura-ext/std",
"cumulus-pallet-dmp-queue/std",
"cumulus-pallet-xcmp-queue/std",
"cumulus-pallet-xcm/std",
"pallet-collator-selection/std",
"frame-benchmarking/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
]
runtime-benchmarks = [
'frame-benchmarking',
Expand All @@ -163,6 +183,8 @@ runtime-benchmarks = [
"pallet-ethereum/runtime-benchmarks",
"pallet-dapps-staking/runtime-benchmarks",
"pallet-block-reward/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
try-runtime = [
"frame-try-runtime",
Expand Down
87 changes: 78 additions & 9 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
pub use sp_runtime::BuildStorage;

mod precompiles;
pub use precompiles::AstarNetworkPrecompiles;
pub use precompiles::{AstarNetworkPrecompiles, ASSET_PRECOMPILE_ADDRESS_PREFIX};
pub type Precompiles = AstarNetworkPrecompiles<Runtime>;

mod weights;
mod xcm_config;

/// Constant values used within the runtime.
pub const MILLIASTR: Balance = 1_000_000_000_000_000;
Expand Down Expand Up @@ -87,7 +88,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("astar"),
impl_name: create_runtime_str!("astar"),
authoring_version: 1,
spec_version: 14,
spec_version: 15,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -147,9 +148,14 @@ pub struct BaseFilter;
impl Contains<Call> for BaseFilter {
fn contains(call: &Call) -> bool {
match call {
// Filter permission-less assets creation/destroying
Call::Assets(method) => match method {
pallet_assets::Call::create { id, .. } => *id < u32::max_value().into(),
pallet_assets::Call::destroy { id, .. } => *id < u32::max_value().into(),
_ => true,
},
// These modules are not allowed to be called by transactions:
// To leave collator just shutdown it, next session funds will be released
Call::CollatorSelection(pallet_collator_selection::Call::leave_intent { .. }) => false,
// Other modules should works:
_ => true,
}
Expand Down Expand Up @@ -338,19 +344,19 @@ impl pallet_utility::Config for Runtime {
}

parameter_types! {
// We do anything the parent chain tells us in this runtime.
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT / 2;
pub const ReservedXcmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT / 4;
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT / 4;
}

impl cumulus_pallet_parachain_system::Config for Runtime {
type Event = Event;
type OnSystemEvent = ();
type SelfParaId = parachain_info::Pallet<Runtime>;
type OutboundXcmpMessageSource = ();
type DmpMessageHandler = ();
type OutboundXcmpMessageSource = XcmpQueue;
type DmpMessageHandler = DmpQueue;
type ReservedDmpWeight = ReservedDmpWeight;
type XcmpMessageHandler = ();
type ReservedXcmpWeight = ();
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
}

impl parachain_info::Config for Runtime {}
Expand Down Expand Up @@ -486,6 +492,63 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = ();
}

/// Id used for identifying assets.
///
/// AssetId allocation:
/// [1; 2^32-1] Custom user assets (permissionless)
/// [2^32; 2^64-1] Statemine assets (simple map)
/// [2^64; 2^128-1] Ecosystem assets
/// 2^128-1 Relay chain token (KSM)
pub type AssetId = u128;

impl pallet_evm_precompile_assets_erc20::AddressToAssetId<AssetId> for Runtime {
fn address_to_asset_id(address: H160) -> Option<AssetId> {
let mut data = [0u8; 16];
let address_bytes: [u8; 20] = address.into();
if ASSET_PRECOMPILE_ADDRESS_PREFIX.eq(&address_bytes[0..4]) {
data.copy_from_slice(&address_bytes[4..20]);
Some(u128::from_be_bytes(data))
} else {
None
}
}

fn asset_id_to_address(asset_id: AssetId) -> H160 {
let mut data = [0u8; 20];
data[0..4].copy_from_slice(ASSET_PRECOMPILE_ADDRESS_PREFIX);
data[4..20].copy_from_slice(&asset_id.to_be_bytes());
H160::from(data)
}
}

parameter_types! {
pub const AssetDeposit: Balance = 1_000_000;
pub const ApprovalDeposit: Balance = 1_000_000;
pub const AssetsStringLimit: u32 = 50;
/// Key = 32 bytes, Value = 36 bytes (32+1+1+1+1)
// https://github.com/paritytech/substrate/blob/069917b/frame/assets/src/lib.rs#L257L271
pub const MetadataDepositBase: Balance = deposit(1, 68);
pub const MetadataDepositPerByte: Balance = deposit(0, 1);
pub const AssetAccountDeposit: Balance = deposit(1, 18);
}

impl pallet_assets::Config for Runtime {
type Event = Event;
type Balance = Balance;
type AssetId = AssetId;
type Currency = Balances;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type AssetDeposit = AssetDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type AssetAccountDeposit = AssetAccountDeposit;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = AssetsStringLimit;
type Freezer = ();
type Extra = ();
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const MinVestedTransfer: Balance = 1 * ASTR;
}
Expand Down Expand Up @@ -689,13 +752,19 @@ construct_runtime!(
Vesting: pallet_vesting::{Pallet, Call, Storage, Config<T>, Event<T>} = 32,
DappsStaking: pallet_dapps_staking::{Pallet, Call, Storage, Event<T>} = 34,
BlockReward: pallet_block_reward::{Pallet, Call, Storage, Config, Event<T>} = 35,
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>} = 36,

Authorship: pallet_authorship::{Pallet, Call, Storage, Inherent} = 40,
CollatorSelection: pallet_collator_selection::{Pallet, Call, Storage, Event<T>, Config<T>} = 41,
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 42,
Aura: pallet_aura::{Pallet, Storage, Config<T>} = 43,
AuraExt: cumulus_pallet_aura_ext::{Pallet, Storage, Config} = 44,

XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event<T>} = 50,
PolkadotXcm: pallet_xcm::{Pallet, Call, Storage, Event<T>, Origin} = 51,
CumulusXcm: cumulus_pallet_xcm::{Pallet, Event<T>, Origin} = 52,
DmpQueue: cumulus_pallet_dmp_queue::{Pallet, Call, Storage, Event<T>} = 53,

EVM: pallet_evm::{Pallet, Config, Call, Storage, Event<T>} = 60,
Ethereum: pallet_ethereum::{Pallet, Call, Storage, Event, Origin, Config} = 61,
EthCall: pallet_custom_signatures::{Pallet, Call, Event<T>, ValidateUnsigned} = 62,
Expand Down
14 changes: 12 additions & 2 deletions runtime/astar/src/precompiles.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! The Astar Network EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.

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

use pallet_evm::{Context, Precompile, PrecompileResult, PrecompileSet};
use pallet_evm_precompile_assets_erc20::Erc20AssetsPrecompileSet;
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use pallet_evm_precompile_dispatch::Dispatch;
Expand All @@ -16,6 +15,10 @@ use sp_core::H160;
use sp_std::fmt::Debug;
use sp_std::marker::PhantomData;

/// The asset precompile address prefix. Addresses that match against this prefix will be routed
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];

/// The PrecompileSet installed in the Astar runtime.
#[derive(Debug, Clone, Copy)]
pub struct AstarNetworkPrecompiles<R>(PhantomData<R>);
Expand All @@ -42,6 +45,7 @@ where
R: pallet_evm::Config,
Dispatch<R>: Precompile,
DappsStakingWrapper<R>: Precompile,
Erc20AssetsPrecompileSet<R>: PrecompileSet,
{
fn execute(
&self,
Expand Down Expand Up @@ -84,13 +88,19 @@ where
a if a == hash(20482) => Some(Sr25519Precompile::<R>::execute(
input, target_gas, context, is_static,
)),
// If the address matches asset prefix, the we route through the asset precompile set
a if &a.to_fixed_bytes()[0..4] == ASSET_PRECOMPILE_ADDRESS_PREFIX => {
Erc20AssetsPrecompileSet::<R>::new()
.execute(address, input, target_gas, context, is_static)
}
// Default
_ => None,
}
}

fn is_precompile(&self, address: H160) -> bool {
Self::used_addresses().find(|x| x == &address).is_some()
|| Erc20AssetsPrecompileSet::<R>::new().is_precompile(address)
}
}

Expand Down
Loading

0 comments on commit ecdca1c

Please sign in to comment.