rbf: update the channel's funding_txid to match what's mined

If the peer is offline when we see the funding txid, we don't actually
update the channel's info. Here, we move it up to where the scid is set,
so that we always update the channel's funding_txid to the correct
(mined) information.
This commit is contained in:
niftynei 2021-05-20 16:50:42 -05:00 committed by Rusty Russell
parent e45b09358a
commit 062bc12813
9 changed files with 98 additions and 44 deletions

View File

@ -159,9 +159,10 @@ new_inflight(struct channel *channel,
struct amount_sat total_funds,
struct amount_sat our_funds,
struct wally_psbt *psbt STEALS,
struct bitcoin_tx *last_tx STEALS,
struct bitcoin_tx *last_tx,
const struct bitcoin_signature last_sig)
{
struct wally_psbt *last_tx_psbt_clone;
struct channel_inflight *inflight
= tal(channel, struct channel_inflight);
struct funding_info *funding
@ -177,7 +178,10 @@ new_inflight(struct channel *channel,
inflight->channel = channel;
inflight->remote_tx_sigs = false;
inflight->funding_psbt = tal_steal(inflight, psbt);
inflight->last_tx = tal_steal(inflight, last_tx);
/* Make a 'clone' of this tx */
last_tx_psbt_clone = clone_psbt(inflight, last_tx->psbt);
inflight->last_tx = bitcoin_tx_with_psbt(inflight, last_tx_psbt_clone);
inflight->last_sig = last_sig;
inflight->tx_broadcast = false;

View File

@ -1033,9 +1033,9 @@ static struct amount_sat calculate_reserve(struct channel_config *their_config,
return reserve;
}
static void channel_update_reserve(struct channel *channel,
struct channel_config *their_config,
struct amount_sat funding_total)
void channel_update_reserve(struct channel *channel,
struct channel_config *their_config,
struct amount_sat funding_total)
{
struct amount_sat reserve;
@ -1081,8 +1081,11 @@ wallet_update_channel(struct lightningd *ld,
channel->our_msat = our_msat;
channel->msat_to_us_min = our_msat;
channel->msat_to_us_max = our_msat;
channel->last_tx = tal_steal(channel, remote_commit);
channel->last_sig = *remote_commit_sig;
channel_set_last_tx(channel,
tal_steal(channel, remote_commit),
remote_commit_sig,
TX_CHANNEL_UNILATERAL);
/* Update in database */
wallet_channel_save(ld->wallet, channel);
@ -1151,8 +1154,11 @@ wallet_commit_channel(struct lightningd *ld,
channel->our_msat = our_msat;
channel->msat_to_us_min = our_msat;
channel->msat_to_us_max = our_msat;
channel->last_tx = tal_steal(channel, remote_commit);
channel->last_sig = *remote_commit_sig;
channel->last_tx_type = TX_CHANNEL_UNILATERAL;
channel->channel_info = *channel_info;
channel->fee_states = new_fee_states(channel,
channel->opener,
@ -1559,40 +1565,14 @@ void dualopen_tell_depth(struct subd *dualopend,
/* Are we there yet? */
if (to_go == 0) {
assert(channel->scid);
assert(bitcoin_txid_eq(&channel->funding_txid, txid));
/* Update the channel's info to the correct tx, if we need to */
if (!bitcoin_txid_eq(&channel->funding_txid, txid)) {
struct channel_inflight *inf;
inf = channel_inflight_find(channel, txid);
if (!inf) {
channel_internal_error(channel,
"Txid %s for channel"
" not found in available inflights."
" (peer %s)",
type_to_string(tmpctx,
struct bitcoin_txid,
txid),
type_to_string(tmpctx,
struct node_id,
&channel->peer->id));
return;
}
channel_set_billboard(channel, false,
tal_fmt(tmpctx, "Funding depth reached"
" %d confirmations, alerting peer"
" we're locked-in.",
to_go));
channel->funding_txid = inf->funding->txid;
channel->funding_outnum = inf->funding->outnum;
channel->funding = inf->funding->total_funds;
channel->our_funds = inf->funding->our_funds;
channel->last_tx = tal_steal(channel, inf->last_tx);
channel->last_sig = inf->last_sig;
/* Update the reserve */
channel_update_reserve(channel,
&channel->channel_info.their_config,
inf->funding->total_funds);
wallet_channel_save(dualopend->ld->wallet, channel);
/* FIXME: delete inflights */
}
msg = towire_dualopend_depth_reached(NULL, depth);
subd_send_msg(dualopend, take(msg));
} else

View File

@ -25,4 +25,8 @@ void channel_unsaved_close_conn(struct channel *channel, const char *why);
void json_add_unsaved_channel(struct json_stream *response,
const struct channel *channel);
void channel_update_reserve(struct channel *channel,
struct channel_config *their_config,
struct amount_sat funding_total);
#endif /* LIGHTNING_LIGHTNINGD_DUAL_OPEN_CONTROL_H */

View File

@ -1312,6 +1312,32 @@ static bool check_funding_tx(const struct bitcoin_tx *tx,
return false;
}
static void update_channel_from_inflight(struct lightningd *ld,
struct channel *channel,
struct channel_inflight *inflight)
{
struct wally_psbt *psbt_copy;
channel->funding_txid = inflight->funding->txid;
channel->funding_outnum = inflight->funding->outnum;
channel->funding = inflight->funding->total_funds;
channel->our_funds = inflight->funding->our_funds;
/* Make a 'clone' of this tx */
psbt_copy = clone_psbt(channel, inflight->last_tx->psbt);
channel_set_last_tx(channel,
bitcoin_tx_with_psbt(channel, psbt_copy),
&inflight->last_sig,
TX_CHANNEL_UNILATERAL);
/* Update the reserve */
channel_update_reserve(channel,
&channel->channel_info.their_config,
inflight->funding->total_funds);
wallet_channel_save(ld->wallet, channel);
}
static enum watch_result funding_depth_cb(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
@ -1342,6 +1368,26 @@ static enum watch_result funding_depth_cb(struct lightningd *ld,
* means the stale block with our funding tx was removed) */
if ((min_depth_reached && !channel->scid) || (depth && channel->scid)) {
struct txlocator *loc;
struct channel_inflight *inf;
/* Update the channel's info to the correct tx, if needed to
* It's possible an 'inflight' has reached depth */
if (!list_empty(&channel->inflights)) {
inf = channel_inflight_find(channel, txid);
if (!inf) {
channel_fail_permanent(channel, REASON_LOCAL,
"Txid %s for channel"
" not found in inflights. (peer %s)",
type_to_string(tmpctx,
struct bitcoin_txid,
txid),
type_to_string(tmpctx,
struct node_id,
&channel->peer->id));
return DELETE_WATCH;
}
update_channel_from_inflight(ld, channel, inf);
}
wallet_annotate_txout(ld->wallet, txid, channel->funding_outnum,
TX_CHANNEL_FUNDING, channel->dbid);

View File

@ -82,6 +82,10 @@ struct htlc_in *channel_has_htlc_in(struct channel *channel UNNEEDED)
/* Generated stub for channel_has_htlc_out */
struct htlc_out *channel_has_htlc_out(struct channel *channel UNNEEDED)
{ fprintf(stderr, "channel_has_htlc_out called!\n"); abort(); }
/* Generated stub for channel_inflight_find */
struct channel_inflight *channel_inflight_find(struct channel *channel UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED)
{ fprintf(stderr, "channel_inflight_find called!\n"); abort(); }
/* Generated stub for channel_internal_error */
void channel_internal_error(struct channel *channel UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "channel_internal_error called!\n"); abort(); }
@ -92,6 +96,12 @@ u32 channel_last_funding_feerate(const struct channel *channel UNNEEDED)
void channel_set_billboard(struct channel *channel UNNEEDED, bool perm UNNEEDED,
const char *str TAKES UNNEEDED)
{ fprintf(stderr, "channel_set_billboard called!\n"); abort(); }
/* Generated stub for channel_set_last_tx */
void channel_set_last_tx(struct channel *channel UNNEEDED,
struct bitcoin_tx *tx UNNEEDED,
const struct bitcoin_signature *sig UNNEEDED,
enum wallet_tx_type type UNNEEDED)
{ fprintf(stderr, "channel_set_last_tx called!\n"); abort(); }
/* Generated stub for channel_set_state */
void channel_set_state(struct channel *channel UNNEEDED,
enum channel_state old_state UNNEEDED,
@ -114,6 +124,11 @@ bool channel_tell_depth(struct lightningd *ld UNNEEDED,
/* Generated stub for channel_unsaved_close_conn */
void channel_unsaved_close_conn(struct channel *channel UNNEEDED, const char *why UNNEEDED)
{ fprintf(stderr, "channel_unsaved_close_conn called!\n"); abort(); }
/* Generated stub for channel_update_reserve */
void channel_update_reserve(struct channel *channel UNNEEDED,
struct channel_config *their_config UNNEEDED,
struct amount_sat funding_total UNNEEDED)
{ fprintf(stderr, "channel_update_reserve called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, errcode_t code UNNEEDED,
const char *fmt UNNEEDED, ...)

View File

@ -1900,4 +1900,4 @@ struct db_query db_postgres_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */
// SHA256STAMP:dbebcde72bd359ea7edaf5732c78549224c3b891e45f123696b4cfd60dd9037b
// SHA256STAMP:afab339ef2b9f164d7cf2e71a19f6c000779e6334f348ddf4720176100cf6de8

View File

@ -1900,4 +1900,4 @@ struct db_query db_sqlite3_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */
// SHA256STAMP:dbebcde72bd359ea7edaf5732c78549224c3b891e45f123696b4cfd60dd9037b
// SHA256STAMP:afab339ef2b9f164d7cf2e71a19f6c000779e6334f348ddf4720176100cf6de8

View File

@ -1246,11 +1246,11 @@ msgstr ""
msgid "not a valid SQL statement"
msgstr ""
#: wallet/test/run-wallet.c:1444
#: wallet/test/run-wallet.c:1449
msgid "SELECT COUNT(1) FROM channel_funding_inflights WHERE channel_id = ?;"
msgstr ""
#: wallet/test/run-wallet.c:1642
#: wallet/test/run-wallet.c:1647
msgid "INSERT INTO channels (id) VALUES (1);"
msgstr ""
# SHA256STAMP:383f8fff6066f7be166c121b10d6f4325eea0e02124251e0ceb2ba288de7426c
# SHA256STAMP:31b48bebfb4b2c1eef0c740313068f0c129170852a3ae75c76ebfa5a546be219

View File

@ -69,6 +69,11 @@ bool channel_tell_depth(struct lightningd *ld UNNEEDED,
/* Generated stub for channel_unsaved_close_conn */
void channel_unsaved_close_conn(struct channel *channel UNNEEDED, const char *why UNNEEDED)
{ fprintf(stderr, "channel_unsaved_close_conn called!\n"); abort(); }
/* Generated stub for channel_update_reserve */
void channel_update_reserve(struct channel *channel UNNEEDED,
struct channel_config *their_config UNNEEDED,
struct amount_sat funding_total UNNEEDED)
{ fprintf(stderr, "channel_update_reserve called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, errcode_t code UNNEEDED,
const char *fmt UNNEEDED, ...)