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