mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-01-30 23:42:13 +00:00
example and neccessary fixes
This commit is contained in:
parent
872ac5af7b
commit
6b36cb5a51
219
examples/producer.rs
Normal file
219
examples/producer.rs
Normal file
@ -0,0 +1,219 @@
|
||||
extern crate zcash_mmr as mmr;
|
||||
|
||||
use mmr::{NodeData, Tree, EntryLink, Entry};
|
||||
|
||||
fn prepare_tree(vec: Vec<NodeData>) -> (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<<h) - 1) as u32),
|
||||
EntryLink::Stored((peak_pos - 2) as u32),
|
||||
);
|
||||
nodes.push(((peak_pos-1) as u32, root_peak));
|
||||
|
||||
// + 2^(h+1)-1
|
||||
peak_pos = peak_pos + (1 << (h+1)) - 1;
|
||||
|
||||
loop {
|
||||
|
||||
if peak_pos > vec.len() {
|
||||
// left child, -2^h
|
||||
peak_pos = peak_pos - (1<<h);
|
||||
h = h - 1;
|
||||
}
|
||||
|
||||
if peak_pos <= vec.len() {
|
||||
let mut peak: Entry = vec[peak_pos-1].clone().into();
|
||||
if h != 0 {
|
||||
peak.update_siblings(
|
||||
EntryLink::Stored((peak_pos - (1<<h) - 1) as u32),
|
||||
EntryLink::Stored((peak_pos - 2) as u32),
|
||||
);
|
||||
}
|
||||
nodes.push(((peak_pos-1) as u32, peak));
|
||||
|
||||
// right sibling
|
||||
peak_pos = peak_pos + (1 << (h+1)) - 1;
|
||||
}
|
||||
|
||||
if h == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Tree::new(vec.len() as u32, nodes, vec![])
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut initial_tree_vec = vec![
|
||||
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 10,
|
||||
end_time: 20,
|
||||
start_target: 100,
|
||||
end_target: 110,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 1,
|
||||
end_height: 1,
|
||||
shielded_tx: 5,
|
||||
},
|
||||
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 21,
|
||||
end_time: 30,
|
||||
start_target: 110,
|
||||
end_target: 120,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 2,
|
||||
end_height: 2,
|
||||
shielded_tx: 4,
|
||||
},
|
||||
];
|
||||
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[0], &initial_tree_vec[1]));
|
||||
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 31,
|
||||
end_time: 40,
|
||||
start_target: 120,
|
||||
end_target: 130,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 3,
|
||||
end_height: 3,
|
||||
shielded_tx: 6,
|
||||
},
|
||||
);
|
||||
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 41,
|
||||
end_time: 50,
|
||||
start_target: 130,
|
||||
end_target: 140,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 4,
|
||||
end_height: 4,
|
||||
shielded_tx: 7,
|
||||
},
|
||||
);
|
||||
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[3], &initial_tree_vec[4]));
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[2], &initial_tree_vec[5]));
|
||||
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 51,
|
||||
end_time: 60,
|
||||
start_target: 140,
|
||||
end_target: 150,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 5,
|
||||
end_height: 5,
|
||||
shielded_tx: 8,
|
||||
},
|
||||
);
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 61,
|
||||
end_time: 70,
|
||||
start_target: 150,
|
||||
end_target: 160,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 6,
|
||||
end_height: 6,
|
||||
shielded_tx: 9,
|
||||
},
|
||||
);
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[7], &initial_tree_vec[8]));
|
||||
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 71,
|
||||
end_time: 80,
|
||||
start_target: 160,
|
||||
end_target: 170,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 7,
|
||||
end_height: 7,
|
||||
shielded_tx: 10,
|
||||
},
|
||||
);
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 81,
|
||||
end_time: 90,
|
||||
start_target: 170,
|
||||
end_target: 180,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 8,
|
||||
end_height: 8,
|
||||
shielded_tx: 11,
|
||||
},
|
||||
);
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[10], &initial_tree_vec[11]));
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[9], &initial_tree_vec[12]));
|
||||
initial_tree_vec.push(NodeData::combine(&initial_tree_vec[6], &initial_tree_vec[13]));
|
||||
|
||||
initial_tree_vec.push(
|
||||
NodeData {
|
||||
consensus_branch_id: 0,
|
||||
subtree_commitment: [0u8; 32],
|
||||
start_time: 91,
|
||||
end_time: 100,
|
||||
start_target: 180,
|
||||
end_target: 190,
|
||||
start_sapling_root: [0u8; 32],
|
||||
end_sapling_root: [0u8; 32],
|
||||
subtree_total_work: Default::default(),
|
||||
start_height: 9,
|
||||
end_height: 9,
|
||||
shielded_tx: 12,
|
||||
},
|
||||
);
|
||||
|
||||
let (_tree, root) = prepare_tree(initial_tree_vec);
|
||||
println!("root: {}", root);
|
||||
|
||||
}
|
@ -45,12 +45,17 @@ pub enum EntryKind {
|
||||
Node(EntryLink, EntryLink),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Entry {
|
||||
kind: EntryKind,
|
||||
data: NodeData,
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
pub fn update_siblings(&mut self, left: EntryLink, right: EntryLink) {
|
||||
self.kind = EntryKind::Node(left, right);
|
||||
}
|
||||
|
||||
pub fn complete(&self) -> bool {
|
||||
let leaves = self.leaf_count();
|
||||
leaves & (leaves - 1) == 0
|
||||
|
@ -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],
|
||||
|
27
src/tree.rs
27
src/tree.rs
@ -88,23 +88,28 @@ impl Tree {
|
||||
|
||||
pub fn new(
|
||||
length: u32,
|
||||
stored: Vec<(u32, Entry)>,
|
||||
generated: Vec<Entry>,
|
||||
) -> 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<EntryLink>) -> Result<(), Error> {
|
||||
|
Loading…
Reference in New Issue
Block a user