Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 88602b7

Browse files
committedApr 3, 2022
Add non-trivial multi-node example
1 parent b64bf26 commit 88602b7

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
 

‎src/policy/mod.rs

+72
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,77 @@ mod tests {
418418
"Policy contains duplicate keys"
419419
);
420420
}
421+
422+
// Non-trivial multi-node compilation
423+
{
424+
let node_policies = [
425+
"and(pk(A),pk(B))",
426+
"and(pk(C),older(12960))",
427+
"pk(D)",
428+
"pk(E)",
429+
"thresh(3,pk(F),pk(G),pk(H))",
430+
"and(and(or(2@pk(I),1@pk(J)),or(1@pk(K),20@pk(L))),pk(M))",
431+
"pk(N)",
432+
];
433+
434+
// Floating-point precision errors cause the minor errors
435+
let node_probabilities: [f64; 7] =
436+
[0.12000002, 0.28, 0.08, 0.12, 0.19, 0.18999998, 0.02];
437+
438+
let policy: Concrete<String> = policy_str!(
439+
"{}",
440+
&format!(
441+
"or(4@or(3@{},7@{}),6@thresh(1,or(4@{},6@{}),{},or(9@{},1@{})))",
442+
node_policies[0],
443+
node_policies[1],
444+
node_policies[2],
445+
node_policies[3],
446+
node_policies[4],
447+
node_policies[5],
448+
node_policies[6]
449+
)
450+
);
451+
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
452+
453+
let mut sorted_policy_prob = node_policies
454+
.into_iter()
455+
.zip(node_probabilities.into_iter())
456+
.collect::<Vec<_>>();
457+
sorted_policy_prob.sort_by(|a, b| (a.1).partial_cmp(&b.1).unwrap());
458+
let sorted_policies = sorted_policy_prob
459+
.into_iter()
460+
.map(|(x, _prob)| x)
461+
.collect::<Vec<_>>();
462+
463+
// Generate TapTree leaves compilations from the given sub-policies
464+
let node_compilations = sorted_policies
465+
.into_iter()
466+
.map(|x| {
467+
let leaf_policy: Concrete<String> = policy_str!("{}", x);
468+
TapTree::Leaf(Arc::from(leaf_policy.compile::<Tap>().unwrap()))
469+
})
470+
.collect::<Vec<_>>();
471+
472+
// Arrange leaf compilations (acc. to probabilities) using huffman encoding into a TapTree
473+
let tree = TapTree::Tree(
474+
Arc::from(TapTree::Tree(
475+
Arc::from(node_compilations[4].clone()),
476+
Arc::from(node_compilations[5].clone()),
477+
)),
478+
Arc::from(TapTree::Tree(
479+
Arc::from(TapTree::Tree(
480+
Arc::from(TapTree::Tree(
481+
Arc::from(node_compilations[0].clone()),
482+
Arc::from(node_compilations[1].clone()),
483+
)),
484+
Arc::from(node_compilations[3].clone()),
485+
)),
486+
Arc::from(node_compilations[6].clone()),
487+
)),
488+
);
489+
490+
let expected_descriptor = Descriptor::new_tr("E".to_string(), Some(tree)).unwrap();
491+
assert_eq!(descriptor, expected_descriptor);
492+
}
421493
}
422494
}

0 commit comments

Comments
 (0)
Please sign in to comment.