From b8b910e4c42a928f2dd88c439bee2225ad25f60b Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 7 Jan 2021 13:03:06 -0600 Subject: [PATCH] df-rbf: update channel data on depth reached When the funding tx reaches depth, update the channel's data to the "correct" funding transaction info from inflights (if necessary). This will be necessary if: - the transaction has been successfully RBF'd and - the lesser fee transaction is the one successfully mined, OR - the channel is in the process of being RBF'd --- lightningd/channel.c | 12 ++++++++++++ lightningd/channel.h | 4 ++++ lightningd/channel_control.c | 3 ++- lightningd/dual_open_control.c | 31 +++++++++++++++++++++++++++++++ lightningd/dual_open_control.h | 1 + 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index 71c413628..399c4a714 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -372,6 +372,18 @@ struct channel *peer_active_channel(struct peer *peer) return NULL; } +struct channel_inflight *channel_inflight_find(struct channel *channel, + const struct bitcoin_txid *txid) +{ + struct channel_inflight *inflight; + list_for_each(&channel->inflights, inflight, list) { + if (bitcoin_txid_eq(txid, &inflight->funding->txid)) + return inflight; + } + + return NULL; +} + struct channel *peer_normal_channel(struct peer *peer) { struct channel *channel; diff --git a/lightningd/channel.h b/lightningd/channel.h index 12acf0527..1f6934134 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -262,6 +262,10 @@ new_inflight(struct channel *channel, struct bitcoin_tx *last_tx STEALS, const struct bitcoin_signature last_sig); +/* Given a txid, find an inflight channel stub. Returns NULL if none found */ +struct channel_inflight *channel_inflight_find(struct channel *channel, + const struct bitcoin_txid *txid); + void delete_channel(struct channel *channel STEALS); const char *channel_state_name(const struct channel *channel); diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index edb7c2aef..99661de1d 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -639,7 +639,8 @@ bool channel_tell_depth(struct lightningd *ld, } #if EXPERIMENTAL_FEATURES - dualopen_tell_depth(channel->owner, channel, depth); + dualopen_tell_depth(channel->owner, channel, + txid, depth); return true; #endif /* EXPERIMENTAL_FEATURES */ } else if (channel->state != CHANNELD_AWAITING_LOCKIN diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index 9414215ab..e54a408d4 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -1392,6 +1392,7 @@ cleanup: void dualopen_tell_depth(struct subd *dualopend, struct channel *channel, + const struct bitcoin_txid *txid, u32 depth) { const u8 *msg; @@ -1405,6 +1406,36 @@ void dualopen_tell_depth(struct subd *dualopend, /* Are we there yet? */ if (to_go == 0) { assert(channel->scid); + + /* 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->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->psbt = clone_psbt(channel, inf->funding_psbt); + channel->last_tx = tal_steal(channel, inf->last_tx); + channel->last_sig = inf->last_sig; + + wallet_channel_save(dualopend->ld->wallet, channel); + /* FIXME: delete inflights */ + } msg = towire_dualopend_depth_reached(NULL, depth); subd_send_msg(dualopend, take(msg)); } else diff --git a/lightningd/dual_open_control.h b/lightningd/dual_open_control.h index a1335381d..a069b8505 100644 --- a/lightningd/dual_open_control.h +++ b/lightningd/dual_open_control.h @@ -17,5 +17,6 @@ void peer_restart_dualopend(struct peer *peer, void dualopen_tell_depth(struct subd *dualopend, struct channel *channel, + const struct bitcoin_txid *txid, u32 depth); #endif /* LIGHTNING_LIGHTNINGD_DUAL_OPEN_CONTROL_H */