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