From da039859967e3fce8a71163d11665f11beb91689 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 13 Oct 2021 14:15:36 +1030 Subject: [PATCH] wallet: only hand onchaind the HTLCs it needs to know. This will make closing long-lived channels more efficient, and it's just nicer. Signed-off-by: Rusty Russell --- lightningd/onchain_control.c | 3 ++- wallet/wallet.c | 6 ++++-- wallet/wallet.h | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index d2ee1ab7d..c325a6c9d 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -130,7 +130,8 @@ static void handle_onchain_init_reply(struct channel *channel, const u8 *msg) /* Tell it about any relevant HTLCs */ /* FIXME: Filter by commitnum! */ - stubs = wallet_htlc_stubs(tmpctx, channel->peer->ld->wallet, channel); + stubs = wallet_htlc_stubs(tmpctx, channel->peer->ld->wallet, channel, + commit_num); tell = tal_arr(stubs, bool, tal_count(stubs)); tell_immediate = tal_arr(stubs, bool, tal_count(stubs)); diff --git a/wallet/wallet.c b/wallet/wallet.c index a28d99ec9..d483e1ee7 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2836,7 +2836,7 @@ const struct invoice_details *wallet_invoice_details(const tal_t *ctx, } struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet, - struct channel *chan) + struct channel *chan, u64 commit_num) { struct htlc_stub *stubs; struct sha256 payment_hash; @@ -2845,9 +2845,11 @@ struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet, stmt = db_prepare_v2(wallet->db, SQL("SELECT channel_id, direction, cltv_expiry, " "channel_htlc_id, payment_hash " - "FROM channel_htlcs WHERE channel_id = ?;")); + "FROM channel_htlcs WHERE channel_id = ? AND min_commit_num <= ? AND ((max_commit_num IS NULL) OR max_commit_num >= ?);")); db_bind_u64(stmt, 0, chan->dbid); + db_bind_u64(stmt, 1, commit_num); + db_bind_u64(stmt, 2, commit_num); db_query_prepared(stmt); stubs = tal_arr(ctx, struct htlc_stub, 0); diff --git a/wallet/wallet.h b/wallet/wallet.h index 85de90b1f..ad2bcbfc0 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1058,9 +1058,10 @@ const struct invoice_details *wallet_invoice_details(const tal_t *ctx, * @ctx: Allocation context for the return value * @wallet: Wallet to load from * @chan: Channel to fetch stubs for + * @commit_num: The commitment number of the commit tx. */ struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet, - struct channel *chan); + struct channel *chan, u64 commit_num); /** * wallet_payment_setup - Remember this payment for later committing.