cormorant: threading and scan date initialization improvements

This commit is contained in:
Craig Raw 2023-01-20 12:56:25 +02:00
parent 4ad9cdedb6
commit 7c64d689fd
2 changed files with 10 additions and 2 deletions

View file

@ -249,7 +249,9 @@ public class BitcoindClient {
} }
private void importDescriptors(Map<String, ScanDate> descriptors) { private void importDescriptors(Map<String, ScanDate> descriptors) {
for(String descriptor : descriptors.keySet()) { //Sort descriptors in alphanumeric order to avoid deadlocks, particularly with BIP47 wallets
Set<String> sortedDescriptors = new TreeSet<>(descriptors.keySet());
for(String descriptor : sortedDescriptors) {
Lock lock = descriptorLocks.computeIfAbsent(descriptor, desc -> new ReentrantLock()); Lock lock = descriptorLocks.computeIfAbsent(descriptor, desc -> new ReentrantLock());
lock.lock(); lock.lock();
descriptorBirthDates.put(descriptor, descriptors.get(descriptor).rescanSince); descriptorBirthDates.put(descriptor, descriptors.get(descriptor).rescanSince);
@ -278,8 +280,10 @@ public class BitcoindClient {
for(ListDescriptorResult result : listDescriptorsResult.descriptors()) { for(ListDescriptorResult result : listDescriptorsResult.descriptors()) {
String descriptor = OutputDescriptor.normalize(result.desc()); String descriptor = OutputDescriptor.normalize(result.desc());
ScanDate previousScanDate = importedDescriptors.get(descriptor); 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); 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));
} }
} }

View file

@ -2,8 +2,12 @@ package com.sparrowwallet.sparrow.net.cormorant.bitcoind;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Date;
import java.util.List; import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public record ListDescriptorResult(String desc, long timestamp, boolean active, boolean internal, List<Integer> range) { public record ListDescriptorResult(String desc, long timestamp, boolean active, boolean internal, List<Integer> range) {
public Date getScanDate() {
return timestamp > 0 ? new Date(timestamp * 1000) : null;
}
} }