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