referrerpolicy=no-referrer-when-downgrade

bp_beefy/
lib.rs

1// Copyright 2019-2021 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 that are used to interact with BEEFY bridge pallet.
18
19#![cfg_attr(not(feature = "std"), no_std)]
20#![warn(missing_docs)]
21
22pub use binary_merkle_tree::merkle_root;
23pub use pallet_beefy_mmr::BeefyEcdsaToEthereum;
24pub use pallet_mmr::{
25	primitives::{DataOrHash as MmrDataOrHash, LeafProof as MmrProof},
26	verify_leaves_proof as verify_mmr_leaves_proof,
27};
28pub use sp_consensus_beefy::{
29	ecdsa_crypto::{
30		AuthorityId as EcdsaValidatorId, AuthoritySignature as EcdsaValidatorSignature,
31	},
32	known_payloads::MMR_ROOT_ID as MMR_ROOT_PAYLOAD_ID,
33	mmr::{BeefyAuthoritySet, MmrLeafVersion},
34	BeefyAuthorityId, Commitment, Payload as BeefyPayload, SignedCommitment, ValidatorSet,
35	ValidatorSetId, BEEFY_ENGINE_ID,
36};
37
38use bp_runtime::{BasicOperatingMode, BlockNumberOf, Chain, HashOf};
39use codec::{Decode, Encode};
40use frame_support::Parameter;
41use scale_info::TypeInfo;
42use serde::{Deserialize, Serialize};
43use sp_runtime::{
44	traits::{Convert, MaybeSerializeDeserialize},
45	RuntimeAppPublic, RuntimeDebug,
46};
47use sp_std::prelude::*;
48
49/// Substrate-based chain with BEEFY && MMR pallets deployed.
50///
51/// Both BEEFY and MMR pallets and their clients may be configured to use different
52/// primitives. Some of types can be configured in low-level pallets, but are constrained
53/// when BEEFY+MMR bundle is used.
54pub trait ChainWithBeefy: Chain {
55	/// The hashing algorithm used to compute the digest of the BEEFY commitment.
56	///
57	/// Corresponds to the hashing algorithm, used by `sc_consensus_beefy::BeefyKeystore`.
58	type CommitmentHasher: sp_runtime::traits::Hash;
59
60	/// The hashing algorithm used to build the MMR.
61	///
62	/// The same algorithm is also used to compute merkle roots in BEEFY
63	/// (e.g. validator addresses root in leaf data).
64	///
65	/// Corresponds to the `Hashing` field of the `pallet-mmr` configuration.
66	type MmrHashing: sp_runtime::traits::Hash<Output = Self::MmrHash>;
67
68	/// The output type of the hashing algorithm used to build the MMR.
69	///
70	/// This type is actually stored in the MMR.
71
72	/// Corresponds to the `Hash` field of the `pallet-mmr` configuration.
73	type MmrHash: sp_std::hash::Hash
74		+ Parameter
75		+ Copy
76		+ AsRef<[u8]>
77		+ Default
78		+ MaybeSerializeDeserialize
79		+ PartialOrd;
80
81	/// The type expected for the MMR leaf extra data.
82	type BeefyMmrLeafExtra: Parameter;
83
84	/// A way to identify a BEEFY validator.
85	///
86	/// Corresponds to the `BeefyId` field of the `pallet-beefy` configuration.
87	type AuthorityId: BeefyAuthorityId<Self::CommitmentHasher> + Parameter;
88
89	/// A way to convert validator id to its raw representation in the BEEFY merkle tree.
90	///
91	/// Corresponds to the `BeefyAuthorityToMerkleLeaf` field of the `pallet-beefy-mmr`
92	/// configuration.
93	type AuthorityIdToMerkleLeaf: Convert<Self::AuthorityId, Vec<u8>>;
94}
95
96/// BEEFY validator id used by given Substrate chain.
97pub type BeefyAuthorityIdOf<C> = <C as ChainWithBeefy>::AuthorityId;
98/// BEEFY validator set, containing both validator identifiers and the numeric set id.
99pub type BeefyAuthoritySetOf<C> = ValidatorSet<BeefyAuthorityIdOf<C>>;
100/// BEEFY authority set, containing both validator identifiers and the numeric set id.
101pub type BeefyAuthoritySetInfoOf<C> = sp_consensus_beefy::mmr::BeefyAuthoritySet<MmrHashOf<C>>;
102/// BEEFY validator signature used by given Substrate chain.
103pub type BeefyValidatorSignatureOf<C> =
104	<<C as ChainWithBeefy>::AuthorityId as RuntimeAppPublic>::Signature;
105/// Signed BEEFY commitment used by given Substrate chain.
106pub type BeefySignedCommitmentOf<C> =
107	SignedCommitment<BlockNumberOf<C>, BeefyValidatorSignatureOf<C>>;
108/// Hash algorithm, used to compute the digest of the BEEFY commitment before signing it.
109pub type BeefyCommitmentHasher<C> = <C as ChainWithBeefy>::CommitmentHasher;
110/// Hash algorithm used in Beefy MMR construction by given Substrate chain.
111pub type MmrHashingOf<C> = <C as ChainWithBeefy>::MmrHashing;
112/// Hash type, used in MMR construction by given Substrate chain.
113pub type MmrHashOf<C> = <C as ChainWithBeefy>::MmrHash;
114/// BEEFY MMR proof type used by the given Substrate chain.
115pub type MmrProofOf<C> = MmrProof<MmrHashOf<C>>;
116/// The type of the MMR leaf extra data used by the given Substrate chain.
117pub type BeefyMmrLeafExtraOf<C> = <C as ChainWithBeefy>::BeefyMmrLeafExtra;
118/// A way to convert a validator id to its raw representation in the BEEFY merkle tree, used by
119/// the given Substrate chain.
120pub type BeefyAuthorityIdToMerkleLeafOf<C> = <C as ChainWithBeefy>::AuthorityIdToMerkleLeaf;
121/// Actual type of leafs in the BEEFY MMR.
122pub type BeefyMmrLeafOf<C> = sp_consensus_beefy::mmr::MmrLeaf<
123	BlockNumberOf<C>,
124	HashOf<C>,
125	MmrHashOf<C>,
126	BeefyMmrLeafExtraOf<C>,
127>;
128
129/// Data required for initializing the BEEFY pallet.
130///
131/// Provides the initial context that the bridge needs in order to know
132/// where to start the sync process from.
133#[derive(Encode, Decode, RuntimeDebug, PartialEq, Clone, TypeInfo, Serialize, Deserialize)]
134pub struct InitializationData<BlockNumber, Hash> {
135	/// Pallet operating mode.
136	pub operating_mode: BasicOperatingMode,
137	/// Number of the best block, finalized by BEEFY.
138	pub best_block_number: BlockNumber,
139	/// BEEFY authority set that will be finalizing descendants of the `best_beefy_block_number`
140	/// block.
141	pub authority_set: BeefyAuthoritySet<Hash>,
142}
143
144/// Basic data, stored by the pallet for every imported commitment.
145#[derive(Encode, Decode, RuntimeDebug, PartialEq, TypeInfo)]
146pub struct ImportedCommitment<BlockNumber, BlockHash, MmrHash> {
147	/// Block number and hash of the finalized block parent.
148	pub parent_number_and_hash: (BlockNumber, BlockHash),
149	/// MMR root at the imported block.
150	pub mmr_root: MmrHash,
151}