Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Rewards for relayers setup (#2194)
Browse files Browse the repository at this point in the history
* Rewards for relayers setup

* ".git/.scripts/commands/bench/bench.sh" pallet bridge-hub-rococo bridge-hubs pallet_bridge_relayers

* Setup weight for relayer

* Setup `DeliveryConfirmationPayments` + `RefundRelayerForMessagesFromParachain`

* No need to have more than one collator per parachain

* Setup multi refund signed extensions

* Rewards sign ext test

* test

* fixes

---------

Co-authored-by: command-bot <>
  • Loading branch information
bkontur authored Feb 22, 2023
1 parent dba6f86 commit 22ad782
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"bridges/modules/parachains",
"bridges/modules/relayers",
"bridges/primitives/messages",
"bridges/primitives/relayers",
"bridges/primitives/runtime",
"bridges/primitives/chain-bridge-hub-rococo",
"bridges/primitives/chain-bridge-hub-wococo",
Expand Down
2 changes: 1 addition & 1 deletion docs/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ for `release-parachains-v0.9.270`
5. Clone `it/release-<version>-fast-sudo` from Polkadot
In case the branch does not exists (it is a manual process): cherry pick paritytech/polkadot@791c8b8 and run
`find . -type f -name "*.toml" -print0 | xargs -0 sed -i '' -e 's/polkadot-vX.X.X/polkadot-v<version>/g'`
6. `cargo build --release features --fast-runtime`
6. `cargo build --release --features fast-runtime`
7. Copy `./target/polkadot` into `./bin` (in Cumulus)
8. Run the tests:
- Statemint: `yarn zombienet-test -c ./examples/statemint/config.toml -t ./examples/statemint`
Expand Down
2 changes: 2 additions & 0 deletions parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ bp-bridge-hub-wococo = { path = "../../../../bridges/primitives/chain-bridge-hub
bp-messages = { path = "../../../../bridges/primitives/messages", default-features = false }
bp-parachains = { path = "../../../../bridges/primitives/parachains", default-features = false }
bp-polkadot-core = { path = "../../../../bridges/primitives/polkadot-core", default-features = false }
bp-relayers = { path = "../../../../bridges/primitives/relayers", default-features = false }
bp-runtime = { path = "../../../../bridges/primitives/runtime", default-features = false }
bp-rococo = { path = "../../../../bridges/primitives/chain-rococo", default-features = false }
bp-wococo = { path = "../../../../bridges/primitives/chain-wococo", default-features = false }
Expand All @@ -101,6 +102,7 @@ std = [
"bp-messages/std",
"bp-parachains/std",
"bp-polkadot-core/std",
"bp-relayers/std",
"bp-runtime/std",
"bp-rococo/std",
"bp-wococo/std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ use bridge_runtime_common::{
source::FromBridgedChainMessagesDeliveryProof, target::FromBridgedChainMessagesProof,
MessageBridge, ThisChainWithMessages, UnderlyingChainProvider,
},
refund_relayer_extension::{
ActualFeeRefund, RefundBridgedParachainMessages, RefundableMessagesLane,
RefundableParachain,
},
};
use frame_support::{parameter_types, RuntimeDebug};
use xcm::{
Expand Down Expand Up @@ -87,7 +91,7 @@ const DEFAULT_XCM_LANE_TO_BRIDGE_HUB_WOCOCO: LaneId = LaneId([0, 0, 0, 1]);
pub struct WithBridgeHubWococoMessageBridge;
impl MessageBridge for WithBridgeHubWococoMessageBridge {
const THIS_CHAIN_ID: ChainId = bp_runtime::BRIDGE_HUB_ROCOCO_CHAIN_ID;
const BRIDGED_CHAIN_ID: ChainId = bp_runtime::BRIDGE_HUB_WOCOCO_CHAIN_ID;
const BRIDGED_CHAIN_ID: ChainId = BridgeHubWococoChainId::get();
const BRIDGED_MESSAGES_PALLET_NAME: &'static str =
bp_bridge_hub_rococo::WITH_BRIDGE_HUB_ROCOCO_MESSAGES_PALLET_NAME;
type ThisChain = BridgeHubRococo;
Expand Down Expand Up @@ -147,6 +151,24 @@ impl ThisChainWithMessages for BridgeHubRococo {
}
}

/// Signed extension that refunds relayers that are delivering messages from the Wococo parachain.
pub type BridgeRefundBridgeHubWococoMessages = RefundBridgedParachainMessages<
Runtime,
RefundableParachain<BridgeParachainWococoInstance, BridgeHubWococoParachainId>,
RefundableMessagesLane<WithBridgeHubWococoMessagesInstance, BridgeHubWococoMessagesLane>,
ActualFeeRefund<Runtime>,
StrBridgeRefundBridgeHubWococoMessages,
>;
bp_runtime::generate_static_str_provider!(BridgeRefundBridgeHubWococoMessages);

parameter_types! {
pub const BridgeHubWococoMessagesLane: bp_messages::LaneId = DEFAULT_XCM_LANE_TO_BRIDGE_HUB_WOCOCO;
pub const BridgeHubWococoParachainId: u32 = {
use bp_runtime::Parachain;
BridgeHubWococo::PARACHAIN_ID
};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ use bridge_runtime_common::{
source::FromBridgedChainMessagesDeliveryProof, target::FromBridgedChainMessagesProof,
MessageBridge, ThisChainWithMessages, UnderlyingChainProvider,
},
refund_relayer_extension::{
ActualFeeRefund, RefundBridgedParachainMessages, RefundableMessagesLane,
RefundableParachain,
},
};
use frame_support::{parameter_types, RuntimeDebug};
use xcm::{
Expand Down Expand Up @@ -87,7 +91,7 @@ const DEFAULT_XCM_LANE_TO_BRIDGE_HUB_ROCOCO: LaneId = LaneId([0, 0, 0, 1]);
pub struct WithBridgeHubRococoMessageBridge;
impl MessageBridge for WithBridgeHubRococoMessageBridge {
const THIS_CHAIN_ID: ChainId = bp_runtime::BRIDGE_HUB_WOCOCO_CHAIN_ID;
const BRIDGED_CHAIN_ID: ChainId = bp_runtime::BRIDGE_HUB_ROCOCO_CHAIN_ID;
const BRIDGED_CHAIN_ID: ChainId = BridgeHubRococoChainId::get();
const BRIDGED_MESSAGES_PALLET_NAME: &'static str =
bp_bridge_hub_wococo::WITH_BRIDGE_HUB_WOCOCO_MESSAGES_PALLET_NAME;
type ThisChain = BridgeHubWococo;
Expand Down Expand Up @@ -147,6 +151,24 @@ impl ThisChainWithMessages for BridgeHubWococo {
}
}

/// Signed extension that refunds relayers that are delivering messages from the Rococo parachain.
pub type BridgeRefundBridgeHubRococoMessages = RefundBridgedParachainMessages<
Runtime,
RefundableParachain<BridgeParachainRococoInstance, BridgeHubRococoParachainId>,
RefundableMessagesLane<WithBridgeHubRococoMessagesInstance, BridgeHubRococoMessagesLane>,
ActualFeeRefund<Runtime>,
StrBridgeRefundBridgeHubRococoMessages,
>;
bp_runtime::generate_static_str_provider!(BridgeRefundBridgeHubRococoMessages);

parameter_types! {
pub const BridgeHubRococoMessagesLane: bp_messages::LaneId = DEFAULT_XCM_LANE_TO_BRIDGE_HUB_ROCOCO;
pub const BridgeHubRococoParachainId: u32 = {
use bp_runtime::Parachain;
BridgeHubRococo::PARACHAIN_ID
};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
124 changes: 90 additions & 34 deletions parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

use crate::{
bridge_hub_rococo_config::{OnBridgeHubRococoBlobDispatcher, WithBridgeHubWococoMessageBridge},
bridge_hub_wococo_config::{OnBridgeHubWococoBlobDispatcher, WithBridgeHubRococoMessageBridge},
bridge_hub_rococo_config::{
BridgeRefundBridgeHubWococoMessages, OnBridgeHubRococoBlobDispatcher,
WithBridgeHubWococoMessageBridge,
},
bridge_hub_wococo_config::{
BridgeRefundBridgeHubRococoMessages, OnBridgeHubWococoBlobDispatcher,
WithBridgeHubRococoMessageBridge,
},
constants::fee::WeightToFee,
xcm_config::XcmRouter,
};
Expand Down Expand Up @@ -113,6 +119,7 @@ pub type SignedExtra = (
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
BridgeRejectObsoleteHeadersAndMessages,
(BridgeRefundBridgeHubRococoMessages, BridgeRefundBridgeHubWococoMessages),
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down Expand Up @@ -398,7 +405,7 @@ impl pallet_bridge_grandpa::Config<BridgeGrandpaWococoInstance> for Runtime {
type WeightInfo = weights::pallet_bridge_grandpa_bridge_wococo_grandpa::WeightInfo<Runtime>;
}

/// Add granda bridge pallet to track Rococo relay chain
/// Add granda bridge pallet to track Rococo relay chain on Wococo BridgeHub
pub type BridgeGrandpaRococoInstance = pallet_bridge_grandpa::Instance2;
impl pallet_bridge_grandpa::Config<BridgeGrandpaRococoInstance> for Runtime {
type BridgedChain = bp_rococo::Rococo;
Expand All @@ -407,17 +414,19 @@ impl pallet_bridge_grandpa::Config<BridgeGrandpaRococoInstance> for Runtime {
type WeightInfo = weights::pallet_bridge_grandpa_bridge_rococo_grandpa::WeightInfo<Runtime>;
}

pub const ROCOCO_BRIDGE_PARA_PALLET_NAME: &str = "Paras";
pub const WOCOCO_BRIDGE_PARA_PALLET_NAME: &str = "Paras";
parameter_types! {
pub const RelayChainHeadersToKeep: u32 = 1024;
pub const ParachainHeadsToKeep: u32 = 64;
pub const MaxRequests: u32 = 64;

pub const RococoBridgeParachainPalletName: &'static str = ROCOCO_BRIDGE_PARA_PALLET_NAME;
pub const WococoBridgeParachainPalletName: &'static str = WOCOCO_BRIDGE_PARA_PALLET_NAME;
pub const RococoBridgeParachainPalletName: &'static str = "Paras";
pub const WococoBridgeParachainPalletName: &'static str = "Paras";
pub const MaxRococoParaHeadDataSize: u32 = bp_rococo::MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE;
pub const MaxWococoParaHeadDataSize: u32 = bp_wococo::MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE;

// TODO:check-parameter - setup initial values https://github.com/paritytech/parity-bridges-common/issues/1677
pub storage DeliveryRewardInBalance: u64 = 1_000_000;
pub storage ConfirmationRewardInBalance: u64 = 100_000;
}

/// Add parachain bridge pallet to track Wococo bridge hub parachain
Expand Down Expand Up @@ -464,13 +473,15 @@ impl pallet_bridge_messages::Config<WithBridgeHubWococoMessagesInstance> for Run

type InboundPayload = XcmAsPlainPayload;
type InboundRelayer = AccountId;
// TODO:check-parameter - check delivery
type DeliveryPayments = ();

type TargetHeaderChain = TargetHeaderChainAdapter<WithBridgeHubWococoMessageBridge>;
type LaneMessageVerifier = bridge_hub_rococo_config::ToBridgeHubWococoMessageVerifier;
// TODO:check-parameter - check delivery
type DeliveryConfirmationPayments = ();
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
DeliveryRewardInBalance,
ConfirmationRewardInBalance,
>;

type SourceHeaderChain = SourceHeaderChainAdapter<WithBridgeHubWococoMessageBridge>;
type MessageDispatch = XcmBlobMessageDispatch<
Expand Down Expand Up @@ -498,13 +509,15 @@ impl pallet_bridge_messages::Config<WithBridgeHubRococoMessagesInstance> for Run

type InboundPayload = XcmAsPlainPayload;
type InboundRelayer = AccountId;
// TODO:check-parameter - check delivery
type DeliveryPayments = ();

type TargetHeaderChain = TargetHeaderChainAdapter<WithBridgeHubRococoMessageBridge>;
type LaneMessageVerifier = bridge_hub_wococo_config::ToBridgeHubRococoMessageVerifier;
// TODO:check-parameter - check delivery
type DeliveryConfirmationPayments = ();
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
DeliveryRewardInBalance,
ConfirmationRewardInBalance,
>;

type SourceHeaderChain = SourceHeaderChainAdapter<WithBridgeHubRococoMessageBridge>;
type MessageDispatch = XcmBlobMessageDispatch<
Expand All @@ -514,6 +527,15 @@ impl pallet_bridge_messages::Config<WithBridgeHubRococoMessagesInstance> for Run
>;
}

/// Allows collect and claim rewards for relayers
impl pallet_bridge_relayers::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Reward = Balance;
type PaymentProcedure =
bp_relayers::PayLaneRewardFromAccount<pallet_balances::Pallet<Runtime>, AccountId>;
type WeightInfo = weights::pallet_bridge_relayers::WeightInfo<Runtime>;
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime where
Expand Down Expand Up @@ -564,6 +586,8 @@ construct_runtime!(
BridgeRococoGrandpa: pallet_bridge_grandpa::<Instance2>::{Pallet, Call, Storage, Config<T>} = 43,
BridgeRococoParachain: pallet_bridge_parachains::<Instance2>::{Pallet, Call, Storage, Event<T>} = 44,
BridgeRococoMessages: pallet_bridge_messages::<Instance2>::{Pallet, Call, Storage, Event<T>, Config<T>} = 45,

BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>} = 47,
}
);

Expand Down Expand Up @@ -606,6 +630,8 @@ mod benches {
[pallet_bridge_grandpa, BridgeRococoGrandpa]
[pallet_bridge_parachains, BridgeParachainsBench::<Runtime, BridgeParachainRococoInstance>]
[pallet_bridge_messages, BridgeMessagesBench::<Runtime, WithBridgeHubRococoMessagesInstance>]
// Bridge relayer pallets
[pallet_bridge_relayers, BridgeRelayersBench::<Runtime>]
);
}

Expand Down Expand Up @@ -841,6 +867,7 @@ impl_runtime_apis! {

use pallet_bridge_parachains::benchmarking::Pallet as BridgeParachainsBench;
use pallet_bridge_messages::benchmarking::Pallet as BridgeMessagesBench;
use pallet_bridge_relayers::benchmarking::Pallet as BridgeRelayersBench;

let mut list = Vec::<BenchmarkList>::new();
list_benchmarks!(list, extra);
Expand Down Expand Up @@ -950,9 +977,9 @@ impl_runtime_apis! {
};

impl BridgeMessagesConfig<WithBridgeHubWococoMessagesInstance> for Runtime {
fn is_relayer_rewarded(_: &Self::AccountId) -> bool {
// TODO: implement me properly
true
fn is_relayer_rewarded(relayer: &Self::AccountId) -> bool {
let bench_lane_id = <Self as BridgeMessagesConfig<WithBridgeHubWococoMessagesInstance>>::bench_lane_id();
pallet_bridge_relayers::Pallet::<Runtime>::relayer_reward(relayer, &bench_lane_id).is_some()
}

fn prepare_message_proof(
Expand All @@ -977,9 +1004,9 @@ impl_runtime_apis! {
}

impl BridgeMessagesConfig<WithBridgeHubRococoMessagesInstance> for Runtime {
fn is_relayer_rewarded(_: &Self::AccountId) -> bool {
// TODO: implement me properly
true
fn is_relayer_rewarded(relayer: &Self::AccountId) -> bool {
let bench_lane_id = <Self as BridgeMessagesConfig<WithBridgeHubRococoMessagesInstance>>::bench_lane_id();
pallet_bridge_relayers::Pallet::<Runtime>::relayer_reward(relayer, &bench_lane_id).is_some()
}

fn prepare_message_proof(
Expand Down Expand Up @@ -1008,6 +1035,10 @@ impl_runtime_apis! {
Config as BridgeParachainsConfig,
Pallet as BridgeParachainsBench,
};
use pallet_bridge_relayers::benchmarking::{
Pallet as BridgeRelayersBench,
Config as BridgeRelayersConfig,
};

impl BridgeParachainsConfig<BridgeParachainWococoInstance> for Runtime {
fn parachains() -> Vec<bp_polkadot_core::parachains::ParaId> {
Expand Down Expand Up @@ -1057,6 +1088,20 @@ impl_runtime_apis! {
}
}

impl BridgeRelayersConfig for Runtime {
fn prepare_environment(
lane: bp_messages::LaneId,
reward: Balance,
) {
use frame_support::traits::fungible::Mutate;
let lane_rewards_account = bp_relayers::PayLaneRewardFromAccount::<
Balances,
AccountId
>::lane_rewards_account(lane);
Balances::mint_into(&lane_rewards_account, reward).unwrap();
}
}

let whitelist: Vec<TrackedStorageKey> = vec![
// Block Number
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
Expand Down Expand Up @@ -1111,37 +1156,48 @@ cumulus_pallet_parachain_system::register_validate_block! {
#[cfg(test)]
mod tests {
use super::*;
use bridge_runtime_common::integrity::check_additional_signed;
use bp_runtime::TransactionEra;
use bridge_hub_test_utils::test_header;
use codec::Encode;
use sp_runtime::generic::Era;

#[test]
fn ensure_signed_extension_definition_is_correct() {
fn ensure_signed_extension_definition_is_compatible_with_relay() {
let payload: SignedExtra = (
frame_system::CheckNonZeroSender::new(),
frame_system::CheckSpecVersion::new(),
frame_system::CheckTxVersion::new(),
frame_system::CheckGenesis::new(),
frame_system::CheckEra::from(Era::Immortal),
frame_system::CheckEra::from(sp_runtime::generic::Era::Immortal),
frame_system::CheckNonce::from(10),
frame_system::CheckWeight::new(),
pallet_transaction_payment::ChargeTransactionPayment::from(10),
BridgeRejectObsoleteHeadersAndMessages {},
(
BridgeRefundBridgeHubRococoMessages::default(),
BridgeRefundBridgeHubWococoMessages::default(),
),
);

let bhr_indirect_payload = bp_bridge_hub_rococo::SignedExtension::new(
((), (), (), (), Era::Immortal, 10.into(), (), 10.into(), ()),
None,
);
let bhr_indirect_payload =
bp_bridge_hub_rococo::rewarding_bridge_signed_extension::from_params(
10,
10,
TransactionEra::Immortal,
test_header::<bridge_hub_test_utils::RelayBlockHeader>(1).hash(),
10,
10,
);
assert_eq!(payload.encode(), bhr_indirect_payload.encode());

let bhw_indirect_payload = bp_bridge_hub_wococo::SignedExtension::new(
((), (), (), (), Era::Immortal, 10.into(), (), 10.into(), ()),
None,
);
let bhw_indirect_payload =
bp_bridge_hub_wococo::rewarding_bridge_signed_extension::from_params(
10,
10,
TransactionEra::Immortal,
test_header::<bridge_hub_test_utils::RelayBlockHeader>(1).hash(),
10,
10,
);
assert_eq!(payload.encode(), bhw_indirect_payload.encode());

check_additional_signed::<SignedExtra, bp_bridge_hub_rococo::SignedExtension>();
check_additional_signed::<SignedExtra, bp_bridge_hub_wococo::SignedExtension>();
}
}
Loading

0 comments on commit 22ad782

Please sign in to comment.