From de053e1d8f23ca7ccf60619ef3443d709ad8ff6e Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 7 Sep 2019 14:00:34 +0300 Subject: [PATCH] reading for Entry --- src/entry.rs | 27 ++++++++++++++++++++++++++- src/node_data.rs | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index da0b131..3ed1c71 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,4 +1,4 @@ -use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt, ByteOrder}; +use byteorder::{LittleEndian, ReadBytesExt}; use crate::{EntryKind, NodeData, Error, EntryLink}; @@ -39,6 +39,31 @@ impl Entry { EntryKind::Node(_, right) => Ok(right) } } + + pub fn read(&self, consensus_branch_id: u32, r: &mut R) -> std::io::Result { + let kind = { + match r.read_u8()? { + 0 => { + let left = r.read_u32::()?; + let right = r.read_u32::()?; + EntryKind::Node(EntryLink::Stored(left), EntryLink::Stored(right)) + }, + 1 => { + EntryKind::Leaf + }, + _ => { + return Err(std::io::Error::from(std::io::ErrorKind::InvalidData)) + }, + } + }; + + let data = NodeData::read(consensus_branch_id, r)?; + + Ok(Entry { + kind, + data, + }) + } } impl From for Entry { diff --git a/src/node_data.rs b/src/node_data.rs index ab08d4a..b95f643 100644 --- a/src/node_data.rs +++ b/src/node_data.rs @@ -121,7 +121,7 @@ impl NodeData { Ok(()) } - pub fn read(&self, consensus_branch_id: u32, r: &mut R) -> std::io::Result { + pub fn read(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)?;