Skip to content

Commit ba7cb48

Browse files
bkonturgithub-actions[bot]franciscoaguirreserban300
authored
Make pallet-bridge-rewards generic over RewardKind (paritytech#7492)
Closes: paritytech#7272 Relates to: paritytech#6578 Relates to: paritytech#7274 ## Description The PR enhances the `pallet-bridge-rewards` by making it generic over the `RewardKind` type (previously hardcoded as `RewardsAccountParams`). This modification allows the pallet to support multiple reward types (e.g., P/K bridge, Snowbridge), increasing its flexibility and applicability across various bridge scenarios. Other pallets can register rewards using `bp_relayers::RewardLedger`, which is implemented by the rewards pallet. The runtime can then be configured with different mechanisms for paying/claiming rewards via `bp_relayers::PaymentProcedure` (e.g., see the `pub struct BridgeRewardPayer;` implementation for BridgeHubWestend). ## Follow-up The removed balances/rewards statistics from the complex relay (no longer used) will eventually be reintroduced or fixed in the standalone relayers via paritytech/parity-bridges-common#3004 (comment). --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Francisco Aguirre <[email protected]> Co-authored-by: Serban Iorga <[email protected]>
1 parent 0c258c6 commit ba7cb48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1395
-560
lines changed

.github/workflows/runtimes-matrix.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
{
6969
"name": "bridge-hub-westend",
70-
"package": "bridge-hub-rococo-runtime",
70+
"package": "bridge-hub-westend-runtime",
7171
"path": "cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend",
7272
"header": "cumulus/file_header.txt",
7373
"template": "cumulus/templates/xcm-bench-template.hbs",

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridges/bin/runtime-common/src/extensions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ where
111111
// let's slash registered relayer
112112
RelayersPallet::<T>::slash_and_deregister(
113113
relayer,
114-
ExplicitOrAccountParams::Explicit(SlashAccount::get()),
114+
ExplicitOrAccountParams::Explicit::<_, ()>(SlashAccount::get()),
115115
);
116116
}
117117
}
@@ -182,7 +182,7 @@ where
182182
// let's slash registered relayer
183183
RelayersPallet::<T>::slash_and_deregister(
184184
relayer,
185-
ExplicitOrAccountParams::Explicit(SlashAccount::get()),
185+
ExplicitOrAccountParams::Explicit::<_, ()>(SlashAccount::get()),
186186
);
187187
}
188188
}

bridges/bin/runtime-common/src/mock.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bp_messages::{
2424
ChainWithMessages, HashedLaneId, LaneIdType, MessageNonce,
2525
};
2626
use bp_parachains::SingleParaStoredHeaderDataBuilder;
27-
use bp_relayers::PayRewardFromAccount;
27+
use bp_relayers::{PayRewardFromAccount, RewardsAccountParams};
2828
use bp_runtime::{messages::MessageDispatchResult, Chain, ChainId, Parachain};
2929
use codec::Encode;
3030
use frame_support::{
@@ -70,7 +70,8 @@ pub type BridgedChainHeader =
7070
sp_runtime::generic::Header<BridgedChainBlockNumber, BridgedChainHasher>;
7171

7272
/// Rewards payment procedure.
73-
pub type TestPaymentProcedure = PayRewardFromAccount<Balances, ThisChainAccountId, TestLaneIdType>;
73+
pub type TestPaymentProcedure =
74+
PayRewardFromAccount<Balances, ThisChainAccountId, TestLaneIdType, RewardBalance>;
7475
/// Stake that we are using in tests.
7576
pub type TestStake = ConstU64<5_000>;
7677
/// Stake and slash mechanism to use in tests.
@@ -89,6 +90,8 @@ pub type TestLaneIdType = HashedLaneId;
8990
pub fn test_lane_id() -> TestLaneIdType {
9091
TestLaneIdType::try_new(1, 2).unwrap()
9192
}
93+
/// Reward measurement type.
94+
pub type RewardBalance = u32;
9295

9396
/// Bridged chain id used in tests.
9497
pub const TEST_BRIDGED_CHAIN_ID: ChainId = *b"brdg";
@@ -197,7 +200,7 @@ impl pallet_bridge_messages::Config for TestRuntime {
197200
TestRuntime,
198201
(),
199202
(),
200-
ConstU64<100_000>,
203+
ConstU32<100_000>,
201204
>;
202205
type OnMessagesDelivered = ();
203206

@@ -210,11 +213,12 @@ impl pallet_bridge_messages::Config for TestRuntime {
210213

211214
impl pallet_bridge_relayers::Config for TestRuntime {
212215
type RuntimeEvent = RuntimeEvent;
213-
type Reward = ThisChainBalance;
216+
type RewardBalance = RewardBalance;
217+
type Reward = RewardsAccountParams<pallet_bridge_messages::LaneIdOf<TestRuntime, ()>>;
214218
type PaymentProcedure = TestPaymentProcedure;
215219
type StakeAndSlash = TestStakeAndSlash;
220+
type Balance = ThisChainBalance;
216221
type WeightInfo = ();
217-
type LaneId = TestLaneIdType;
218222
}
219223

220224
/// Dummy message dispatcher.

bridges/modules/relayers/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ frame-system = { workspace = true }
3131
pallet-transaction-payment = { workspace = true }
3232
sp-arithmetic = { workspace = true }
3333
sp-runtime = { workspace = true }
34-
sp-std = { workspace = true }
3534

3635
[dev-dependencies]
3736
bp-parachains = { workspace = true }
@@ -69,7 +68,6 @@ std = [
6968
"sp-core/std",
7069
"sp-io/std",
7170
"sp-runtime/std",
72-
"sp-std/std",
7371
]
7472
runtime-benchmarks = [
7573
"frame-benchmarking/runtime-benchmarks",

bridges/modules/relayers/src/benchmarking.rs

+64-29
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
use crate::*;
2222

23-
use bp_relayers::RewardsAccountOwner;
24-
use frame_benchmarking::{benchmarks_instance_pallet, whitelisted_caller};
23+
use frame_benchmarking::{
24+
benchmarks_instance_pallet, whitelisted_caller, BenchmarkError, BenchmarkResult,
25+
};
26+
use frame_support::{assert_ok, weights::Weight};
2527
use frame_system::RawOrigin;
2628
use sp_runtime::traits::One;
2729

@@ -33,35 +35,70 @@ pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);
3335

3436
/// Trait that must be implemented by runtime.
3537
pub trait Config<I: 'static = ()>: crate::Config<I> {
36-
/// Lane id to use in benchmarks.
37-
fn bench_lane_id() -> Self::LaneId {
38-
Self::LaneId::default()
39-
}
38+
/// `T::Reward` to use in benchmarks.
39+
fn bench_reward() -> Self::Reward;
4040
/// Prepare environment for paying given reward for serving given lane.
4141
fn prepare_rewards_account(
42-
account_params: RewardsAccountParams<Self::LaneId>,
43-
reward: Self::Reward,
44-
);
42+
reward_kind: Self::Reward,
43+
reward: Self::RewardBalance,
44+
) -> Option<BeneficiaryOf<Self, I>>;
4545
/// Give enough balance to given account.
46-
fn deposit_account(account: Self::AccountId, balance: Self::Reward);
46+
fn deposit_account(account: Self::AccountId, balance: Self::Balance);
47+
}
48+
49+
fn assert_last_event<T: Config<I>, I: 'static>(
50+
generic_event: <T as pallet::Config<I>>::RuntimeEvent,
51+
) {
52+
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
4753
}
4854

4955
benchmarks_instance_pallet! {
56+
where_clause { where
57+
BeneficiaryOf<T, I>: From<<T as frame_system::Config>::AccountId>,
58+
}
59+
5060
// Benchmark `claim_rewards` call.
5161
claim_rewards {
52-
let lane = T::bench_lane_id();
53-
let account_params =
54-
RewardsAccountParams::new(lane, *b"test", RewardsAccountOwner::ThisChain);
62+
let reward_kind = T::bench_reward();
5563
let relayer: T::AccountId = whitelisted_caller();
56-
let reward = T::Reward::from(REWARD_AMOUNT);
64+
let reward_balance = T::RewardBalance::from(REWARD_AMOUNT);
5765

58-
T::prepare_rewards_account(account_params, reward);
59-
RelayerRewards::<T, I>::insert(&relayer, account_params, reward);
60-
}: _(RawOrigin::Signed(relayer), account_params)
66+
let _ = T::prepare_rewards_account(reward_kind, reward_balance);
67+
RelayerRewards::<T, I>::insert(&relayer, reward_kind, reward_balance);
68+
}: _(RawOrigin::Signed(relayer.clone()), reward_kind)
6169
verify {
6270
// we can't check anything here, because `PaymentProcedure` is responsible for
6371
// payment logic, so we assume that if call has succeeded, the procedure has
6472
// also completed successfully
73+
assert_last_event::<T, I>(Event::RewardPaid {
74+
relayer: relayer.clone(),
75+
reward_kind,
76+
reward_balance,
77+
beneficiary: relayer.into(),
78+
}.into());
79+
}
80+
81+
// Benchmark `claim_rewards_to` call.
82+
claim_rewards_to {
83+
let reward_kind = T::bench_reward();
84+
let relayer: T::AccountId = whitelisted_caller();
85+
let reward_balance = T::RewardBalance::from(REWARD_AMOUNT);
86+
87+
let Some(alternative_beneficiary) = T::prepare_rewards_account(reward_kind, reward_balance) else {
88+
return Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)));
89+
};
90+
RelayerRewards::<T, I>::insert(&relayer, reward_kind, reward_balance);
91+
}: _(RawOrigin::Signed(relayer.clone()), reward_kind, alternative_beneficiary.clone())
92+
verify {
93+
// we can't check anything here, because `PaymentProcedure` is responsible for
94+
// payment logic, so we assume that if call has succeeded, the procedure has
95+
// also completed successfully
96+
assert_last_event::<T, I>(Event::RewardPaid {
97+
relayer,
98+
reward_kind,
99+
reward_balance,
100+
beneficiary: alternative_beneficiary,
101+
}.into());
65102
}
66103

67104
// Benchmark `register` call.
@@ -95,7 +132,7 @@ benchmarks_instance_pallet! {
95132
}
96133

97134
// Benchmark `slash_and_deregister` method of the pallet. We are adding this weight to
98-
// the weight of message delivery call if `RefundBridgedParachainMessages` signed extension
135+
// the weight of message delivery call if `BridgeRelayersTransactionExtension` signed extension
99136
// is deployed at runtime level.
100137
slash_and_deregister {
101138
// prepare and register relayer account
@@ -105,32 +142,30 @@ benchmarks_instance_pallet! {
105142
.saturating_add(One::one())
106143
.saturating_add(One::one());
107144
T::deposit_account(relayer.clone(), crate::Pallet::<T, I>::required_stake());
108-
crate::Pallet::<T, I>::register(RawOrigin::Signed(relayer.clone()).into(), valid_till).unwrap();
145+
assert_ok!(crate::Pallet::<T, I>::register(RawOrigin::Signed(relayer.clone()).into(), valid_till));
109146

110147
// create slash destination account
111-
let lane = T::bench_lane_id();
112-
let slash_destination = RewardsAccountParams::new(lane, *b"test", RewardsAccountOwner::ThisChain);
113-
T::prepare_rewards_account(slash_destination, Zero::zero());
148+
let slash_destination: T::AccountId = whitelisted_caller();
149+
T::deposit_account(slash_destination.clone(), Zero::zero());
114150
}: {
115-
crate::Pallet::<T, I>::slash_and_deregister(&relayer, slash_destination.into())
151+
crate::Pallet::<T, I>::slash_and_deregister(&relayer, bp_relayers::ExplicitOrAccountParams::Explicit::<_, ()>(slash_destination))
116152
}
117153
verify {
118154
assert!(!crate::Pallet::<T, I>::is_registration_active(&relayer));
119155
}
120156

121157
// Benchmark `register_relayer_reward` method of the pallet. We are adding this weight to
122-
// the weight of message delivery call if `RefundBridgedParachainMessages` signed extension
158+
// the weight of message delivery call if `BridgeRelayersTransactionExtension` signed extension
123159
// is deployed at runtime level.
124160
register_relayer_reward {
125-
let lane = T::bench_lane_id();
161+
let reward_kind = T::bench_reward();
126162
let relayer: T::AccountId = whitelisted_caller();
127-
let account_params =
128-
RewardsAccountParams::new(lane, *b"test", RewardsAccountOwner::ThisChain);
163+
129164
}: {
130-
crate::Pallet::<T, I>::register_relayer_reward(account_params, &relayer, One::one());
165+
crate::Pallet::<T, I>::register_relayer_reward(reward_kind, &relayer, One::one());
131166
}
132167
verify {
133-
assert_eq!(RelayerRewards::<T, I>::get(relayer, &account_params), Some(One::one()));
168+
assert_eq!(RelayerRewards::<T, I>::get(relayer, &reward_kind), Some(One::one()));
134169
}
135170

136171
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)

bridges/modules/relayers/src/extension/grandpa_adapter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{
2323

2424
use bp_relayers::{BatchCallUnpacker, ExtensionCallData, ExtensionCallInfo, ExtensionConfig};
2525
use bp_runtime::{Chain, StaticStrProvider};
26+
use core::marker::PhantomData;
2627
use frame_support::dispatch::{DispatchInfo, PostDispatchInfo};
2728
use frame_system::Config as SystemConfig;
2829
use pallet_bridge_grandpa::{
@@ -37,7 +38,6 @@ use sp_runtime::{
3738
transaction_validity::{TransactionPriority, TransactionValidityError},
3839
Saturating,
3940
};
40-
use sp_std::marker::PhantomData;
4141

4242
/// Adapter to be used in signed extension configuration, when bridging with remote
4343
/// chains that are using GRANDPA finality.

bridges/modules/relayers/src/extension/messages_adapter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::{extension::verify_messages_call_succeeded, Config as BridgeRelayersC
2121

2222
use bp_relayers::{ExtensionCallData, ExtensionCallInfo, ExtensionConfig};
2323
use bp_runtime::StaticStrProvider;
24+
use core::marker::PhantomData;
2425
use frame_support::dispatch::{DispatchInfo, PostDispatchInfo};
2526
use pallet_bridge_messages::{
2627
CallSubType as BridgeMessagesCallSubType, Config as BridgeMessagesConfig, LaneIdOf,
@@ -29,7 +30,6 @@ use sp_runtime::{
2930
traits::{Dispatchable, Get},
3031
transaction_validity::{TransactionPriority, TransactionValidityError},
3132
};
32-
use sp_std::marker::PhantomData;
3333

3434
/// Transaction extension that refunds a relayer for standalone messages delivery and confirmation
3535
/// transactions. Finality transactions are not refunded.

0 commit comments

Comments
 (0)