Skip to content

Commit

Permalink
psbt-utils: ignore the 'unknown' map for input + output comparison
Browse files Browse the repository at this point in the history
There's no stable ordering on unknown serialization, so linearizing
identical but mis-ordered unknown data will lead to 'wrong' results.
Instead, we just ignore any data that's in the psbt unknown struct.

There's probably also problems here with other PSBT maps. Really, this
needs a finer grained comparison function .... fuck
  • Loading branch information
niftynei committed Aug 7, 2020
1 parent 896b83d commit 79712de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions common/psbt_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ static const u8 *linearize_input(const tal_t *ctx,
struct wally_psbt *psbt = create_psbt(NULL, 1, 0);
size_t byte_len;


if (wally_tx_add_input(psbt->tx, tx_in) != WALLY_OK)
abort();
psbt->inputs[0] = *in;
psbt->num_inputs++;
/* Blank out unknowns. These are unordered and serializing
* them might have different outputs for identical data */
psbt->inputs[0].unknowns.num_items = 0;

const u8 *bytes = psbt_get_bytes(ctx, psbt, &byte_len);

Expand All @@ -89,8 +91,12 @@ static const u8 *linearize_output(const tal_t *ctx,

if (wally_tx_add_output(psbt->tx, tx_out) != WALLY_OK)
abort();

psbt->outputs[0] = *out;
psbt->num_outputs++;
/* Blank out unknowns. These are unordered and serializing
* them might have different outputs for identical data */
psbt->outputs[0].unknowns.num_items = 0;

const u8 *bytes = psbt_get_bytes(ctx, psbt, &byte_len);

Expand Down Expand Up @@ -127,7 +133,6 @@ static bool output_identical(const struct wally_psbt *a,
const u8 *b_out = linearize_output(tmpctx,
&b->outputs[b_index],
&b->tx->outputs[b_index]);

return memeq(a_out, tal_bytelen(a_out),
b_out, tal_bytelen(b_out));
}
Expand Down
15 changes: 15 additions & 0 deletions common/test/run-psbt_diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ int main(int argc, const char *argv[])
diff_count(start, end, 1, 1);
diff_count(end, start, 1, 1);

/* Add some extra unknown info to a PSBT */
psbt_input_add_max_witness_len(&end->inputs[1], 100);
psbt_input_add_max_witness_len(&start->inputs[1], 100);

/* Swap locations */
struct wally_map_item tmp;
tmp = end->inputs[1].unknowns.items[0];
end->inputs[1].unknowns.items[0] = end->inputs[1].unknowns.items[1];
end->inputs[1].unknowns.items[1] = tmp;

/* We expect nothing to change ? */
/* FIXME: stable ordering of unknowns ? */
diff_count(start, end, 1, 1);
diff_count(end, start, 1, 1);

/* No memory leaks please */
common_shutdown();
return 0;
Expand Down

0 comments on commit 79712de

Please sign in to comment.