From a4847b5af45a4f2a971fef23af0993e093bc57ca Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 13 Nov 2024 13:24:28 +1030 Subject: [PATCH] lightningd: only trim old UTXO entries after gossipd acks block. If it gets really far behind, then we restart, it could miss some. Signed-off-by: Rusty Russell --- lightningd/gossip_control.c | 4 ++++ wallet/wallet.c | 5 +---- wallet/wallet.h | 3 +++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 13ec4e5ed..3c049e1aa 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -222,6 +222,10 @@ static void gossipd_new_blockheight_reply(struct subd *gossipd, /* Now, finally update getinfo's blockheight */ gossipd->ld->gossip_blockheight = ptr2int(blockheight); + + /* And use that to trim old entries in the UTXO set */ + wallet_utxoset_prune(gossipd->ld->wallet, + gossipd->ld->gossip_blockheight); } void gossip_notify_new_block(struct lightningd *ld, u32 blockheight) diff --git a/wallet/wallet.c b/wallet/wallet.c index 5a15c4ace..15d249692 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -4198,7 +4198,7 @@ bool wallet_sanity_check(struct wallet *w) /** * wallet_utxoset_prune -- Remove spent UTXO entries that are old */ -static void wallet_utxoset_prune(struct wallet *w, const u32 blockheight) +void wallet_utxoset_prune(struct wallet *w, u32 blockheight) { struct db_stmt *stmt; @@ -4236,9 +4236,6 @@ void wallet_block_add(struct wallet *w, struct block *b) db_bind_null(stmt); } db_exec_prepared_v2(take(stmt)); - - /* Now cleanup UTXOs that we don't care about anymore */ - wallet_utxoset_prune(w, b->height); } void wallet_block_remove(struct wallet *w, struct block *b) diff --git a/wallet/wallet.h b/wallet/wallet.h index 56b56dd7c..7609a54df 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1135,6 +1135,9 @@ void wallet_utxoset_add(struct wallet *w, const struct short_channel_id * wallet_utxoset_get_spent(const tal_t *ctx, struct wallet *w, u32 blockheight); +/* Prune all UTXO entries spent (far) below this block height */ +void wallet_utxoset_prune(struct wallet *w, u32 blockheight); + /** * Retrieve all UTXO entries that were created at a given blockheight. */