Skip to content

Commit

Permalink
Add non-trivial multi-node example
Browse files Browse the repository at this point in the history
  • Loading branch information
SarcasticNastik committed May 11, 2022
1 parent 1c2a80e commit 2b13694
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/policy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,77 @@ mod tests {
"Policy contains duplicate keys"
);
}

// Non-trivial multi-node compilation
{
let node_policies = [
"and(pk(A),pk(B))",
"and(pk(C),older(12960))",
"pk(D)",
"pk(E)",
"thresh(3,pk(F),pk(G),pk(H))",
"and(and(or(2@pk(I),1@pk(J)),or(1@pk(K),20@pk(L))),pk(M))",
"pk(N)",
];

// Floating-point precision errors cause the minor errors
let node_probabilities: [f64; 7] =
[0.12000002, 0.28, 0.08, 0.12, 0.19, 0.18999998, 0.02];

let policy: Concrete<String> = policy_str!(
"{}",
&format!(
"or(4@or(3@{},7@{}),6@thresh(1,or(4@{},6@{}),{},or(9@{},1@{})))",
node_policies[0],
node_policies[1],
node_policies[2],
node_policies[3],
node_policies[4],
node_policies[5],
node_policies[6]
)
);
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();

let mut sorted_policy_prob = node_policies
.into_iter()
.zip(node_probabilities.into_iter())
.collect::<Vec<_>>();
sorted_policy_prob.sort_by(|a, b| (a.1).partial_cmp(&b.1).unwrap());
let sorted_policies = sorted_policy_prob
.into_iter()
.map(|(x, _prob)| x)
.collect::<Vec<_>>();

// Generate TapTree leaves compilations from the given sub-policies
let node_compilations = sorted_policies
.into_iter()
.map(|x| {
let leaf_policy: Concrete<String> = policy_str!("{}", x);
TapTree::Leaf(Arc::from(leaf_policy.compile::<Tap>().unwrap()))
})
.collect::<Vec<_>>();

// Arrange leaf compilations (acc. to probabilities) using huffman encoding into a TapTree
let tree = TapTree::Tree(
Arc::from(TapTree::Tree(
Arc::from(node_compilations[4].clone()),
Arc::from(node_compilations[5].clone()),
)),
Arc::from(TapTree::Tree(
Arc::from(TapTree::Tree(
Arc::from(TapTree::Tree(
Arc::from(node_compilations[0].clone()),
Arc::from(node_compilations[1].clone()),
)),
Arc::from(node_compilations[3].clone()),
)),
Arc::from(node_compilations[6].clone()),
)),
);

let expected_descriptor = Descriptor::new_tr("E".to_string(), Some(tree)).unwrap();
assert_eq!(descriptor, expected_descriptor);
}
}
}

0 comments on commit 2b13694

Please sign in to comment.