From fb8661714e5ca8ff7dd9e495933853b959ba1b17 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 14 Apr 2020 15:36:58 +0200 Subject: [PATCH] wallet: Add a gap limit when checking for incoming transactions Changelog-Added: wallet: The wallet now has a gap limit that is used to check for incoming transactions when scanning the blockchain. --- lightningd/lightningd.c | 2 +- wallet/wallet.c | 7 ++++++- wallet/wallet.h | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index bbe66324c..2b0ded7e7 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -572,7 +572,7 @@ static void init_txfilter(struct wallet *w, struct txfilter *filter) bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0); /*~ One of the C99 things I unequivocally approve: for-loop scope. */ - for (u64 i = 0; i <= bip32_max_index; i++) { + for (u64 i = 0; i <= bip32_max_index + w->keyscan_gap; i++) { if (bip32_key_from_parent(w->bip32_base, i, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { abort(); } diff --git a/wallet/wallet.c b/wallet/wallet.c index 2c2b0655f..86f1b6c0f 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -60,6 +60,7 @@ struct wallet *wallet_new(struct lightningd *ld, struct timers *timers) wallet->db = db_setup(wallet, ld); wallet->log = new_log(wallet, ld->log_book, NULL, "wallet"); wallet->bip32_base = NULL; + wallet->keyscan_gap = 50; list_head_init(&wallet->unstored_payments); list_head_init(&wallet->unreleased_txs); @@ -551,7 +552,7 @@ bool wallet_can_spend(struct wallet *w, const u8 *script, else return false; - for (i = 0; i <= bip32_max_index; i++) { + for (i = 0; i <= bip32_max_index + w->keyscan_gap; i++) { u8 *s; if (bip32_key_from_parent(w->bip32_base, i, @@ -566,6 +567,10 @@ bool wallet_can_spend(struct wallet *w, const u8 *script, s = p2sh; } if (scripteq(s, script)) { + /* If we found a used key in the keyscan_gap we should + * remember that. */ + if (i > bip32_max_index) + db_set_intvar(w->db, "bip32_max_index", i); tal_free(s); *index = i; return true; diff --git a/wallet/wallet.h b/wallet/wallet.h index d9e693b4a..540c26e0e 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -50,6 +50,9 @@ struct wallet { /* Unreleased txs, waiting for txdiscard/txsend */ struct list_head unreleased_txs; + + /* How many keys should we look ahead at most? */ + u64 keyscan_gap; }; /* A transaction we've txprepared, but haven't signed and released yet */