From 1bdbb9261e22720ddb922bd437e31a272d211cbc Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 24 Oct 2023 12:11:30 +1030 Subject: [PATCH] lightningd: add context arg to bitcoind_sendrawtx() If the context is freed, the callback isn't called. This doesn't matter yet, since our callbacks tend to be such that the callback itself is required to free things, but it's clearer this way and allows more flexible usage in following patches. Signed-off-by: Rusty Russell --- lightningd/bitcoind.c | 10 ++++++---- lightningd/bitcoind.h | 8 +++++--- lightningd/chaintopology.c | 4 ++-- lightningd/channel_control.c | 1 + lightningd/dual_open_control.c | 1 + wallet/walletrpc.c | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index 60b8474eb..e02ad8042 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -404,11 +404,13 @@ static void sendrawtx_callback(const char *buf, const jsmntok_t *toks, err); } + /* In case they don't free it, we will. */ + tal_steal(tmpctx, call); call->cb(call->bitcoind, success, errmsg, call->cb_arg); - tal_free(call); } -void bitcoind_sendrawtx_(struct bitcoind *bitcoind, +void bitcoind_sendrawtx_(const tal_t *ctx, + struct bitcoind *bitcoind, const char *id_prefix, const char *hextx, bool allowhighfees, @@ -417,14 +419,14 @@ void bitcoind_sendrawtx_(struct bitcoind *bitcoind, void *cb_arg) { struct jsonrpc_request *req; - struct sendrawtx_call *call = tal(bitcoind, struct sendrawtx_call); + struct sendrawtx_call *call = tal(ctx, struct sendrawtx_call); call->bitcoind = bitcoind; call->cb = cb; call->cb_arg = cb_arg; log_debug(bitcoind->log, "sendrawtransaction: %s", hextx); - req = jsonrpc_request_start(bitcoind, "sendrawtransaction", + req = jsonrpc_request_start(call, "sendrawtransaction", id_prefix, true, bitcoind->log, NULL, sendrawtx_callback, diff --git a/lightningd/bitcoind.h b/lightningd/bitcoind.h index 5a1425451..bf16eb96a 100644 --- a/lightningd/bitcoind.h +++ b/lightningd/bitcoind.h @@ -63,15 +63,17 @@ void bitcoind_estimate_fees(struct bitcoind *bitcoind, u32 feerate_floor, const struct feerate_est *feerates)); -void bitcoind_sendrawtx_(struct bitcoind *bitcoind, +/* If ctx is freed, cb won't be called! */ +void bitcoind_sendrawtx_(const tal_t *ctx, + struct bitcoind *bitcoind, const char *id_prefix TAKES, const char *hextx, bool allowhighfees, void (*cb)(struct bitcoind *bitcoind, bool success, const char *msg, void *), void *arg); -#define bitcoind_sendrawtx(bitcoind_, id_prefix, hextx, allowhighfees, cb, arg) \ - bitcoind_sendrawtx_((bitcoind_), (id_prefix), (hextx), \ +#define bitcoind_sendrawtx(ctx, bitcoind_, id_prefix, hextx, allowhighfees, cb, arg) \ + bitcoind_sendrawtx_((ctx), (bitcoind_), (id_prefix), (hextx), \ (allowhighfees), \ typesafe_cb_preargs(void, void *, \ (cb), (arg), \ diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 4fb2d8abf..5d55c2a45 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -145,7 +145,7 @@ static void broadcast_remainder(struct bitcoind *bitcoind, } /* Broadcast next one. */ - bitcoind_sendrawtx(bitcoind, + bitcoind_sendrawtx(bitcoind, bitcoind, txs->cmd_id[txs->cursor], txs->txs[txs->cursor], txs->allowhighfees[txs->cursor], broadcast_remainder, txs); @@ -299,7 +299,7 @@ void broadcast_tx_(struct chain_topology *topo, cmd_id ? " for " : "", cmd_id ? cmd_id : ""); wallet_transaction_add(topo->ld->wallet, tx->wtx, 0, 0); - bitcoind_sendrawtx(topo->bitcoind, otx->cmd_id, + bitcoind_sendrawtx(topo->bitcoind, topo->bitcoind, otx->cmd_id, fmt_bitcoin_tx(tmpctx, otx->tx), allowhighfees, broadcast_done, otx); diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index bfae7643e..2220494a3 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -484,6 +484,7 @@ static void send_splice_tx(struct channel *channel, info->err_msg = NULL; bitcoind_sendrawtx(ld->topology->bitcoind, + ld->topology->bitcoind, cc ? cc->cmd->id : NULL, tal_hex(tmpctx, tx_bytes), false, diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index 1e1697648..302930ceb 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -1742,6 +1742,7 @@ static void send_funding_tx(struct channel *channel, type_to_string(tmpctx, struct wally_tx, cs->wtx)); bitcoind_sendrawtx(ld->topology->bitcoind, + ld->topology->bitcoind, channel->open_attempt ? (channel->open_attempt->cmd ? channel->open_attempt->cmd->id diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 198bfb559..57c0ff1fe 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -1000,7 +1000,7 @@ static struct command_result *json_sendpsbt(struct command *cmd, } /* Now broadcast the transaction */ - bitcoind_sendrawtx(cmd->ld->topology->bitcoind, + bitcoind_sendrawtx(sending, cmd->ld->topology->bitcoind, cmd->id, tal_hex(tmpctx, linearize_wtx(tmpctx, sending->wtx)),