pallet_hyperbridge/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright (C) Polytope Labs Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Pallet Hyperbridge
//!
//! Pallet hyperbridge mediates the connection between hyperbridge and substrate-based chains. This
//! pallet provides:
//!
//!  - An [`IsmpDispatcher`] implementation which collects hyperbridge's protocol fees and commits
//!    the reciepts for these fees to child storage. Hyperbridge will only accept messages that have
//!    been paid for using this module.
//!  - An [`IsmpModule`] which recieves and processes requests from hyperbridge. These requests are
//!    dispatched by hyperbridge governance and may adjust fees or request payouts for both relayers
//!    and protocol revenue.
//!
//! This pallet contains no calls and dispatches no requests. Substrate based chains should use this
//! to dispatch requests that should be processed by hyperbridge.
//!
//! ## Usage
//!
//! This module must be configured as an [`IsmpModule`] in your
//! [`IsmpRouter`](ismp::router::IsmpRouter) implementation so that it may receive important
//! messages from hyperbridge such as paramter updates or relayer fee withdrawals.
//!
//! ```rust,ignore
//! use ismp::module::IsmpModule;
//! use ismp::router::IsmpRouter;
//!
//! #[derive(Default)]
//! struct ModuleRouter;
//!
//! impl IsmpRouter for ModuleRouter {
//!     fn module_for_id(&self, id: Vec<u8>) -> Result<Box<dyn IsmpModule>, anyhow::Error> {
//!         return match id.as_slice() {
//!             pallet_hyperbridge::PALLET_HYPERBRIDGE_ID => Ok(Box::new(pallet_hyperbridge::Pallet::<Runtime>::default())),
//!             _ => Err(Error::ModuleNotFound(id)),
//!         };
//!     }
//! }
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]

extern crate alloc;

use alloc::{collections::BTreeMap, format};
use codec::{Decode, Encode};
use frame_support::{
	sp_runtime::traits::AccountIdConversion,
	traits::{fungible::Mutate, tokens::Preservation, Get},
};
use ismp::{
	dispatcher::{DispatchRequest, FeeMetadata, IsmpDispatcher},
	host::StateMachine,
	module::IsmpModule,
	router::{PostRequest, PostResponse, Response, Timeout},
};
pub use pallet::*;
use pallet_ismp::RELAYER_FEE_ACCOUNT;
use primitive_types::H256;

pub mod child_trie;

/// Host params for substrate based chains
#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq, Default)]
pub struct SubstrateHostParams<B> {
	/// The default per byte fee
	pub default_per_byte_fee: B,
	/// Per byte fee configured for specific chains
	pub per_byte_fees: BTreeMap<StateMachine, B>,
	/// Asset registration fee
	pub asset_registration_fee: B,
}

/// Parameters that govern the working operations of this module. Versioned for ease of migration.
#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq)]
pub enum VersionedHostParams<Balance> {
	/// The per-byte fee that hyperbridge charges for outgoing requests and responses.
	V1(SubstrateHostParams<Balance>),
}

impl<Balance: Default> Default for VersionedHostParams<Balance> {
	fn default() -> Self {
		VersionedHostParams::V1(Default::default())
	}
}

#[frame_support::pallet]
pub mod pallet {
	use super::*;
	use frame_support::{pallet_prelude::*, PalletId};

	/// [`IsmpModule`] module identifier for incoming requests from hyperbridge
	pub const PALLET_HYPERBRIDGE_ID: &'static [u8] = b"HYPR-FEE";

	/// [`PalletId`] where protocol fees will be collected
	pub const PALLET_HYPERBRIDGE: PalletId = PalletId(*b"HYPR-FEE");

	#[pallet::config]
	pub trait Config: frame_system::Config + pallet_ismp::Config {
		/// Because this pallet emits events, it depends on the runtime's definition of an event.
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

		/// The underlying [`IsmpHost`] implementation
		type IsmpHost: IsmpDispatcher<Account = Self::AccountId, Balance = Self::Balance> + Default;
	}

	#[pallet::pallet]
	#[pallet::without_storage_info]
	pub struct Pallet<T>(_);

	/// The host parameters of the pallet-hyperbridge.
	#[pallet::storage]
	#[pallet::getter(fn host_params)]
	pub type HostParams<T> =
		StorageValue<_, VersionedHostParams<<T as pallet_ismp::Config>::Balance>, ValueQuery>;

	#[pallet::event]
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
	pub enum Event<T: Config> {
		/// Hyperbridge governance has now updated it's host params on this chain.
		HostParamsUpdated {
			/// The old host params
			old: VersionedHostParams<<T as pallet_ismp::Config>::Balance>,
			/// The new host params
			new: VersionedHostParams<<T as pallet_ismp::Config>::Balance>,
		},
		/// A relayer has withdrawn some fees
		RelayerFeeWithdrawn {
			/// The amount that was withdrawn
			amount: <T as pallet_ismp::Config>::Balance,
			/// The withdrawal beneficiary
			account: T::AccountId,
		},
		/// Hyperbridge has withdrawn it's protocol revenue
		ProtocolRevenueWithdrawn {
			/// The amount that was withdrawn
			amount: <T as pallet_ismp::Config>::Balance,
			/// The withdrawal beneficiary
			account: T::AccountId,
		},
	}

	// Errors encountered by pallet-hyperbridge
	#[pallet::error]
	pub enum Error<T> {}

	// Hack for implementing the [`Default`] bound needed for
	// [`IsmpDispatcher`](ismp::dispatcher::IsmpDispatcher) and
	// [`IsmpModule`](ismp::module::IsmpModule)
	impl<T> Default for Pallet<T> {
		fn default() -> Self {
			Self(PhantomData)
		}
	}
}

/// [`IsmpDispatcher`] implementation for dispatching requests to the hyperbridge coprocessor.
/// Charges the hyperbridge protocol fee on a per-byte basis.
///
/// **NOTE** Hyperbridge WILL NOT accept requests that were not dispatched through this
/// implementation.
impl<T> IsmpDispatcher for Pallet<T>
where
	T: Config,
	T::Balance: Into<u128> + From<u128>,
{
	type Account = T::AccountId;
	type Balance = T::Balance;

	fn dispatch_request(
		&self,
		request: DispatchRequest,
		fee: FeeMetadata<Self::Account, Self::Balance>,
	) -> Result<H256, anyhow::Error> {
		let fees = match request {
			DispatchRequest::Post(ref post) => {
				let VersionedHostParams::V1(params) = Self::host_params();
				let per_byte_fee: u128 =
					(*params.per_byte_fees.get(&post.dest).unwrap_or(&params.default_per_byte_fee))
						.into();
				// minimum fee is 32 bytes
				let fees = if post.body.len() < 32 {
					per_byte_fee * 32u128
				} else {
					per_byte_fee * post.body.len() as u128
				};

				// collect protocol fees
				if fees != 0 {
					T::Currency::transfer(
						&fee.payer,
						&PALLET_HYPERBRIDGE.into_account_truncating(),
						fees.into(),
						Preservation::Expendable,
					)
					.map_err(|err| {
						ismp::Error::Custom(format!("Error withdrawing request fees: {err:?}"))
					})?;
				}

				fees
			},
			DispatchRequest::Get(_) => Default::default(),
		};

		let host = <T as Config>::IsmpHost::default();
		let commitment = host.dispatch_request(request, fee)?;

		// commit the fee collected to child-trie
		child_trie::RequestPayments::insert(commitment, fees);

		Ok(commitment)
	}

	fn dispatch_response(
		&self,
		response: PostResponse,
		fee: FeeMetadata<Self::Account, Self::Balance>,
	) -> Result<H256, anyhow::Error> {
		// collect protocol fees
		let VersionedHostParams::V1(params) = Self::host_params();
		let per_byte_fee: u128 = (*params
			.per_byte_fees
			.get(&response.dest_chain())
			.unwrap_or(&params.default_per_byte_fee))
		.into();
		// minimum fee is 32 bytes
		let fees = if response.response.len() < 32 {
			per_byte_fee * 32u128
		} else {
			per_byte_fee * response.response.len() as u128
		};

		if fees != 0 {
			T::Currency::transfer(
				&fee.payer,
				&PALLET_HYPERBRIDGE.into_account_truncating(),
				fees.into(),
				Preservation::Expendable,
			)
			.map_err(|err| {
				ismp::Error::Custom(format!("Error withdrawing request fees: {err:?}"))
			})?;
		}

		let host = <T as Config>::IsmpHost::default();
		let commitment = host.dispatch_response(response, fee)?;

		// commit the collected to child-trie
		child_trie::ResponsePayments::insert(commitment, fees);

		Ok(commitment)
	}
}

/// A request to withdraw some funds. Could either be for protocol revenue or relayer fees.
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct WithdrawalRequest<Account, Amount> {
	/// The amount to be withdrawn
	pub amount: Amount,
	/// The withdrawal beneficiary
	pub account: Account,
}

/// Cross-chain messages to this module. This module will only accept messages from the hyperbridge
/// chain. Assumed to be configured in [`pallet_ismp::Config`]
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum Message<Account, Balance> {
	/// Set some new host params
	UpdateHostParams(VersionedHostParams<Balance>),
	/// Withdraw the hyperbridge protocol reveneue
	WithdrawProtocolFees(WithdrawalRequest<Account, Balance>),
	/// Withdraw the fees owed to a relayer
	WithdrawRelayerFees(WithdrawalRequest<Account, Balance>),
}

impl<T> IsmpModule for Pallet<T>
where
	T: Config,
	T::Balance: Into<u128> + From<u128>,
{
	fn on_accept(&self, request: PostRequest) -> Result<(), anyhow::Error> {
		// this of course assumes that hyperbridge is configured as the coprocessor.
		let source = request.source;
		if Some(source) != T::Coprocessor::get() {
			Err(ismp::Error::Custom(format!("Invalid request source: {source}")))?
		}

		let message =
			Message::<T::AccountId, T::Balance>::decode(&mut &request.body[..]).map_err(|err| {
				ismp::Error::Custom(format!("Failed to decode per-byte fee: {err:?}"))
			})?;

		match message {
			Message::UpdateHostParams(new) => {
				let old = HostParams::<T>::get();
				HostParams::<T>::put(new.clone());
				Self::deposit_event(Event::<T>::HostParamsUpdated { old, new });
			},
			Message::WithdrawProtocolFees(WithdrawalRequest { account, amount }) => {
				T::Currency::transfer(
					&PALLET_HYPERBRIDGE.into_account_truncating(),
					&account,
					amount,
					Preservation::Expendable,
				)
				.map_err(|err| {
					ismp::Error::Custom(format!("Error withdrawing protocol fees: {err:?}"))
				})?;

				Self::deposit_event(Event::<T>::ProtocolRevenueWithdrawn { account, amount })
			},
			Message::WithdrawRelayerFees(WithdrawalRequest { account, amount }) => {
				T::Currency::transfer(
					&RELAYER_FEE_ACCOUNT.into_account_truncating(),
					&account,
					amount,
					Preservation::Expendable,
				)
				.map_err(|err| {
					ismp::Error::Custom(format!("Error withdrawing protocol fees: {err:?}"))
				})?;

				Self::deposit_event(Event::<T>::RelayerFeeWithdrawn { account, amount })
			},
		};

		Ok(())
	}

	fn on_response(&self, _response: Response) -> Result<(), anyhow::Error> {
		// this module does not expect responses
		Err(ismp::Error::CannotHandleMessage.into())
	}

	fn on_timeout(&self, _request: Timeout) -> Result<(), anyhow::Error> {
		// this module does not dispatch requests
		Err(ismp::Error::CannotHandleMessage.into())
	}
}