Skip to content

Commit 75f400f

Browse files
committed
error: remove two more variants which were redundant with threshold errors
1 parent cdac32e commit 75f400f

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,6 @@ pub enum Error {
423423
AddrError(bitcoin::address::ParseError),
424424
/// rust-bitcoin p2sh address error
425425
AddrP2shError(bitcoin::address::P2shError),
426-
/// A `CHECKMULTISIG` opcode was preceded by a number > 20
427-
CmsTooManyKeys(u32),
428-
/// A tapscript multi_a cannot support more than Weight::MAX_BLOCK/32 keys
429-
MultiATooManyKeys(u64),
430426
/// While parsing backward, hit beginning of script
431427
UnexpectedStart,
432428
/// Got something we were not expecting
@@ -504,7 +500,6 @@ impl fmt::Display for Error {
504500
Error::Script(ref e) => fmt::Display::fmt(e, f),
505501
Error::AddrError(ref e) => fmt::Display::fmt(e, f),
506502
Error::AddrP2shError(ref e) => fmt::Display::fmt(e, f),
507-
Error::CmsTooManyKeys(n) => write!(f, "checkmultisig with {} keys", n),
508503
Error::UnexpectedStart => f.write_str("unexpected start of script"),
509504
Error::Unexpected(ref s) => write!(f, "unexpected «{}»", s),
510505
Error::MultiColon(ref s) => write!(f, "«{}» has multiple instances of «:»", s),
@@ -539,7 +534,6 @@ impl fmt::Display for Error {
539534
Error::PubKeyCtxError(ref pk, ref ctx) => {
540535
write!(f, "Pubkey error: {} under {} scriptcontext", pk, ctx)
541536
}
542-
Error::MultiATooManyKeys(k) => write!(f, "MultiA too many keys {}", k),
543537
Error::TrNoScriptCode => write!(f, "No script code for Tr descriptors"),
544538
Error::MultipathDescLenMismatch => write!(f, "At least two BIP389 key expressions in the descriptor contain tuples of derivation indexes of different lengths"),
545539
Error::AbsoluteLockTime(ref e) => e.fmt(f),
@@ -560,8 +554,6 @@ impl std::error::Error for Error {
560554
InvalidOpcode(_)
561555
| NonMinimalVerify(_)
562556
| InvalidPush(_)
563-
| CmsTooManyKeys(_)
564-
| MultiATooManyKeys(_)
565557
| UnexpectedStart
566558
| Unexpected(_)
567559
| MultiColon(_)

src/miniscript/decode.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::miniscript::lex::{Token as Tk, TokenIter};
1717
use crate::miniscript::limits::{MAX_PUBKEYS_IN_CHECKSIGADD, MAX_PUBKEYS_PER_MULTISIG};
1818
use crate::miniscript::ScriptContext;
1919
use crate::prelude::*;
20+
use crate::primitives::threshold;
2021
#[cfg(doc)]
2122
use crate::Descriptor;
2223
use crate::{
@@ -538,10 +539,8 @@ pub fn parse<Ctx: ScriptContext>(
538539
},
539540
// CHECKMULTISIG based multisig
540541
Tk::CheckMultiSig, Tk::Num(n) => {
541-
// Check size before allocating keys
542-
if n as usize > MAX_PUBKEYS_PER_MULTISIG {
543-
return Err(Error::CmsTooManyKeys(n));
544-
}
542+
threshold::validate_k_n::<MAX_PUBKEYS_PER_MULTISIG>(1, n as usize).map_err(Error::Threshold)?;
543+
545544
let mut keys = Vec::with_capacity(n as usize);
546545
for _ in 0..n {
547546
match_token!(
@@ -562,11 +561,9 @@ pub fn parse<Ctx: ScriptContext>(
562561
},
563562
// MultiA
564563
Tk::NumEqual, Tk::Num(k) => {
565-
// Check size before allocating keys
566-
if k as usize > MAX_PUBKEYS_IN_CHECKSIGADD {
567-
return Err(Error::MultiATooManyKeys(MAX_PUBKEYS_IN_CHECKSIGADD as u64))
568-
}
569-
let mut keys = Vec::with_capacity(k as usize); // atleast k capacity
564+
threshold::validate_k_n::<MAX_PUBKEYS_IN_CHECKSIGADD>(k as usize, k as usize).map_err(Error::Threshold)?;
565+
566+
let mut keys = Vec::with_capacity(k as usize); // at least k capacity
570567
while tokens.peek() == Some(&Tk::CheckSigAdd) {
571568
match_token!(
572569
tokens,

src/primitives/threshold.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ impl std::error::Error for ThresholdError {
4040
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
4141
}
4242

43+
/// Check whether `k` and `n` are valid for an instance of [`Self`].
44+
pub fn validate_k_n<const MAX: usize>(k: usize, n: usize) -> Result<(), ThresholdError> {
45+
if k == 0 || k > n || (MAX > 0 && n > MAX) {
46+
Err(ThresholdError { k, n, max: (MAX > 0).then_some(MAX) })
47+
} else {
48+
Ok(())
49+
}
50+
}
51+
4352
/// Structure representing a k-of-n threshold collection of some arbitrary
4453
/// object `T`.
4554
///
@@ -54,11 +63,8 @@ pub struct Threshold<T, const MAX: usize> {
5463
impl<T, const MAX: usize> Threshold<T, MAX> {
5564
/// Constructs a threshold directly from a threshold value and collection.
5665
pub fn new(k: usize, inner: Vec<T>) -> Result<Self, ThresholdError> {
57-
if k == 0 || k > inner.len() || (MAX > 0 && inner.len() > MAX) {
58-
Err(ThresholdError { k, n: inner.len(), max: (MAX > 0).then_some(MAX) })
59-
} else {
60-
Ok(Threshold { k, inner })
61-
}
66+
validate_k_n::<MAX>(k, inner.len())?;
67+
Ok(Threshold { k, inner })
6268
}
6369

6470
/// Constructs a threshold from a threshold value and an iterator that yields collection

0 commit comments

Comments
 (0)