-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple relay pallet indices from runtimes (#2131)
* 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
1 parent
c26c59d
commit fa45df8
Showing
42 changed files
with
2,162 additions
and
735 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.