bp_bridge_hub_cumulus/lib.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//! Primitives of all Cumulus-based bridge hubs.
18
19#![warn(missing_docs)]
20#![cfg_attr(not(feature = "std"), no_std)]
21
22pub use bp_polkadot_core::{
23 AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber, Hash, Hasher,
24 Hashing, Header, Nonce, Perbill, Signature, SignedBlock, UncheckedExtrinsic,
25 EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
26};
27
28use bp_messages::*;
29use bp_polkadot_core::SuffixedCommonTransactionExtension;
30use bp_runtime::extensions::{
31 BridgeRejectObsoleteHeadersAndMessages, RefundBridgedParachainMessagesSchema,
32};
33use frame_support::{
34 dispatch::DispatchClass,
35 parameter_types,
36 sp_runtime::{MultiAddress, MultiSigner},
37 weights::constants,
38};
39use frame_system::limits;
40use sp_std::time::Duration;
41
42/// Maximal asset hub header size.
43pub const MAX_ASSET_HUB_HEADER_SIZE: u32 = 4_096;
44
45/// Maximal bridge hub header size.
46pub const MAX_BRIDGE_HUB_HEADER_SIZE: u32 = 4_096;
47
48/// Average block interval in Cumulus-based parachains.
49///
50/// Corresponds to the `MILLISECS_PER_BLOCK` from `parachains_common` crate.
51pub const AVERAGE_BLOCK_INTERVAL: Duration = Duration::from_secs(12);
52
53/// All cumulus bridge hubs allow normal extrinsics to fill block up to 75 percent.
54///
55/// This is a copy-paste from the cumulus repo's `parachains-common` crate.
56pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
57
58/// All cumulus bridge hubs chains allow for 0.5 seconds of compute with a 6-second average block
59/// time.
60///
61/// This is a copy-paste from the cumulus repo's `parachains-common` crate.
62const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(constants::WEIGHT_REF_TIME_PER_SECOND, 0)
63 .saturating_div(2)
64 .set_proof_size(polkadot_primitives::MAX_POV_SIZE as u64);
65
66/// We allow for 2 seconds of compute with a 6 second average block.
67const MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING: Weight = Weight::from_parts(
68 constants::WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2),
69 polkadot_primitives::MAX_POV_SIZE as u64,
70);
71
72/// All cumulus bridge hubs assume that about 5 percent of the block weight is consumed by
73/// `on_initialize` handlers. This is used to limit the maximal weight of a single extrinsic.
74///
75/// This is a copy-paste from the cumulus repo's `parachains-common` crate.
76pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);
77
78parameter_types! {
79 /// Size limit of the Cumulus-based bridge hub blocks.
80 pub BlockLength: limits::BlockLength = limits::BlockLength::max_with_normal_ratio(
81 5 * 1024 * 1024,
82 NORMAL_DISPATCH_RATIO,
83 );
84
85 /// Importing a block with 0 Extrinsics.
86 pub const BlockExecutionWeight: Weight = Weight::from_parts(constants::WEIGHT_REF_TIME_PER_NANOS, 0)
87 .saturating_mul(5_000_000);
88 /// Executing a NO-OP `System::remarks` Extrinsic.
89 pub const ExtrinsicBaseWeight: Weight = Weight::from_parts(constants::WEIGHT_REF_TIME_PER_NANOS, 0)
90 .saturating_mul(125_000);
91
92 /// Weight limit of the Cumulus-based bridge hub blocks.
93 pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
94 .base_block(BlockExecutionWeight::get())
95 .for_class(DispatchClass::all(), |weights| {
96 weights.base_extrinsic = ExtrinsicBaseWeight::get();
97 })
98 .for_class(DispatchClass::Normal, |weights| {
99 weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
100 })
101 .for_class(DispatchClass::Operational, |weights| {
102 weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
103 // Operational transactions have an extra reserved space, so that they
104 // are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
105 weights.reserved = Some(
106 MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT,
107 );
108 })
109 .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
110 .build_or_panic();
111
112 /// Weight limit of the Cumulus-based bridge hub blocks when async backing is enabled.
113 pub BlockWeightsForAsyncBacking: limits::BlockWeights = limits::BlockWeights::builder()
114 .base_block(BlockExecutionWeight::get())
115 .for_class(DispatchClass::all(), |weights| {
116 weights.base_extrinsic = ExtrinsicBaseWeight::get();
117 })
118 .for_class(DispatchClass::Normal, |weights| {
119 weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING);
120 })
121 .for_class(DispatchClass::Operational, |weights| {
122 weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING);
123 // Operational transactions have an extra reserved space, so that they
124 // are included even if block reached `MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING`.
125 weights.reserved = Some(
126 MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT_FOR_ASYNC_BACKING,
127 );
128 })
129 .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
130 .build_or_panic();
131}
132
133/// Public key of the chain account that may be used to verify signatures.
134pub type AccountSigner = MultiSigner;
135
136/// The address format for describing accounts.
137pub type Address = MultiAddress<AccountId, ()>;
138
139// Note about selecting values of two following constants:
140//
141// Normal transactions have limit of 75% of 1/2 second weight for Cumulus parachains. Let's keep
142// some reserve for the rest of stuff there => let's select values that fit in 50% of maximal limit.
143//
144// Using current constants, the limit would be:
145//
146// `75% * WEIGHT_REF_TIME_PER_SECOND * 1 / 2 * 50% = 0.75 * 1_000_000_000_000 / 2 * 0.5 =
147// 187_500_000_000`
148//
149// According to (preliminary) weights of messages pallet, cost of additional message is zero and the
150// cost of additional relayer is `8_000_000 + db read + db write`. Let's say we want no more than
151// 4096 unconfirmed messages (no any scientific justification for that - it just looks large
152// enough). And then we can't have more than 4096 relayers. E.g. for 1024 relayers is (using
153// `RocksDbWeight`):
154//
155// `1024 * (8_000_000 + db read + db write) = 1024 * (8_000_000 + 25_000_000 + 100_000_000) =
156// 136_192_000_000`
157//
158// So 1024 looks like good approximation for the number of relayers. If something is wrong in those
159// assumptions, or something will change, it shall be caught by the
160// `ensure_able_to_receive_confirmation` test.
161
162/// Maximal number of unrewarded relayer entries at inbound lane for Cumulus-based parachains.
163/// Note: this value is security-relevant, decreasing it should not be done without careful
164/// analysis (like the one above).
165pub const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce = 1024;
166
167/// Maximal number of unconfirmed messages at inbound lane for Cumulus-based parachains.
168/// Note: this value is security-relevant, decreasing it should not be done without careful
169/// analysis (like the one above).
170pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 4096;
171
172/// Signed extension that is used by all bridge hubs.
173pub type TransactionExtension = SuffixedCommonTransactionExtension<(
174 BridgeRejectObsoleteHeadersAndMessages,
175 RefundBridgedParachainMessagesSchema,
176)>;