From 6c826a6c3419160278bec94778deec040f5eef88 Mon Sep 17 00:00:00 2001 From: Serban Iorga Date: Mon, 9 Jan 2023 15:29:53 +0200 Subject: [PATCH] Deduplicate pallet call structs used for indirect runtime calls (#1744) * Small changes * Define generic bridge pallets call structs * polkadot-core SignedExtension simplifications - we don't seem to need to pass the Call as a generic param - we can use codec(skip) instead of implementing Encode and Decode * Split BridgeHubRococo and BridgeHubWococo calls * code review fixes --- Cargo.lock | 17 +- bin/runtime-common/src/messages.rs | 7 + modules/grandpa/src/lib.rs | 29 +++ modules/messages/src/lib.rs | 67 ++++++- modules/parachains/src/lib.rs | 23 ++- .../chain-bridge-hub-cumulus/Cargo.toml | 8 +- primitives/chain-bridge-hub-rococo/Cargo.toml | 2 - primitives/chain-bridge-hub-wococo/src/lib.rs | 4 - primitives/header-chain/src/lib.rs | 17 +- primitives/messages/src/lib.rs | 12 ++ primitives/parachains/src/lib.rs | 17 +- primitives/polkadot-core/src/lib.rs | 41 +--- primitives/runtime/src/calls.rs | 30 +++ primitives/runtime/src/lib.rs | 1 + relays/bin-substrate/Cargo.toml | 1 + .../rococo_headers_to_bridge_hub_wococo.rs | 2 +- .../wococo_parachains_to_bridge_hub_rococo.rs | 2 +- relays/bin-substrate/src/cli/init_bridge.rs | 2 +- relays/client-bridge-hub-rococo/Cargo.toml | 7 +- relays/client-bridge-hub-rococo/src/lib.rs | 6 +- .../src/runtime_wrapper.rs | 187 ++---------------- relays/client-bridge-hub-wococo/Cargo.toml | 12 +- .../src/runtime_wrapper.rs | 97 ++++++++- relays/client-substrate/src/chain.rs | 4 +- .../src/on_demand/headers.rs | 2 +- 25 files changed, 354 insertions(+), 243 deletions(-) create mode 100644 primitives/runtime/src/calls.rs diff --git a/Cargo.lock b/Cargo.lock index fcbc05b6487..43fc2dc8c2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -737,7 +737,6 @@ dependencies = [ "frame-system", "polkadot-primitives", "polkadot-runtime-constants", - "smallvec", "sp-api", "sp-std", ] @@ -751,7 +750,6 @@ dependencies = [ "bp-runtime", "frame-support", "polkadot-runtime-constants", - "smallvec", "sp-api", "sp-std", ] @@ -8463,14 +8461,15 @@ dependencies = [ "bp-bridge-hub-wococo", "bp-header-chain", "bp-messages", - "bp-polkadot-core", + "bp-parachains", + "bp-rococo", "bp-runtime", + "bp-wococo", "bridge-runtime-common", "parity-scale-codec", "relay-substrate-client", "scale-info", "sp-core", - "sp-finality-grandpa", "sp-runtime", ] @@ -8478,13 +8477,20 @@ dependencies = [ name = "relay-bridge-hub-wococo-client" version = "0.1.0" dependencies = [ + "bp-bridge-hub-rococo", "bp-bridge-hub-wococo", + "bp-header-chain", "bp-messages", + "bp-parachains", + "bp-polkadot-core", + "bp-rococo", + "bp-runtime", + "bridge-runtime-common", "parity-scale-codec", - "relay-bridge-hub-rococo-client", "relay-substrate-client", "scale-info", "sp-core", + "sp-finality-grandpa", "sp-runtime", ] @@ -11626,6 +11632,7 @@ dependencies = [ "bp-header-chain", "bp-messages", "bp-millau", + "bp-parachains", "bp-polkadot-core", "bp-rialto", "bp-rialto-parachain", diff --git a/bin/runtime-common/src/messages.rs b/bin/runtime-common/src/messages.rs index 9aced1c361a..3e69463480d 100644 --- a/bin/runtime-common/src/messages.rs +++ b/bin/runtime-common/src/messages.rs @@ -692,6 +692,13 @@ pub mod target { } } +/// The `BridgeMessagesCall` used by a chain. +pub type BridgeMessagesCallOf = bp_messages::BridgeMessagesCall< + bp_runtime::AccountIdOf, + target::FromBridgedChainMessagesProof>, + source::FromBridgedChainMessagesDeliveryProof>, +>; + #[cfg(test)] mod tests { use super::*; diff --git a/modules/grandpa/src/lib.rs b/modules/grandpa/src/lib.rs index 926594ad796..6853bb9cb3b 100644 --- a/modules/grandpa/src/lib.rs +++ b/modules/grandpa/src/lib.rs @@ -612,6 +612,7 @@ mod tests { run_test, test_header, RuntimeOrigin, TestHeader, TestNumber, TestRuntime, MAX_BRIDGED_AUTHORITIES, }; + use bp_header_chain::BridgeGrandpaCall; use bp_runtime::BasicOperatingMode; use bp_test_utils::{ authority_list, generate_owned_bridge_module_tests, make_default_justification, @@ -1160,5 +1161,33 @@ mod tests { ); } + #[test] + fn test_bridge_grandpa_call_is_correctly_defined() { + let header = test_header(0); + let init_data = InitializationData { + header: Box::new(header.clone()), + authority_list: authority_list(), + set_id: 1, + operating_mode: BasicOperatingMode::Normal, + }; + let justification = make_default_justification(&header); + + let direct_initialize_call = + Call::::initialize { init_data: init_data.clone() }; + let indirect_initialize_call = BridgeGrandpaCall::::initialize(init_data); + assert_eq!(direct_initialize_call.encode(), indirect_initialize_call.encode()); + + let direct_submit_finality_proof_call = Call::::submit_finality_proof { + finality_target: Box::new(header.clone()), + justification: justification.clone(), + }; + let indirect_submit_finality_proof_call = + BridgeGrandpaCall::::submit_finality_proof(Box::new(header), justification); + assert_eq!( + direct_submit_finality_proof_call.encode(), + indirect_submit_finality_proof_call.encode() + ); + } + generate_owned_bridge_module_tests!(BasicOperatingMode::Normal, BasicOperatingMode::Halted); } diff --git a/modules/messages/src/lib.rs b/modules/messages/src/lib.rs index f2d7f4c728c..b8d79b83b41 100644 --- a/modules/messages/src/lib.rs +++ b/modules/messages/src/lib.rs @@ -885,13 +885,13 @@ fn verify_and_decode_messages_proof::receive_messages_proof { + relayer_id_at_bridged_chain: account_id, + proof: message_proof.clone(), + messages_count: 1, + dispatch_weight: REGULAR_PAYLOAD.declared_weight, + }; + let indirect_receive_messages_proof_call = BridgeMessagesCall::< + AccountId, + TestMessagesProof, + TestMessagesDeliveryProof, + >::receive_messages_proof( + account_id, + message_proof, + 1, + REGULAR_PAYLOAD.declared_weight, + ); + assert_eq!( + direct_receive_messages_proof_call.encode(), + indirect_receive_messages_proof_call.encode() + ); + + let direct_receive_messages_delivery_proof_call = + Call::::receive_messages_delivery_proof { + proof: message_delivery_proof.clone(), + relayers_state: unrewarded_relayer_state.clone(), + }; + let indirect_receive_messages_delivery_proof_call = BridgeMessagesCall::< + AccountId, + TestMessagesProof, + TestMessagesDeliveryProof, + >::receive_messages_delivery_proof( + message_delivery_proof, + unrewarded_relayer_state, + ); + assert_eq!( + direct_receive_messages_delivery_proof_call.encode(), + indirect_receive_messages_delivery_proof_call.encode() + ); + } + generate_owned_bridge_module_tests!( MessagesOperatingMode::Basic(BasicOperatingMode::Normal), MessagesOperatingMode::Basic(BasicOperatingMode::Halted) diff --git a/modules/parachains/src/lib.rs b/modules/parachains/src/lib.rs index 3cfda001273..633b39329a0 100644 --- a/modules/parachains/src/lib.rs +++ b/modules/parachains/src/lib.rs @@ -661,7 +661,9 @@ mod tests { }; use codec::Encode; - use bp_parachains::{BestParaHeadHash, ImportedParaHeadsKeyProvider, ParasInfoKeyProvider}; + use bp_parachains::{ + BestParaHeadHash, BridgeParachainCall, ImportedParaHeadsKeyProvider, ParasInfoKeyProvider, + }; use bp_runtime::{ record_all_trie_keys, BasicOperatingMode, OwnedBridgeModuleError, StorageDoubleMapKeyProvider, StorageMapKeyProvider, @@ -1471,5 +1473,24 @@ mod tests { }); } + #[test] + fn test_bridge_parachain_call_is_correctly_defined() { + let (state_root, proof, _) = prepare_parachain_heads_proof(vec![(1, head_data(1, 0))]); + let parachains = vec![(ParaId(2), Default::default())]; + let relay_header_id = (0, test_relay_header(0, state_root).hash()); + + let direct_submit_parachain_heads_call = Call::::submit_parachain_heads { + at_relay_block: relay_header_id, + parachains: parachains.clone(), + parachain_heads_proof: proof.clone(), + }; + let indirect_submit_parachain_heads_call = + BridgeParachainCall::submit_parachain_heads(relay_header_id, parachains, proof); + assert_eq!( + direct_submit_parachain_heads_call.encode(), + indirect_submit_parachain_heads_call.encode() + ); + } + generate_owned_bridge_module_tests!(BasicOperatingMode::Normal, BasicOperatingMode::Halted); } diff --git a/primitives/chain-bridge-hub-cumulus/Cargo.toml b/primitives/chain-bridge-hub-cumulus/Cargo.toml index d413ce2ca4c..a4a8f9663df 100644 --- a/primitives/chain-bridge-hub-cumulus/Cargo.toml +++ b/primitives/chain-bridge-hub-cumulus/Cargo.toml @@ -7,8 +7,6 @@ edition = "2021" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] -smallvec = "1.10.0" - # Bridge Dependencies bp-polkadot-core = { path = "../../primitives/polkadot-core", default-features = false } @@ -23,15 +21,15 @@ sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", d sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } # Polkadot Dependencies -polkadot-primitives = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" } -polkadot-runtime-constants = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" } +polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false } +polkadot-runtime-constants = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false } [features] default = ["std"] std = [ "bp-polkadot-core/std", - "bp-messages/std", "bp-runtime/std", + "bp-messages/std", "frame-system/std", "frame-support/std", "sp-api/std", diff --git a/primitives/chain-bridge-hub-rococo/Cargo.toml b/primitives/chain-bridge-hub-rococo/Cargo.toml index 806587bb9a1..0cc57f39959 100644 --- a/primitives/chain-bridge-hub-rococo/Cargo.toml +++ b/primitives/chain-bridge-hub-rococo/Cargo.toml @@ -7,8 +7,6 @@ edition = "2021" license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] -smallvec = "1.10.0" - # Bridge Dependencies bp-bridge-hub-cumulus = { path = "../chain-bridge-hub-cumulus", default-features = false } diff --git a/primitives/chain-bridge-hub-wococo/src/lib.rs b/primitives/chain-bridge-hub-wococo/src/lib.rs index e9a170223b9..e04ca3ff228 100644 --- a/primitives/chain-bridge-hub-wococo/src/lib.rs +++ b/primitives/chain-bridge-hub-wococo/src/lib.rs @@ -16,13 +16,9 @@ //! Module with configuration which reflects BridgeHubWococo runtime setup //! (AccountId, Headers, Hashes...) -//! -//! but actually this is just reexported BridgeHubRococo stuff, because they are supposed to be -//! identical, at least uses the same parachain runtime #![cfg_attr(not(feature = "std"), no_std)] -// Re-export only what is really needed pub use bp_bridge_hub_cumulus::*; use bp_messages::*; use bp_runtime::{ diff --git a/primitives/header-chain/src/lib.rs b/primitives/header-chain/src/lib.rs index 99243663264..dffa7f7dc6e 100644 --- a/primitives/header-chain/src/lib.rs +++ b/primitives/header-chain/src/lib.rs @@ -19,7 +19,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -use bp_runtime::{BasicOperatingMode, Chain, HashOf, HasherOf, StorageProofChecker}; +use bp_runtime::{BasicOperatingMode, Chain, HashOf, HasherOf, HeaderOf, StorageProofChecker}; use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen}; use core::{clone::Clone, cmp::Eq, default::Default, fmt::Debug}; use frame_support::PalletError; @@ -169,3 +169,18 @@ impl ConsensusLogReader for GrandpaConsensusLogReader { GrandpaConsensusLogReader::::find_authorities_change(digest).is_some() } } + +/// A minimized version of `pallet-bridge-grandpa::Call` that can be used without a runtime. +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +#[allow(non_camel_case_types)] +pub enum BridgeGrandpaCall { + /// `pallet-bridge-grandpa::Call::submit_finality_proof` + #[codec(index = 0)] + submit_finality_proof(Box
, justification::GrandpaJustification
), + /// `pallet-bridge-grandpa::Call::initialize` + #[codec(index = 1)] + initialize(InitializationData
), +} + +/// The `BridgeGrandpaCall` used by a chain. +pub type BridgeGrandpaCallOf = BridgeGrandpaCall>; diff --git a/primitives/messages/src/lib.rs b/primitives/messages/src/lib.rs index 8262586567f..61d475cb46c 100644 --- a/primitives/messages/src/lib.rs +++ b/primitives/messages/src/lib.rs @@ -391,6 +391,18 @@ where relayers_rewards } +/// A minimized version of `pallet-bridge-messages::Call` that can be used without a runtime. +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +#[allow(non_camel_case_types)] +pub enum BridgeMessagesCall { + /// `pallet-bridge-messages::Call::receive_messages_proof` + #[codec(index = 2)] + receive_messages_proof(AccountId, MessagesProof, u32, Weight), + /// `pallet-bridge-messages::Call::receive_messages_delivery_proof` + #[codec(index = 3)] + receive_messages_delivery_proof(MessagesDeliveryProof, UnrewardedRelayersState), +} + #[cfg(test)] mod tests { use super::*; diff --git a/primitives/parachains/src/lib.rs b/primitives/parachains/src/lib.rs index cafe40da056..6b38d648d79 100644 --- a/primitives/parachains/src/lib.rs +++ b/primitives/parachains/src/lib.rs @@ -21,8 +21,8 @@ pub use bp_header_chain::StoredHeaderData; use bp_polkadot_core::{ - parachains::{ParaHash, ParaHead, ParaId}, - BlockNumber as RelayBlockNumber, + parachains::{ParaHash, ParaHead, ParaHeadsProof, ParaId}, + BlockNumber as RelayBlockNumber, Hash as RelayBlockHash, }; use bp_runtime::{ BlockNumberOf, Chain, HashOf, HeaderOf, Parachain, StorageDoubleMapKeyProvider, @@ -150,3 +150,16 @@ impl ParaStoredHeaderDataBuilder for C { None } } + +/// A minimized version of `pallet-bridge-parachains::Call` that can be used without a runtime. +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +#[allow(non_camel_case_types)] +pub enum BridgeParachainCall { + /// `pallet-bridge-parachains::Call::submit_parachain_heads` + #[codec(index = 0)] + submit_parachain_heads( + (RelayBlockNumber, RelayBlockHash), + Vec<(ParaId, ParaHash)>, + ParaHeadsProof, + ), +} diff --git a/primitives/polkadot-core/src/lib.rs b/primitives/polkadot-core/src/lib.rs index 9e1f4b0452f..57025f2f3f6 100644 --- a/primitives/polkadot-core/src/lib.rs +++ b/primitives/polkadot-core/src/lib.rs @@ -20,7 +20,7 @@ use bp_messages::MessageNonce; use bp_runtime::{Chain, EncodedOrDecodedCall, StorageMapKeyProvider}; use codec::Compact; use frame_support::{ - dispatch::{DispatchClass, Dispatchable}, + dispatch::DispatchClass, parameter_types, weights::{ constants::{BlockExecutionWeight, WEIGHT_PER_SECOND}, @@ -29,7 +29,7 @@ use frame_support::{ Blake2_128Concat, RuntimeDebug, }; use frame_system::limits; -use scale_info::{StaticTypeInfo, TypeInfo}; +use scale_info::TypeInfo; use sp_core::{storage::StorageKey, Hasher as HasherT}; use sp_runtime::{ generic, @@ -194,7 +194,7 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic< AccountAddress, EncodedOrDecodedCall, Signature, - SignedExtensions, + SignedExtensions, >; /// Account address, used by the Polkadot-like chain. @@ -210,34 +210,18 @@ pub type AdditionalSigned = ((), u32, u32, Hash, Hash, (), (), ()); /// A simplified version of signed extensions meant for producing signed transactions /// and signed payload in the client code. -#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)] -pub struct SignedExtensions { +#[derive(codec::Encode, codec::Decode, PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)] +pub struct SignedExtensions { encode_payload: SignedExtra, // It may be set to `None` if extensions are decoded. We are never reconstructing transactions // (and it makes no sense to do that) => decoded version of `SignedExtensions` is only used to // read fields of `encode_payload`. And when resigning transaction, we're reconstructing // `SignedExtensions` from the scratch. + #[codec(skip)] additional_signed: Option, - _data: sp_std::marker::PhantomData, } -impl codec::Encode for SignedExtensions { - fn using_encoded R>(&self, f: F) -> R { - self.encode_payload.using_encoded(f) - } -} - -impl codec::Decode for SignedExtensions { - fn decode(input: &mut I) -> Result { - SignedExtra::decode(input).map(|encode_payload| SignedExtensions { - encode_payload, - additional_signed: None, - _data: Default::default(), - }) - } -} - -impl SignedExtensions { +impl SignedExtensions { pub fn new( spec_version: u32, transaction_version: u32, @@ -267,12 +251,11 @@ impl SignedExtensions { (), (), )), - _data: Default::default(), } } } -impl SignedExtensions { +impl SignedExtensions { /// Return signer nonce, used to craft transaction. pub fn nonce(&self) -> Nonce { self.encode_payload.5.into() @@ -284,15 +267,11 @@ impl SignedExtensions { } } -impl sp_runtime::traits::SignedExtension for SignedExtensions -where - Call: codec::Codec + sp_std::fmt::Debug + Sync + Send + Clone + Eq + PartialEq + StaticTypeInfo, - Call: Dispatchable, -{ +impl sp_runtime::traits::SignedExtension for SignedExtensions { const IDENTIFIER: &'static str = "Not needed."; type AccountId = AccountId; - type Call = Call; + type Call = (); type AdditionalSigned = AdditionalSigned; type Pre = (); diff --git a/primitives/runtime/src/calls.rs b/primitives/runtime/src/calls.rs new file mode 100644 index 00000000000..194c2126ba3 --- /dev/null +++ b/primitives/runtime/src/calls.rs @@ -0,0 +1,30 @@ +// Copyright 2019-2023 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 . + +//! Basic runtime calls. + +use codec::{Decode, Encode}; +use scale_info::TypeInfo; +use sp_std::vec::Vec; + +/// A minimized version of `frame-system::Call` that can be used without a runtime. +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +#[allow(non_camel_case_types)] +pub enum SystemCall { + /// `frame-system::Call::remark` + #[codec(index = 1)] + remark(Vec), +} diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 340088a85b1..4c3fb152fa7 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -47,6 +47,7 @@ pub use storage_types::BoundedStorageValue; #[cfg(feature = "std")] pub use storage_proof::craft_valid_storage_proof; +pub mod calls; pub mod messages; mod chain; diff --git a/relays/bin-substrate/Cargo.toml b/relays/bin-substrate/Cargo.toml index de1807cdc97..c88b930cbb0 100644 --- a/relays/bin-substrate/Cargo.toml +++ b/relays/bin-substrate/Cargo.toml @@ -26,6 +26,7 @@ bp-bridge-hub-rococo = { path = "../../primitives/chain-bridge-hub-rococo" } bp-bridge-hub-wococo = { path = "../../primitives/chain-bridge-hub-wococo" } bp-header-chain = { path = "../../primitives/header-chain" } bp-messages = { path = "../../primitives/messages" } +bp-parachains = { path = "../../primitives/parachains" } bp-millau = { path = "../../primitives/chain-millau" } bp-polkadot-core = { path = "../../primitives/polkadot-core" } bp-rialto = { path = "../../primitives/chain-rialto" } diff --git a/relays/bin-substrate/src/chains/rococo_headers_to_bridge_hub_wococo.rs b/relays/bin-substrate/src/chains/rococo_headers_to_bridge_hub_wococo.rs index 2fac858336d..6cdae2b212b 100644 --- a/relays/bin-substrate/src/chains/rococo_headers_to_bridge_hub_wococo.rs +++ b/relays/bin-substrate/src/chains/rococo_headers_to_bridge_hub_wococo.rs @@ -33,7 +33,7 @@ substrate_relay_helper::generate_mocked_submit_finality_proof_call_builder!( RococoFinalityToBridgeHubWococo, RococoFinalityToBridgeHubWococoCallBuilder, relay_bridge_hub_wococo_client::runtime::Call::BridgeRococoGrandpa, - relay_bridge_hub_wococo_client::runtime::BridgeGrandpaRococoCall::submit_finality_proof + relay_bridge_hub_wococo_client::runtime::BridgeRococoGrandpaCall::submit_finality_proof ); #[async_trait] diff --git a/relays/bin-substrate/src/chains/wococo_parachains_to_bridge_hub_rococo.rs b/relays/bin-substrate/src/chains/wococo_parachains_to_bridge_hub_rococo.rs index f821af1a0cf..f79979ff832 100644 --- a/relays/bin-substrate/src/chains/wococo_parachains_to_bridge_hub_rococo.rs +++ b/relays/bin-substrate/src/chains/wococo_parachains_to_bridge_hub_rococo.rs @@ -51,7 +51,7 @@ impl SubmitParachainHeadsCallBuilder parachain_heads_proof: ParaHeadsProof, ) -> CallOf { relay_bridge_hub_rococo_client::runtime::Call::BridgeWococoParachain( - relay_bridge_hub_rococo_client::runtime::BridgeParachainCall::submit_parachain_heads( + bp_parachains::BridgeParachainCall::submit_parachain_heads( (at_relay_block.0, at_relay_block.1), parachains, parachain_heads_proof, diff --git a/relays/bin-substrate/src/cli/init_bridge.rs b/relays/bin-substrate/src/cli/init_bridge.rs index cadc6be4df2..322cca1bef5 100644 --- a/relays/bin-substrate/src/cli/init_bridge.rs +++ b/relays/bin-substrate/src/cli/init_bridge.rs @@ -176,7 +176,7 @@ impl BridgeInitializer for RococoToBridgeHubWococoCliBridge { init_data: >::InitializationData, ) -> ::Call { relay_bridge_hub_wococo_client::runtime::Call::BridgeRococoGrandpa( - relay_bridge_hub_wococo_client::runtime::BridgeGrandpaRococoCall::initialize(init_data), + relay_bridge_hub_wococo_client::runtime::BridgeRococoGrandpaCall::initialize(init_data), ) } } diff --git a/relays/client-bridge-hub-rococo/Cargo.toml b/relays/client-bridge-hub-rococo/Cargo.toml index 4a01d81e777..6872c59f760 100644 --- a/relays/client-bridge-hub-rococo/Cargo.toml +++ b/relays/client-bridge-hub-rococo/Cargo.toml @@ -16,14 +16,13 @@ bp-bridge-hub-rococo = { path = "../../primitives/chain-bridge-hub-rococo" } bp-bridge-hub-wococo = { path = "../../primitives/chain-bridge-hub-wococo" } bp-header-chain = { path = "../../primitives/header-chain" } bp-messages = { path = "../../primitives/messages" } -bp-polkadot-core = { path = "../../primitives/polkadot-core" } +bp-parachains = { path = "../../primitives/parachains" } +bp-rococo = { path = "../../primitives/chain-rococo" } bp-runtime = { path = "../../primitives/runtime" } +bp-wococo = { path = "../../primitives/chain-wococo" } bridge-runtime-common = { path = "../../bin/runtime-common" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } - -[dev-dependencies] -sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/relays/client-bridge-hub-rococo/src/lib.rs b/relays/client-bridge-hub-rococo/src/lib.rs index 7e9047e32ce..08a9e688487 100644 --- a/relays/client-bridge-hub-rococo/src/lib.rs +++ b/relays/client-bridge-hub-rococo/src/lib.rs @@ -132,8 +132,10 @@ mod tests { #[test] fn parse_transaction_works() { let unsigned = UnsignedTransaction { - call: runtime::Call::System(runtime::SystemCall::remark(b"Hello world!".to_vec())) - .into(), + call: runtime::Call::System(bp_runtime::calls::SystemCall::remark( + b"Hello world!".to_vec(), + )) + .into(), nonce: 777, tip: 888, era: TransactionEra::immortal(), diff --git a/relays/client-bridge-hub-rococo/src/runtime_wrapper.rs b/relays/client-bridge-hub-rococo/src/runtime_wrapper.rs index 0571f82f5a6..d973bb3bea5 100644 --- a/relays/client-bridge-hub-rococo/src/runtime_wrapper.rs +++ b/relays/client-bridge-hub-rococo/src/runtime_wrapper.rs @@ -18,201 +18,48 @@ //! Types that are specific to the BridgeHubRococo runtime. -use bp_polkadot_core::PolkadotLike; use codec::{Decode, Encode}; use scale_info::TypeInfo; -use bp_messages::UnrewardedRelayersState; -use bp_polkadot_core::parachains::{ParaHash, ParaHeadsProof, ParaId}; -use bp_runtime::Chain; +pub use bp_header_chain::BridgeGrandpaCallOf; +pub use bp_parachains::BridgeParachainCall; +pub use bp_runtime::calls::SystemCall; +pub use bridge_runtime_common::messages::BridgeMessagesCallOf; // TODO:check-parameter - check SignedExtension /// Unchecked BridgeHubRococo extrinsic. pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic; -/// Rococo Runtime `Call` enum. +// The indirect pallet call used to sync `Wococo` GRANDPA finality to `BHRococo`. +pub type BridgeWococoGrandpaCall = BridgeGrandpaCallOf; +// The indirect pallet call used to sync `BridgeHubWococo` messages to `BHRococo`. +pub type BridgeWococoMessagesCall = BridgeMessagesCallOf; + +/// `BridgeHubRococo` Runtime `Call` enum. /// -/// The enum represents a subset of possible `Call`s we can send to Rococo chain. +/// The enum represents a subset of possible `Call`s we can send to `BridgeHubRococo` chain. /// Ideally this code would be auto-generated from metadata, because we want to /// avoid depending directly on the ENTIRE runtime just to get the encoding of `Dispatchable`s. /// -/// All entries here (like pretty much in the entire file) must be kept in sync with Rococo -/// `construct_runtime`, so that we maintain SCALE-compatibility. +/// All entries here (like pretty much in the entire file) must be kept in sync with +/// `BridgeHubRococo` `construct_runtime`, so that we maintain SCALE-compatibility. /// -/// // TODO:check-parameter -> change bko-bridge-rococo-wococo when merged to master in cumulus -/// See: [link](https://github.com/paritytech/cumulus/blob/bko-bridge-rococo-wococo/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs) +/// // TODO:check-parameter -> change bridge-hub-rococo-wococo when merged to master in cumulus +/// See: [link](https://github.com/paritytech/cumulus/blob/bridge-hub-rococo-wococo/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs) #[allow(clippy::large_enum_variant)] #[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] pub enum Call { - /// System pallet. + #[cfg(test)] #[codec(index = 0)] System(SystemCall), + /// Wococo bridge pallet. #[codec(index = 41)] BridgeWococoGrandpa(BridgeWococoGrandpaCall), - /// Rococo bridge pallet. - #[codec(index = 43)] - BridgeRococoGrandpa(BridgeRococoGrandpaCall), - /// Wococo parachain bridge pallet. #[codec(index = 42)] BridgeWococoParachain(BridgeParachainCall), - /// Rococo parachain bridge pallet. - #[codec(index = 44)] - BridgeRococoParachain(BridgeParachainCall), - /// Wococo messages bridge pallet. #[codec(index = 46)] BridgeWococoMessages(BridgeWococoMessagesCall), - /// Rococo messages bridge pallet. - #[codec(index = 45)] - BridgeRococoMessages(BridgeRococoMessagesCall), -} - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] -#[allow(non_camel_case_types)] -pub enum SystemCall { - #[codec(index = 1)] - remark(Vec), -} - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] -#[allow(non_camel_case_types)] -pub enum BridgeWococoGrandpaCall { - #[codec(index = 0)] - submit_finality_proof( - Box<::Header>, - bp_header_chain::justification::GrandpaJustification<::Header>, - ), - #[codec(index = 1)] - initialize(bp_header_chain::InitializationData<::Header>), -} - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] -#[allow(non_camel_case_types)] -pub enum BridgeRococoGrandpaCall { - #[codec(index = 0)] - submit_finality_proof( - Box<::Header>, - bp_header_chain::justification::GrandpaJustification<::Header>, - ), - #[codec(index = 1)] - initialize(bp_header_chain::InitializationData<::Header>), -} - -pub type RelayBlockHash = bp_polkadot_core::Hash; -pub type RelayBlockNumber = bp_polkadot_core::BlockNumber; - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] -#[allow(non_camel_case_types)] -pub enum BridgeParachainCall { - #[codec(index = 0)] - submit_parachain_heads( - (RelayBlockNumber, RelayBlockHash), - Vec<(ParaId, ParaHash)>, - ParaHeadsProof, - ), -} - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] -#[allow(non_camel_case_types)] -pub enum BridgeWococoMessagesCall { - #[codec(index = 2)] - receive_messages_proof( - relay_substrate_client::AccountIdOf, - bridge_runtime_common::messages::target::FromBridgedChainMessagesProof< - relay_substrate_client::HashOf, - >, - u32, - bp_messages::Weight, - ), - - #[codec(index = 3)] - receive_messages_delivery_proof( - bridge_runtime_common::messages::source::FromBridgedChainMessagesDeliveryProof< - relay_substrate_client::HashOf, - >, - UnrewardedRelayersState, - ), -} - -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] -#[allow(non_camel_case_types)] -pub enum BridgeRococoMessagesCall { - #[codec(index = 2)] - receive_messages_proof( - relay_substrate_client::AccountIdOf, - bridge_runtime_common::messages::target::FromBridgedChainMessagesProof< - relay_substrate_client::HashOf, - >, - u32, - bp_messages::Weight, - ), - - #[codec(index = 3)] - receive_messages_delivery_proof( - bridge_runtime_common::messages::source::FromBridgedChainMessagesDeliveryProof< - relay_substrate_client::HashOf, - >, - UnrewardedRelayersState, - ), -} - -impl sp_runtime::traits::Dispatchable for Call { - type RuntimeOrigin = (); - type Config = (); - type Info = (); - type PostInfo = (); - - fn dispatch( - self, - _origin: Self::RuntimeOrigin, - ) -> sp_runtime::DispatchResultWithInfo { - unimplemented!("The Call is not expected to be dispatched.") - } -} - -#[cfg(test)] -mod tests { - use super::*; - use bp_runtime::BasicOperatingMode; - use sp_core::hexdisplay::HexDisplay; - use sp_finality_grandpa::AuthorityList; - use sp_runtime::traits::Header; - use std::str::FromStr; - - pub type RelayBlockHasher = bp_polkadot_core::Hasher; - pub type RelayBlockHeader = sp_runtime::generic::Header; - - #[test] - fn encode_decode_calls() { - let header = RelayBlockHeader::new( - 75, - bp_polkadot_core::Hash::from_str( - "0xd2c0afaab32de0cb8f7f0d89217e37c5ea302c1ffb5a7a83e10d20f12c32874d", - ) - .expect("invalid value"), - bp_polkadot_core::Hash::from_str( - "0x92b965f0656a4e0e5fc0167da2d4b5ee72b3be2c1583c4c1e5236c8c12aa141b", - ) - .expect("invalid value"), - bp_polkadot_core::Hash::from_str( - "0xae4a25acf250d72ed02c149ecc7dd3c9ee976d41a2888fc551de8064521dc01d", - ) - .expect("invalid value"), - Default::default(), - ); - let init_data = bp_header_chain::InitializationData { - header: Box::new(header), - authority_list: AuthorityList::default(), - set_id: 6, - operating_mode: BasicOperatingMode::Normal, - }; - let call = BridgeRococoGrandpaCall::initialize(init_data); - let tx = Call::BridgeRococoGrandpa(call); - - // encode call as hex string - let hex_encoded_call = format!("0x{:?}", HexDisplay::from(&Encode::encode(&tx))); - assert_eq!(hex_encoded_call, "0x2b01ae4a25acf250d72ed02c149ecc7dd3c9ee976d41a2888fc551de8064521dc01d2d0192b965f0656a4e0e5fc0167da2d4b5ee72b3be2c1583c4c1e5236c8c12aa141bd2c0afaab32de0cb8f7f0d89217e37c5ea302c1ffb5a7a83e10d20f12c32874d0000060000000000000000"); - } } diff --git a/relays/client-bridge-hub-wococo/Cargo.toml b/relays/client-bridge-hub-wococo/Cargo.toml index fa28dfde202..7c9b90975d6 100644 --- a/relays/client-bridge-hub-wococo/Cargo.toml +++ b/relays/client-bridge-hub-wococo/Cargo.toml @@ -12,11 +12,21 @@ relay-substrate-client = { path = "../client-substrate" } # Bridge dependencies +bp-bridge-hub-rococo = { path = "../../primitives/chain-bridge-hub-rococo" } bp-bridge-hub-wococo = { path = "../../primitives/chain-bridge-hub-wococo" } +bp-header-chain = { path = "../../primitives/header-chain" } bp-messages = { path = "../../primitives/messages" } -relay-bridge-hub-rococo-client = { path = "../client-bridge-hub-rococo" } +bp-parachains = { path = "../../primitives/parachains" } +bp-rococo = { path = "../../primitives/chain-rococo" } +bp-runtime = { path = "../../primitives/runtime" } + +bridge-runtime-common = { path = "../../bin/runtime-common" } # Substrate Dependencies sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } + +[dev-dependencies] +bp-polkadot-core = { path = "../../primitives/polkadot-core" } +sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/relays/client-bridge-hub-wococo/src/runtime_wrapper.rs b/relays/client-bridge-hub-wococo/src/runtime_wrapper.rs index 05176af471a..c7a92ef9ba4 100644 --- a/relays/client-bridge-hub-wococo/src/runtime_wrapper.rs +++ b/relays/client-bridge-hub-wococo/src/runtime_wrapper.rs @@ -16,11 +16,94 @@ //! Types that are specific to the BridgeHubWococo runtime. -// We reuse everything from rococo runtime wrapper -pub type Call = relay_bridge_hub_rococo_client::runtime::Call; +use codec::{Decode, Encode}; +use scale_info::TypeInfo; + +pub use bp_header_chain::BridgeGrandpaCallOf; +pub use bp_parachains::BridgeParachainCall; +pub use bp_runtime::calls::SystemCall; +pub use bridge_runtime_common::messages::BridgeMessagesCallOf; + +// TODO:check-parameter - check SignedExtension +/// Unchecked BridgeHubWococo extrinsic. pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic; -pub type BridgeGrandpaRococoCall = relay_bridge_hub_rococo_client::runtime::BridgeRococoGrandpaCall; -pub type BridgeParachainCall = relay_bridge_hub_rococo_client::runtime::BridgeParachainCall; -pub type BridgeRococoMessagesCall = - relay_bridge_hub_rococo_client::runtime::BridgeRococoMessagesCall; -pub type SystemCall = relay_bridge_hub_rococo_client::runtime::SystemCall; + +// The indirect pallet call used to sync `Rococo` GRANDPA finality to `BHWococo`. +pub type BridgeRococoGrandpaCall = BridgeGrandpaCallOf; +// The indirect pallet call used to sync `BridgeHubRococo` messages to `BridgeHubWococo`. +pub type BridgeRococoMessagesCall = BridgeMessagesCallOf; + +/// `BridgeHubWococo` Runtime `Call` enum. +/// +/// The enum represents a subset of possible `Call`s we can send to `BridgeHubWococo` chain. +/// Ideally this code would be auto-generated from metadata, because we want to +/// avoid depending directly on the ENTIRE runtime just to get the encoding of `Dispatchable`s. +/// +/// All entries here (like pretty much in the entire file) must be kept in sync with +/// `BridgeHubWococo` `construct_runtime`, so that we maintain SCALE-compatibility. +/// +/// TODO:check-parameter -> change bridge-hub-rococo-wococo when merged to master in cumulus +/// See: [link](https://github.com/paritytech/cumulus/blob/bridge-hub-rococo-wococo/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs) +#[allow(clippy::large_enum_variant)] +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +pub enum Call { + #[cfg(test)] + #[codec(index = 0)] + System(SystemCall), + + /// Rococo bridge pallet. + #[codec(index = 43)] + BridgeRococoGrandpa(BridgeRococoGrandpaCall), + /// Rococo parachain bridge pallet. + #[codec(index = 44)] + BridgeRococoParachain(BridgeParachainCall), + /// Rococo messages bridge pallet. + #[codec(index = 45)] + BridgeRococoMessages(BridgeRococoMessagesCall), +} + +#[cfg(test)] +mod tests { + use super::*; + use bp_runtime::BasicOperatingMode; + use sp_core::hexdisplay::HexDisplay; + use sp_finality_grandpa::AuthorityList; + use sp_runtime::traits::Header; + use std::str::FromStr; + + pub type RelayBlockNumber = bp_polkadot_core::BlockNumber; + pub type RelayBlockHasher = bp_polkadot_core::Hasher; + pub type RelayBlockHeader = sp_runtime::generic::Header; + + #[test] + fn encode_decode_calls() { + let header = RelayBlockHeader::new( + 75, + bp_polkadot_core::Hash::from_str( + "0xd2c0afaab32de0cb8f7f0d89217e37c5ea302c1ffb5a7a83e10d20f12c32874d", + ) + .expect("invalid value"), + bp_polkadot_core::Hash::from_str( + "0x92b965f0656a4e0e5fc0167da2d4b5ee72b3be2c1583c4c1e5236c8c12aa141b", + ) + .expect("invalid value"), + bp_polkadot_core::Hash::from_str( + "0xae4a25acf250d72ed02c149ecc7dd3c9ee976d41a2888fc551de8064521dc01d", + ) + .expect("invalid value"), + Default::default(), + ); + let init_data = bp_header_chain::InitializationData { + header: Box::new(header), + authority_list: AuthorityList::default(), + set_id: 6, + operating_mode: BasicOperatingMode::Normal, + }; + let call = BridgeRococoGrandpaCall::initialize(init_data); + let tx = Call::BridgeRococoGrandpa(call); + + // encode call as hex string + let hex_encoded_call = format!("0x{:?}", HexDisplay::from(&Encode::encode(&tx))); + assert_eq!(hex_encoded_call, "0x2b01ae4a25acf250d72ed02c149ecc7dd3c9ee976d41a2888fc551de8064521dc01d2d0192b965f0656a4e0e5fc0167da2d4b5ee72b3be2c1583c4c1e5236c8c12aa141bd2c0afaab32de0cb8f7f0d89217e37c5ea302c1ffb5a7a83e10d20f12c32874d0000060000000000000000"); + } +} diff --git a/relays/client-substrate/src/chain.rs b/relays/client-substrate/src/chain.rs index c199540cbfb..db08fc1f98c 100644 --- a/relays/client-substrate/src/chain.rs +++ b/relays/client-substrate/src/chain.rs @@ -26,7 +26,7 @@ use sc_transaction_pool_api::TransactionStatus; use sp_core::{storage::StorageKey, Pair}; use sp_runtime::{ generic::SignedBlock, - traits::{Block as BlockT, Dispatchable, Member}, + traits::{Block as BlockT, Member}, ConsensusEngineId, EncodedJustification, }; use std::{fmt::Debug, time::Duration}; @@ -56,7 +56,7 @@ pub trait Chain: ChainBase + Clone { /// Block type. type SignedBlock: Member + Serialize + DeserializeOwned + BlockWithJustification; /// The aggregated `Call` type. - type Call: Clone + Codec + Dispatchable + Debug + Send; + type Call: Clone + Codec + Debug + Send; } /// Substrate-based relay chain that supports parachains. diff --git a/relays/lib-substrate-relay/src/on_demand/headers.rs b/relays/lib-substrate-relay/src/on_demand/headers.rs index 465b03c04a4..75b402aff87 100644 --- a/relays/lib-substrate-relay/src/on_demand/headers.rs +++ b/relays/lib-substrate-relay/src/on_demand/headers.rs @@ -316,7 +316,7 @@ async fn background_task( stall_timeout, only_mandatory_headers, }, - metrics_params.clone().unwrap_or_else(|| MetricsParams::disabled()), + metrics_params.clone().unwrap_or_else(MetricsParams::disabled), futures::future::pending(), ) .fuse(),