zecwallet_parser/zwl/
block.rs1use std::{
2 fmt,
3 io::{self, Read},
4};
5
6use byteorder::{LittleEndian, ReadBytesExt};
7use sapling_crypto::CommitmentTree;
8use zcash_encoding::Vector;
9use zcash_primitives::merkle_tree::read_commitment_tree;
10
11#[derive(Clone, Debug)]
12pub struct CompactBlockData {
13 pub ecb: Vec<u8>,
14 pub height: u64,
15}
16
17impl CompactBlockData {
18 pub fn serialized_version() -> u64 {
19 20
20 }
21
22 pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
23 let height = reader.read_i32::<LittleEndian>()? as u64;
25
26 let mut hash_bytes = [0; 32];
28 reader.read_exact(&mut hash_bytes)?;
29 hash_bytes.reverse();
30 let _hash = hex::encode(hash_bytes);
31
32 let tree: CommitmentTree = read_commitment_tree(&mut reader)?;
35 let _tree = if tree.size() == 0 { None } else { Some(tree) };
36
37 let _version = reader.read_u64::<LittleEndian>()?;
39
40 let ecb = Vector::read(&mut reader, |r| r.read_u8()).unwrap_or_default();
42
43 Ok(Self { ecb, height })
44 }
45}
46
47impl fmt::Display for CompactBlockData {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 writeln!(f, "Block height: {}", self.height).unwrap();
50
51 writeln!(f, "ECB size in bytes: {}", self.ecb.len()).unwrap();
52 Ok(())
53 }
54}