beefy_verifier_primitives/lib.rs
1// Copyright (C) 2022 Polytope Labs.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Primitive BEEFY types used by verifier and prover
17
18#![cfg_attr(not(feature = "std"), no_std)]
19#![allow(clippy::all)]
20#![deny(missing_docs)]
21
22use codec::{Decode, Encode};
23use polkadot_sdk::*;
24use sp_consensus_beefy::mmr::{BeefyAuthoritySet, MmrLeaf, MmrLeafVersion};
25use sp_core::H256;
26use sp_std::prelude::*;
27
28/// Client state definition for the light client
29#[derive(sp_std::fmt::Debug, Encode, Decode, PartialEq, Eq, Clone)]
30pub struct ConsensusState {
31 /// Latest beefy height
32 pub latest_beefy_height: u32,
33 /// Height at which beefy was activated.
34 pub beefy_activation_block: u32,
35 /// Latest mmr root hash
36 pub mmr_root_hash: H256,
37 /// Authorities for the current session
38 pub current_authorities: BeefyAuthoritySet<H256>,
39 /// Authorities for the next session
40 pub next_authorities: BeefyAuthoritySet<H256>,
41}
42
43/// Hash length definition for hashing algorithms used
44pub const HASH_LENGTH: usize = 32;
45/// Authority Signature type
46pub type TSignature = [u8; 65];
47/// Represents a Hash in this library
48pub type Hash = [u8; 32];
49
50#[derive(Clone, sp_std::fmt::Debug, PartialEq, Eq, Encode, Decode)]
51/// Authority signature and its index in the signatures array
52pub struct SignatureWithAuthorityIndex {
53 /// Authority signature
54 pub signature: TSignature,
55 /// 0-based index of the authority in the authority set
56 pub index: u32,
57}
58
59#[derive(Clone, sp_std::fmt::Debug, PartialEq, Eq, Encode, Decode)]
60/// Signed commitment
61pub struct SignedCommitment {
62 /// Commitment
63 pub commitment: sp_consensus_beefy::Commitment<u32>,
64 /// Signatures for this commitment
65 pub signatures: Vec<SignatureWithAuthorityIndex>,
66}
67
68#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
69/// Mmr Update with proof
70pub struct MmrProof {
71 /// Signed commitment
72 pub signed_commitment: SignedCommitment,
73 /// Latest leaf added to mmr
74 pub latest_mmr_leaf: MmrLeaf<u32, H256, H256, H256>,
75 /// Proof for the latest mmr leaf
76 pub mmr_proof: sp_mmr_primitives::LeafProof<H256>,
77 /// Flat proof hashes for authorities merkle multi-proof.
78 pub authority_proof: Vec<[u8; 32]>,
79}
80
81#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
82/// A partial representation of the mmr leaf
83pub struct PartialMmrLeaf {
84 /// Leaf version
85 pub version: MmrLeafVersion,
86 /// Parent block number and hash
87 pub parent_number_and_hash: (u32, H256),
88 /// Next beefy authorities
89 pub beefy_next_authority_set: BeefyAuthoritySet<H256>,
90}
91
92#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
93/// Parachain header and metadata needed for merkle inclusion proof
94pub struct ParachainHeader {
95 /// scale encoded parachain header
96 pub header: Vec<u8>,
97 /// 0-based leaf index in the parachain heads merkle tree
98 pub index: u32,
99 /// ParaId for parachain
100 pub para_id: u32,
101}
102
103#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
104/// Parachain proofs definition
105pub struct ParachainProof {
106 /// List of parachains we have a proof for
107 pub parachains: Vec<ParachainHeader>,
108 /// Flat proof hashes for parachain header merkle multi-proof.
109 pub proof: Vec<[u8; 32]>,
110 /// Total leaves count for the proof
111 pub total_leaves: u32,
112}
113
114#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
115/// Parachain headers update with proof
116pub struct ConsensusMessage {
117 /// Parachain headers
118 pub parachain: ParachainProof,
119 /// proof for finalized mmr root
120 pub mmr: MmrProof,
121}
122
123/// Proof type identifier for naive proofs
124pub const PROOF_TYPE_NAIVE: u8 = 0x00;
125
126/// Proof type identifier for SP1 ZK proofs
127pub const PROOF_TYPE_SP1: u8 = 0x01;
128
129/// SP1 BEEFY proof. The proof bytes are prefixed with [`PROOF_TYPE_SP1`] by the prover.
130#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
131pub struct Sp1BeefyProof {
132 /// Relay chain block number
133 pub block_number: u32,
134 /// Validator set ID that signed the commitment
135 pub validator_set_id: u64,
136 /// Latest MMR leaf data
137 pub mmr_leaf: MmrLeaf<u32, H256, H256, H256>,
138 /// Parachain headers finalized by this proof. SP1 proves inclusion via public inputs.
139 pub headers: Vec<ParachainHeader>,
140 /// SP1 proof bytes
141 pub proof: Vec<u8>,
142 /// Prover-chosen nonce committed into the SP1 public values. The verifier reconstructs the
143 /// committed public inputs with this value, so the proof is only valid for this exact nonce.
144 /// Rewarding verifiers (`pallet-beefy-consensus-proofs`) bind it to the extrinsic submission
145 /// account, making the proof non-transferable.
146 pub nonce: H256,
147}
148
149/// finality proof
150#[cfg(feature = "std")]
151#[derive(Clone, serde::Serialize, serde::Deserialize)]
152pub struct EncodedVersionedFinalityProof(pub sp_core::Bytes);