bp_relayers/registration.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
16
17//! Bridge relayers registration and slashing scheme.
18//!
19//! There is an option to add a refund-relayer signed extension that will compensate
20//! relayer costs of the message delivery and confirmation transactions (as well as
21//! required finality proofs). This extension boosts priority of message delivery
22//! transactions, based on the number of bundled messages. So transaction with more
23//! messages has larger priority than the transaction with less messages.
24//! See `bridge_runtime_common::extensions::priority_calculator` for details;
25//!
26//! This encourages relayers to include more messages to their delivery transactions.
27//! At the same time, we are not verifying storage proofs before boosting
28//! priority. Instead, we simply trust relayer, when it says that transaction delivers
29//! `N` messages.
30//!
31//! This allows relayers to submit transactions which declare large number of bundled
32//! transactions to receive priority boost for free, potentially pushing actual delivery
33//! transactions from the block (or even transaction queue). Such transactions are
34//! not free, but their cost is relatively small.
35//!
36//! To alleviate that, we only boost transactions of relayers that have some stake
37//! that guarantees that their transactions are valid. Such relayers get priority
38//! for free, but they risk to lose their stake.
39
40use crate::RewardsAccountParams;
41
42use codec::{Decode, Encode, MaxEncodedLen};
43use scale_info::TypeInfo;
44use sp_runtime::{
45 traits::{Get, Zero},
46 DispatchError, DispatchResult,
47};
48
49/// Either explicit account reference or `RewardsAccountParams`.
50#[derive(Clone, Debug)]
51pub enum ExplicitOrAccountParams<AccountId, LaneId: Decode + Encode> {
52 /// Explicit account reference.
53 Explicit(AccountId),
54 /// Account, referenced using `RewardsAccountParams`.
55 Params(RewardsAccountParams<LaneId>),
56}
57
58impl<AccountId, LaneId: Decode + Encode> From<RewardsAccountParams<LaneId>>
59 for ExplicitOrAccountParams<AccountId, LaneId>
60{
61 fn from(params: RewardsAccountParams<LaneId>) -> Self {
62 ExplicitOrAccountParams::Params(params)
63 }
64}
65
66/// Relayer registration.
67#[derive(Copy, Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
68pub struct Registration<BlockNumber, Balance> {
69 /// The last block number, where this registration is considered active.
70 ///
71 /// Relayer has an option to renew his registration (this may be done before it
72 /// is spoiled as well). Starting from block `valid_till + 1`, relayer may `deregister`
73 /// himself and get his stake back.
74 ///
75 /// Please keep in mind that priority boost stops working some blocks before the
76 /// registration ends (see [`StakeAndSlash::RequiredRegistrationLease`]).
77 pub valid_till: BlockNumber,
78 /// Active relayer stake, which is mapped to the relayer reserved balance.
79 ///
80 /// If `stake` is less than the [`StakeAndSlash::RequiredStake`], the registration
81 /// is considered inactive even if `valid_till + 1` is not yet reached.
82 pub stake: Balance,
83}
84
85/// Relayer stake-and-slash mechanism.
86pub trait StakeAndSlash<AccountId, BlockNumber, Balance> {
87 /// The stake that the relayer must have to have its transactions boosted.
88 type RequiredStake: Get<Balance>;
89 /// Required **remaining** registration lease to be able to get transaction priority boost.
90 ///
91 /// If the difference between registration's `valid_till` and the current block number
92 /// is less than the `RequiredRegistrationLease`, it becomes inactive and relayer transaction
93 /// won't get priority boost. This period exists, because priority is calculated when
94 /// transaction is placed to the queue (and it is reevaluated periodically) and then some time
95 /// may pass before transaction will be included into the block.
96 type RequiredRegistrationLease: Get<BlockNumber>;
97
98 /// Reserve the given amount at relayer account.
99 fn reserve(relayer: &AccountId, amount: Balance) -> DispatchResult;
100 /// `Unreserve` the given amount from relayer account.
101 ///
102 /// Returns amount that we have failed to `unreserve`.
103 fn unreserve(relayer: &AccountId, amount: Balance) -> Balance;
104 /// Slash up to `amount` from reserved balance of account `relayer` and send funds to given
105 /// `beneficiary`.
106 ///
107 /// Returns `Ok(_)` with non-zero balance if we have failed to repatriate some portion of stake.
108 fn repatriate_reserved<LaneId: Decode + Encode>(
109 relayer: &AccountId,
110 beneficiary: ExplicitOrAccountParams<AccountId, LaneId>,
111 amount: Balance,
112 ) -> Result<Balance, DispatchError>;
113}
114
115impl<AccountId, BlockNumber, Balance> StakeAndSlash<AccountId, BlockNumber, Balance> for ()
116where
117 Balance: Default + Zero,
118 BlockNumber: Default,
119{
120 type RequiredStake = ();
121 type RequiredRegistrationLease = ();
122
123 fn reserve(_relayer: &AccountId, _amount: Balance) -> DispatchResult {
124 Ok(())
125 }
126
127 fn unreserve(_relayer: &AccountId, _amount: Balance) -> Balance {
128 Zero::zero()
129 }
130
131 fn repatriate_reserved<LaneId: Decode + Encode>(
132 _relayer: &AccountId,
133 _beneficiary: ExplicitOrAccountParams<AccountId, LaneId>,
134 _amount: Balance,
135 ) -> Result<Balance, DispatchError> {
136 Ok(Zero::zero())
137 }
138}