beefy-verifier-primitives 0.1.1

Primitive types for the BEEFY consensus client
Documentation
// Copyright (C) 2022 Polytope Labs.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Primitive BEEFY types used by verifier and prover

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::all)]
#![deny(missing_docs)]

use codec::{Decode, Encode};
use polkadot_sdk::*;
use sp_consensus_beefy::mmr::{BeefyAuthoritySet, MmrLeaf, MmrLeafVersion};
use sp_core::H256;
use sp_std::prelude::*;

/// Client state definition for the light client
#[derive(sp_std::fmt::Debug, Encode, Decode, PartialEq, Eq, Clone)]
pub struct ConsensusState {
	/// Latest beefy height
	pub latest_beefy_height: u32,
	/// Height at which beefy was activated.
	pub beefy_activation_block: u32,
	/// Latest mmr root hash
	pub mmr_root_hash: H256,
	/// Authorities for the current session
	pub current_authorities: BeefyAuthoritySet<H256>,
	/// Authorities for the next session
	pub next_authorities: BeefyAuthoritySet<H256>,
}

/// Hash length definition for hashing algorithms used
pub const HASH_LENGTH: usize = 32;
/// Authority Signature type
pub type TSignature = [u8; 65];
/// Represents a Hash in this library
pub type Hash = [u8; 32];

#[derive(Clone, sp_std::fmt::Debug, PartialEq, Eq, Encode, Decode)]
/// Authority signature and its index in the signatures array
pub struct SignatureWithAuthorityIndex {
	/// Authority signature
	pub signature: TSignature,
	/// 0-based index of the authority in the authority set
	pub index: u32,
}

#[derive(Clone, sp_std::fmt::Debug, PartialEq, Eq, Encode, Decode)]
/// Signed commitment
pub struct SignedCommitment {
	/// Commitment
	pub commitment: sp_consensus_beefy::Commitment<u32>,
	/// Signatures for this commitment
	pub signatures: Vec<SignatureWithAuthorityIndex>,
}

#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// Mmr Update with proof
pub struct MmrProof {
	/// Signed commitment
	pub signed_commitment: SignedCommitment,
	/// Latest leaf added to mmr
	pub latest_mmr_leaf: MmrLeaf<u32, H256, H256, H256>,
	/// Proof for the latest mmr leaf
	pub mmr_proof: sp_mmr_primitives::LeafProof<H256>,
	/// Flat proof hashes for authorities merkle multi-proof.
	pub authority_proof: Vec<[u8; 32]>,
}

#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// A partial representation of the mmr leaf
pub struct PartialMmrLeaf {
	/// Leaf version
	pub version: MmrLeafVersion,
	/// Parent block number and hash
	pub parent_number_and_hash: (u32, H256),
	/// Next beefy authorities
	pub beefy_next_authority_set: BeefyAuthoritySet<H256>,
}

#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// Parachain header and metadata needed for merkle inclusion proof
pub struct ParachainHeader {
	/// scale encoded parachain header
	pub header: Vec<u8>,
	/// 0-based leaf index in the parachain heads merkle tree
	pub index: u32,
	/// ParaId for parachain
	pub para_id: u32,
}

#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// Parachain proofs definition
pub struct ParachainProof {
	/// List of parachains we have a proof for
	pub parachains: Vec<ParachainHeader>,
	/// Flat proof hashes for parachain header merkle multi-proof.
	pub proof: Vec<[u8; 32]>,
	/// Total leaves count for the proof
	pub total_leaves: u32,
}

#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// Parachain headers update with proof
pub struct ConsensusMessage {
	/// Parachain headers
	pub parachain: ParachainProof,
	/// proof for finalized mmr root
	pub mmr: MmrProof,
}

/// Proof type identifier for naive proofs
pub const PROOF_TYPE_NAIVE: u8 = 0x00;

/// Proof type identifier for SP1 ZK proofs
pub const PROOF_TYPE_SP1: u8 = 0x01;

/// SP1 BEEFY proof. The proof bytes are prefixed with [`PROOF_TYPE_SP1`] by the prover.
#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub struct Sp1BeefyProof {
	/// Relay chain block number
	pub block_number: u32,
	/// Validator set ID that signed the commitment
	pub validator_set_id: u64,
	/// Latest MMR leaf data
	pub mmr_leaf: MmrLeaf<u32, H256, H256, H256>,
	/// Parachain headers finalized by this proof. SP1 proves inclusion via public inputs.
	pub headers: Vec<ParachainHeader>,
	/// SP1 proof bytes
	pub proof: Vec<u8>,
	/// Prover-chosen nonce committed into the SP1 public values. The verifier reconstructs the
	/// committed public inputs with this value, so the proof is only valid for this exact nonce.
	/// Rewarding verifiers (`pallet-beefy-consensus-proofs`) bind it to the extrinsic submission
	/// account, making the proof non-transferable.
	pub nonce: H256,
}

/// finality proof
#[cfg(feature = "std")]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct EncodedVersionedFinalityProof(pub sp_core::Bytes);