From 06489d83392dbf4bc49263168498c32cde546654 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 1 Dec 2023 09:09:28 +0200 Subject: [PATCH] cormorant: round up wallet range to avoid frequent rescans with a large gap limit --- .../sparrow/net/cormorant/bitcoind/BitcoindClient.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindClient.java b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindClient.java index f96a7e5b..0cb57248 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindClient.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindClient.java @@ -260,7 +260,7 @@ public class BitcoindClient { } private ScanDate getScanDate(String normalizedDescriptor, Wallet wallet, KeyPurpose keyPurpose, Date earliestBirthDate) { - Integer range = (keyPurpose == null ? null : Math.max(getHighestUsedIndex(normalizedDescriptor, wallet, keyPurpose) + wallet.getGapLimit(), getDefaultRange(wallet, keyPurpose))); + Integer range = (keyPurpose == null ? null : Math.max(getWalletRange(normalizedDescriptor, wallet, keyPurpose), getDefaultRange(wallet, keyPurpose))); //Force a rescan if loading a wallet with a birthday later than existing transactions, or if the wallet birthdate has been set or changed to an earlier date from the last check boolean forceRescan = false; @@ -274,6 +274,13 @@ public class BitcoindClient { return new ScanDate(earliestBirthDate, range, forceRescan); } + private int getWalletRange(String normalizedDescriptor, Wallet wallet, KeyPurpose keyPurpose) { + int maxIndex = getHighestUsedIndex(normalizedDescriptor, wallet, keyPurpose) + wallet.getGapLimit(); + //Default keypool size of 1000 will mean no rescans with a normal (<1000) gap limit as range is automatically extended by keypool size on address use + //Rounding up to the nearest 500 will ensure reduced rescans where gap limit is over 1000 + return maxIndex % 500 == 0 ? maxIndex : ((maxIndex / 500) + 1) * 500; + } + private int getHighestUsedIndex(String descriptor, Wallet wallet, KeyPurpose keyPurpose) { int highestUsedIndex = wallet.getFreshNode(keyPurpose).getIndex() - 1; return descriptorUsedIndexes.compute(descriptor, (d, lastHighestUsedIndex) -> lastHighestUsedIndex == null ? highestUsedIndex : Math.max(highestUsedIndex, lastHighestUsedIndex));