-
Notifications
You must be signed in to change notification settings - Fork 52
/
lib.rs
186 lines (162 loc) · 5.71 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2022 ComposableFi.
// 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)]
pub mod error;
use beefy_primitives::mmr::MmrLeafVersion;
pub use beefy_primitives::mmr::{BeefyNextAuthoritySet, MmrLeaf};
use codec::{Decode, Encode};
use core::marker::PhantomData;
use sp_core::H256;
use sp_std::prelude::*;
#[derive(sp_std::fmt::Debug, Encode, Decode, PartialEq, Eq, Clone)]
/// Client state definition for the light client
pub struct ClientState {
/// Latest beefy height
pub latest_beefy_height: u32,
/// Latest mmr root hash
pub mmr_root_hash: H256,
/// Authorities for the current session
pub current_authorities: BeefyNextAuthoritySet<H256>,
/// Authorities for the next session
pub next_authorities: BeefyNextAuthoritySet<H256>,
}
/// Host functions that allow the light client perform cryptographic operations in native.
pub trait HostFunctions: light_client_common::HostFunctions {
/// Keccak 256 hash function
fn keccak_256(input: &[u8]) -> [u8; 32];
/// Compressed Ecdsa public key recovery from a signature
fn secp256k1_ecdsa_recover_compressed(
signature: &[u8; 65],
value: &[u8; 32],
) -> Option<Vec<u8>>;
}
/// 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,
/// Index in signatures vector
pub index: u32,
}
#[derive(Clone, sp_std::fmt::Debug, PartialEq, Eq, Encode, Decode)]
/// Signed commitment
pub struct SignedCommitment {
/// Commitment
pub commitment: beefy_primitives::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 MmrUpdateProof {
/// 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: pallet_mmr_primitives::Proof<H256>,
/// Proof for authorities in current session
pub authority_proof: Vec<Hash>,
}
#[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: BeefyNextAuthoritySet<H256>,
}
#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// Parachain header definition
pub struct ParachainHeader {
/// scale encoded parachain header
pub parachain_header: Vec<u8>,
/// Reconstructed mmr leaf
pub partial_mmr_leaf: PartialMmrLeaf,
/// parachain id
pub para_id: u32,
/// Proof for our parachain header inclusion in the parachain headers root
pub parachain_heads_proof: Vec<Hash>,
/// leaf index for parachain heads proof
pub heads_leaf_index: u32,
/// Total number of parachain heads
pub heads_total_count: u32,
/// Trie merkle proof of inclusion of the set timestamp extrinsic in header.extrinsic_root
/// this already encodes the actual extrinsic
pub extrinsic_proof: Vec<Vec<u8>>,
/// Timestamp extrinsic
pub timestamp_extrinsic: Vec<u8>,
}
#[derive(sp_std::fmt::Debug, Clone, PartialEq, Eq, Encode, Decode)]
/// Parachain headers update with proof
pub struct ParachainsUpdateProof {
/// Parachai headers
pub parachain_headers: Vec<ParachainHeader>,
/// Mmr Batch proof for parachain headers
pub mmr_proof: pallet_mmr_primitives::Proof<H256>,
}
#[cfg(feature = "std")]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
/// finality proof
pub struct EncodedVersionedFinalityProof(pub sp_core::Bytes);
/// MMR nodes & size -related utilities.
pub struct NodesUtils {
no_of_leaves: u64,
}
impl NodesUtils {
/// Create new instance of MMR nodes utilities for given number of leaves.
pub fn new(no_of_leaves: u64) -> Self {
Self { no_of_leaves }
}
/// Calculate number of peaks in the MMR.
pub fn number_of_peaks(&self) -> u64 {
self.number_of_leaves().count_ones() as u64
}
/// Return the number of leaves in the MMR.
pub fn number_of_leaves(&self) -> u64 {
self.no_of_leaves
}
/// Calculate the total size of MMR (number of nodes).
pub fn size(&self) -> u64 {
2 * self.no_of_leaves - self.number_of_peaks()
}
}
/// Merkle Hasher for mmr library
#[derive(Clone)]
pub struct MerkleHasher<T: HostFunctions>(PhantomData<T>);
impl<T: HostFunctions + Clone> mmr_lib::Merge for MerkleHasher<T> {
type Item = H256;
fn merge(left: &Self::Item, right: &Self::Item) -> Self::Item {
let mut concat = left.as_bytes().to_vec();
concat.extend_from_slice(right.as_bytes());
T::keccak_256(&*concat).into()
}
}
impl<T: HostFunctions + Clone> rs_merkle::Hasher for MerkleHasher<T> {
type Hash = [u8; 32];
fn hash(data: &[u8]) -> Self::Hash {
T::keccak_256(data)
}
}