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::{PayRewardFromAccount, RewardsAccountParams};
41
42use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
43use scale_info::TypeInfo;
44use sp_runtime::{
45 traits::{Get, IdentifyAccount, 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
66impl<AccountId: Decode + Encode, LaneId: Decode + Encode> IdentifyAccount
67 for ExplicitOrAccountParams<AccountId, LaneId>
68{
69 type AccountId = AccountId;
70
71 fn into_account(self) -> Self::AccountId {
72 match self {
73 ExplicitOrAccountParams::Explicit(account_id) => account_id,
74 ExplicitOrAccountParams::Params(params) => {
75 PayRewardFromAccount::<(), AccountId, LaneId, ()>::rewards_account(params)
76 },
77 }
78 }
79}
80
81/// Relayer registration.
82#[derive(
83 Copy,
84 Clone,
85 Debug,
86 Decode,
87 DecodeWithMemTracking,
88 Encode,
89 Eq,
90 PartialEq,
91 TypeInfo,
92 MaxEncodedLen,
93)]
94pub struct Registration<BlockNumber, Balance> {
95 /// The last block number, where this registration is considered active.
96 ///
97 /// Relayer has an option to renew his registration (this may be done before it
98 /// is spoiled as well). Starting from block `valid_till + 1`, relayer may `deregister`
99 /// himself and get his stake back.
100 ///
101 /// Please keep in mind that priority boost stops working some blocks before the
102 /// registration ends (see [`StakeAndSlash::RequiredRegistrationLease`]).
103 pub valid_till: BlockNumber,
104 /// Active relayer stake, which is mapped to the relayer reserved balance.
105 ///
106 /// If `stake` is less than the [`StakeAndSlash::RequiredStake`], the registration
107 /// is considered inactive even if `valid_till + 1` is not yet reached.
108 pub stake: Balance,
109}
110
111/// Relayer stake-and-slash mechanism.
112pub trait StakeAndSlash<AccountId, BlockNumber, Balance> {
113 /// The stake that the relayer must have to have its transactions boosted.
114 type RequiredStake: Get<Balance>;
115 /// Required **remaining** registration lease to be able to get transaction priority boost.
116 ///
117 /// If the difference between registration's `valid_till` and the current block number
118 /// is less than the `RequiredRegistrationLease`, it becomes inactive and relayer transaction
119 /// won't get priority boost. This period exists, because priority is calculated when
120 /// transaction is placed to the queue (and it is reevaluated periodically) and then some time
121 /// may pass before transaction will be included into the block.
122 type RequiredRegistrationLease: Get<BlockNumber>;
123
124 /// Reserve the given amount at relayer account.
125 fn reserve(relayer: &AccountId, amount: Balance) -> DispatchResult;
126 /// `Unreserve` the given amount from relayer account.
127 ///
128 /// Returns amount that we have failed to `unreserve`.
129 fn unreserve(relayer: &AccountId, amount: Balance) -> Balance;
130 /// Slash up to `amount` from reserved balance of account `relayer` and send funds to given
131 /// `beneficiary`.
132 ///
133 /// Returns `Ok(_)` with non-zero balance if we have failed to repatriate some portion of stake.
134 fn repatriate_reserved(
135 relayer: &AccountId,
136 beneficiary: &AccountId,
137 amount: Balance,
138 ) -> Result<Balance, DispatchError>;
139}
140
141impl<AccountId, BlockNumber, Balance> StakeAndSlash<AccountId, BlockNumber, Balance> for ()
142where
143 Balance: Default + Zero,
144 BlockNumber: Default,
145{
146 type RequiredStake = ();
147 type RequiredRegistrationLease = ();
148
149 fn reserve(_relayer: &AccountId, _amount: Balance) -> DispatchResult {
150 Ok(())
151 }
152
153 fn unreserve(_relayer: &AccountId, _amount: Balance) -> Balance {
154 Zero::zero()
155 }
156
157 fn repatriate_reserved(
158 _relayer: &AccountId,
159 _beneficiary: &AccountId,
160 _amount: Balance,
161 ) -> Result<Balance, DispatchError> {
162 Ok(Zero::zero())
163 }
164}