Skip to content

Commit

Permalink
miniscript: make display prefer 'u' over 'l' in the fragment l:0
Browse files Browse the repository at this point in the history
When fuzzing my new non-recursive tree parsing logic, I noticed that we
were deviating from the released 12.0 in the way that we display l:0.
This is an ambiguous fragment which can be displayed either as l:0 or
u:0. In our released code we use u:0 so stick with that.

This was unintentially changed as part of #722. Change it back.

While we are at it add a regression test for #735
  • Loading branch information
apoelstra committed Sep 1, 2024
1 parent 67fdc50 commit 1259375
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,21 @@ mod tests {
);
}

#[test]
fn display_prefers_u() {
// The fragments u:0 and l:0 are identical in terms of Script and
// in terms of the in-memory representation -- OrI(False, False).
// Test that the way we display the ambiguous fragment doesn't
// change, in case somebody somehow is depending on it.
let desc = StdDescriptor::from_str("sh(u:0)").unwrap();
assert_eq!("sh(u:0)#ncq3yf9h", desc.to_string());

// This is a regression test for https://github.com/rust-bitcoin/rust-miniscript/pull/735
// which was found at the same time. It's just a bug plain and simple.
let desc = StdDescriptor::from_str("sh(and_n(u:0,1))").unwrap();
assert_eq!("sh(and_n(u:0,1))#5j5tw8nm", desc.to_string());
}

#[test]
fn desc_rtt_tests() {
roundtrip_descriptor("c:pk_k()");
Expand Down
4 changes: 2 additions & 2 deletions src/miniscript/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
Terminal::ZeroNotEqual(..) => "n",
Terminal::AndV(_, ref r) if matches!(r.as_inner(), Terminal::True) => "t",
Terminal::AndV(..) => "and_v",
Terminal::AndB(..) => "and_b",
Terminal::AndOr(_, _, ref c) if matches!(c.as_inner(), Terminal::False) => "and_n",
Terminal::AndB(..) => "and_b",
Terminal::AndOr(..) => "andor",
Terminal::OrB(..) => "or_b",
Terminal::OrD(..) => "or_d",
Terminal::OrC(..) => "or_c",
Terminal::OrI(ref l, _) if matches!(l.as_inner(), Terminal::False) => "l",
Terminal::OrI(_, ref r) if matches!(r.as_inner(), Terminal::False) => "u",
Terminal::OrI(ref l, _) if matches!(l.as_inner(), Terminal::False) => "l",
Terminal::OrI(..) => "or_i",
Terminal::Thresh(..) => "thresh",
Terminal::Multi(..) => "multi",
Expand Down

0 comments on commit 1259375

Please sign in to comment.