Skip to content

Commit

Permalink
miniscript: use new TRUE/FALSE constants throughout the codebase
Browse files Browse the repository at this point in the history
This eliminates a bunch of error paths, which is nice.
  • Loading branch information
apoelstra committed Mar 4, 2024
1 parent be41d8b commit f22dc3a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 49 deletions.
8 changes: 2 additions & 6 deletions src/interpreter/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ fn script_from_stack_elem<Ctx: ScriptContext>(
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),
}
}

Expand Down
23 changes: 4 additions & 19 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<Pk: crate::FromStrKey, Ctx: ScriptContext> 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])?,
Expand Down Expand Up @@ -400,24 +400,9 @@ impl<Pk: crate::FromStrKey, Ctx: ScriptContext> 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)),
}
}
Expand Down
27 changes: 3 additions & 24 deletions src/policy/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,27 +704,13 @@ fn all_casts<Pk: MiniscriptKey, Ctx: ScriptContext>() -> [Cast<Pk, Ctx>; 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,
},
Expand All @@ -742,14 +728,7 @@ fn all_casts<Pk: MiniscriptKey, Ctx: ScriptContext>() -> [Cast<Pk, Ctx>; 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,
},
Expand Down

0 comments on commit f22dc3a

Please sign in to comment.