bitcoin: helpers to clone a bitcoin_tx, and format one.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-03-23 16:13:52 +10:30
parent aef5b1b844
commit 528f44c2d3
5 changed files with 40 additions and 7 deletions

View file

@ -38,6 +38,9 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u
/* Generated stub for amount_tx_fee */ /* Generated stub for amount_tx_fee */
struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); } { 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 */ /* Generated stub for fromwire */
const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED)
{ fprintf(stderr, "fromwire called!\n"); abort(); } { fprintf(stderr, "fromwire called!\n"); abort(); }

View file

@ -3,6 +3,7 @@
#include <bitcoin/psbt.h> #include <bitcoin/psbt.h>
#include <bitcoin/script.h> #include <bitcoin/script.h>
#include <bitcoin/tx.h> #include <bitcoin/tx.h>
#include <ccan/cast/cast.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
@ -590,6 +591,30 @@ struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psb
return tx; 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, static struct wally_tx *pull_wtx(const tal_t *ctx,
const u8 **cursor, const u8 **cursor,
size_t *max) 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); 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); u8 *lin = linearize_tx(ctx, tx);
char *s = tal_hex(ctx, lin); char *s = tal_hex(ctx, lin);

View file

@ -69,6 +69,10 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
varint_t input_count, varint_t output_count, varint_t input_count, varint_t output_count,
u32 nlocktime); 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. */ /* This takes a raw bitcoin tx in hex. */
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex, struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
size_t hexlen); 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 towire_bitcoin_outpoint(u8 **pptr, const struct bitcoin_outpoint *outp);
void fromwire_bitcoin_outpoint(const u8 **cursor, size_t *max, void fromwire_bitcoin_outpoint(const u8 **cursor, size_t *max,
struct bitcoin_outpoint *outp); struct bitcoin_outpoint *outp);
char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx);
/* Various weights of transaction parts. */ /* Various weights of transaction parts. */
size_t bitcoin_tx_core_weight(size_t num_inputs, size_t num_outputs); size_t bitcoin_tx_core_weight(size_t num_inputs, size_t num_outputs);

View file

@ -54,9 +54,9 @@ char *bolt11_encode_(const tal_t *ctx UNNEEDED,
void broadcast_tx(struct chain_topology *topo UNNEEDED, void broadcast_tx(struct chain_topology *topo UNNEEDED,
struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED,
const char *cmd_id UNNEEDED, bool allowhighfees UNNEEDED, const char *cmd_id UNNEEDED, bool allowhighfees UNNEEDED,
void (*failed)(struct channel * UNNEEDED, void (*finished)(struct channel * UNNEEDED,
bool success UNNEEDED, bool success UNNEEDED,
const char *err)) const char *err))
{ fprintf(stderr, "broadcast_tx called!\n"); abort(); } { fprintf(stderr, "broadcast_tx called!\n"); abort(); }
/* Generated stub for channel_change_state_reason_str */ /* Generated stub for channel_change_state_reason_str */
const char *channel_change_state_reason_str(enum state_change reason UNNEEDED) const char *channel_change_state_reason_str(enum state_change reason UNNEEDED)

View file

@ -72,9 +72,9 @@ bool blinding_next_pubkey(const struct pubkey *pk UNNEEDED,
void broadcast_tx(struct chain_topology *topo UNNEEDED, void broadcast_tx(struct chain_topology *topo UNNEEDED,
struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED,
const char *cmd_id UNNEEDED, bool allowhighfees UNNEEDED, const char *cmd_id UNNEEDED, bool allowhighfees UNNEEDED,
void (*failed)(struct channel * UNNEEDED, void (*finished)(struct channel * UNNEEDED,
bool success UNNEEDED, bool success UNNEEDED,
const char *err)) const char *err))
{ fprintf(stderr, "broadcast_tx called!\n"); abort(); } { fprintf(stderr, "broadcast_tx called!\n"); abort(); }
/* Generated stub for channel_tell_depth */ /* Generated stub for channel_tell_depth */
bool channel_tell_depth(struct lightningd *ld UNNEEDED, bool channel_tell_depth(struct lightningd *ld UNNEEDED,