Skip to content

Commit cc404a7

Browse files
committed
compiler: test opcode limit and key duplication
Signed-off-by: Antoine Poinsot <[email protected]>
1 parent ad786f5 commit cc404a7

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/policy/compiler.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ mod tests {
11651165
use std::str::FromStr;
11661166
use std::string::String;
11671167

1168-
use miniscript::{satisfy, Segwitv0};
1168+
use miniscript::{satisfy, Legacy, Segwitv0};
11691169
use policy::Liftable;
11701170
use script_num_size;
11711171
use BitcoinSig;
@@ -1514,6 +1514,54 @@ mod tests {
15141514
n_elements,
15151515
);
15161516
}
1517+
1518+
#[test]
1519+
fn shared_limits() {
1520+
// Test the maximum number of OPs with a 67-of-68 multisig
1521+
let (keys, _) = pubkeys_and_a_sig(68);
1522+
let keys: Vec<Concrete<bitcoin::PublicKey>> =
1523+
keys.iter().map(|pubkey| Concrete::Key(*pubkey)).collect();
1524+
let thresh_res: Result<SegwitMiniScript, _> =
1525+
Concrete::Threshold(keys.len() - 1, keys).compile();
1526+
let ops_count = thresh_res.clone().and_then(|m| Ok(m.ext.ops_count_sat));
1527+
assert_eq!(
1528+
thresh_res,
1529+
Err(CompilerError::LimitsExceeded),
1530+
"Compilation succeeded with '{:?}' OP count (sat)",
1531+
ops_count,
1532+
);
1533+
// For legacy too..
1534+
let (keys, _) = pubkeys_and_a_sig(68);
1535+
let keys: Vec<Concrete<bitcoin::PublicKey>> =
1536+
keys.iter().map(|pubkey| Concrete::Key(*pubkey)).collect();
1537+
let thresh_res = Concrete::Threshold(keys.len() - 1, keys).compile::<Legacy>();
1538+
let ops_count = thresh_res.clone().and_then(|m| Ok(m.ext.ops_count_sat));
1539+
assert_eq!(
1540+
thresh_res,
1541+
Err(CompilerError::LimitsExceeded),
1542+
"Compilation succeeded with '{:?}' OP count (sat)",
1543+
ops_count,
1544+
);
1545+
1546+
// Test that we refuse to compile policies with duplicated keys
1547+
let (keys, _) = pubkeys_and_a_sig(1);
1548+
let key = Concrete::Key(keys[0]);
1549+
let res = Concrete::Or(vec![(1, key.clone()), (1, key.clone())]).compile::<Segwitv0>();
1550+
assert_eq!(
1551+
res,
1552+
Err(CompilerError::PolicyError(
1553+
policy::concrete::PolicyError::DuplicatePubKeys
1554+
))
1555+
);
1556+
// Same for legacy
1557+
let res = Concrete::Or(vec![(1, key.clone()), (1, key)]).compile::<Legacy>();
1558+
assert_eq!(
1559+
res,
1560+
Err(CompilerError::PolicyError(
1561+
policy::concrete::PolicyError::DuplicatePubKeys
1562+
))
1563+
);
1564+
}
15171565
}
15181566

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

0 commit comments

Comments
 (0)