Skip to content

Commit

Permalink
Deduplicate pallet call structs used for indirect runtime calls (#1744)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
serban300 authored Jan 9, 2023
1 parent e575269 commit 6c826a6
Show file tree
Hide file tree
Showing 25 changed files with 354 additions and 243 deletions.
17 changes: 12 additions & 5 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,13 @@ pub mod target {
}
}

/// The `BridgeMessagesCall` used by a chain.
pub type BridgeMessagesCallOf<C> = bp_messages::BridgeMessagesCall<
bp_runtime::AccountIdOf<C>,
target::FromBridgedChainMessagesProof<bp_runtime::HashOf<C>>,
source::FromBridgedChainMessagesDeliveryProof<bp_runtime::HashOf<C>>,
>;

#[cfg(test)]
mod tests {
use super::*;
Expand Down
29 changes: 29 additions & 0 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<TestRuntime>::initialize { init_data: init_data.clone() };
let indirect_initialize_call = BridgeGrandpaCall::<TestHeader>::initialize(init_data);
assert_eq!(direct_initialize_call.encode(), indirect_initialize_call.encode());

let direct_submit_finality_proof_call = Call::<TestRuntime>::submit_finality_proof {
finality_target: Box::new(header.clone()),
justification: justification.clone(),
};
let indirect_submit_finality_proof_call =
BridgeGrandpaCall::<TestHeader>::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);
}
67 changes: 65 additions & 2 deletions modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,13 @@ fn verify_and_decode_messages_proof<Chain: SourceHeaderChain, DispatchPayload: D
mod tests {
use super::*;
use crate::mock::{
message, message_payload, run_test, unrewarded_relayer, DbWeight,
message, message_payload, run_test, unrewarded_relayer, AccountId, DbWeight,
RuntimeEvent as TestEvent, RuntimeOrigin, TestDeliveryConfirmationPayments,
TestDeliveryPayments, TestMessagesDeliveryProof, TestMessagesProof, TestRuntime,
MAX_OUTBOUND_PAYLOAD_SIZE, PAYLOAD_REJECTED_BY_TARGET_CHAIN, REGULAR_PAYLOAD, TEST_LANE_ID,
TEST_LANE_ID_2, TEST_LANE_ID_3, TEST_RELAYER_A, TEST_RELAYER_B,
};
use bp_messages::{UnrewardedRelayer, UnrewardedRelayersState};
use bp_messages::{BridgeMessagesCall, UnrewardedRelayer, UnrewardedRelayersState};
use bp_test_utils::generate_owned_bridge_module_tests;
use frame_support::{
assert_noop, assert_ok,
Expand Down Expand Up @@ -1891,6 +1891,69 @@ mod tests {
});
}

#[test]
fn test_bridge_messages_call_is_correctly_defined() {
let account_id = 1;
let message_proof: TestMessagesProof = Ok(vec![message(1, REGULAR_PAYLOAD)]).into();
let message_delivery_proof = TestMessagesDeliveryProof(Ok((
TEST_LANE_ID,
InboundLaneData {
last_confirmed_nonce: 1,
relayers: vec![UnrewardedRelayer {
relayer: 0,
messages: DeliveredMessages::new(1),
}]
.into_iter()
.collect(),
},
)));
let unrewarded_relayer_state = UnrewardedRelayersState {
unrewarded_relayer_entries: 1,
total_messages: 1,
last_delivered_nonce: 1,
..Default::default()
};

let direct_receive_messages_proof_call = Call::<TestRuntime>::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::<TestRuntime>::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)
Expand Down
23 changes: 22 additions & 1 deletion modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<TestRuntime>::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);
}
8 changes: 3 additions & 5 deletions primitives/chain-bridge-hub-cumulus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions primitives/chain-bridge-hub-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 0 additions & 4 deletions primitives/chain-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
17 changes: 16 additions & 1 deletion primitives/header-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,3 +169,18 @@ impl<Number: Codec> ConsensusLogReader for GrandpaConsensusLogReader<Number> {
GrandpaConsensusLogReader::<Number>::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<Header: HeaderT> {
/// `pallet-bridge-grandpa::Call::submit_finality_proof`
#[codec(index = 0)]
submit_finality_proof(Box<Header>, justification::GrandpaJustification<Header>),
/// `pallet-bridge-grandpa::Call::initialize`
#[codec(index = 1)]
initialize(InitializationData<Header>),
}

/// The `BridgeGrandpaCall` used by a chain.
pub type BridgeGrandpaCallOf<C> = BridgeGrandpaCall<HeaderOf<C>>;
12 changes: 12 additions & 0 deletions primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountId, MessagesProof, MessagesDeliveryProof> {
/// `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::*;
Expand Down
17 changes: 15 additions & 2 deletions primitives/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
),
}
Loading

0 comments on commit 6c826a6

Please sign in to comment.