Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable2412] Backport #6465 #6536 #6579 #6649

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 73 additions & 22 deletions bridges/bin/runtime-common/src/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ macro_rules! assert_bridge_messages_pallet_types(

/// Macro that combines four other macro calls - `assert_chain_types`, `assert_bridge_types`,
/// and `assert_bridge_messages_pallet_types`. It may be used
/// at the chain that is implementing complete standard messages bridge (i.e. with bridge GRANDPA
/// and messages pallets deployed).
/// at the chain that is implementing standard messages bridge with messages pallets deployed.
#[macro_export]
macro_rules! assert_complete_bridge_types(
(
runtime: $r:path,
with_bridged_chain_grandpa_instance: $gi:path,
with_bridged_chain_messages_instance: $mi:path,
this_chain: $this:path,
bridged_chain: $bridged:path,
Expand Down Expand Up @@ -186,34 +184,55 @@ where
);
}

/// Parameters for asserting bridge pallet names.
/// Parameters for asserting bridge GRANDPA pallet names.
#[derive(Debug)]
pub struct AssertBridgePalletNames<'a> {
struct AssertBridgeGrandpaPalletNames<'a> {
/// Name of the GRANDPA pallet, deployed at this chain and used to bridge with the bridged
/// chain.
pub with_bridged_chain_grandpa_pallet_name: &'a str,
/// Name of the messages pallet, deployed at this chain and used to bridge with the bridged
/// chain.
pub with_bridged_chain_messages_pallet_name: &'a str,
}

/// Tests that bridge pallet names used in `construct_runtime!()` macro call are matching constants
/// from chain primitives crates.
fn assert_bridge_pallet_names<R, GI, MI>(params: AssertBridgePalletNames)
fn assert_bridge_grandpa_pallet_names<R, GI>(params: AssertBridgeGrandpaPalletNames)
where
R: pallet_bridge_grandpa::Config<GI> + pallet_bridge_messages::Config<MI>,
R: pallet_bridge_grandpa::Config<GI>,
GI: 'static,
MI: 'static,
{
// check that the bridge GRANDPA pallet has required name
assert_eq!(
pallet_bridge_grandpa::PalletOwner::<R, GI>::storage_value_final_key().to_vec(),
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,
"PalletOwner",
).0,
"PalletOperatingMode",
)
.0,
);
}

/// Parameters for asserting bridge messages pallet names.
#[derive(Debug)]
struct AssertBridgeMessagesPalletNames<'a> {
/// Name of the messages pallet, deployed at this chain and used to bridge with the bridged
/// chain.
pub with_bridged_chain_messages_pallet_name: &'a str,
}

/// Tests that bridge pallet names used in `construct_runtime!()` macro call are matching constants
/// from chain primitives crates.
fn assert_bridge_messages_pallet_names<R, MI>(params: AssertBridgeMessagesPalletNames)
where
R: pallet_bridge_messages::Config<MI>,
MI: 'static,
{
// check that the bridge messages pallet has required name
assert_eq!(
pallet_bridge_messages::PalletOwner::<R, MI>::storage_value_final_key().to_vec(),
Expand All @@ -223,6 +242,14 @@ where
)
.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,
);
}

/// Parameters for asserting complete standard messages bridge.
Expand All @@ -246,31 +273,55 @@ pub fn assert_complete_with_relay_chain_bridge_constants<R, GI, MI>(
assert_chain_constants::<R>(params.this_chain_constants);
assert_bridge_grandpa_pallet_constants::<R, GI>();
assert_bridge_messages_pallet_constants::<R, MI>();
assert_bridge_pallet_names::<R, GI, MI>(AssertBridgePalletNames {
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,
});
}

/// All bridge-related constants tests for the complete standard parachain messages bridge
/// (i.e. with bridge GRANDPA, parachains and messages pallets deployed).
pub fn assert_complete_with_parachain_bridge_constants<R, GI, MI, RelayChain>(
pub fn assert_complete_with_parachain_bridge_constants<R, PI, MI>(
params: AssertCompleteBridgeConstants,
) where
R: frame_system::Config
+ pallet_bridge_grandpa::Config<GI>
+ pallet_bridge_parachains::Config<PI>
+ pallet_bridge_messages::Config<MI>,
GI: 'static,
<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,
});
}

/// All bridge-related constants tests for the standalone messages bridge deployment (only with
/// messages pallets deployed).
pub fn assert_standalone_messages_bridge_constants<R, MI>(params: AssertCompleteBridgeConstants)
where
R: frame_system::Config + pallet_bridge_messages::Config<MI>,
MI: 'static,
RelayChain: ChainWithGrandpa,
{
assert_chain_constants::<R>(params.this_chain_constants);
assert_bridge_grandpa_pallet_constants::<R, GI>();
assert_bridge_messages_pallet_constants::<R, MI>();
assert_bridge_pallet_names::<R, GI, MI>(AssertBridgePalletNames {
with_bridged_chain_grandpa_pallet_name: RelayChain::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,
});
Expand Down
1 change: 1 addition & 0 deletions bridges/bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl pallet_bridge_messages::Config for TestRuntime {
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
TestRuntime,
(),
(),
ConstU64<100_000>,
>;
type OnMessagesDelivered = ();
Expand Down
5 changes: 3 additions & 2 deletions bridges/modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@

use bp_relayers::{
ExplicitOrAccountParams, PaymentProcedure, Registration, RelayerRewardsKeyProvider,
RewardsAccountParams, StakeAndSlash,
StakeAndSlash,
};
pub use bp_relayers::{RewardsAccountOwner, RewardsAccountParams};
use bp_runtime::StorageDoubleMapKeyProvider;
use frame_support::fail;
use sp_arithmetic::traits::{AtLeast32BitUnsigned, Zero};
use sp_runtime::{traits::CheckedSub, Saturating};
use sp_std::marker::PhantomData;

pub use pallet::*;
pub use payment_adapter::DeliveryConfirmationPaymentsAdapter;
pub use payment_adapter::{DeliveryConfirmationPaymentsAdapter, PayRewardFromAccount};
pub use stake_adapter::StakeAndSlashNamed;
pub use weights::WeightInfo;
pub use weights_ext::WeightInfoExt;
Expand Down
15 changes: 8 additions & 7 deletions bridges/modules/relayers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ pub type TestStakeAndSlash = pallet_bridge_relayers::StakeAndSlashNamed<
frame_support::construct_runtime! {
pub enum TestRuntime
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
System: frame_system,
Utility: pallet_utility,
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
BridgeGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
BridgeParachains: pallet_bridge_parachains::{Pallet, Call, Storage, Event<T>},
BridgeMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment,
BridgeRelayers: pallet_bridge_relayers,
BridgeGrandpa: pallet_bridge_grandpa,
BridgeParachains: pallet_bridge_parachains,
BridgeMessages: pallet_bridge_messages,
}
}

Expand Down Expand Up @@ -267,6 +267,7 @@ impl pallet_bridge_messages::Config for TestRuntime {
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
TestRuntime,
(),
(),
ConstU64<100_000>,
>;
type OnMessagesDelivered = ();
Expand Down
24 changes: 13 additions & 11 deletions bridges/modules/relayers/src/payment_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use bp_messages::{
source_chain::{DeliveryConfirmationPayments, RelayersRewards},
MessageNonce,
};
pub use bp_relayers::PayRewardFromAccount;
use bp_relayers::{RewardsAccountOwner, RewardsAccountParams};
use bp_runtime::Chain;
use frame_support::{sp_runtime::SaturatedConversion, traits::Get};
Expand All @@ -31,15 +32,16 @@ use sp_std::{collections::vec_deque::VecDeque, marker::PhantomData, ops::RangeIn

/// Adapter that allows relayers pallet to be used as a delivery+dispatch payment mechanism
/// for the messages pallet.
pub struct DeliveryConfirmationPaymentsAdapter<T, MI, DeliveryReward>(
PhantomData<(T, MI, DeliveryReward)>,
pub struct DeliveryConfirmationPaymentsAdapter<T, MI, RI, DeliveryReward>(
PhantomData<(T, MI, RI, DeliveryReward)>,
);

impl<T, MI, DeliveryReward> DeliveryConfirmationPayments<T::AccountId, LaneIdOf<T, MI>>
for DeliveryConfirmationPaymentsAdapter<T, MI, DeliveryReward>
impl<T, MI, RI, DeliveryReward> DeliveryConfirmationPayments<T::AccountId, LaneIdOf<T, MI>>
for DeliveryConfirmationPaymentsAdapter<T, MI, RI, DeliveryReward>
where
T: Config + pallet_bridge_messages::Config<MI, LaneId = <T as Config>::LaneId>,
T: Config<RI> + pallet_bridge_messages::Config<MI, LaneId = <T as Config<RI>>::LaneId>,
MI: 'static,
RI: 'static,
DeliveryReward: Get<T::Reward>,
{
type Error = &'static str;
Expand All @@ -54,7 +56,7 @@ where
bp_messages::calc_relayers_rewards::<T::AccountId>(messages_relayers, received_range);
let rewarded_relayers = relayers_rewards.len();

register_relayers_rewards::<T>(
register_relayers_rewards::<T, RI>(
confirmation_relayer,
relayers_rewards,
RewardsAccountParams::new(
Expand All @@ -70,7 +72,7 @@ where
}

// Update rewards to given relayers, optionally rewarding confirmation relayer.
fn register_relayers_rewards<T: Config>(
fn register_relayers_rewards<T: Config<I>, I: 'static>(
confirmation_relayer: &T::AccountId,
relayers_rewards: RelayersRewards<T::AccountId>,
lane_id: RewardsAccountParams<T::LaneId>,
Expand All @@ -84,15 +86,15 @@ fn register_relayers_rewards<T: Config>(
let relayer_reward = T::Reward::saturated_from(messages).saturating_mul(delivery_fee);

if relayer != *confirmation_relayer {
Pallet::<T>::register_relayer_reward(lane_id, &relayer, relayer_reward);
Pallet::<T, I>::register_relayer_reward(lane_id, &relayer, relayer_reward);
} else {
confirmation_relayer_reward =
confirmation_relayer_reward.saturating_add(relayer_reward);
}
}

// finally - pay reward to confirmation relayer
Pallet::<T>::register_relayer_reward(
Pallet::<T, I>::register_relayer_reward(
lane_id,
confirmation_relayer,
confirmation_relayer_reward,
Expand All @@ -115,7 +117,7 @@ mod tests {
#[test]
fn confirmation_relayer_is_rewarded_if_it_has_also_delivered_messages() {
run_test(|| {
register_relayers_rewards::<TestRuntime>(
register_relayers_rewards::<TestRuntime, ()>(
&RELAYER_2,
relayers_rewards(),
test_reward_account_param(),
Expand All @@ -136,7 +138,7 @@ mod tests {
#[test]
fn confirmation_relayer_is_not_rewarded_if_it_has_not_delivered_any_messages() {
run_test(|| {
register_relayers_rewards::<TestRuntime>(
register_relayers_rewards::<TestRuntime, ()>(
&RELAYER_3,
relayers_rewards(),
test_reward_account_param(),
Expand Down
Loading
Loading