diff --git a/examples/producer.rs b/examples/producer.rs new file mode 100644 index 0000000..b439947 --- /dev/null +++ b/examples/producer.rs @@ -0,0 +1,219 @@ +extern crate zcash_mmr as mmr; + +use mmr::{NodeData, Tree, EntryLink, Entry}; + +fn prepare_tree(vec: Vec) -> (Tree, EntryLink) { + + assert!(vec.len() > 0); + + // integer log2 of (vec.len()+1), -1 + let mut h = (32 - ((vec.len()+1) as u32).leading_zeros() - 1)-1; + let mut peak_pos = (1 << (h+1)) - 1; + + let mut nodes = Vec::new(); + let mut root_peak: Entry = vec[peak_pos-1].clone().into(); + + root_peak.update_siblings( + EntryLink::Stored((peak_pos - (1< vec.len() { + // left child, -2^h + peak_pos = peak_pos - (1< bool { let leaves = self.leaf_count(); leaves & (leaves - 1) == 0 diff --git a/src/node_data.rs b/src/node_data.rs index 23452c2..d5cec61 100644 --- a/src/node_data.rs +++ b/src/node_data.rs @@ -4,7 +4,7 @@ use blake2::blake2b::Blake2b; /// Node metadata. #[repr(C)] -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct NodeData { pub consensus_branch_id: u32, pub subtree_commitment: [u8; 32], diff --git a/src/tree.rs b/src/tree.rs index bc85416..11e93e3 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -88,23 +88,28 @@ impl Tree { pub fn new( length: u32, - stored: Vec<(u32, Entry)>, - generated: Vec, - ) -> Self { + peaks: Vec<(u32, Entry)>, + extra: Vec<(u32, Entry)>, + ) -> (Self, EntryLink) { let mut result = Tree::default(); result.stored_count = length; - for (idx, node) in stored.into_iter() { + let mut gen = 0; + let mut root = EntryLink::Stored(peaks[0].0); + for (idx, node) in peaks.into_iter() { result.stored.insert(idx, node); + if gen != 0 { + let next_generated = + combine_nodes(result. + resolve_link(root).expect("Inserted before, cannot fail; qed"), + result.resolve_link(EntryLink::Stored(idx)).expect("Inserted before, cannot fail; qed") + ); + root = result.push_generated(next_generated); + } + gen += 1; } - result.generated_count = generated.len() as u32; - - for (idx, node) in generated.into_iter().enumerate() { - result.generated.insert(idx as u32, node); - } - - result + (result, root) } fn get_peaks(&self, root: EntryLink, target: &mut Vec) -> Result<(), Error> {