cormorant: round up wallet range to avoid frequent rescans with a large gap limit

This commit is contained in:
Craig Raw 2023-12-01 09:09:28 +02:00
parent 6ee3755ce4
commit 06489d8339

View file

@ -260,7 +260,7 @@ public class BitcoindClient {
} }
private ScanDate getScanDate(String normalizedDescriptor, Wallet wallet, KeyPurpose keyPurpose, Date earliestBirthDate) { 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 //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; boolean forceRescan = false;
@ -274,6 +274,13 @@ public class BitcoindClient {
return new ScanDate(earliestBirthDate, range, forceRescan); 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) { private int getHighestUsedIndex(String descriptor, Wallet wallet, KeyPurpose keyPurpose) {
int highestUsedIndex = wallet.getFreshNode(keyPurpose).getIndex() - 1; int highestUsedIndex = wallet.getFreshNode(keyPurpose).getIndex() - 1;
return descriptorUsedIndexes.compute(descriptor, (d, lastHighestUsedIndex) -> lastHighestUsedIndex == null ? highestUsedIndex : Math.max(highestUsedIndex, lastHighestUsedIndex)); return descriptorUsedIndexes.compute(descriptor, (d, lastHighestUsedIndex) -> lastHighestUsedIndex == null ? highestUsedIndex : Math.max(highestUsedIndex, lastHighestUsedIndex));