Skip to content

Commit

Permalink
Implement the Pollard
Browse files Browse the repository at this point in the history
Pollard is a middle-ground between full tree and Stump that allows
holding only a subset of UTXOs, while still allowing proving those
elements.
This implementation only allows for full pollards, where all leaves are
present. Allowing partial pollard will be accomplished later.

The implementation uses interior mutability to work. A Pollard is
different from a normal Binary Tree because nodes points to it's
sibling, not their children. This makes recursive algoritms more
evolved, specially during deletions.

This implementation is all iteractive, and carefully uses interior
mutability with Cell and RefCell. Those smart pointers allows
runtime borrow checking, if some of the Borrowing Rules is broken, it
panics. Here, we take care of not holding reference to the interior of a
Cell or RefCell for too log, and not passing `mut ref` arround. Only a
few functions with very limited scope can use `mut ref`s.

A extensible collection of tests is also provided, to check for code
correctness and soundness.
  • Loading branch information
Davidson-Souza committed May 29, 2023
1 parent 5292b46 commit 93bec68
Show file tree
Hide file tree
Showing 6 changed files with 819 additions and 64 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bitcoin_hashes = "0.12.0"
lazy_static = "1.4.0"

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand Down
22 changes: 20 additions & 2 deletions src/accumulator/node_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@
//! assert_eq!(parent, expected_parent);
//! ```
use bitcoin_hashes::{hex, sha256, sha512_256, Hash, HashEngine};
use std::{convert::TryFrom, fmt::Display, ops::Deref, str::FromStr};
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash, PartialOrd, Ord)]
use std::{
convert::TryFrom,
fmt::{Debug, Display},
ops::Deref,
str::FromStr,
};
#[derive(Eq, PartialEq, Copy, Clone, Hash, PartialOrd, Ord)]
/// NodeHash is a wrapper around a 32 byte array that represents a hash of a node in the tree.
/// # Example
/// ```
Expand Down Expand Up @@ -75,6 +80,19 @@ impl Display for NodeHash {
}
}
}
impl Debug for NodeHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
if let NodeHash::Some(ref inner) = self {
let mut s = String::new();
for byte in inner.iter() {
s.push_str(&format!("{:02x}", byte));
}
write!(f, "{}", s)
} else {
write!(f, "empty")
}
}
}

impl From<sha512_256::Hash> for NodeHash {
fn from(hash: sha512_256::Hash) -> Self {
Expand Down
Loading

0 comments on commit 93bec68

Please sign in to comment.