diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c index c721dbb8a92b..e813a0e107fa 100644 --- a/bitcoin/psbt.c +++ b/bitcoin/psbt.c @@ -36,7 +36,7 @@ struct wally_psbt *create_psbt(const tal_t *ctx, size_t num_inputs, size_t num_o return psbt; } -struct wally_psbt *clone_psbt(const tal_t *ctx, struct wally_psbt *psbt) +struct wally_psbt *clone_psbt(const tal_t *ctx, const struct wally_psbt *psbt) { struct wally_psbt *clone; tal_wally_start(); diff --git a/bitcoin/psbt.h b/bitcoin/psbt.h index dc22b3bbe926..d84985217c6c 100644 --- a/bitcoin/psbt.h +++ b/bitcoin/psbt.h @@ -48,7 +48,7 @@ struct wally_psbt *new_psbt(const tal_t *ctx, * @ctx - allocation context * @psbt - psbt to be cloned */ -struct wally_psbt *clone_psbt(const tal_t *ctx, struct wally_psbt *psbt); +struct wally_psbt *clone_psbt(const tal_t *ctx, const struct wally_psbt *psbt); /** * psbt_is_finalized - Check if tx is ready to be extracted diff --git a/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c b/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c index 582461423359..68d1c9409bcf 100644 --- a/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c +++ b/bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c @@ -45,7 +45,7 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) { fprintf(stderr, "amount_tx_fee called!\n"); abort(); } /* Generated stub for clone_psbt */ -struct wally_psbt *clone_psbt(const tal_t *ctx UNNEEDED, struct wally_psbt *psbt UNNEEDED) +struct wally_psbt *clone_psbt(const tal_t *ctx UNNEEDED, const struct wally_psbt *psbt UNNEEDED) { fprintf(stderr, "clone_psbt called!\n"); abort(); } /* Generated stub for fromwire */ const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) diff --git a/common/psbt_open.c b/common/psbt_open.c index fbd2e9c40578..7ba4defd0d36 100644 --- a/common/psbt_open.c +++ b/common/psbt_open.c @@ -494,20 +494,31 @@ bool psbt_output_to_external(const struct wally_psbt_output *output) return !(!result); } -bool psbt_contribs_changed(struct wally_psbt *orig, +bool psbt_contribs_changed(const struct wally_psbt *orig, struct wally_psbt *new) { - assert(orig->version == 2 && new->version == 2); - + struct wally_psbt *orig_deep_copy; struct psbt_changeset *cs; bool ok; - cs = psbt_get_changeset(NULL, orig, new); + + assert(orig->version == 2 && new->version == 2); + // The `psbt_get_changeset` function modifies the original PSBT, which + // is not needed by the . + // + // This behavior also caused issues with a different hsmd, as documented in: + // https://github.com/ElementsProject/lightning/issues/6672 + // + // FIXME(vincenzopalazzo): There is no need to clone all the psbt because + // the mutation happens only on the inputs, so we should clone only + // the subpart of the psbt, but this require to add the support to the libwally. + orig_deep_copy = clone_psbt(NULL, orig); + cs = psbt_get_changeset(NULL, orig_deep_copy, new); ok = tal_count(cs->added_ins) > 0 || tal_count(cs->rm_ins) > 0 || tal_count(cs->added_outs) > 0 || tal_count(cs->rm_outs) > 0; - + tal_free(orig_deep_copy); tal_free(cs); return ok; } diff --git a/common/psbt_open.h b/common/psbt_open.h index be3c4995b46f..0cafbe33a293 100644 --- a/common/psbt_open.h +++ b/common/psbt_open.h @@ -190,9 +190,9 @@ bool psbt_output_to_external(const struct wally_psbt_output *output); /* psbt_contribs_changed - Returns true if the psbt's inputs/outputs * have changed. * - * @orig - originating psbt + * @orig - originating psbt that will be untouch * @new - 'updated' psbt, to verify is unchanged */ -bool psbt_contribs_changed(struct wally_psbt *orig, +bool psbt_contribs_changed(const struct wally_psbt *orig, struct wally_psbt *new); #endif /* LIGHTNING_COMMON_PSBT_OPEN_H */