Skip to content

Commit

Permalink
Merge #366: Actually return the error
Browse files Browse the repository at this point in the history
4d0e8fb Add unit test SortedMultiVec constructor (Tobin C. Harding)
fb932f6 Actually return the error (Tobin C. Harding)

Pull request description:

  We have what appears to be an error return but do not actually return
  the error. This line needs an explicit `return` statement otherwise it
  is a noop.

ACKs for top commit:
  apoelstra:
    ACK 4d0e8fb

Tree-SHA512: 7a79b678fb77e092415b78857a5b0bfd2b4cf58382858729f2e6c2d57d8c6b50fabcbe498fd12fee05d9886574a545ad568310df76609fe2eb123af861c4ad14
  • Loading branch information
apoelstra committed Apr 25, 2022
2 parents 148112c + 4d0e8fb commit 6fe4bce
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/descriptor/sortedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
pub fn new(k: usize, pks: Vec<Pk>) -> Result<Self, Error> {
// A sortedmulti() is only defined for <= 20 keys (it maps to CHECKMULTISIG)
if pks.len() > MAX_PUBKEYS_PER_MULTISIG {
Error::BadDescriptor("Too many public keys".to_string());
return Err(Error::BadDescriptor("Too many public keys".to_string()));
}

// Check the limits before creating a new SortedMultiVec
Expand Down Expand Up @@ -237,3 +237,34 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for SortedMultiVec<Pk,
f.write_str(")")
}
}

#[cfg(test)]
mod tests {
use super::*;
use bitcoin::secp256k1::PublicKey;
use miniscript::context::Legacy;

#[test]
fn too_many_pubkeys() {
// Arbitrary pubic key.
let pk = PublicKey::from_str(
"02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443",
)
.unwrap();

let over = 1 + MAX_PUBKEYS_PER_MULTISIG;

let mut pks = Vec::new();
for _ in 0..over {
pks.push(pk.clone());
}

let res: Result<SortedMultiVec<PublicKey, Legacy>, Error> = SortedMultiVec::new(0, pks);
let error = res.err().expect("constructor should err");

match error {
Error::BadDescriptor(_) => {} // ok
other => panic!("unexpected error: {:?}", other),
}
}
}

0 comments on commit 6fe4bce

Please sign in to comment.