Skip to content

df: check mempool/block for funding output on broadcast fail #5367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 71 additions & 35 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,50 +1413,19 @@ static void handle_local_private_channel(struct subd *dualopend,
struct channel_send {
const struct wally_tx *wtx;
struct channel *channel;
const char *err_msg;
};

static void sendfunding_done(struct bitcoind *bitcoind UNUSED,
bool success, const char *msg,
struct channel_send *cs)
static void handle_tx_broadcast(struct channel_send *cs)
{
struct lightningd *ld = cs->channel->peer->ld;
struct channel *channel = cs->channel;
const struct wally_tx *wtx = cs->wtx;
struct channel *channel = cs->channel;
struct command *cmd = channel->openchannel_signed_cmd;
struct json_stream *response;
struct bitcoin_txid txid;
struct amount_sat unused;
int num_utxos;
struct command *cmd = channel->openchannel_signed_cmd;
channel->openchannel_signed_cmd = NULL;

if (!cmd && channel->opener == LOCAL)
log_unusual(channel->log,
"No outstanding command for channel %s,"
" funding sent was success? %d",
type_to_string(tmpctx, struct channel_id,
&channel->cid),
success);

if (!success) {
if (cmd)
was_pending(command_fail(cmd,
FUNDING_BROADCAST_FAIL,
"Error broadcasting funding "
"tx: %s. Unsent tx discarded "
"%s.",
msg,
type_to_string(tmpctx,
struct wally_tx,
wtx)));
log_unusual(channel->log,
"Error broadcasting funding "
"tx: %s. Unsent tx discarded "
"%s.",
msg,
type_to_string(tmpctx, struct wally_tx, wtx));
tal_free(cs);
return;
}

/* This might have spent UTXOs from our wallet */
num_utxos = wallet_extract_owned_outputs(ld->wallet,
Expand All @@ -1472,11 +1441,78 @@ static void sendfunding_done(struct bitcoind *bitcoind UNUSED,
json_add_txid(response, "txid", &txid);
json_add_channel_id(response, "channel_id", &channel->cid);
was_pending(command_success(cmd, response));

cs->channel->openchannel_signed_cmd = NULL;
}
}

static void check_utxo_block(struct bitcoind *bitcoind UNUSED,
const struct bitcoin_tx_output *txout,
void *arg)
{
struct channel_send *cs = arg;
struct command *cmd = cs->channel->openchannel_signed_cmd;
const struct wally_tx *wtx = cs->wtx;

/* note: if this tx has been included in a block *and spent*
* then this will also fail... */
if (!txout) {
if (cmd) {
was_pending(command_fail(cmd,
FUNDING_BROADCAST_FAIL,
"Error broadcasting funding "
"tx: %s. Unsent tx discarded "
"%s.",
cs->err_msg,
type_to_string(tmpctx,
struct wally_tx,
wtx)));
cs->channel->openchannel_signed_cmd = NULL;
}

log_unusual(cs->channel->log,
"Error broadcasting funding "
"tx: %s. Unsent tx discarded "
"%s.",
cs->err_msg,
type_to_string(tmpctx, struct wally_tx, wtx));
} else
handle_tx_broadcast(cs);

tal_free(cs);
}

static void sendfunding_done(struct bitcoind *bitcoind UNUSED,
bool success, const char *msg,
struct channel_send *cs)
{
struct lightningd *ld = cs->channel->peer->ld;
struct channel *channel = cs->channel;
struct command *cmd = channel->openchannel_signed_cmd;

if (!cmd && channel->opener == LOCAL)
log_unusual(channel->log,
"No outstanding command for channel %s,"
" funding sent was success? %d",
type_to_string(tmpctx, struct channel_id,
&channel->cid),
success);

if (success) {
handle_tx_broadcast(cs);
tal_free(cs);
} else {
/* If the tx was mined into a block, it's possible
* that the broadcast would fail. Verify that's not
* the case here. */
cs->err_msg = tal_strdup(cs, msg);
bitcoind_getutxout(ld->topology->bitcoind,
&channel->funding,
check_utxo_block,
cs);
}
}


static void send_funding_tx(struct channel *channel,
const struct wally_tx *wtx TAKES)
Expand Down