Skip to content

Commit 7a360b8

Browse files
committed
wallet: Clean up spent UTXOs once they are buried deeply enough
Signed-off-by: Christian Decker <[email protected]>
1 parent ddfe7ea commit 7a360b8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

wallet/wallet.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF
1616
#define DIRECTION_INCOMING 0
1717
#define DIRECTION_OUTGOING 1
18+
/* How many blocks must a UTXO entry be buried under to be considered old enough
19+
* to prune? */
20+
#define UTXO_PRUNE_DEPTH 144
1821

1922
static void outpointfilters_init(struct wallet *w)
2023
{
@@ -1762,6 +1765,17 @@ bool wallet_network_check(struct wallet *w,
17621765
return true;
17631766
}
17641767

1768+
/**
1769+
* wallet_utxoset_prune -- Remove spent UTXO entries that are old
1770+
*/
1771+
static void wallet_utxoset_prune(struct wallet *w, const u32 blockheight)
1772+
{
1773+
sqlite3_stmt *stmt;
1774+
stmt = db_prepare(w->db, "DELETE FROM utxoset WHERE spendheight < ?");
1775+
sqlite3_bind_int(stmt, 1, blockheight - UTXO_PRUNE_DEPTH);
1776+
db_exec_prepared(w->db, stmt);
1777+
}
1778+
17651779
void wallet_block_add(struct wallet *w, struct block *b)
17661780
{
17671781
sqlite3_stmt *stmt = db_prepare(w->db,
@@ -1776,6 +1790,9 @@ void wallet_block_add(struct wallet *w, struct block *b)
17761790
sqlite3_bind_null(stmt, 3);
17771791
}
17781792
db_exec_prepared(w->db, stmt);
1793+
1794+
/* Now cleanup UTXOs that we don't care about anymore */
1795+
wallet_utxoset_prune(w, b->height);
17791796
}
17801797

17811798
void wallet_block_remove(struct wallet *w, struct block *b)

0 commit comments

Comments
 (0)