bp_moonbeam/
lib.rs

1// Copyright 2025 Moonbeam foundation
2// This file is part of Moonbeam.
3
4// Moonbeam 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// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17//! # Moonbeam bridge primitives
18
19#![cfg_attr(not(feature = "std"), no_std)]
20
21pub use bp_bridge_hub_cumulus::{
22	BlockLength, BlockWeights, Hasher, Nonce, SignedBlock, AVERAGE_BLOCK_INTERVAL,
23	MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX, MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
24};
25use bp_messages::{ChainWithMessages, MessageNonce};
26
27use bp_runtime::{
28	decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, Chain, ChainId, Parachain,
29};
30use frame_support::{dispatch::DispatchClass, weights::Weight};
31pub use moonbeam_core_primitives::{AccountId, Balance, BlockNumber, Hash, Header, Signature};
32use sp_runtime::StateVersion;
33use xcm::latest::prelude::{Junction, Location, NetworkId};
34
35/// Bridge lane identifier.
36pub type LaneId = bp_messages::HashedLaneId;
37
38/// Moonbeam parachain.
39pub struct Moonbeam;
40
41impl Chain for Moonbeam {
42	const ID: ChainId = *b"mnbm";
43
44	type BlockNumber = BlockNumber;
45	type Hash = Hash;
46	type Hasher = Hasher;
47	type Header = Header;
48
49	type AccountId = AccountId;
50	type Balance = Balance;
51	type Nonce = Nonce;
52	type Signature = Signature;
53
54	const STATE_VERSION: StateVersion = StateVersion::V1;
55
56	fn max_extrinsic_size() -> u32 {
57		*BlockLength::get().max.get(DispatchClass::Normal)
58	}
59
60	fn max_extrinsic_weight() -> Weight {
61		BlockWeights::get()
62			.get(DispatchClass::Normal)
63			.max_extrinsic
64			.unwrap_or(Weight::MAX)
65	}
66}
67
68impl Parachain for Moonbeam {
69	const PARACHAIN_ID: u32 = PARACHAIN_ID;
70	const MAX_HEADER_SIZE: u32 = 4_096;
71}
72
73impl ChainWithMessages for Moonbeam {
74	const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
75		WITH_MOONBEAM_POLKADOT_MESSAGES_PALLET_NAME;
76
77	const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce =
78		MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX;
79	const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce =
80		MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX;
81}
82
83/// Identifier of Moonbeam parachain in the Polkadot relay chain.
84pub const PARACHAIN_ID: u32 = 2004;
85
86/// Name of the With-MoonbeamPolkadot messages pallet instance that is deployed at bridged chains.
87pub const WITH_MOONBEAM_POLKADOT_MESSAGES_PALLET_NAME: &str = "BridgePolkadotMessages";
88
89decl_bridge_finality_runtime_apis!(moonbeam_polkadot);
90decl_bridge_messages_runtime_apis!(moonbeam_polkadot, LaneId);
91
92frame_support::parameter_types! {
93	pub GlobalConsensusLocation: Location = Location::new(
94		2,
95		[
96			Junction::GlobalConsensus(NetworkId::Polkadot),
97			Junction::Parachain(Moonbeam::PARACHAIN_ID)
98		]
99	);
100}
101
102/// Bridging primitives describing the Kusama relay chain, which we need for the other side.
103/// Same approach as in https://github.com/polkadot-fellows/runtimes/pull/627
104pub mod bp_kusama {
105	use super::{decl_bridge_finality_runtime_apis, Chain, ChainId, StateVersion, Weight};
106	use bp_header_chain::ChainWithGrandpa;
107	pub use bp_polkadot_core::*;
108
109	/// Kusama Chain
110	pub struct Kusama;
111
112	impl Chain for Kusama {
113		const ID: ChainId = *b"ksma";
114
115		type BlockNumber = BlockNumber;
116		type Hash = Hash;
117		type Hasher = Hasher;
118		type Header = Header;
119
120		type AccountId = AccountId;
121		type Balance = Balance;
122		type Nonce = Nonce;
123		type Signature = Signature;
124
125		const STATE_VERSION: StateVersion = StateVersion::V1;
126
127		fn max_extrinsic_size() -> u32 {
128			max_extrinsic_size()
129		}
130
131		fn max_extrinsic_weight() -> Weight {
132			max_extrinsic_weight()
133		}
134	}
135
136	impl ChainWithGrandpa for Kusama {
137		const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = WITH_KUSAMA_GRANDPA_PALLET_NAME;
138		const MAX_AUTHORITIES_COUNT: u32 = MAX_AUTHORITIES_COUNT;
139		const REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY: u32 =
140			REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY;
141		const MAX_MANDATORY_HEADER_SIZE: u32 = MAX_MANDATORY_HEADER_SIZE;
142		const AVERAGE_HEADER_SIZE: u32 = AVERAGE_HEADER_SIZE;
143	}
144
145	/// Name of the parachains pallet in the Kusama runtime.
146	pub const PARAS_PALLET_NAME: &str = "Paras";
147	/// Name of the With-Kusama GRANDPA pallet instance that is deployed at bridged chains.
148	pub const WITH_KUSAMA_GRANDPA_PALLET_NAME: &str = "BridgeKusamaGrandpa";
149	/// Name of the With-Kusama parachains pallet instance that is deployed at bridged chains.
150	pub const WITH_KUSAMA_BRIDGE_PARACHAINS_PALLET_NAME: &str = "BridgeKusamaParachains";
151
152	/// Maximal size of encoded `bp_parachains::ParaStoredHeaderData` structure among all Polkadot
153	/// parachains.
154	///
155	/// It includes the block number and state root, so it shall be near 40 bytes, but let's have
156	/// some reserve.
157	pub const MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE: u32 = 128;
158
159	decl_bridge_finality_runtime_apis!(kusama, grandpa);
160}