mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-01-30 15:32:14 +00:00
commit
e2c131fdc3
@ -40,7 +40,7 @@ impl Entry {
|
||||
/// Left child
|
||||
pub fn left(&self) -> Result<EntryLink, Error> {
|
||||
match self.kind {
|
||||
EntryKind::Leaf => { Err(Error::ExpectedNode) }
|
||||
EntryKind::Leaf => { Err(Error::node_expected()) }
|
||||
EntryKind::Node(left, _) => Ok(left)
|
||||
}
|
||||
}
|
||||
@ -48,7 +48,7 @@ impl Entry {
|
||||
/// Right child.
|
||||
pub fn right(&self) -> Result<EntryLink, Error> {
|
||||
match self.kind {
|
||||
EntryKind::Leaf => { Err(Error::ExpectedNode) }
|
||||
EntryKind::Leaf => { Err(Error::node_expected()) }
|
||||
EntryKind::Node(_, right) => Ok(right)
|
||||
}
|
||||
}
|
||||
|
16
src/lib.rs
16
src/lib.rs
@ -17,18 +17,16 @@ pub use entry::{Entry, MAX_ENTRY_SIZE};
|
||||
pub enum Error {
|
||||
/// Entry expected to be presented in the tree view while it was not.
|
||||
ExpectedInMemory(EntryLink),
|
||||
/// Entry expected to be a node.
|
||||
ExpectedNode,
|
||||
/// Entry expected to be a node (specifying for which link this is not true).
|
||||
ExpectedNodeForLink(EntryLink),
|
||||
ExpectedNode(Option<EntryLink>),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match *self {
|
||||
Self::ExpectedInMemory(l) => write!(f, "Node/leaf expected to be in memory: {}", l),
|
||||
Self::ExpectedNode => write!(f, "Node expected"),
|
||||
Self::ExpectedNodeForLink(l) => write!(f, "Node expected, not leaf: {}", l),
|
||||
Self::ExpectedNode(None) => write!(f, "Node expected"),
|
||||
Self::ExpectedNode(Some(l)) => write!(f, "Node expected, not leaf: {}", l),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -63,9 +61,15 @@ pub enum EntryKind {
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Entry expected to be a node (specifying for which link this is not true).
|
||||
pub fn link_node_expected(link: EntryLink) -> Self { Self::ExpectedNode(Some(link)) }
|
||||
|
||||
/// Some entry is expected to be node
|
||||
pub fn node_expected() -> Self { Self::ExpectedNode(None) }
|
||||
|
||||
pub (crate) fn augment(self, link: EntryLink) -> Self {
|
||||
match self {
|
||||
Error::ExpectedNode => Error::ExpectedNodeForLink(link),
|
||||
Error::ExpectedNode(_) => Error::ExpectedNode(Some(link)),
|
||||
val => val
|
||||
}
|
||||
}
|
||||
|
@ -194,9 +194,8 @@ impl NodeData {
|
||||
Self::read(consensus_branch_id, &mut cursor)
|
||||
}
|
||||
|
||||
/// Hash node metadata
|
||||
pub fn hash(&self) -> [u8; 32] {
|
||||
let mut buf = [0u8; 32];
|
||||
|
||||
let bytes = self.to_bytes();
|
||||
|
||||
blake2b_personal(&personalization(self.consensus_branch_id), &bytes)
|
||||
|
25
src/tree.rs
25
src/tree.rs
@ -28,22 +28,15 @@ pub struct Tree {
|
||||
impl Tree {
|
||||
/// Resolve link originated from this tree
|
||||
pub fn resolve_link(&self, link: EntryLink) -> Result<IndexedNode, Error> {
|
||||
match link {
|
||||
EntryLink::Generated(index) => {
|
||||
let node = self.generated.get(index as usize).ok_or(Error::ExpectedInMemory(link))?;
|
||||
Ok(IndexedNode {
|
||||
node,
|
||||
link,
|
||||
})
|
||||
},
|
||||
EntryLink::Stored(index) => {
|
||||
let node = self.stored.get(&index).ok_or(Error::ExpectedInMemory(link))?;
|
||||
Ok(IndexedNode {
|
||||
node,
|
||||
link,
|
||||
})
|
||||
},
|
||||
}
|
||||
match link {
|
||||
EntryLink::Generated(index) => self.generated.get(index as usize),
|
||||
EntryLink::Stored(index) => self.stored.get(&index),
|
||||
}
|
||||
.map(|node| IndexedNode {
|
||||
node,
|
||||
link,
|
||||
})
|
||||
.ok_or(Error::ExpectedInMemory(link))
|
||||
}
|
||||
|
||||
fn push(&mut self, data: Entry) -> EntryLink {
|
||||
|
Loading…
Reference in New Issue
Block a user