Skip to content

Commit

Permalink
New relayer rewards scheme integration (paritytech#1652)
Browse files Browse the repository at this point in the history
* relayer rewards integration: initial commit

* added refund-relayer-extension to the millau runtime

* spelling

* spelling again

* new -> Default
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent ff24106 commit d569f7d
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 138 deletions.
40 changes: 28 additions & 12 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,15 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {

type InboundPayload = crate::rialto_messages::FromRialtoMessagePayload;
type InboundRelayer = bp_rialto::AccountId;
type DeliveryPayments = ();

type TargetHeaderChain = crate::rialto_messages::Rialto;
type LaneMessageVerifier = crate::rialto_messages::ToRialtoMessageVerifier;
type MessageDeliveryAndDispatchPayment =
pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU64<100_000>,
frame_support::traits::ConstU64<100_000>,
>;
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU64<100_000>,
frame_support::traits::ConstU64<100_000>,
>;

type SourceHeaderChain = crate::rialto_messages::Rialto;
type MessageDispatch = crate::rialto_messages::FromRialtoMessageDispatch;
Expand All @@ -502,22 +502,24 @@ impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Run

type InboundPayload = crate::rialto_parachain_messages::FromRialtoParachainMessagePayload;
type InboundRelayer = bp_rialto_parachain::AccountId;
type DeliveryPayments = ();

type TargetHeaderChain = crate::rialto_parachain_messages::RialtoParachain;
type LaneMessageVerifier = crate::rialto_parachain_messages::ToRialtoParachainMessageVerifier;
type MessageDeliveryAndDispatchPayment =
pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU64<100_000>,
frame_support::traits::ConstU64<100_000>,
>;
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU64<100_000>,
frame_support::traits::ConstU64<100_000>,
>;

type SourceHeaderChain = crate::rialto_parachain_messages::RialtoParachain;
type MessageDispatch = crate::rialto_parachain_messages::FromRialtoParachainMessageDispatch;
type BridgedChainId = RialtoParachainChainId;
}

parameter_types! {
pub const RialtoParachainMessagesLane: bp_messages::LaneId = rialto_parachain_messages::XCM_LANE;
pub const RialtoParachainId: u32 = bp_rialto_parachain::RIALTO_PARACHAIN_ID;
pub const RialtoParasPalletName: &'static str = bp_rialto::PARAS_PALLET_NAME;
pub const WestendParasPalletName: &'static str = bp_westend::PARAS_PALLET_NAME;
pub const MaxRialtoParaHeadSize: u32 = bp_rialto::MAX_NESTED_PARACHAIN_HEAD_SIZE;
Expand Down Expand Up @@ -613,6 +615,19 @@ generate_bridge_reject_obsolete_headers_and_messages! {
BridgeRialtoMessages, BridgeRialtoParachainMessages
}

/// Signed extension that refunds relayers that are delivering messages from the Rialto parachain.
pub type BridgeRefundRialtoParachainRelayers =
bridge_runtime_common::refund_relayer_extension::RefundRelayerForMessagesFromParachain<
Runtime,
RialtoGrandpaInstance,
WithRialtoParachainsInstance,
WithRialtoParachainMessagesInstance,
BridgeRejectObsoleteHeadersAndMessages,
RialtoParachainId,
RialtoParachainMessagesLane,
Runtime,
>;

/// The address format for describing accounts.
pub type Address = AccountId;
/// Block header type as expected by this runtime.
Expand All @@ -634,6 +649,7 @@ pub type SignedExtra = (
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
BridgeRejectObsoleteHeadersAndMessages,
BridgeRefundRialtoParachainRelayers,
);
/// The payload being signed in transactions.
pub type SignedPayload = generic::SignedPayload<RuntimeCall, SignedExtra>;
Expand Down
12 changes: 6 additions & 6 deletions bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,15 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {

type InboundPayload = crate::millau_messages::FromMillauMessagePayload;
type InboundRelayer = bp_millau::AccountId;
type DeliveryPayments = ();

type TargetHeaderChain = crate::millau_messages::Millau;
type LaneMessageVerifier = crate::millau_messages::ToMillauMessageVerifier;
type MessageDeliveryAndDispatchPayment =
pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU64<100_000>,
frame_support::traits::ConstU64<100_000>,
>;
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU128<100_000>,
frame_support::traits::ConstU128<100_000>,
>;

type SourceHeaderChain = crate::millau_messages::Millau;
type MessageDispatch = crate::millau_messages::FromMillauMessageDispatch;
Expand Down
12 changes: 6 additions & 6 deletions bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,15 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {

type InboundPayload = crate::millau_messages::FromMillauMessagePayload;
type InboundRelayer = bp_millau::AccountId;
type DeliveryPayments = ();

type TargetHeaderChain = crate::millau_messages::Millau;
type LaneMessageVerifier = crate::millau_messages::ToMillauMessageVerifier;
type MessageDeliveryAndDispatchPayment =
pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU64<100_000>,
frame_support::traits::ConstU64<100_000>,
>;
type DeliveryConfirmationPayments = pallet_bridge_relayers::DeliveryConfirmationPaymentsAdapter<
Runtime,
frame_support::traits::ConstU128<100_000>,
frame_support::traits::ConstU128<100_000>,
>;

type SourceHeaderChain = crate::millau_messages::Millau;
type MessageDispatch = crate::millau_messages::FromMillauMessageDispatch;
Expand Down
11 changes: 9 additions & 2 deletions bridges/bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use codec::{Decode, Encode};
use frame_support::{
dispatch::{CallableCallFor, DispatchInfo, Dispatchable, PostDispatchInfo},
traits::IsSubType,
CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
CloneNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use pallet_bridge_grandpa::{
BridgedChain, Call as GrandpaCall, Config as GrandpaConfig, Pallet as GrandpaPallet,
Expand Down Expand Up @@ -95,7 +95,14 @@ where
///
/// Extension does not refund transaction tip due to security reasons.
#[derive(
CloneNoBound, Decode, Encode, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo,
CloneNoBound,
Decode,
DefaultNoBound,
Encode,
EqNoBound,
PartialEqNoBound,
RuntimeDebugNoBound,
TypeInfo,
)]
#[scale_info(skip_type_params(RT, GI, PI, MI, BE, PID, LID, FEE))]
#[allow(clippy::type_complexity)] // TODO: get rid of that in https://github.com/paritytech/parity-bridges-common/issues/1666
Expand Down
47 changes: 28 additions & 19 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ use crate::{

use bp_messages::{
source_chain::{
LaneMessageVerifier, MessageDeliveryAndDispatchPayment, SendMessageArtifacts,
TargetHeaderChain,
DeliveryConfirmationPayments, LaneMessageVerifier, SendMessageArtifacts, TargetHeaderChain,
},
target_chain::{
DispatchMessage, MessageDispatch, ProvedLaneMessages, ProvedMessages, SourceHeaderChain,
DeliveryPayments, DispatchMessage, MessageDispatch, ProvedLaneMessages, ProvedMessages,
SourceHeaderChain,
},
total_unrewarded_messages, DeliveredMessages, InboundLaneData, InboundMessageDetails, LaneId,
MessageKey, MessageNonce, MessagePayload, MessagesOperatingMode, OutboundLaneData,
Expand Down Expand Up @@ -143,18 +143,17 @@ pub mod pallet {
/// Identifier of relayer that deliver messages to this chain. Relayer reward is paid on the
/// bridged chain.
type InboundRelayer: Parameter + MaxEncodedLen;
/// Delivery payments.
type DeliveryPayments: DeliveryPayments<Self::AccountId>;

// Types that are used by outbound_lane (on source chain).

/// Target header chain.
type TargetHeaderChain: TargetHeaderChain<Self::OutboundPayload, Self::AccountId>;
/// Message payload verifier.
type LaneMessageVerifier: LaneMessageVerifier<Self::RuntimeOrigin, Self::OutboundPayload>;
/// Message delivery payment.
type MessageDeliveryAndDispatchPayment: MessageDeliveryAndDispatchPayment<
Self::RuntimeOrigin,
Self::AccountId,
>;
/// Delivery confirmation payments.
type DeliveryConfirmationPayments: DeliveryConfirmationPayments<Self::AccountId>;

// Types that are used by inbound_lane (on target chain).

Expand Down Expand Up @@ -377,9 +376,17 @@ pub mod pallet {
messages_received_status.push(lane_messages_received_status);
}

// let's now deal with relayer payments
T::DeliveryPayments::pay_reward(
relayer_id_at_this_chain,
total_messages,
valid_messages,
actual_weight,
);

log::debug!(
target: LOG_TARGET,
"Received messages: total={}, valid={}. Weight used: {}/{}",
"Received messages: total={}, valid={}. Weight used: {}/{}.",
total_messages,
valid_messages,
actual_weight,
Expand All @@ -388,7 +395,7 @@ pub mod pallet {

Self::deposit_event(Event::MessagesReceived(messages_received_status));

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::No })
}

/// Receive messages delivery proof from bridged chain.
Expand Down Expand Up @@ -475,7 +482,7 @@ pub mod pallet {
});

// if some new messages have been confirmed, reward relayers
<T as Config<I>>::MessageDeliveryAndDispatchPayment::pay_relayers_rewards(
T::DeliveryConfirmationPayments::pay_reward(
lane_id,
lane_data.relayers,
&confirmation_relayer,
Expand Down Expand Up @@ -879,10 +886,10 @@ mod tests {
use super::*;
use crate::mock::{
message, message_payload, run_test, unrewarded_relayer, DbWeight,
RuntimeEvent as TestEvent, RuntimeOrigin, TestMessageDeliveryAndDispatchPayment,
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,
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_test_utils::generate_owned_bridge_module_tests;
Expand Down Expand Up @@ -1165,6 +1172,8 @@ mod tests {
));

assert_eq!(InboundLanes::<TestRuntime>::get(TEST_LANE_ID).0.last_delivered_nonce(), 1);

assert!(TestDeliveryPayments::is_reward_paid(1));
});
}

Expand Down Expand Up @@ -1326,8 +1335,8 @@ mod tests {
..Default::default()
},
));
assert!(TestMessageDeliveryAndDispatchPayment::is_reward_paid(TEST_RELAYER_A, 1));
assert!(!TestMessageDeliveryAndDispatchPayment::is_reward_paid(TEST_RELAYER_B, 1));
assert!(TestDeliveryConfirmationPayments::is_reward_paid(TEST_RELAYER_A, 1));
assert!(!TestDeliveryConfirmationPayments::is_reward_paid(TEST_RELAYER_B, 1));

// this reports delivery of both message 1 and message 2 => reward is paid only to
// TEST_RELAYER_B
Expand All @@ -1352,8 +1361,8 @@ mod tests {
..Default::default()
},
));
assert!(!TestMessageDeliveryAndDispatchPayment::is_reward_paid(TEST_RELAYER_A, 1));
assert!(TestMessageDeliveryAndDispatchPayment::is_reward_paid(TEST_RELAYER_B, 1));
assert!(!TestDeliveryConfirmationPayments::is_reward_paid(TEST_RELAYER_A, 1));
assert!(TestDeliveryConfirmationPayments::is_reward_paid(TEST_RELAYER_B, 1));
});
}

Expand Down
52 changes: 39 additions & 13 deletions bridges/modules/messages/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use crate::Config;

use bp_messages::{
calc_relayers_rewards,
source_chain::{LaneMessageVerifier, MessageDeliveryAndDispatchPayment, TargetHeaderChain},
source_chain::{DeliveryConfirmationPayments, LaneMessageVerifier, TargetHeaderChain},
target_chain::{
DispatchMessage, DispatchMessageData, MessageDispatch, ProvedLaneMessages, ProvedMessages,
SourceHeaderChain,
DeliveryPayments, DispatchMessage, DispatchMessageData, MessageDispatch,
ProvedLaneMessages, ProvedMessages, SourceHeaderChain,
},
DeliveredMessages, InboundLaneData, LaneId, Message, MessageKey, MessageNonce, MessagePayload,
OutboundLaneData, UnrewardedRelayer,
Expand Down Expand Up @@ -154,10 +154,11 @@ impl Config for TestRuntime {

type InboundPayload = TestPayload;
type InboundRelayer = TestRelayer;
type DeliveryPayments = TestDeliveryPayments;

type TargetHeaderChain = TestTargetHeaderChain;
type LaneMessageVerifier = TestLaneMessageVerifier;
type MessageDeliveryAndDispatchPayment = TestMessageDeliveryAndDispatchPayment;
type DeliveryConfirmationPayments = TestDeliveryConfirmationPayments;

type SourceHeaderChain = TestSourceHeaderChain;
type MessageDispatch = TestMessageDispatch;
Expand Down Expand Up @@ -288,11 +289,38 @@ impl LaneMessageVerifier<RuntimeOrigin, TestPayload> for TestLaneMessageVerifier
}
}

/// Message fee payment system that is used in tests.
/// Reward payments at the target chain during delivery transaction.
#[derive(Debug, Default)]
pub struct TestMessageDeliveryAndDispatchPayment;
pub struct TestDeliveryPayments;

impl TestMessageDeliveryAndDispatchPayment {
impl TestDeliveryPayments {
/// Returns true if given relayer has been rewarded with given balance. The reward-paid flag is
/// cleared after the call.
pub fn is_reward_paid(relayer: AccountId) -> bool {
let key = (b":delivery-relayer-reward:", relayer).encode();
frame_support::storage::unhashed::take::<bool>(&key).is_some()
}
}

impl DeliveryPayments<AccountId> for TestDeliveryPayments {
type Error = &'static str;

fn pay_reward(
relayer: AccountId,
_total_messages: MessageNonce,
_valid_messages: MessageNonce,
_actual_weight: Weight,
) {
let key = (b":delivery-relayer-reward:", relayer).encode();
frame_support::storage::unhashed::put(&key, &true);
}
}

/// Reward payments at the source chain during delivery confirmation transaction.
#[derive(Debug, Default)]
pub struct TestDeliveryConfirmationPayments;

impl TestDeliveryConfirmationPayments {
/// Returns true if given relayer has been rewarded with given balance. The reward-paid flag is
/// cleared after the call.
pub fn is_reward_paid(relayer: AccountId, fee: TestMessageFee) -> bool {
Expand All @@ -301,18 +329,16 @@ impl TestMessageDeliveryAndDispatchPayment {
}
}

impl MessageDeliveryAndDispatchPayment<RuntimeOrigin, AccountId>
for TestMessageDeliveryAndDispatchPayment
{
impl DeliveryConfirmationPayments<AccountId> for TestDeliveryConfirmationPayments {
type Error = &'static str;

fn pay_relayers_rewards(
fn pay_reward(
_lane_id: LaneId,
message_relayers: VecDeque<UnrewardedRelayer<AccountId>>,
messages_relayers: VecDeque<UnrewardedRelayer<AccountId>>,
_confirmation_relayer: &AccountId,
received_range: &RangeInclusive<MessageNonce>,
) {
let relayers_rewards = calc_relayers_rewards(message_relayers, received_range);
let relayers_rewards = calc_relayers_rewards(messages_relayers, received_range);
for (relayer, reward) in &relayers_rewards {
let key = (b":relayer-reward:", relayer, reward).encode();
frame_support::storage::unhashed::put(&key, &true);
Expand Down
Loading

0 comments on commit d569f7d

Please sign in to comment.