Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Companion for upstream#1653 relayers
Browse files Browse the repository at this point in the history
  • Loading branch information
Guantong committed Apr 23, 2023
1 parent 2464f01 commit eb26684
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
2 changes: 0 additions & 2 deletions modules/relayers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ scale-info = { workspace = true }
# darwinia
bp-messages = { workspace = true, default-features = false }
bp-relayers = { workspace = true, default-features = false }
pallet-bridge-messages = { workspace = true, default-features = false }

# paritytech
frame-benchmarking = { workspace = true, default-features = false, optional = true }
Expand All @@ -41,7 +40,6 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-bridge-messages/std",
"scale-info/std",
"sp-arithmetic/std",
"sp-runtime/std",
Expand Down
2 changes: 1 addition & 1 deletion modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sp_std::marker::PhantomData;
use weights::WeightInfo;

pub use pallet::*;
pub use payment_adapter::MessageDeliveryAndDispatchPaymentAdapter;
pub use payment_adapter::DeliveryConfirmationPaymentsAdapter;

pub mod benchmarking;

Expand Down
36 changes: 1 addition & 35 deletions modules/relayers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

use crate as pallet_bridge_relayers;

use bp_messages::{
source_chain::ForbidOutboundMessages, target_chain::ForbidInboundMessages, LaneId,
};
use bp_messages::LaneId;
use bp_relayers::PaymentProcedure;
use frame_support::{parameter_types, weights::RuntimeDbWeight};
use sp_core::H256;
Expand All @@ -43,42 +41,10 @@ frame_support::construct_runtime! {
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Event<T>},
Messages: pallet_bridge_messages::{Pallet, Event<T>},
Relayers: pallet_bridge_relayers::{Pallet, Call, Event<T>},
}
}

parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1, write: 2 };
}

impl frame_system::Config for TestRuntime {
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type RuntimeCall = RuntimeCall;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = SubstrateHeader;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = frame_support::traits::ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type BaseCallFilter = frame_support::traits::Everything;
type SystemWeightInfo = ();
type BlockWeights = ();
type BlockLength = ();
type DbWeight = DbWeight;
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl pallet_balances::Config for TestRuntime {
type MaxLocks = ();
type Balance = Balance;
Expand Down
55 changes: 41 additions & 14 deletions modules/relayers/src/payment_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@
use crate::{Config, Pallet};

use bp_messages::source_chain::{MessageDeliveryAndDispatchPayment, RelayersRewards};
use frame_support::sp_runtime::SaturatedConversion;
use bp_messages::source_chain::{
DeliveryConfirmationPayments, MessageDeliveryAndDispatchPayment, RelayersRewards,
};
use frame_support::{sp_runtime::SaturatedConversion, traits::Get};
use sp_arithmetic::traits::{Saturating, UniqueSaturatedFrom, Zero};
use sp_std::{collections::vec_deque::VecDeque, marker::PhantomData, ops::RangeInclusive};

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

impl<T, MessagesInstance> MessageDeliveryAndDispatchPayment<T::RuntimeOrigin, T::AccountId>
for MessageDeliveryAndDispatchPaymentAdapter<T, MessagesInstance>
// TODO (https://github.com/paritytech/parity-bridges-common/pull/1652): this impl must be removed
impl<T, DeliveryReward, ConfirmationReward>
MessageDeliveryAndDispatchPayment<T::RuntimeOrigin, T::AccountId>
for DeliveryConfirmationPaymentsAdapter<T, DeliveryReward, ConfirmationReward>
where
T: Config + pallet_bridge_messages::Config<MessagesInstance>,
MessagesInstance: 'static,
T: Config,
{
type Error = &'static str;

Expand All @@ -44,23 +47,47 @@ where
confirmation_relayer: &T::AccountId,
received_range: &RangeInclusive<bp_messages::MessageNonce>,
) {
let relayers_rewards = pallet_bridge_messages::calc_relayers_rewards::<T, MessagesInstance>(
messages_relayers,
received_range,
);
let relayers_rewards =
bp_messages::calc_relayers_rewards::<T::AccountId>(messages_relayers, received_range);

register_relayers_rewards::<T>(
confirmation_relayer,
relayers_rewards,
lane_id,
// TODO (https://github.com/paritytech/parity-bridges-common/issues/1318): this shall be fixed
// in some way. ATM the future of the `register_relayer_reward` is not yet known
100_000_u32.into(),
10_000_u32.into(),
);
}
}

impl<T, DeliveryReward, ConfirmationReward> DeliveryConfirmationPayments<T::AccountId>
for DeliveryConfirmationPaymentsAdapter<T, DeliveryReward, ConfirmationReward>
where
T: Config,
DeliveryReward: Get<T::Reward>,
ConfirmationReward: Get<T::Reward>,
{
type Error = &'static str;

fn pay_reward(
lane_id: bp_messages::LaneId,
messages_relayers: VecDeque<bp_messages::UnrewardedRelayer<T::AccountId>>,
confirmation_relayer: &T::AccountId,
received_range: &RangeInclusive<bp_messages::MessageNonce>,
) {
let relayers_rewards =
bp_messages::calc_relayers_rewards::<T::AccountId>(messages_relayers, received_range);

register_relayers_rewards::<T>(
confirmation_relayer,
relayers_rewards,
lane_id,
DeliveryReward::get(),
ConfirmationReward::get(),
);
}
}

// Update rewards to given relayers, optionally rewarding confirmation relayer.
fn register_relayers_rewards<T: Config>(
confirmation_relayer: &T::AccountId,
Expand Down

0 comments on commit eb26684

Please sign in to comment.