Skip to content

Commit

Permalink
Decouple relay pallet indices from runtimes (#2131)
Browse files Browse the repository at this point in the history
* init with storage item for indices but did not include call structure in the pallet

* start testing discussed approach

* messy encoding but test does pass will clean code

* cleaner by making storage value not optional

* move encoder impls to pallet so CommonEncoder is no longer necessary

* impl Emcoder traits for pallet xcm transactor to use it as CommonEncoder

* common migration such that configured input will be hardcoded value with tests to verify correctnesss in relay encoder todo

* clean

* clean

* save some tests may need to partially revert dep mess

* westend test common encoder impl

* refactor to DRY and init kusama encoder tests

* finish relay encoder backwards compat test changes and use tdd to resolve storage indices for each network

* update xcm runtime tests

* clean

* update default encoder impl for cancel open request hrmp

* toml sort

* editorconfig

* remove HrmpEncoder associated type and MockHrmpEncoder types

* fix pallet unit tests

* clean

* replace encoder in relay encoder precompile with pallet xcm transactor encoder impls based on stored indices

* rm old comment

* try impl without having local compilation errors report output in comment

* try imports for relay encoder

* try fix

* try fix

* fix

* try fix

* try fix

* fmt const var name

* try fix

* try

* fix hacky encoding of accountid lookups

* remove runtime dep on hardcoded relay encoder crate and move relay indices to each runtime

* add relay indices storage item to xcm transactor

* configure moonbase genesis for TS tests try

* cargo lock

* fix

* DRY for encode account id lookups

* Revert "DRY for encode account id lookups"

This reverts commit 5d6058f.

* fix

* Fixes imports

* typo

* Fix formatting and clippy issues

* Fix test failures

* Move XXXEncodeCall impls to the new encode.rs file

* use WESTEND_RELAY_INDICES in the test

* Move relay indices const definitions to moonbeam_relay_encoder

* Fix for the breaking change from polkadot v1.1.0

* Revert "use WESTEND_RELAY_INDICES in the test"

This reverts commit 21d87d1.

* Update Cargo.lock

* Flatten RelayChainIndices struct

* Fix file doc

* Fix file doc

* Flatten RelayChainIndices struct

* Fix unit test build error

---------

Co-authored-by: crystalin <[email protected]>
Co-authored-by: Alan Sapede <[email protected]>
Co-authored-by: Cem Eliguzel <[email protected]>
  • Loading branch information
4 people authored Oct 20, 2023
1 parent c26c59d commit fa45df8
Show file tree
Hide file tree
Showing 42 changed files with 2,162 additions and 735 deletions.
828 changes: 412 additions & 416 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ members = [
"runtime/moonbase",
"runtime/moonbeam",
"runtime/moonriver",
"runtime/relay-encoder",
"runtime/summarize-precompile-checks",
]
resolver = "2"
Expand Down
2 changes: 2 additions & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ moonbeam-cli-opt = { workspace = true }
moonbeam-core-primitives = { workspace = true }
moonbeam-finality-rpc = { workspace = true }
moonbeam-primitives-ext = { workspace = true }
moonbeam-relay-encoder = { workspace = true }
moonbeam-rpc-debug = { workspace = true }
moonbeam-rpc-primitives-debug = { workspace = true }
moonbeam-rpc-primitives-txpool = { workspace = true }
Expand All @@ -42,6 +43,7 @@ moonbeam-rpc-txpool = { workspace = true }
moonbeam-runtime-common = { workspace = true }
moonbeam-vrf = { workspace = true }
pallet-parachain-staking = { workspace = true }
pallet-xcm-transactor = { workspace = true }
session-keys-primitives = { workspace = true }

# Moonbeam runtimes
Expand Down
6 changes: 5 additions & 1 deletion node/service/src/chain_spec/moonbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use moonbase_runtime::{
OpenTechCommitteeCollectiveConfig, ParachainInfoConfig, ParachainStakingConfig,
PolkadotXcmConfig, Precompiles, Range, RuntimeGenesisConfig, SudoConfig, SystemConfig,
TechCommitteeCollectiveConfig, TransactionPaymentConfig, TreasuryCouncilCollectiveConfig,
HOURS, WASM_BINARY,
XcmTransactorConfig, HOURS, WASM_BINARY,
};
use nimbus_primitives::NimbusId;
use pallet_transaction_payment::Multiplier;
Expand Down Expand Up @@ -357,6 +357,10 @@ pub fn testnet_genesis(
multiplier: Multiplier::from(8u128),
..Default::default()
},
xcm_transactor: XcmTransactorConfig {
relay_indices: moonbeam_relay_encoder::westend::WESTEND_RELAY_INDICES,
..Default::default()
},
}
}

Expand Down
3 changes: 1 addition & 2 deletions pallets/xcm-transactor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "0.2.0"

[dependencies]
log = { version = "0.4", default-features = false }
serde = { version = "1.0.124", optional = true }
serde = { workspace = true }

# Moonbeam
xcm-primitives = { workspace = true }
Expand Down Expand Up @@ -49,7 +49,6 @@ std = [
"frame-system/std",
"orml-traits/std",
"parity-scale-codec/std",
"serde",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
228 changes: 228 additions & 0 deletions pallets/xcm-transactor/src/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! # Encode Module
//!
//! ## Overview
//!
//! Module to provide `StakeEncodeCall`, `HrmpEncodeCall` and `UtilityEncodeCall` implementations
//! for the Xcm Transactor pallet
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::pallet_prelude::*;
use sp_runtime::traits::{AccountIdLookup, StaticLookup};
use sp_std::prelude::*;
use xcm_primitives::{
AvailableStakeCalls, HrmpAvailableCalls, HrmpEncodeCall, StakeEncodeCall,
UtilityAvailableCalls, UtilityEncodeCall,
};

pub use crate::pallet::*;

pub use crate::weights::WeightInfo;

impl<T: Config> UtilityEncodeCall for Pallet<T> {
fn encode_call(self, call: UtilityAvailableCalls) -> Vec<u8> {
match call {
UtilityAvailableCalls::AsDerivative(a, b) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().utility);
// call index
encoded_call.push(RelayIndices::<T>::get().as_derivative);
// encoded argument
encoded_call.append(&mut a.encode());
encoded_call.append(&mut b.clone());
encoded_call
}
}
}
}

impl<T: Config> HrmpEncodeCall for Pallet<T> {
fn hrmp_encode_call(call: HrmpAvailableCalls) -> Result<Vec<u8>, xcm::latest::Error> {
match call {
HrmpAvailableCalls::InitOpenChannel(a, b, c) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().hrmp);
// call index
encoded_call.push(RelayIndices::<T>::get().init_open_channel);
// encoded arguments
encoded_call.append(&mut a.encode());
encoded_call.append(&mut b.encode());
encoded_call.append(&mut c.encode());
Ok(encoded_call)
}
HrmpAvailableCalls::AcceptOpenChannel(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().hrmp);
// call index
encoded_call.push(RelayIndices::<T>::get().accept_open_channel);
// encoded argument
encoded_call.append(&mut a.encode());
Ok(encoded_call)
}
HrmpAvailableCalls::CloseChannel(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().hrmp);
// call index
encoded_call.push(RelayIndices::<T>::get().close_channel);
// encoded argument
encoded_call.append(&mut a.encode());
Ok(encoded_call)
}
HrmpAvailableCalls::CancelOpenRequest(a, b) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().hrmp);
// call index
encoded_call.push(RelayIndices::<T>::get().cancel_open_request);
// encoded argument
encoded_call.append(&mut a.encode());
encoded_call.append(&mut b.encode());
Ok(encoded_call)
}
}
}
}

fn encode_compact_arg<T: parity_scale_codec::HasCompact>(input: T) -> Vec<u8> {
#[derive(Encode)]
struct CompactWrapper<T: parity_scale_codec::HasCompact> {
#[codec(compact)]
input: T,
}
CompactWrapper { input }.encode()
}

impl<T: Config> StakeEncodeCall for Pallet<T> {
fn encode_call(call: AvailableStakeCalls) -> Vec<u8> {
match call {
AvailableStakeCalls::Bond(b, c) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().bond);
// encoded arguments
encoded_call.append(&mut encode_compact_arg(b));
encoded_call.append(&mut c.encode());
encoded_call
}

AvailableStakeCalls::BondExtra(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().bond_extra);
// encoded argument
encoded_call.append(&mut encode_compact_arg(a));
encoded_call
}

AvailableStakeCalls::Unbond(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().unbond);
// encoded argument
encoded_call.append(&mut encode_compact_arg(a));
encoded_call
}

AvailableStakeCalls::WithdrawUnbonded(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().withdraw_unbonded);
// encoded argument
encoded_call.append(&mut a.encode());
encoded_call
}

AvailableStakeCalls::Validate(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().validate);
// encoded argument
encoded_call.append(&mut a.encode());
encoded_call
}

AvailableStakeCalls::Chill => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().chill);
encoded_call
}

AvailableStakeCalls::SetPayee(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().set_payee);
// encoded argument
encoded_call.append(&mut a.encode());
encoded_call
}

AvailableStakeCalls::SetController => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().set_controller);
encoded_call
}

AvailableStakeCalls::Rebond(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().rebond);
// encoded argument
encoded_call.append(&mut encode_compact_arg(a));
encoded_call
}

AvailableStakeCalls::Nominate(a) => {
let mut encoded_call: Vec<u8> = Vec::new();
// pallet index
encoded_call.push(RelayIndices::<T>::get().staking);
// call index
encoded_call.push(RelayIndices::<T>::get().nominate);
let nominated: Vec<
<AccountIdLookup<sp_runtime::AccountId32, ()> as StaticLookup>::Source,
> = a.iter().map(|add| (*add).clone().into()).collect();
encoded_call.append(&mut nominated.encode());
encoded_call
}
}
}
}
Loading

0 comments on commit fa45df8

Please sign in to comment.