diff --git a/src/lib.rs b/src/lib.rs index db965492..083752bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,3 +34,4 @@ pub use tree::{Batch, BatchEntry, Hash, Op, PanicSource, HASH_LENGTH}; pub use proofs::query::verify_query; pub use proofs::query::verify; +pub use proofs::query::execute_proof; diff --git a/src/proofs/query/map.rs b/src/proofs/query/map.rs index 5c82f008..dc2f7a35 100644 --- a/src/proofs/query/map.rs +++ b/src/proofs/query/map.rs @@ -4,6 +4,7 @@ use failure::{bail, ensure, format_err}; use std::collections::btree_map; use std::collections::BTreeMap; use std::ops::{Bound, RangeBounds}; +use std::collections::btree_map::Iter; /// `MapBuilder` allows a consumer to construct a `Map` by inserting the nodes /// contained in a proof, in key-order. @@ -77,6 +78,10 @@ impl Map { Ok(entry) } + pub fn all(&self) -> Iter<'_, Vec, (bool, Vec)> { + self.entries.iter() + } + /// Returns an iterator over all (key, value) entries in the requested range /// of keys. If during iteration we encounter a gap in the data (e.g. the /// proof did not include all nodes within the range), the iterator will diff --git a/src/proofs/query/mod.rs b/src/proofs/query/mod.rs index fb66f8a7..84e5a1a5 100644 --- a/src/proofs/query/mod.rs +++ b/src/proofs/query/mod.rs @@ -355,6 +355,16 @@ pub fn verify(bytes: &[u8], expected_hash: Hash) -> Result { Ok(map_builder.build()) } +pub fn execute_proof(bytes: &[u8]) -> Result<(Hash,Map)> { + let ops = Decoder::new(bytes); + let mut map_builder = MapBuilder::new(); + + let root = execute(ops, true, |node| map_builder.insert(node))?; + + Ok((root.hash(), map_builder.build())) +} + + /// Verifies the encoded proof with the given query and expected hash. /// /// Every key in `keys` is checked to either have a key/value pair in the proof,