referrerpolicy=no-referrer-when-downgrade

bp_polkadot_bulletin/
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//! Polkadot Bulletin Chain primitives.
18
19#![warn(missing_docs)]
20#![cfg_attr(not(feature = "std"), no_std)]
21
22use bp_header_chain::ChainWithGrandpa;
23use bp_messages::{ChainWithMessages, MessageNonce};
24use bp_runtime::{
25	decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis,
26	extensions::{
27		CheckEra, CheckGenesis, CheckNonZeroSender, CheckNonce, CheckSpecVersion, CheckTxVersion,
28		CheckWeight, GenericTransactionExtension, GenericTransactionExtensionSchema,
29	},
30	Chain, ChainId, TransactionEra,
31};
32use codec::{Decode, DecodeWithMemTracking, Encode};
33use frame_support::{
34	dispatch::DispatchClass,
35	parameter_types,
36	weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
37};
38use frame_system::limits;
39use scale_info::TypeInfo;
40use sp_runtime::{
41	impl_tx_ext_default, traits::Dispatchable, transaction_validity::TransactionValidityError,
42	Perbill, StateVersion,
43};
44
45// This chain reuses most of Polkadot primitives.
46pub use bp_polkadot_core::{
47	AccountAddress, AccountId, Balance, Block, BlockNumber, Hash, Hasher, Header, Nonce, Signature,
48	SignedBlock, UncheckedExtrinsic, AVERAGE_HEADER_SIZE, EXTRA_STORAGE_PROOF_SIZE,
49	MAX_MANDATORY_HEADER_SIZE, REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY,
50};
51
52/// Maximal number of GRANDPA authorities at Polkadot Bulletin chain.
53pub const MAX_AUTHORITIES_COUNT: u32 = 100;
54
55/// Name of the With-Polkadot Bulletin chain GRANDPA pallet instance that is deployed at bridged
56/// chains.
57pub const WITH_POLKADOT_BULLETIN_GRANDPA_PALLET_NAME: &str = "BridgePolkadotBulletinGrandpa";
58/// Name of the With-Polkadot Bulletin chain messages pallet instance that is deployed at bridged
59/// chains.
60pub const WITH_POLKADOT_BULLETIN_MESSAGES_PALLET_NAME: &str = "BridgePolkadotBulletinMessages";
61
62// There are fewer system operations on this chain (e.g. staking, governance, etc.). Use a higher
63// percentage of the block for data storage.
64const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(90);
65
66// Re following constants - we are using the same values at Cumulus parachains. They are limited
67// by the maximal transaction weight/size. Since block limits at Bulletin Chain are larger than
68// at the Cumulus Bridge Hubs, we could reuse the same values.
69
70/// Maximal number of unrewarded relayer entries at inbound lane for Cumulus-based parachains.
71pub const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce = 1024;
72
73/// Maximal number of unconfirmed messages at inbound lane for Cumulus-based parachains.
74pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 4096;
75
76/// This signed extension is used to ensure that the chain transactions are signed by proper
77pub type ValidateSigned = GenericTransactionExtensionSchema<(), ()>;
78
79/// Signed extension schema, used by Polkadot Bulletin.
80pub type TransactionExtensionSchema = GenericTransactionExtension<(
81	(
82		CheckNonZeroSender,
83		CheckSpecVersion,
84		CheckTxVersion,
85		CheckGenesis<Hash>,
86		CheckEra<Hash>,
87		CheckNonce<Nonce>,
88		CheckWeight,
89	),
90	ValidateSigned,
91)>;
92
93/// Transaction extension, used by Polkadot Bulletin.
94#[derive(Encode, Decode, DecodeWithMemTracking, Debug, PartialEq, Eq, Clone, TypeInfo)]
95pub struct TransactionExtension(TransactionExtensionSchema);
96
97impl<C> sp_runtime::traits::TransactionExtension<C> for TransactionExtension
98where
99	C: Dispatchable,
100{
101	const IDENTIFIER: &'static str = "Not needed.";
102	type Implicit =
103		<TransactionExtensionSchema as sp_runtime::traits::TransactionExtension<C>>::Implicit;
104
105	fn implicit(&self) -> Result<Self::Implicit, TransactionValidityError> {
106		<TransactionExtensionSchema as sp_runtime::traits::TransactionExtension<C>>::implicit(
107			&self.0,
108		)
109	}
110	type Pre = ();
111	type Val = ();
112
113	impl_tx_ext_default!(C; weight validate prepare);
114}
115
116impl TransactionExtension {
117	/// Create signed extension from its components.
118	pub fn from_params(
119		spec_version: u32,
120		transaction_version: u32,
121		era: TransactionEra<BlockNumber, Hash>,
122		genesis_hash: Hash,
123		nonce: Nonce,
124	) -> Self {
125		Self(GenericTransactionExtension::new(
126			(
127				(
128					(),              // non-zero sender
129					(),              // spec version
130					(),              // tx version
131					(),              // genesis
132					era.frame_era(), // era
133					nonce.into(),    // nonce (compact encoding)
134					(),              // Check weight
135				),
136				(),
137			),
138			Some((
139				(
140					(),
141					spec_version,
142					transaction_version,
143					genesis_hash,
144					era.signed_payload(genesis_hash),
145					(),
146					(),
147				),
148				(),
149			)),
150		))
151	}
152
153	/// Return transaction nonce.
154	pub fn nonce(&self) -> Nonce {
155		let common_payload = self.0.payload.0;
156		common_payload.5 .0
157	}
158}
159
160parameter_types! {
161	/// We allow for 2 seconds of compute with a 6 second average block time.
162	pub BlockWeights: limits::BlockWeights = limits::BlockWeights::with_sensible_defaults(
163			Weight::from_parts(2u64 * WEIGHT_REF_TIME_PER_SECOND, u64::MAX),
164			NORMAL_DISPATCH_RATIO,
165		);
166	// Note: Max transaction size is 8 MB. Set max block size to 10 MB to facilitate data storage.
167	// This is double the "normal" Relay Chain block length limit.
168	/// Maximal block length at Polkadot Bulletin chain.
169	pub BlockLength: limits::BlockLength = limits::BlockLength::max_with_normal_ratio(
170		10 * 1024 * 1024,
171		NORMAL_DISPATCH_RATIO,
172	);
173}
174
175/// Polkadot Bulletin Chain declaration.
176pub struct PolkadotBulletin;
177
178impl Chain for PolkadotBulletin {
179	const ID: ChainId = *b"pdbc";
180
181	type BlockNumber = BlockNumber;
182	type Hash = Hash;
183	type Hasher = Hasher;
184	type Header = Header;
185
186	type AccountId = AccountId;
187	// The Bulletin Chain is a permissioned blockchain without any balances. Our `Chain` trait
188	// requires balance type, which is then used by various bridge infrastructure code. However
189	// this code is optional and we are not planning to use it in our bridge.
190	type Balance = Balance;
191	type Nonce = Nonce;
192	type Signature = Signature;
193
194	const STATE_VERSION: StateVersion = StateVersion::V1;
195
196	fn max_extrinsic_size() -> u32 {
197		*BlockLength::get().max.get(DispatchClass::Normal)
198	}
199
200	fn max_extrinsic_weight() -> Weight {
201		BlockWeights::get()
202			.get(DispatchClass::Normal)
203			.max_extrinsic
204			.unwrap_or(Weight::MAX)
205	}
206}
207
208impl ChainWithGrandpa for PolkadotBulletin {
209	const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = WITH_POLKADOT_BULLETIN_GRANDPA_PALLET_NAME;
210	const MAX_AUTHORITIES_COUNT: u32 = MAX_AUTHORITIES_COUNT;
211	const REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY: u32 =
212		REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY;
213	const MAX_MANDATORY_HEADER_SIZE: u32 = MAX_MANDATORY_HEADER_SIZE;
214	const AVERAGE_HEADER_SIZE: u32 = AVERAGE_HEADER_SIZE;
215}
216
217impl ChainWithMessages for PolkadotBulletin {
218	const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
219		WITH_POLKADOT_BULLETIN_MESSAGES_PALLET_NAME;
220
221	const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce =
222		MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX;
223	const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce =
224		MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX;
225}
226
227decl_bridge_finality_runtime_apis!(polkadot_bulletin, grandpa);
228decl_bridge_messages_runtime_apis!(polkadot_bulletin, bp_messages::LegacyLaneId);