From a1f74441b50092a553ae50b442e9d70ebdef68eb Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Wed, 17 Oct 2018 16:04:33 +0100 Subject: [PATCH] cleanup build warnings (#1773) add docs/comments --- core/src/core/hash.rs | 1 + core/src/core/pmmr/backend.rs | 4 ++++ core/src/core/pmmr/db_pmmr.rs | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/core/src/core/hash.rs b/core/src/core/hash.rs index 873a3b001..834747ae1 100644 --- a/core/src/core/hash.rs +++ b/core/src/core/hash.rs @@ -53,6 +53,7 @@ impl fmt::Display for Hash { } impl Hash { + /// Size of a hash in bytes. pub const SIZE: usize = 32; /// Builds a Hash from a byte vector. If the vector is too short, it will be diff --git a/core/src/core/pmmr/backend.rs b/core/src/core/pmmr/backend.rs index 5ce9e0b19..b0ca5cdd9 100644 --- a/core/src/core/pmmr/backend.rs +++ b/core/src/core/pmmr/backend.rs @@ -18,11 +18,15 @@ use core::hash::Hash; use core::BlockHeader; use ser::PMMRable; +/// Simple "hash only" backend (used for header MMR, headers stored in the db). pub trait HashOnlyBackend { + /// Append a vec of hashes to the backend. fn append(&mut self, data: Vec) -> Result<(), String>; + /// Rewind the backend to the specified position. fn rewind(&mut self, position: u64) -> Result<(), String>; + /// Get the hash at the specified position. fn get_hash(&self, position: u64) -> Option; } diff --git a/core/src/core/pmmr/db_pmmr.rs b/core/src/core/pmmr/db_pmmr.rs index 58a8904f8..a899a95b6 100644 --- a/core/src/core/pmmr/db_pmmr.rs +++ b/core/src/core/pmmr/db_pmmr.rs @@ -58,14 +58,17 @@ where } } + /// Get the unpruned size of the MMR. pub fn unpruned_size(&self) -> u64 { self.last_pos } + /// Is the MMR empty? pub fn is_empty(&self) -> bool { self.last_pos == 0 } + /// Rewind the MMR to the specified position. pub fn rewind(&mut self, position: u64) -> Result<(), String> { // Identify which actual position we should rewind to as the provided // position is a leaf. We traverse the MMR to include any parent(s) that @@ -126,6 +129,7 @@ where Ok(elmt_pos) } + /// Return the vec of peak hashes for this MMR. pub fn peaks(&self) -> Vec { let peaks_pos = peaks(self.last_pos); peaks_pos @@ -134,6 +138,7 @@ where .collect() } + /// Return the overall root hash for this MMR. pub fn root(&self) -> Hash { let mut res = None; for peak in self.peaks().iter().rev() { @@ -145,6 +150,9 @@ where res.expect("no root, invalid tree") } + /// Validate all the hashes in the MMR. + /// For every parent node we check hashes of the children produce the parent hash + /// by hashing them together. pub fn validate(&self) -> Result<(), String> { // iterate on all parent nodes for n in 1..(self.last_pos + 1) {