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 402c0a94..df3935bd 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 @@ -249,7 +249,9 @@ public class BitcoindClient { } private void importDescriptors(Map descriptors) { - for(String descriptor : descriptors.keySet()) { + //Sort descriptors in alphanumeric order to avoid deadlocks, particularly with BIP47 wallets + Set sortedDescriptors = new TreeSet<>(descriptors.keySet()); + for(String descriptor : sortedDescriptors) { Lock lock = descriptorLocks.computeIfAbsent(descriptor, desc -> new ReentrantLock()); lock.lock(); descriptorBirthDates.put(descriptor, descriptors.get(descriptor).rescanSince); @@ -278,8 +280,10 @@ public class BitcoindClient { for(ListDescriptorResult result : listDescriptorsResult.descriptors()) { String descriptor = OutputDescriptor.normalize(result.desc()); ScanDate previousScanDate = importedDescriptors.get(descriptor); + Date scanDate = result.getScanDate(); + Date rescanSince = scanDate != null ? scanDate : (previousScanDate != null ? previousScanDate.rescanSince : null); Integer range = result.range() == null ? null : result.range().get(result.range().size() - 1); - importedDescriptors.put(descriptor, new ScanDate(previousScanDate == null ? null : previousScanDate.rescanSince, range, false)); + importedDescriptors.put(descriptor, new ScanDate(rescanSince, range, false)); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/ListDescriptorResult.java b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/ListDescriptorResult.java index 1e9283a0..3b844686 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/ListDescriptorResult.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/ListDescriptorResult.java @@ -2,8 +2,12 @@ package com.sparrowwallet.sparrow.net.cormorant.bitcoind; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import java.util.Date; import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true) public record ListDescriptorResult(String desc, long timestamp, boolean active, boolean internal, List range) { + public Date getScanDate() { + return timestamp > 0 ? new Date(timestamp * 1000) : null; + } }