mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11:08 +03:00
parent
7754adb834
commit
a1f74441b5
3 changed files with 13 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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<Hash>) -> 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<Hash>;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Hash> {
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue