From 4c57d4425262f41e067762cc9a2fb0036ff27e76 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 28 May 2019 18:06:39 +0200 Subject: [PATCH] channel: Along with the last_tx also remember its type This takes the guesswork out of `drop_to_chain` and allows us to annotate the last_tx consistently. Signed-off-by: Christian Decker --- lightningd/channel.c | 5 ++++- lightningd/channel.h | 4 +++- lightningd/closing_control.c | 3 +-- lightningd/peer_htlcs.c | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index a0df67275..9f1cfe971 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -233,6 +233,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->msat_to_us_min = msat_to_us_min; channel->msat_to_us_max = msat_to_us_max; channel->last_tx = tal_steal(channel, last_tx); + channel->last_tx_type = TX_UNKNOWN; channel->last_sig = *last_sig; channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs); channel->channel_info = *channel_info; @@ -331,11 +332,13 @@ struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid) void channel_set_last_tx(struct channel *channel, struct bitcoin_tx *tx, - const struct bitcoin_signature *sig) + const struct bitcoin_signature *sig, + txtypes txtypes) { channel->last_sig = *sig; tal_free(channel->last_tx); channel->last_tx = tal_steal(channel, tx); + channel->last_tx_type = txtypes; } void channel_set_state(struct channel *channel, diff --git a/lightningd/channel.h b/lightningd/channel.h index 6afd80a99..4fe47a95e 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -76,6 +76,7 @@ struct channel { /* Last tx they gave us. */ struct bitcoin_tx *last_tx; + txtypes last_tx_type; struct bitcoin_signature last_sig; secp256k1_ecdsa_signature *last_htlc_sigs; @@ -201,7 +202,8 @@ struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid); void channel_set_last_tx(struct channel *channel, struct bitcoin_tx *tx, - const struct bitcoin_signature *sig); + const struct bitcoin_signature *sig, + txtypes type); static inline bool channel_can_add_htlc(const struct channel *channel) { diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index ce573c074..0f3f49628 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -95,8 +95,7 @@ static void peer_received_closing_signature(struct channel *channel, /* FIXME: Make sure signature is correct! */ if (better_closing_fee(ld, channel, tx)) { - channel_set_last_tx(channel, tx, &sig); - /* TODO(cdecker) Selectively save updated fields to DB */ + channel_set_last_tx(channel, tx, &sig, TX_CHANNEL_CLOSE); wallet_channel_save(ld->wallet, channel); } diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index eeb79a900..8545f426d 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1240,7 +1240,7 @@ static bool peer_save_commitsig_received(struct channel *channel, u64 commitnum, channel->next_index[LOCAL]++; /* Update channel->last_sig and channel->last_tx before saving to db */ - channel_set_last_tx(channel, tx, commit_sig); + channel_set_last_tx(channel, tx, commit_sig, TX_CHANNEL_UNILATERAL); return true; }