Use indirect runtime calls for RialtoParachain (#1753)

* Use indirect runtime calls for RialtoParachain

* Code review fixes
This commit is contained in:
Serban Iorga
2023-01-10 17:05:00 +02:00
committed by Bastian Köcher
parent e0e84b734b
commit 444dbe7173
20 changed files with 168 additions and 95 deletions
@@ -16,6 +16,8 @@
//! Types used to connect to the Rialto-Substrate chain.
pub mod runtime_wrapper;
use bp_messages::MessageNonce;
use codec::Encode;
use relay_substrate_client::{
@@ -23,12 +25,13 @@ use relay_substrate_client::{
SignParam, UnderlyingChainProvider, UnsignedTransaction,
};
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount, MultiAddress};
use std::time::Duration;
/// Rialto header id.
pub type HeaderId =
relay_utils::HeaderId<rialto_parachain_runtime::Hash, rialto_parachain_runtime::BlockNumber>;
pub use runtime_wrapper as runtime;
/// The address format for describing accounts.
pub type Address = MultiAddress<bp_rialto_parachain::AccountId, ()>;
/// Rialto parachain definition
#[derive(Debug, Clone, Copy)]
@@ -46,18 +49,13 @@ impl Chain for RialtoParachain {
bp_rialto_parachain::BEST_FINALIZED_RIALTO_PARACHAIN_HEADER_METHOD;
const AVERAGE_BLOCK_INTERVAL: Duration = Duration::from_secs(5);
type SignedBlock = rialto_parachain_runtime::SignedBlock;
type Call = rialto_parachain_runtime::RuntimeCall;
type SignedBlock = bp_polkadot_core::SignedBlock;
type Call = runtime::Call;
}
impl ChainWithBalances for RialtoParachain {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
use frame_support::storage::generator::StorageMap;
StorageKey(
frame_system::Account::<rialto_parachain_runtime::Runtime>::storage_map_final_key(
account_id,
),
)
bp_polkadot_core::AccountInfoStorageMapKeyProvider::final_key(account_id)
}
}
@@ -77,45 +75,30 @@ impl ChainWithMessages for RialtoParachain {
impl ChainWithTransactions for RialtoParachain {
type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction = rialto_parachain_runtime::UncheckedExtrinsic;
type SignedTransaction = bp_polkadot_core::UncheckedExtrinsic<Self::Call>;
fn sign_transaction(
param: SignParam<Self>,
unsigned: UnsignedTransaction<Self>,
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::from_raw(
let raw_payload = SignedPayload::new(
unsigned.call,
(
frame_system::CheckNonZeroSender::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckSpecVersion::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckTxVersion::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckGenesis::<rialto_parachain_runtime::Runtime>::new(),
frame_system::CheckEra::<rialto_parachain_runtime::Runtime>::from(
unsigned.era.frame_era(),
),
frame_system::CheckNonce::<rialto_parachain_runtime::Runtime>::from(unsigned.nonce),
frame_system::CheckWeight::<rialto_parachain_runtime::Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<
rialto_parachain_runtime::Runtime,
>::from(unsigned.tip),
),
(
(),
bp_polkadot_core::SignedExtensions::new(
param.spec_version,
param.transaction_version,
unsigned.era,
param.genesis_hash,
unsigned.era.signed_payload(param.genesis_hash),
(),
(),
(),
unsigned.nonce,
unsigned.tip,
),
);
)?;
let signature = raw_payload.using_encoded(|payload| param.signer.sign(payload));
let signer: sp_runtime::MultiSigner = param.signer.public().into();
let (call, extra, _) = raw_payload.deconstruct();
Ok(rialto_parachain_runtime::UncheckedExtrinsic::new_signed(
call.into_decoded()?,
Ok(Self::SignedTransaction::new_signed(
call,
signer.into_account().into(),
signature.into(),
extra,
@@ -129,14 +112,13 @@ impl ChainWithTransactions for RialtoParachain {
fn is_signed_by(signer: &Self::AccountKeyPair, tx: &Self::SignedTransaction) -> bool {
tx.signature
.as_ref()
.map(|(address, _, _)| {
*address == rialto_parachain_runtime::Address::Id(signer.public().into())
})
.map(|(address, _, _)| *address == Address::Id(signer.public().into()))
.unwrap_or(false)
}
fn parse_transaction(_tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
None
fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
let extra = &tx.signature.as_ref()?.2;
Some(UnsignedTransaction::new(tx.function, extra.nonce()).tip(extra.tip()))
}
}
@@ -144,4 +126,4 @@ impl ChainWithTransactions for RialtoParachain {
pub type SigningParams = sp_core::sr25519::Pair;
/// RialtoParachain header type used in headers sync.
pub type SyncHeader = relay_substrate_client::SyncHeader<rialto_parachain_runtime::Header>;
pub type SyncHeader = relay_substrate_client::SyncHeader<bp_rialto_parachain::Header>;
@@ -0,0 +1,57 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common 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.
// Parity Bridges Common 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 Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! Types that are specific to the `RialtoParachain` runtime. Normally we could use the full
//! `RialtoParachain` runtime here, since it is constructed in this repo and we have access to it.
//! However we use a wrapped runtime instead in order to test the indirect runtime calls
//! functionality.
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use bp_header_chain::BridgeGrandpaCallOf;
use bridge_runtime_common::messages::BridgeMessagesCallOf;
use relay_substrate_client::calls::{SudoCall, XcmCall};
// The indirect pallet call used to sync `Millau` GRANDPA finality to `RialtoParachain`.
pub type BridgeMillauGrandpaCall = BridgeGrandpaCallOf<bp_millau::Millau>;
// The indirect pallet call used to sync `Millau` messages to `RialtoParachain`.
pub type BridgeMillauMessagesCall = BridgeMessagesCallOf<bp_millau::Millau>;
/// `RialtoParachain` Runtime `Call` enum.
///
/// The enum represents a subset of possible `Call`s we can send to `RialtoParachain` chain.
///
/// All entries here (like pretty much in the entire file) must be kept in sync with
/// `RialtoParachain` `construct_runtime`, so that we maintain SCALE-compatibility.
#[allow(clippy::large_enum_variant)]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub enum Call {
/// `Sudo` pallet.
#[codec(index = 2)]
Sudo(SudoCall<Call>),
/// `Xcm` pallet.
#[codec(index = 51)]
PolkadotXcm(XcmCall),
/// Millau GRANDPA bridge pallet.
#[codec(index = 55)]
BridgeMillauGrandpa(BridgeMillauGrandpaCall),
/// Millau messages bridge pallet.
#[codec(index = 56)]
BridgeMillauMessages(BridgeMillauMessagesCall),
}