From f22dc3aa000438bdb2c38f4b7acf70d0903f5960 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Sun, 3 Mar 2024 02:28:03 +0000 Subject: [PATCH] miniscript: use new TRUE/FALSE constants throughout the codebase This eliminates a bunch of error paths, which is nice. --- src/interpreter/inner.rs | 8 ++------ src/miniscript/astelem.rs | 23 ++++------------------- src/policy/compiler.rs | 27 +++------------------------ 3 files changed, 9 insertions(+), 49 deletions(-) diff --git a/src/interpreter/inner.rs b/src/interpreter/inner.rs index cb9fbfc35..9fc3d11b5 100644 --- a/src/interpreter/inner.rs +++ b/src/interpreter/inner.rs @@ -46,12 +46,8 @@ fn script_from_stack_elem( Miniscript::parse_with_ext(bitcoin::Script::from_bytes(sl), &ExtParams::allow_all()) .map_err(Error::from) } - stack::Element::Satisfied => { - Miniscript::from_ast(crate::Terminal::True).map_err(Error::from) - } - stack::Element::Dissatisfied => { - Miniscript::from_ast(crate::Terminal::False).map_err(Error::from) - } + stack::Element::Satisfied => Ok(Miniscript::TRUE), + stack::Element::Dissatisfied => Ok(Miniscript::FALSE), } } diff --git a/src/miniscript/astelem.rs b/src/miniscript/astelem.rs index e3dd0edf0..2b7417a34 100644 --- a/src/miniscript/astelem.rs +++ b/src/miniscript/astelem.rs @@ -331,7 +331,7 @@ impl crate::expression::FromTree for ("and_n", 2) => Ok(Terminal::AndOr( expression::FromTree::from_tree(&top.args[0])?, expression::FromTree::from_tree(&top.args[1])?, - Arc::new(Miniscript::from_ast(Terminal::False)?), + Arc::new(Miniscript::FALSE), )), ("andor", 3) => Ok(Terminal::AndOr( expression::FromTree::from_tree(&top.args[0])?, @@ -400,24 +400,9 @@ impl crate::expression::FromTree for 'v' => unwrapped = Terminal::Verify(Arc::new(ms)), 'j' => unwrapped = Terminal::NonZero(Arc::new(ms)), 'n' => unwrapped = Terminal::ZeroNotEqual(Arc::new(ms)), - 't' => { - unwrapped = Terminal::AndV( - Arc::new(ms), - Arc::new(Miniscript::from_ast(Terminal::True)?), - ) - } - 'u' => { - unwrapped = Terminal::OrI( - Arc::new(ms), - Arc::new(Miniscript::from_ast(Terminal::False)?), - ) - } - 'l' => { - unwrapped = Terminal::OrI( - Arc::new(Miniscript::from_ast(Terminal::False)?), - Arc::new(ms), - ) - } + 't' => unwrapped = Terminal::AndV(Arc::new(ms), Arc::new(Miniscript::TRUE)), + 'u' => unwrapped = Terminal::OrI(Arc::new(ms), Arc::new(Miniscript::FALSE)), + 'l' => unwrapped = Terminal::OrI(Arc::new(Miniscript::FALSE), Arc::new(ms)), x => return Err(Error::UnknownWrapper(x)), } } diff --git a/src/policy/compiler.rs b/src/policy/compiler.rs index 5a0b88809..1b1ced6e7 100644 --- a/src/policy/compiler.rs +++ b/src/policy/compiler.rs @@ -704,27 +704,13 @@ fn all_casts() -> [Cast; 10] { }, Cast { ext_data: types::ExtData::cast_likely, - node: |ms| { - Terminal::OrI( - Arc::new( - Miniscript::from_ast(Terminal::False).expect("False Miniscript creation"), - ), - ms, - ) - }, + node: |ms| Terminal::OrI(Arc::new(Miniscript::FALSE), ms), ast_type: types::Type::cast_likely, comp_ext_data: CompilerExtData::cast_likely, }, Cast { ext_data: types::ExtData::cast_unlikely, - node: |ms| { - Terminal::OrI( - ms, - Arc::new( - Miniscript::from_ast(Terminal::False).expect("False Miniscript creation"), - ), - ) - }, + node: |ms| Terminal::OrI(ms, Arc::new(Miniscript::FALSE)), ast_type: types::Type::cast_unlikely, comp_ext_data: CompilerExtData::cast_unlikely, }, @@ -742,14 +728,7 @@ fn all_casts() -> [Cast; 10] { }, Cast { ext_data: types::ExtData::cast_true, - node: |ms| { - Terminal::AndV( - ms, - Arc::new( - Miniscript::from_ast(Terminal::True).expect("True Miniscript creation"), - ), - ) - }, + node: |ms| Terminal::AndV(ms, Arc::new(Miniscript::TRUE)), ast_type: types::Type::cast_true, comp_ext_data: CompilerExtData::cast_true, },