// Copyright 2019-2021 Parity Technologies (UK) Ltd. // This file is part of Parity Bridges Common. // Parity Bridges Common is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity Bridges Common is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity Bridges Common. If not, see . //! Primitives that are used to interact with BEEFY bridge pallet. #![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] pub use beefy_merkle_tree::{merkle_root, Keccak256 as BeefyKeccak256}; pub use beefy_primitives::{ crypto::{AuthorityId as EcdsaValidatorId, AuthoritySignature as EcdsaValidatorSignature}, known_payloads::MMR_ROOT_ID as MMR_ROOT_PAYLOAD_ID, mmr::{BeefyAuthoritySet, MmrLeafVersion}, BeefyAuthorityId, BeefyVerify, Commitment, Payload as BeefyPayload, SignedCommitment, ValidatorSet, ValidatorSetId, BEEFY_ENGINE_ID, }; pub use pallet_beefy_mmr::BeefyEcdsaToEthereum; pub use pallet_mmr::{ primitives::{DataOrHash as MmrDataOrHash, Proof as MmrProof}, verify_leaves_proof as verify_mmr_leaves_proof, }; use bp_runtime::{BasicOperatingMode, BlockNumberOf, Chain, HashOf}; use codec::{Decode, Encode}; use frame_support::Parameter; use scale_info::TypeInfo; use sp_runtime::{ traits::{Convert, MaybeSerializeDeserialize}, RuntimeDebug, }; use sp_std::prelude::*; /// Substrate-based chain with BEEFY && MMR pallets deployed. /// /// Both BEEFY and MMR pallets and their clients may be configured to use different /// primitives. Some of types can be configured in low-level pallets, but are constrained /// when BEEFY+MMR bundle is used. pub trait ChainWithBeefy: Chain { /// The hashing algorithm used to compute the digest of the BEEFY commitment. /// /// Corresponds to the hashing algorithm, used by `beefy_gadget::BeefyKeystore`. type CommitmentHasher: sp_runtime::traits::Hash; /// The hashing algorithm used to build the MMR. /// /// The same algorithm is also used to compute merkle roots in BEEFY /// (e.g. validator addresses root in leaf data). /// /// Corresponds to the `Hashing` field of the `pallet-mmr` configuration. type MmrHashing: sp_runtime::traits::Hash; /// The output type of the hashing algorithm used to build the MMR. /// /// This type is actually stored in the MMR. /// Corresponds to the `Hash` field of the `pallet-mmr` configuration. type MmrHash: sp_std::hash::Hash + Parameter + Copy + AsRef<[u8]> + Default + MaybeSerializeDeserialize; /// The type expected for the MMR leaf extra data. type BeefyMmrLeafExtra: Parameter; /// A way to identify a BEEFY validator. /// /// Corresponds to the `BeefyId` field of the `pallet-beefy` configuration. type AuthorityId: BeefyAuthorityId + Parameter; /// The signature type used by BEEFY. /// /// Corresponds to the `BeefyId` field of the `pallet-beefy` configuration. type Signature: BeefyVerify + Parameter; /// A way to convert validator id to its raw representation in the BEEFY merkle tree. /// /// Corresponds to the `BeefyAuthorityToMerkleLeaf` field of the `pallet-beefy-mmr` /// configuration. type AuthorityIdToMerkleLeaf: Convert>; } /// BEEFY validator id used by given Substrate chain. pub type BeefyAuthorityIdOf = ::AuthorityId; /// BEEFY validator set, containing both validator identifiers and the numeric set id. pub type BeefyAuthoritySetOf = ValidatorSet>; /// BEEFY authority set, containing both validator identifiers and the numeric set id. pub type BeefyAuthoritySetInfoOf = beefy_primitives::mmr::BeefyAuthoritySet>; /// BEEFY validator signature used by given Substrate chain. pub type BeefyValidatorSignatureOf = ::Signature; /// Signed BEEFY commitment used by given Substrate chain. pub type BeefySignedCommitmentOf = SignedCommitment, BeefyValidatorSignatureOf>; /// Hash algorithm, used to compute the digest of the BEEFY commitment before signing it. pub type BeefyCommitmentHasher = ::CommitmentHasher; /// Hash algorithm used in Beefy MMR construction by given Substrate chain. pub type MmrHashingOf = ::MmrHashing; /// Hash type, used in MMR construction by given Substrate chain. pub type MmrHashOf = ::MmrHash; /// BEEFY MMR proof type used by the given Substrate chain. pub type MmrProofOf = MmrProof>; /// The type of the MMR leaf extra data used by the given Substrate chain. pub type BeefyMmrLeafExtraOf = ::BeefyMmrLeafExtra; /// A way to convert a validator id to its raw representation in the BEEFY merkle tree, used by /// the given Substrate chain. pub type BeefyAuthorityIdToMerkleLeafOf = ::AuthorityIdToMerkleLeaf; /// Actual type of leafs in the BEEFY MMR. pub type BeefyMmrLeafOf = beefy_primitives::mmr::MmrLeaf< BlockNumberOf, HashOf, MmrHashOf, BeefyMmrLeafExtraOf, >; /// Data required for initializing the BEEFY pallet. /// /// Provides the initial context that the bridge needs in order to know /// where to start the sync process from. #[derive(Encode, Decode, RuntimeDebug, PartialEq, Clone, TypeInfo)] #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] pub struct InitializationData { /// Pallet operating mode. pub operating_mode: BasicOperatingMode, /// Number of the best block, finalized by BEEFY. pub best_block_number: BlockNumber, /// BEEFY authority set that will be finalizing descendants of the `best_beefy_block_number` /// block. pub authority_set: BeefyAuthoritySet, } /// Basic data, stored by the pallet for every imported commitment. #[derive(Encode, Decode, RuntimeDebug, PartialEq, TypeInfo)] pub struct ImportedCommitment { /// Block number and hash of the finalized block parent. pub parent_number_and_hash: (BlockNumber, BlockHash), /// MMR root at the imported block. pub mmr_root: MmrHash, }