pub struct Map<D, K, V>{ /* private fields */ }Expand description
Immutable Map w/ Inclusion Proofs
§Usage
Using a Map should feel similar to using a HashMap. All maps begin
by creating an empty map and then populating it with items. Each time you
insert to a map, it creates a new map derived from the prior map. Once
items are inserted, you can get an inclusion proof that demonstrates the
presence of a key/value in a tree.
use warg_transparency::map::Map;
use sha2::Sha256;
let a = Map::<Sha256, &str, &str>::default();
let b = a.insert("foo", "bar");
let c = b.extend([("baz", "bat"), ("foo", "qux")]);
let proof = c.prove(&"foo").unwrap();
assert_eq!(c.root().clone(), proof.evaluate(&"foo", &"qux"));§Design
A Map is a key/value store backed by a Merkle tree. Its values are
stored in a binary tree. A cryptographic hash function is applied to the
key and its resulting bits are interpreted as a path to descend the tree.
This implies that the depth of the tree is n where n is the number of
bits in the cryptographic hash function. Because this would cause the tree
to have 2^(n+1) - 1 entries (far too many!), the tree is sparse and
only creates nodes as necessary to represent the contents in the tree.
§Hashing Strategy
§Leaf Nodes
Leaf node hashes are calculated using the one-byte 0 prefix to prevent collisions with branch hashes.
hash(Leaf(value)) = hash(0x00 || <value>)For leaf nodes that semantically “contain no value”, the <value> provided is empty i.e. the zero-length byte sequence.
§Branch Nodes
Branch node hashes are calculated using the one-byte 1 prefix to prevent collisions with branch hashes.
hash(Branch(left, right)) = hash(0x01 || hash(<left>) || hash(<right>))Implementations§
Source§impl<D, K, V> Map<D, K, V>
impl<D, K, V> Map<D, K, V>
Sourcepub fn root(&self) -> &Hash<D>
pub fn root(&self) -> &Hash<D>
The hash of the root of the tree.
This uniquely identifies the map and its contents.
Sourcepub fn prove(&self, key: K) -> Option<Proof<D, K, V>>
pub fn prove(&self, key: K) -> Option<Proof<D, K, V>>
Gets the value for a given key and a proof of its presence in this map.
Sourcepub fn insert(&self, key: K, val: V) -> Self
pub fn insert(&self, key: K, val: V) -> Self
Insert a value into the map, creating a new map.
This replaces any existing items with the same key.
Sourcepub fn extend(&self, iter: impl IntoIterator<Item = (K, V)>) -> Self
pub fn extend(&self, iter: impl IntoIterator<Item = (K, V)>) -> Self
Inserts all key/value pairs into the map, creating a new map.
Trait Implementations§
impl<D, K, V> Eq for Map<D, K, V>
Auto Trait Implementations§
impl<D, K, V> Freeze for Map<D, K, V>
impl<D, K, V> RefUnwindSafe for Map<D, K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
<<D as OutputSizeUser>::OutputSize as ArrayLength<u8>>::ArrayType: RefUnwindSafe,
impl<D, K, V> Send for Map<D, K, V>
impl<D, K, V> Sync for Map<D, K, V>
impl<D, K, V> Unpin for Map<D, K, V>
impl<D, K, V> UnwindSafe for Map<D, K, V>where
K: UnwindSafe,
V: UnwindSafe,
<<D as OutputSizeUser>::OutputSize as ArrayLength<u8>>::ArrayType: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.