Skip to content

Commit a57fab3

Browse files
committed
miniscript: replace many calls to reduce0 in decode with infallible constructors
We have infallible constructors for all the terminals except multi/multia. We should use them and eliminate a bunch of error paths.
1 parent 5b736bd commit a57fab3

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/miniscript/decode.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,12 @@ macro_rules! match_token {
320320
struct TerminalStack<Pk: MiniscriptKey, Ctx: ScriptContext>(Vec<Miniscript<Pk, Ctx>>);
321321

322322
impl<Pk: MiniscriptKey, Ctx: ScriptContext> TerminalStack<Pk, Ctx> {
323-
///Wrapper around self.0.pop()
323+
/// Wrapper around self.0.pop()
324324
fn pop(&mut self) -> Option<Miniscript<Pk, Ctx>> { self.0.pop() }
325325

326+
/// Wrapper around self.0.push()
327+
fn push(&mut self, ms: Miniscript<Pk, Ctx>) { self.0.push(ms) }
328+
326329
///reduce, type check and push a 0-arg node
327330
fn reduce0(&mut self, ms: Terminal<Pk, Ctx>) -> Result<(), Error> {
328331
let ms = Miniscript::from_ast(ms)?;
@@ -375,12 +378,12 @@ pub fn parse<Ctx: ScriptContext>(
375378
Tk::Bytes33(pk) => {
376379
let ret = Ctx::Key::from_slice(&pk)
377380
.map_err(|e| Error::PubKeyCtxError(e, Ctx::name_str()))?;
378-
term.reduce0(Terminal::PkK(ret))?
381+
term.push(Miniscript::pk_k(ret));
379382
},
380383
Tk::Bytes65(pk) => {
381384
let ret = Ctx::Key::from_slice(&pk)
382385
.map_err(|e| Error::PubKeyCtxError(e, Ctx::name_str()))?;
383-
term.reduce0(Terminal::PkK(ret))?
386+
term.push(Miniscript::pk_k(ret));
384387
},
385388
// Note this does not collide with hash32 because they always followed by equal
386389
// and would be parsed in different branch. If we get a naked Bytes32, it must be
@@ -396,7 +399,7 @@ pub fn parse<Ctx: ScriptContext>(
396399
// Finally for the first case, K being parsed as a solo expression is a Pk type
397400
Tk::Bytes32(pk) => {
398401
let ret = Ctx::Key::from_slice(&pk).map_err(|e| Error::PubKeyCtxError(e, Ctx::name_str()))?;
399-
term.reduce0(Terminal::PkK(ret))?
402+
term.push(Miniscript::pk_k(ret));
400403
},
401404
// checksig
402405
Tk::CheckSig => {
@@ -413,38 +416,38 @@ pub fn parse<Ctx: ScriptContext>(
413416
Tk::Hash160 => match_token!(
414417
tokens,
415418
Tk::Dup => {
416-
term.reduce0(Terminal::RawPkH(
419+
term.push(Miniscript::expr_raw_pkh(
417420
hash160::Hash::from_byte_array(hash)
418-
))?
421+
));
419422
},
420423
Tk::Verify, Tk::Equal, Tk::Num(32), Tk::Size => {
421424
non_term.push(NonTerm::Verify);
422-
term.reduce0(Terminal::Hash160(
425+
term.push(Miniscript::hash160(
423426
hash160::Hash::from_byte_array(hash)
424-
))?
427+
));
425428
},
426429
),
427430
Tk::Ripemd160, Tk::Verify, Tk::Equal, Tk::Num(32), Tk::Size => {
428431
non_term.push(NonTerm::Verify);
429-
term.reduce0(Terminal::Ripemd160(
432+
term.push(Miniscript::ripemd160(
430433
ripemd160::Hash::from_byte_array(hash)
431-
))?
434+
));
432435
},
433436
),
434437
// Tk::Hash20(hash),
435438
Tk::Bytes32(hash) => match_token!(
436439
tokens,
437440
Tk::Sha256, Tk::Verify, Tk::Equal, Tk::Num(32), Tk::Size => {
438441
non_term.push(NonTerm::Verify);
439-
term.reduce0(Terminal::Sha256(
442+
term.push(Miniscript::sha256(
440443
sha256::Hash::from_byte_array(hash)
441-
))?
444+
));
442445
},
443446
Tk::Hash256, Tk::Verify, Tk::Equal, Tk::Num(32), Tk::Size => {
444447
non_term.push(NonTerm::Verify);
445-
term.reduce0(Terminal::Hash256(
448+
term.push(Miniscript::hash256(
446449
hash256::Hash::from_byte_array(hash)
447-
))?
450+
));
448451
},
449452
),
450453
Tk::Num(k) => {
@@ -467,9 +470,9 @@ pub fn parse<Ctx: ScriptContext>(
467470
},
468471
// timelocks
469472
Tk::CheckSequenceVerify, Tk::Num(n)
470-
=> term.reduce0(Terminal::Older(RelLockTime::from_consensus(n).map_err(Error::RelativeLockTime)?))?,
473+
=> term.push(Miniscript::older(RelLockTime::from_consensus(n).map_err(Error::RelativeLockTime)?)),
471474
Tk::CheckLockTimeVerify, Tk::Num(n)
472-
=> term.reduce0(Terminal::After(AbsLockTime::from_consensus(n).map_err(Error::AbsoluteLockTime)?))?,
475+
=> term.push(Miniscript::after(AbsLockTime::from_consensus(n).map_err(Error::AbsoluteLockTime)?)),
473476
// hashlocks
474477
Tk::Equal => match_token!(
475478
tokens,
@@ -479,33 +482,33 @@ pub fn parse<Ctx: ScriptContext>(
479482
Tk::Verify,
480483
Tk::Equal,
481484
Tk::Num(32),
482-
Tk::Size => term.reduce0(Terminal::Sha256(
485+
Tk::Size => term.push(Miniscript::sha256(
483486
sha256::Hash::from_byte_array(hash)
484-
))?,
487+
)),
485488
Tk::Hash256,
486489
Tk::Verify,
487490
Tk::Equal,
488491
Tk::Num(32),
489-
Tk::Size => term.reduce0(Terminal::Hash256(
492+
Tk::Size => term.push(Miniscript::hash256(
490493
hash256::Hash::from_byte_array(hash)
491-
))?,
494+
)),
492495
),
493496
Tk::Hash20(hash) => match_token!(
494497
tokens,
495498
Tk::Ripemd160,
496499
Tk::Verify,
497500
Tk::Equal,
498501
Tk::Num(32),
499-
Tk::Size => term.reduce0(Terminal::Ripemd160(
502+
Tk::Size => term.push(Miniscript::ripemd160(
500503
ripemd160::Hash::from_byte_array(hash)
501-
))?,
504+
)),
502505
Tk::Hash160,
503506
Tk::Verify,
504507
Tk::Equal,
505508
Tk::Num(32),
506-
Tk::Size => term.reduce0(Terminal::Hash160(
509+
Tk::Size => term.push(Miniscript::hash160(
507510
hash160::Hash::from_byte_array(hash)
508-
))?,
511+
)),
509512
),
510513
// thresholds
511514
Tk::Num(k) => {
@@ -519,8 +522,8 @@ pub fn parse<Ctx: ScriptContext>(
519522
},
520523
),
521524
// most other fragments
522-
Tk::Num(0) => term.reduce0(Terminal::False)?,
523-
Tk::Num(1) => term.reduce0(Terminal::True)?,
525+
Tk::Num(0) => term.push(Miniscript::FALSE),
526+
Tk::Num(1) => term.push(Miniscript::TRUE),
524527
Tk::EndIf => {
525528
non_term.push(NonTerm::EndIf);
526529
non_term.push(NonTerm::MaybeAndV);
@@ -557,7 +560,7 @@ pub fn parse<Ctx: ScriptContext>(
557560
);
558561
keys.reverse();
559562
let thresh = Threshold::new(k as usize, keys).map_err(Error::Threshold)?;
560-
term.reduce0(Terminal::Multi(thresh))?;
563+
term.push(Miniscript::multi(thresh));
561564
},
562565
// MultiA
563566
Tk::NumEqual, Tk::Num(k) => {
@@ -579,7 +582,7 @@ pub fn parse<Ctx: ScriptContext>(
579582
);
580583
keys.reverse();
581584
let thresh = Threshold::new(k as usize, keys).map_err(Error::Threshold)?;
582-
term.reduce0(Terminal::MultiA(thresh))?;
585+
term.push(Miniscript::multi_a(thresh));
583586
},
584587
);
585588
}

0 commit comments

Comments
 (0)