use bp_header_chain::ChainWithGrandpa;
use bp_messages::{ChainWithMessages, InboundLaneData, MessageNonce};
use bp_runtime::Chain;
use codec::Encode;
use frame_support::{storage::generator::StorageValue, traits::Get, weights::Weight};
use frame_system::limits;
use pallet_bridge_messages::WeightInfoExt as _;
#[doc(hidden)]
pub mod __private {
pub use bp_xcm_bridge_hub;
pub use static_assertions;
}
#[macro_export]
macro_rules! assert_chain_types(
( runtime: $r:path, this_chain: $this:path ) => {
{
use frame_system::{Config as SystemConfig, pallet_prelude::{BlockNumberFor, HeaderFor}};
use $crate::integrity::__private::static_assertions::assert_type_eq_all;
assert_type_eq_all!(<$r as SystemConfig>::Nonce, bp_runtime::NonceOf<$this>);
assert_type_eq_all!(BlockNumberFor<$r>, bp_runtime::BlockNumberOf<$this>);
assert_type_eq_all!(<$r as SystemConfig>::Hash, bp_runtime::HashOf<$this>);
assert_type_eq_all!(<$r as SystemConfig>::Hashing, bp_runtime::HasherOf<$this>);
assert_type_eq_all!(<$r as SystemConfig>::AccountId, bp_runtime::AccountIdOf<$this>);
assert_type_eq_all!(HeaderFor<$r>, bp_runtime::HeaderOf<$this>);
}
}
);
#[macro_export]
macro_rules! assert_bridge_messages_pallet_types(
(
runtime: $r:path,
with_bridged_chain_messages_instance: $i:path,
this_chain: $this:path,
bridged_chain: $bridged:path,
) => {
{
use $crate::integrity::__private::bp_xcm_bridge_hub::XcmAsPlainPayload;
use $crate::integrity::__private::static_assertions::assert_type_eq_all;
use bp_messages::ChainWithMessages;
use bp_runtime::Chain;
use pallet_bridge_messages::Config as BridgeMessagesConfig;
assert_type_eq_all!(<$r as BridgeMessagesConfig<$i>>::ThisChain, $this);
assert_type_eq_all!(<$r as BridgeMessagesConfig<$i>>::BridgedChain, $bridged);
assert_type_eq_all!(<$r as BridgeMessagesConfig<$i>>::OutboundPayload, XcmAsPlainPayload);
assert_type_eq_all!(<$r as BridgeMessagesConfig<$i>>::InboundPayload, XcmAsPlainPayload);
}
}
);
#[macro_export]
macro_rules! assert_complete_bridge_types(
(
runtime: $r:path,
with_bridged_chain_messages_instance: $mi:path,
this_chain: $this:path,
bridged_chain: $bridged:path,
) => {
$crate::assert_chain_types!(runtime: $r, this_chain: $this);
$crate::assert_bridge_messages_pallet_types!(
runtime: $r,
with_bridged_chain_messages_instance: $mi,
this_chain: $this,
bridged_chain: $bridged,
);
}
);
#[derive(Debug)]
pub struct AssertChainConstants {
pub block_length: limits::BlockLength,
pub block_weights: limits::BlockWeights,
}
pub fn assert_chain_constants<R>(params: AssertChainConstants)
where
R: frame_system::Config,
{
assert_eq!(
R::BlockLength::get().encode(),
params.block_length.encode(),
"BlockLength from runtime ({:?}) differ from hardcoded: {:?}",
R::BlockLength::get(),
params.block_length,
);
assert_eq!(
R::BlockWeights::get().encode(),
params.block_weights.encode(),
"BlockWeights from runtime ({:?}) differ from hardcoded: {:?}",
R::BlockWeights::get(),
params.block_weights,
);
}
pub fn assert_bridge_grandpa_pallet_constants<R, GI>()
where
R: pallet_bridge_grandpa::Config<GI>,
GI: 'static,
{
assert!(
R::HeadersToKeep::get() > 0,
"HeadersToKeep ({}) must be larger than zero",
R::HeadersToKeep::get(),
);
}
pub fn assert_bridge_messages_pallet_constants<R, MI>()
where
R: pallet_bridge_messages::Config<MI>,
MI: 'static,
{
assert!(
pallet_bridge_messages::BridgedChainOf::<R, MI>::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX
<= pallet_bridge_messages::BridgedChainOf::<R, MI>::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
"MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX ({}) of {:?} is larger than \
its MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX ({}). This makes \
no sense",
pallet_bridge_messages::BridgedChainOf::<R, MI>::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
pallet_bridge_messages::BridgedChainOf::<R, MI>::ID,
pallet_bridge_messages::BridgedChainOf::<R, MI>::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
);
}
#[derive(Debug)]
struct AssertBridgeGrandpaPalletNames<'a> {
pub with_bridged_chain_grandpa_pallet_name: &'a str,
}
fn assert_bridge_grandpa_pallet_names<R, GI>(params: AssertBridgeGrandpaPalletNames)
where
R: pallet_bridge_grandpa::Config<GI>,
GI: 'static,
{
assert_eq!(
pallet_bridge_grandpa::PalletOwner::<R, GI>::storage_value_final_key().to_vec(),
bp_runtime::storage_value_key(
params.with_bridged_chain_grandpa_pallet_name,
"PalletOwner",
)
.0,
);
assert_eq!(
pallet_bridge_grandpa::PalletOperatingMode::<R, GI>::storage_value_final_key().to_vec(),
bp_runtime::storage_value_key(
params.with_bridged_chain_grandpa_pallet_name,
"PalletOperatingMode",
)
.0,
);
}
#[derive(Debug)]
struct AssertBridgeMessagesPalletNames<'a> {
pub with_bridged_chain_messages_pallet_name: &'a str,
}
fn assert_bridge_messages_pallet_names<R, MI>(params: AssertBridgeMessagesPalletNames)
where
R: pallet_bridge_messages::Config<MI>,
MI: 'static,
{
assert_eq!(
pallet_bridge_messages::PalletOwner::<R, MI>::storage_value_final_key().to_vec(),
bp_runtime::storage_value_key(
params.with_bridged_chain_messages_pallet_name,
"PalletOwner",
)
.0,
);
assert_eq!(
pallet_bridge_messages::PalletOperatingMode::<R, MI>::storage_value_final_key().to_vec(),
bp_runtime::storage_value_key(
params.with_bridged_chain_messages_pallet_name,
"PalletOperatingMode",
)
.0,
);
}
#[derive(Debug)]
pub struct AssertCompleteBridgeConstants {
pub this_chain_constants: AssertChainConstants,
}
pub fn assert_complete_with_relay_chain_bridge_constants<R, GI, MI>(
params: AssertCompleteBridgeConstants,
) where
R: frame_system::Config
+ pallet_bridge_grandpa::Config<GI>
+ pallet_bridge_messages::Config<MI>,
GI: 'static,
MI: 'static,
{
assert_chain_constants::<R>(params.this_chain_constants);
assert_bridge_grandpa_pallet_constants::<R, GI>();
assert_bridge_messages_pallet_constants::<R, MI>();
assert_bridge_grandpa_pallet_names::<R, GI>(AssertBridgeGrandpaPalletNames {
with_bridged_chain_grandpa_pallet_name:
<R as pallet_bridge_grandpa::Config<GI>>::BridgedChain::WITH_CHAIN_GRANDPA_PALLET_NAME,
});
assert_bridge_messages_pallet_names::<R, MI>(AssertBridgeMessagesPalletNames {
with_bridged_chain_messages_pallet_name:
<R as pallet_bridge_messages::Config<MI>>::BridgedChain::WITH_CHAIN_MESSAGES_PALLET_NAME,
});
}
pub fn assert_complete_with_parachain_bridge_constants<R, PI, MI>(
params: AssertCompleteBridgeConstants,
) where
R: frame_system::Config
+ pallet_bridge_parachains::Config<PI>
+ pallet_bridge_messages::Config<MI>,
<R as pallet_bridge_parachains::BoundedBridgeGrandpaConfig<R::BridgesGrandpaPalletInstance>>::BridgedRelayChain: ChainWithGrandpa,
PI: 'static,
MI: 'static,
{
assert_chain_constants::<R>(params.this_chain_constants);
assert_bridge_grandpa_pallet_constants::<R, R::BridgesGrandpaPalletInstance>();
assert_bridge_messages_pallet_constants::<R, MI>();
assert_bridge_grandpa_pallet_names::<R, R::BridgesGrandpaPalletInstance>(
AssertBridgeGrandpaPalletNames {
with_bridged_chain_grandpa_pallet_name:
<<R as pallet_bridge_parachains::BoundedBridgeGrandpaConfig<
R::BridgesGrandpaPalletInstance,
>>::BridgedRelayChain>::WITH_CHAIN_GRANDPA_PALLET_NAME,
},
);
assert_bridge_messages_pallet_names::<R, MI>(AssertBridgeMessagesPalletNames {
with_bridged_chain_messages_pallet_name:
<R as pallet_bridge_messages::Config<MI>>::BridgedChain::WITH_CHAIN_MESSAGES_PALLET_NAME,
});
}
pub fn assert_standalone_messages_bridge_constants<R, MI>(params: AssertCompleteBridgeConstants)
where
R: frame_system::Config + pallet_bridge_messages::Config<MI>,
MI: 'static,
{
assert_chain_constants::<R>(params.this_chain_constants);
assert_bridge_messages_pallet_constants::<R, MI>();
assert_bridge_messages_pallet_names::<R, MI>(AssertBridgeMessagesPalletNames {
with_bridged_chain_messages_pallet_name:
<R as pallet_bridge_messages::Config<MI>>::BridgedChain::WITH_CHAIN_MESSAGES_PALLET_NAME,
});
}
pub fn check_message_lane_weights<
C: ChainWithMessages,
T: frame_system::Config + pallet_bridge_messages::Config<MessagesPalletInstance>,
MessagesPalletInstance: 'static,
>(
bridged_chain_extra_storage_proof_size: u32,
this_chain_max_unrewarded_relayers: MessageNonce,
this_chain_max_unconfirmed_messages: MessageNonce,
runtime_includes_refund_extension: bool,
) {
type Weights<T, MI> = <T as pallet_bridge_messages::Config<MI>>::WeightInfo;
pallet_bridge_messages::ensure_weights_are_correct::<Weights<T, MessagesPalletInstance>>();
pallet_bridge_messages::ensure_maximal_message_dispatch::<Weights<T, MessagesPalletInstance>>(
C::maximal_incoming_message_size(),
C::maximal_incoming_message_dispatch_weight(),
);
let max_incoming_message_proof_size =
bridged_chain_extra_storage_proof_size.saturating_add(C::maximal_incoming_message_size());
pallet_bridge_messages::ensure_able_to_receive_message::<Weights<T, MessagesPalletInstance>>(
C::max_extrinsic_size(),
C::max_extrinsic_weight(),
max_incoming_message_proof_size,
C::maximal_incoming_message_dispatch_weight(),
);
let max_incoming_inbound_lane_data_proof_size =
InboundLaneData::<()>::encoded_size_hint_u32(this_chain_max_unrewarded_relayers as _);
pallet_bridge_messages::ensure_able_to_receive_confirmation::<Weights<T, MessagesPalletInstance>>(
C::max_extrinsic_size(),
C::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
this_chain_max_unrewarded_relayers,
this_chain_max_unconfirmed_messages,
);
if runtime_includes_refund_extension {
assert_ne!(
Weights::<T, MessagesPalletInstance>::receive_messages_proof_overhead_from_runtime(),
Weight::zero()
);
assert_ne!(
Weights::<T, MessagesPalletInstance>::receive_messages_delivery_proof_overhead_from_runtime(),
Weight::zero()
);
}
}