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