From 5d0aa521c2e8d51f4b42e023004d1657d1965051 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 7 Sep 2019 11:19:06 +0300 Subject: [PATCH] missing deserialization bits --- src/node_data.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/node_data.rs b/src/node_data.rs index d5cec61..ab08d4a 100644 --- a/src/node_data.rs +++ b/src/node_data.rs @@ -1,10 +1,10 @@ -use byteorder::{LittleEndian, WriteBytesExt, ByteOrder}; +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt, ByteOrder}; use bigint::U256; use blake2::blake2b::Blake2b; /// Node metadata. #[repr(C)] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct NodeData { pub consensus_branch_id: u32, pub subtree_commitment: [u8; 32], @@ -91,6 +91,17 @@ impl NodeData { Ok(()) } + fn read_compact(reader: &mut R) -> std::io::Result { + let result = match reader.read_u8()? { + i @ 0..=0xfc => i.into(), + 0xfd => reader.read_u16::()?.into(), + 0xfe => reader.read_u32::()?.into(), + _ => reader.read_u64::()?.into(), + }; + + Ok(result) + } + pub fn write(&self, w: &mut W) -> std::io::Result<()> { w.write_all(&self.subtree_commitment)?; w.write_u32::(self.start_time)?; @@ -110,6 +121,28 @@ impl NodeData { Ok(()) } + pub fn read(&self, consensus_branch_id: u32, r: &mut R) -> std::io::Result { + let mut data = Self::default(); + data.consensus_branch_id = consensus_branch_id; + r.read_exact(&mut data.subtree_commitment)?; + data.start_time = r.read_u32::()?; + data.end_time = r.read_u32::()?; + data.start_target= r.read_u32::()?; + data.end_target= r.read_u32::()?; + r.read_exact(&mut data.start_sapling_root)?; + r.read_exact(&mut data.end_sapling_root)?; + + let mut work_buf = [0u8; 32]; + r.read_exact(&mut work_buf)?; + data.subtree_total_work = U256::from_little_endian(&work_buf); + + data.start_height = Self::read_compact(r)?; + data.end_height = Self::read_compact(r)?; + data.shielded_tx = Self::read_compact(r)?; + + Ok(data) + } + pub fn to_bytes(&self) -> Vec { let mut buf = [0u8; Self::MAX_SERIALIZED_SIZE]; let pos = {