Skip to content

Commit

Permalink
policy/compiler: test the different thresh() policy compilation
Browse files Browse the repository at this point in the history
This tests the compilation of the `tresh(k, [pubkeys])` policy for different
numbers of public keys and different values of k.

The next commit will change part of this compilation.

Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
  • Loading branch information
darosior committed Aug 17, 2020
1 parent 87a47e2 commit b45ac46
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/policy/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ mod tests {

use miniscript::{satisfy, Segwitv0};
use policy::Liftable;
use script_num_size;
use BitcoinSig;
use DummyKey;

Expand Down Expand Up @@ -1377,6 +1378,46 @@ mod tests {
]
);
}

#[test]
fn compile_thresh() {
let (keys, _) = pubkeys_and_a_sig(21);

// Up until 20 keys, thresh should be compiled to a multi no matter the value of k
for k in 1..4 {
let small_thresh: BPolicy = policy_str!(
"thresh({},pk({}),pk({}),pk({}))",
k,
keys[0],
keys[1],
keys[2]
);
let small_thresh_ms: SegwitMiniScript = small_thresh.compile().unwrap();
let small_thresh_ms_expected =
ms_str!("multi({},{},{},{})", k, keys[0], keys[1], keys[2]);
assert_eq!(small_thresh_ms, small_thresh_ms_expected);
}

// Above 20 keys, thresh is compiled to a combination of and()s if it's a N of N,
// and to a ms thresh otherwise.
// k = 1 (or 2) does not compile, see https://github.com/rust-bitcoin/rust-miniscript/issues/114
for k in &[10, 15, 21] {
let pubkeys: Vec<Concrete<bitcoin::PublicKey>> =
keys.iter().map(|pubkey| Concrete::Key(*pubkey)).collect();
let big_thresh = Concrete::Threshold(*k, pubkeys);
let big_thresh_ms: SegwitMiniScript = big_thresh.compile().unwrap();
// N * (PUSH + pubkey + CHECKSIG + ADD + SWAP) + N EQUAL
assert_eq!(
big_thresh_ms.script_size(),
keys.len() * (1 + 33 + 3) + script_num_size(*k) + 1 - 2 // minus one SWAP and one ADD
);
let big_thresh_ms_expected = ms_str!(
"thresh({},pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}),s:pk({}))",
k, keys[0], keys[1], keys[2], keys[3], keys[4], keys[5], keys[6], keys[7], keys[8], keys[9],keys[10], keys[11], keys[12], keys[13], keys[14], keys[15], keys[16], keys[17], keys[18], keys[19], keys[20]
);
assert_eq!(big_thresh_ms, big_thresh_ms_expected);
}
}
}

#[cfg(all(test, feature = "unstable"))]
Expand Down

0 comments on commit b45ac46

Please sign in to comment.