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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-10-24 12:11:30 +10:30
parent 6f1bb6fa41
commit 1bdbb9261e
6 changed files with 16 additions and 10 deletions

View file

@ -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,

View file

@ -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), \

View file

@ -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);

View file

@ -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,

View file

@ -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

View file

@ -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)),