From 528f44c2d32bedd1c1e3170da0b4877e3fded479 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 23 Mar 2023 16:13:52 +1030 Subject: [PATCH] bitcoin: helpers to clone a bitcoin_tx, and format one. Signed-off-by: Rusty Russell --- ...-tx-bitcoin_tx_2of2_input_witness_weight.c | 3 +++ bitcoin/tx.c | 27 ++++++++++++++++++- bitcoin/tx.h | 5 ++++ lightningd/test/run-invoice-select-inchan.c | 6 ++--- wallet/test/run-wallet.c | 6 ++--- 5 files changed, 40 insertions(+), 7 deletions(-) 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 05892c964..305c400c2 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 @@ -38,6 +38,9 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u /* Generated stub for amount_tx_fee */ 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) +{ 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) { fprintf(stderr, "fromwire called!\n"); abort(); } diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 951798234..cd132acc4 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -590,6 +591,30 @@ struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psb return tx; } +struct bitcoin_tx *clone_bitcoin_tx(const tal_t *ctx, + const struct bitcoin_tx *tx) +{ + struct bitcoin_tx *newtx; + + if (taken(tx)) + return cast_const(struct bitcoin_tx *, tx); + + newtx = tal(ctx, struct bitcoin_tx); + + newtx->chainparams = tx->chainparams; + + tal_wally_start(); + if (wally_tx_clone_alloc(tx->wtx, 0, &newtx->wtx) != WALLY_OK) + newtx->wtx = NULL; + tal_wally_end_onto(newtx, newtx->wtx, struct wally_tx); + if (!newtx->wtx) + return tal_free(newtx); + + newtx->psbt = clone_psbt(newtx, tx->psbt); + tal_add_destructor(newtx, bitcoin_tx_destroy); + return newtx; +} + static struct wally_tx *pull_wtx(const tal_t *ctx, const u8 **cursor, size_t *max) @@ -699,7 +724,7 @@ bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid, return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len); } -static char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx) +char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx) { u8 *lin = linearize_tx(ctx, tx); char *s = tal_hex(ctx, lin); diff --git a/bitcoin/tx.h b/bitcoin/tx.h index cb0903ccf..476764b57 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -69,6 +69,10 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, varint_t output_count, u32 nlocktime); +/* Make a (deep) copy */ +struct bitcoin_tx *clone_bitcoin_tx(const tal_t *ctx, + const struct bitcoin_tx *tx TAKES); + /* This takes a raw bitcoin tx in hex. */ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex, size_t hexlen); @@ -285,6 +289,7 @@ void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx); void towire_bitcoin_outpoint(u8 **pptr, const struct bitcoin_outpoint *outp); void fromwire_bitcoin_outpoint(const u8 **cursor, size_t *max, struct bitcoin_outpoint *outp); +char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx); /* Various weights of transaction parts. */ size_t bitcoin_tx_core_weight(size_t num_inputs, size_t num_outputs); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index b36585718..a99daaf72 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -54,9 +54,9 @@ char *bolt11_encode_(const tal_t *ctx UNNEEDED, void broadcast_tx(struct chain_topology *topo UNNEEDED, struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const char *cmd_id UNNEEDED, bool allowhighfees UNNEEDED, - void (*failed)(struct channel * UNNEEDED, - bool success UNNEEDED, - const char *err)) + void (*finished)(struct channel * UNNEEDED, + bool success UNNEEDED, + const char *err)) { fprintf(stderr, "broadcast_tx called!\n"); abort(); } /* Generated stub for channel_change_state_reason_str */ const char *channel_change_state_reason_str(enum state_change reason UNNEEDED) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index b490c57d0..a8d5b930b 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -72,9 +72,9 @@ bool blinding_next_pubkey(const struct pubkey *pk UNNEEDED, void broadcast_tx(struct chain_topology *topo UNNEEDED, struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const char *cmd_id UNNEEDED, bool allowhighfees UNNEEDED, - void (*failed)(struct channel * UNNEEDED, - bool success UNNEEDED, - const char *err)) + void (*finished)(struct channel * UNNEEDED, + bool success UNNEEDED, + const char *err)) { fprintf(stderr, "broadcast_tx called!\n"); abort(); } /* Generated stub for channel_tell_depth */ bool channel_tell_depth(struct lightningd *ld UNNEEDED,