mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-09 19:57:59 +00:00
Squashed 'bridges/' content from commit 062554430
git-subtree-dir: bridges git-subtree-split: 0625544309ff299307f7e110f252f04eac383102
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
[package]
|
||||
name = "pallet-bridge-relayers"
|
||||
description = "Module used to store relayer rewards and coordinate relayers set."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
|
||||
log = { version = "0.4.17", default-features = false }
|
||||
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
|
||||
|
||||
# Bridge dependencies
|
||||
|
||||
bp-messages = { path = "../../primitives/messages", default-features = false }
|
||||
bp-relayers = { path = "../../primitives/relayers", default-features = false }
|
||||
pallet-bridge-messages = { path = "../messages", default-features = false }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
bp-runtime = { path = "../../primitives/runtime" }
|
||||
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-messages/std",
|
||||
"bp-relayers/std",
|
||||
"codec/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"log/std",
|
||||
"pallet-bridge-messages/std",
|
||||
"scale-info/std",
|
||||
"sp-arithmetic/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
]
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Benchmarks for the relayers Pallet.
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use crate::*;
|
||||
|
||||
use frame_benchmarking::{benchmarks, whitelisted_caller};
|
||||
use frame_system::RawOrigin;
|
||||
|
||||
/// Reward amount that is (hopefully) is larger than existential deposit across all chains.
|
||||
const REWARD_AMOUNT: u32 = u32::MAX;
|
||||
|
||||
benchmarks! {
|
||||
// Benchmark `claim_rewards` call.
|
||||
claim_rewards {
|
||||
let lane = [0, 0, 0, 0];
|
||||
let relayer: T::AccountId = whitelisted_caller();
|
||||
RelayerRewards::<T>::insert(&relayer, lane, T::Reward::from(REWARD_AMOUNT));
|
||||
}: _(RawOrigin::Signed(relayer), lane)
|
||||
verify {
|
||||
// we can't check anything here, because `PaymentProcedure` is responsible for
|
||||
// payment logic, so we assume that if call has succeeded, the procedure has
|
||||
// also completed successfully
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Runtime module that is used to store relayer rewards and (in the future) to
|
||||
//! coordinate relations between relayers.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use bp_messages::LaneId;
|
||||
use bp_relayers::PaymentProcedure;
|
||||
use frame_support::sp_runtime::Saturating;
|
||||
use sp_arithmetic::traits::{AtLeast32BitUnsigned, Zero};
|
||||
use sp_std::marker::PhantomData;
|
||||
use weights::WeightInfo;
|
||||
|
||||
pub use pallet::*;
|
||||
pub use payment_adapter::MessageDeliveryAndDispatchPaymentAdapter;
|
||||
|
||||
mod benchmarking;
|
||||
mod mock;
|
||||
mod payment_adapter;
|
||||
|
||||
pub mod weights;
|
||||
|
||||
/// The target that will be used when publishing logs related to this pallet.
|
||||
pub const LOG_TARGET: &str = "runtime::bridge-relayers";
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
/// The overarching event type.
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
/// Type of relayer reward.
|
||||
type Reward: AtLeast32BitUnsigned + Copy + Parameter + MaxEncodedLen;
|
||||
/// Pay rewards adapter.
|
||||
type PaymentProcedure: PaymentProcedure<Self::AccountId, Self::Reward>;
|
||||
/// Pallet call weights.
|
||||
type WeightInfo: WeightInfo;
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_store(pub(super) trait Store)]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
/// Claim accumulated rewards.
|
||||
#[pallet::weight(T::WeightInfo::claim_rewards())]
|
||||
pub fn claim_rewards(origin: OriginFor<T>, lane_id: LaneId) -> DispatchResult {
|
||||
let relayer = ensure_signed(origin)?;
|
||||
|
||||
RelayerRewards::<T>::try_mutate_exists(
|
||||
&relayer,
|
||||
lane_id,
|
||||
|maybe_reward| -> DispatchResult {
|
||||
let reward = maybe_reward.take().ok_or(Error::<T>::NoRewardForRelayer)?;
|
||||
T::PaymentProcedure::pay_reward(&relayer, lane_id, reward).map_err(|e| {
|
||||
log::trace!(
|
||||
target: LOG_TARGET,
|
||||
"Failed to pay {:?} rewards to {:?}: {:?}",
|
||||
lane_id,
|
||||
relayer,
|
||||
e,
|
||||
);
|
||||
Error::<T>::FailedToPayReward
|
||||
})?;
|
||||
|
||||
Self::deposit_event(Event::<T>::RewardPaid {
|
||||
relayer: relayer.clone(),
|
||||
lane_id,
|
||||
reward,
|
||||
});
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> Pallet<T> {
|
||||
/// Register reward for given relayer.
|
||||
pub fn register_relayer_reward(lane_id: LaneId, relayer: &T::AccountId, reward: T::Reward) {
|
||||
if reward.is_zero() {
|
||||
return
|
||||
}
|
||||
|
||||
RelayerRewards::<T>::mutate(relayer, lane_id, |old_reward: &mut Option<T::Reward>| {
|
||||
let new_reward = old_reward.unwrap_or_else(Zero::zero).saturating_add(reward);
|
||||
*old_reward = Some(new_reward);
|
||||
|
||||
log::trace!(
|
||||
target: crate::LOG_TARGET,
|
||||
"Relayer {:?} can now claim reward: {:?}",
|
||||
relayer,
|
||||
new_reward,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
/// Reward has been paid to the relayer.
|
||||
RewardPaid {
|
||||
/// Relayer account that has been rewarded.
|
||||
relayer: T::AccountId,
|
||||
/// Relayer has received reward for serving this lane.
|
||||
lane_id: LaneId,
|
||||
/// Reward amount.
|
||||
reward: T::Reward,
|
||||
},
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
/// No reward can be claimed by given relayer.
|
||||
NoRewardForRelayer,
|
||||
/// Reward payment procedure has failed.
|
||||
FailedToPayReward,
|
||||
}
|
||||
|
||||
/// Map of the relayer => accumulated reward.
|
||||
#[pallet::storage]
|
||||
pub type RelayerRewards<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
T::AccountId,
|
||||
Identity,
|
||||
LaneId,
|
||||
T::Reward,
|
||||
OptionQuery,
|
||||
>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use mock::{RuntimeEvent as TestEvent, *};
|
||||
|
||||
use crate::Event::RewardPaid;
|
||||
use frame_support::{assert_noop, assert_ok, traits::fungible::Inspect};
|
||||
use frame_system::{EventRecord, Pallet as System, Phase};
|
||||
use sp_runtime::DispatchError;
|
||||
|
||||
fn get_ready_for_events() {
|
||||
System::<TestRuntime>::set_block_number(1);
|
||||
System::<TestRuntime>::reset_events();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_cant_claim_anything() {
|
||||
run_test(|| {
|
||||
assert_noop!(
|
||||
Pallet::<TestRuntime>::claim_rewards(RuntimeOrigin::root(), TEST_LANE_ID),
|
||||
DispatchError::BadOrigin,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relayer_cant_claim_if_no_reward_exists() {
|
||||
run_test(|| {
|
||||
assert_noop!(
|
||||
Pallet::<TestRuntime>::claim_rewards(
|
||||
RuntimeOrigin::signed(REGULAR_RELAYER),
|
||||
TEST_LANE_ID
|
||||
),
|
||||
Error::<TestRuntime>::NoRewardForRelayer,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relayer_cant_claim_if_payment_procedure_fails() {
|
||||
run_test(|| {
|
||||
RelayerRewards::<TestRuntime>::insert(FAILING_RELAYER, TEST_LANE_ID, 100);
|
||||
assert_noop!(
|
||||
Pallet::<TestRuntime>::claim_rewards(
|
||||
RuntimeOrigin::signed(FAILING_RELAYER),
|
||||
TEST_LANE_ID
|
||||
),
|
||||
Error::<TestRuntime>::FailedToPayReward,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relayer_can_claim_reward() {
|
||||
run_test(|| {
|
||||
get_ready_for_events();
|
||||
|
||||
RelayerRewards::<TestRuntime>::insert(REGULAR_RELAYER, TEST_LANE_ID, 100);
|
||||
assert_ok!(Pallet::<TestRuntime>::claim_rewards(
|
||||
RuntimeOrigin::signed(REGULAR_RELAYER),
|
||||
TEST_LANE_ID
|
||||
));
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(REGULAR_RELAYER, TEST_LANE_ID), None);
|
||||
|
||||
//Check if the `RewardPaid` event was emitted.
|
||||
assert_eq!(
|
||||
System::<TestRuntime>::events(),
|
||||
vec![EventRecord {
|
||||
phase: Phase::Initialization,
|
||||
event: TestEvent::Relayers(RewardPaid {
|
||||
relayer: REGULAR_RELAYER,
|
||||
lane_id: TEST_LANE_ID,
|
||||
reward: 100
|
||||
}),
|
||||
topics: vec![],
|
||||
}],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mint_reward_payment_procedure_actually_mints_tokens() {
|
||||
type Balances = pallet_balances::Pallet<TestRuntime>;
|
||||
|
||||
run_test(|| {
|
||||
assert_eq!(Balances::balance(&1), 0);
|
||||
assert_eq!(Balances::total_issuance(), 0);
|
||||
bp_relayers::MintReward::<Balances, AccountId>::pay_reward(&1, TEST_LANE_ID, 100)
|
||||
.unwrap();
|
||||
assert_eq!(Balances::balance(&1), 100);
|
||||
assert_eq!(Balances::total_issuance(), 100);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use crate as pallet_bridge_relayers;
|
||||
|
||||
use bp_messages::{
|
||||
source_chain::ForbidOutboundMessages, target_chain::ForbidInboundMessages, LaneId,
|
||||
};
|
||||
use bp_relayers::PaymentProcedure;
|
||||
use frame_support::{parameter_types, weights::RuntimeDbWeight};
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
testing::Header as SubstrateHeader,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
};
|
||||
|
||||
pub type AccountId = u64;
|
||||
pub type Balance = u64;
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<TestRuntime>;
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
|
||||
|
||||
frame_support::construct_runtime! {
|
||||
pub enum TestRuntime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
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;
|
||||
type DustRemoval = ();
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type ExistentialDeposit = frame_support::traits::ConstU64<1>;
|
||||
type AccountStore = frame_system::Pallet<TestRuntime>;
|
||||
type WeightInfo = ();
|
||||
type MaxReserves = ();
|
||||
type ReserveIdentifier = ();
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const TestBridgedChainId: bp_runtime::ChainId = *b"test";
|
||||
pub ActiveOutboundLanes: &'static [bp_messages::LaneId] = &[[0, 0, 0, 0]];
|
||||
}
|
||||
|
||||
// we're not testing messages pallet here, so values in this config might be crazy
|
||||
impl pallet_bridge_messages::Config for TestRuntime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type WeightInfo = ();
|
||||
type ActiveOutboundLanes = ActiveOutboundLanes;
|
||||
type MaxUnrewardedRelayerEntriesAtInboundLane = frame_support::traits::ConstU64<8>;
|
||||
type MaxUnconfirmedMessagesAtInboundLane = frame_support::traits::ConstU64<8>;
|
||||
|
||||
type MaximalOutboundPayloadSize = frame_support::traits::ConstU32<1024>;
|
||||
type OutboundPayload = ();
|
||||
|
||||
type InboundPayload = ();
|
||||
type InboundRelayer = AccountId;
|
||||
|
||||
type TargetHeaderChain = ForbidOutboundMessages;
|
||||
type LaneMessageVerifier = ForbidOutboundMessages;
|
||||
type MessageDeliveryAndDispatchPayment = ();
|
||||
|
||||
type SourceHeaderChain = ForbidInboundMessages;
|
||||
type MessageDispatch = ForbidInboundMessages;
|
||||
type BridgedChainId = TestBridgedChainId;
|
||||
}
|
||||
|
||||
impl pallet_bridge_relayers::Config for TestRuntime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type Reward = Balance;
|
||||
type PaymentProcedure = TestPaymentProcedure;
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
/// Message lane that we're using in tests.
|
||||
pub const TEST_LANE_ID: LaneId = [0, 0, 0, 0];
|
||||
|
||||
/// Regular relayer that may receive rewards.
|
||||
pub const REGULAR_RELAYER: AccountId = 1;
|
||||
|
||||
/// Relayer that can't receive rewards.
|
||||
pub const FAILING_RELAYER: AccountId = 2;
|
||||
|
||||
/// Payment procedure that rejects payments to the `FAILING_RELAYER`.
|
||||
pub struct TestPaymentProcedure;
|
||||
|
||||
impl PaymentProcedure<AccountId, Balance> for TestPaymentProcedure {
|
||||
type Error = ();
|
||||
|
||||
fn pay_reward(
|
||||
relayer: &AccountId,
|
||||
_lane_id: LaneId,
|
||||
_reward: Balance,
|
||||
) -> Result<(), Self::Error> {
|
||||
match *relayer {
|
||||
FAILING_RELAYER => Err(()),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run pallet test.
|
||||
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
|
||||
let t = frame_system::GenesisConfig::default().build_storage::<TestRuntime>().unwrap();
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
ext.execute_with(test)
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Code that allows relayers pallet to be used as a delivery+dispatch payment mechanism
|
||||
//! for the messages pallet.
|
||||
|
||||
use crate::{Config, Pallet};
|
||||
|
||||
use bp_messages::source_chain::{MessageDeliveryAndDispatchPayment, RelayersRewards};
|
||||
use frame_support::sp_runtime::SaturatedConversion;
|
||||
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)>,
|
||||
);
|
||||
|
||||
impl<T, MessagesInstance> MessageDeliveryAndDispatchPayment<T::RuntimeOrigin, T::AccountId>
|
||||
for MessageDeliveryAndDispatchPaymentAdapter<T, MessagesInstance>
|
||||
where
|
||||
T: Config + pallet_bridge_messages::Config<MessagesInstance>,
|
||||
MessagesInstance: 'static,
|
||||
{
|
||||
type Error = &'static str;
|
||||
|
||||
fn pay_relayers_rewards(
|
||||
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 = pallet_bridge_messages::calc_relayers_rewards::<T, MessagesInstance>(
|
||||
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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Update rewards to given relayers, optionally rewarding confirmation relayer.
|
||||
fn register_relayers_rewards<T: Config>(
|
||||
confirmation_relayer: &T::AccountId,
|
||||
relayers_rewards: RelayersRewards<T::AccountId>,
|
||||
lane_id: bp_messages::LaneId,
|
||||
delivery_fee: T::Reward,
|
||||
confirmation_fee: T::Reward,
|
||||
) {
|
||||
// reward every relayer except `confirmation_relayer`
|
||||
let mut confirmation_relayer_reward = T::Reward::zero();
|
||||
for (relayer, messages) in relayers_rewards {
|
||||
// sane runtime configurations guarantee that the number of messages will be below
|
||||
// `u32::MAX`
|
||||
let mut relayer_reward =
|
||||
T::Reward::unique_saturated_from(messages).saturating_mul(delivery_fee);
|
||||
|
||||
if relayer != *confirmation_relayer {
|
||||
// If delivery confirmation is submitted by other relayer, let's deduct confirmation fee
|
||||
// from relayer reward.
|
||||
//
|
||||
// If confirmation fee has been increased (or if it was the only component of message
|
||||
// fee), then messages relayer may receive zero reward.
|
||||
let mut confirmation_reward =
|
||||
T::Reward::saturated_from(messages).saturating_mul(confirmation_fee);
|
||||
confirmation_reward = sp_std::cmp::min(confirmation_reward, relayer_reward);
|
||||
relayer_reward = relayer_reward.saturating_sub(confirmation_reward);
|
||||
confirmation_relayer_reward =
|
||||
confirmation_relayer_reward.saturating_add(confirmation_reward);
|
||||
Pallet::<T>::register_relayer_reward(lane_id, &relayer, relayer_reward);
|
||||
} else {
|
||||
// If delivery confirmation is submitted by this relayer, let's add confirmation fee
|
||||
// from other relayers to this relayer reward.
|
||||
confirmation_relayer_reward =
|
||||
confirmation_relayer_reward.saturating_add(relayer_reward);
|
||||
}
|
||||
}
|
||||
|
||||
// finally - pay reward to confirmation relayer
|
||||
Pallet::<T>::register_relayer_reward(
|
||||
lane_id,
|
||||
confirmation_relayer,
|
||||
confirmation_relayer_reward,
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{mock::*, RelayerRewards};
|
||||
|
||||
const RELAYER_1: AccountId = 1;
|
||||
const RELAYER_2: AccountId = 2;
|
||||
const RELAYER_3: AccountId = 3;
|
||||
|
||||
fn relayers_rewards() -> RelayersRewards<AccountId> {
|
||||
vec![(RELAYER_1, 2), (RELAYER_2, 3)].into_iter().collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirmation_relayer_is_rewarded_if_it_has_also_delivered_messages() {
|
||||
run_test(|| {
|
||||
register_relayers_rewards::<TestRuntime>(
|
||||
&RELAYER_2,
|
||||
relayers_rewards(),
|
||||
TEST_LANE_ID,
|
||||
50,
|
||||
10,
|
||||
);
|
||||
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_1, TEST_LANE_ID), Some(80));
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_2, TEST_LANE_ID), Some(170));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirmation_relayer_is_rewarded_if_it_has_not_delivered_any_delivered_messages() {
|
||||
run_test(|| {
|
||||
register_relayers_rewards::<TestRuntime>(
|
||||
&RELAYER_3,
|
||||
relayers_rewards(),
|
||||
TEST_LANE_ID,
|
||||
50,
|
||||
10,
|
||||
);
|
||||
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_1, TEST_LANE_ID), Some(80));
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_2, TEST_LANE_ID), Some(120));
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_3, TEST_LANE_ID), Some(50));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_confirmation_relayer_is_rewarded_if_confirmation_fee_has_significantly_increased() {
|
||||
run_test(|| {
|
||||
register_relayers_rewards::<TestRuntime>(
|
||||
&RELAYER_3,
|
||||
relayers_rewards(),
|
||||
TEST_LANE_ID,
|
||||
50,
|
||||
1000,
|
||||
);
|
||||
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_1, TEST_LANE_ID), None);
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_2, TEST_LANE_ID), None);
|
||||
assert_eq!(RelayerRewards::<TestRuntime>::get(RELAYER_3, TEST_LANE_ID), Some(250));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Autogenerated weights for `pallet_bridge_relayers`
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2022-11-17, STEPS: 50, REPEAT: 20
|
||||
//! LOW RANGE: [], HIGH RANGE: []
|
||||
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled
|
||||
//! CHAIN: Some("dev"), DB CACHE: 1024
|
||||
|
||||
// Executed Command:
|
||||
// target/release/millau-bridge-node
|
||||
// benchmark
|
||||
// pallet
|
||||
// --chain=dev
|
||||
// --steps=50
|
||||
// --repeat=20
|
||||
// --pallet=pallet_bridge_relayers
|
||||
// --extrinsic=*
|
||||
// --execution=wasm
|
||||
// --wasm-execution=Compiled
|
||||
// --heap-pages=4096
|
||||
// --output=./modules/relayers/src/weights.rs
|
||||
// --template=./.maintain/millau-weight-template.hbs
|
||||
|
||||
#![allow(clippy::all)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use frame_support::{
|
||||
traits::Get,
|
||||
weights::{constants::RocksDbWeight, Weight},
|
||||
};
|
||||
use sp_std::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for `pallet_bridge_relayers`.
|
||||
pub trait WeightInfo {
|
||||
fn claim_rewards() -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for `pallet_bridge_relayers` that are generated using one of the Bridge testnets.
|
||||
///
|
||||
/// Those weights are test only and must never be used in production.
|
||||
pub struct BridgeWeight<T>(PhantomData<T>);
|
||||
impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
|
||||
fn claim_rewards() -> Weight {
|
||||
Weight::from_ref_time(59_334_000 as u64)
|
||||
.saturating_add(T::DbWeight::get().reads(2 as u64))
|
||||
.saturating_add(T::DbWeight::get().writes(2 as u64))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests
|
||||
impl WeightInfo for () {
|
||||
fn claim_rewards() -> Weight {
|
||||
Weight::from_ref_time(59_334_000 as u64)
|
||||
.saturating_add(RocksDbWeight::get().reads(2 as u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(2 as u64))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user