Skip to content

Commit

Permalink
wallet: only hand onchaind the HTLCs it needs to know.
Browse files Browse the repository at this point in the history
This will make closing long-lived channels more efficient, and it's
just nicer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Oct 12, 2021
1 parent 5a8d244 commit 378c9e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
6 changes: 4 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,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;
Expand All @@ -2836,9 +2836,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);
Expand Down
3 changes: 2 additions & 1 deletion wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1057,9 +1057,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.
Expand Down

0 comments on commit 378c9e8

Please sign in to comment.