simperby_core/
light_client.rs1use crate::*;
2use merkle_tree::*;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct LightClient {
8 pub repository_roots: Vec<Hash256>,
9 pub commit_roots: Vec<Hash256>,
10 pub height_offset: u64,
11 pub last_header: BlockHeader,
12}
13
14impl LightClient {
15 pub fn new(initial_header: BlockHeader) -> Self {
17 Self {
18 repository_roots: vec![initial_header.repository_merkle_root],
19 commit_roots: vec![initial_header.commit_merkle_root],
20 height_offset: initial_header.height,
21 last_header: initial_header,
22 }
23 }
24
25 pub fn update(&mut self, header: BlockHeader, proof: FinalizationProof) -> Result<(), String> {
27 verify::verify_header_to_header(&self.last_header, &header).map_err(|e| e.to_string())?;
28 verify::verify_finalization_proof(&header, &proof).map_err(|e| e.to_string())?;
29 self.repository_roots.push(header.repository_merkle_root);
30 self.commit_roots.push(header.commit_merkle_root);
31 self.last_header = header;
32 Ok(())
33 }
34
35 pub fn verify_transaction_commitment(
37 &self,
38 transaction: &Transaction,
39 block_height: u64,
40 proof: MerkleProof,
41 ) -> bool {
42 let message = serde_spb::to_vec(transaction).unwrap();
43 if block_height < self.height_offset
44 || block_height >= self.height_offset + self.commit_roots.len() as u64
45 {
46 return false;
47 }
48 proof
49 .verify(
50 self.commit_roots[(block_height - self.height_offset) as usize],
51 &message,
52 )
53 .is_ok()
54 }
55
56 pub fn verify_state_commitment(
58 &self,
59 _message: Vec<u8>,
60 _block_height: u64,
61 _proof: MerkleProof,
62 ) -> bool {
63 todo!()
64 }
65}