From 41ba8455a0f5fec58adcd9817f37c9d7355e96df Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 6 Feb 2023 13:25:35 +0200 Subject: [PATCH] cormorant: avoid importing wallets when testing connection, only show prune warning once per connection --- src/main/java/com/sparrowwallet/sparrow/net/Bwt.java | 2 +- .../com/sparrowwallet/sparrow/net/ElectrumServer.java | 2 +- .../sparrowwallet/sparrow/net/cormorant/Cormorant.java | 8 +++++++- .../sparrow/net/cormorant/bitcoind/BitcoindClient.java | 5 +++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java b/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java index 159be72c..0a931ea5 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java @@ -361,7 +361,7 @@ public class Bwt { Bwt.this.start(notifier); } else { if(AppServices.get().getOpenWallets().keySet().stream().anyMatch(wallet -> wallet.getScriptType() == ScriptType.P2TR)) { - throw new IllegalStateException("Taproot wallets are not yet supported when connecting to Bitcoin Core"); + throw new IllegalStateException("Upgrade Bitcoin Core to v24 or later for Taproot wallet support"); } Bwt.this.start(AppServices.get().getOpenWallets().keySet(), notifier); diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java index 6e30fd7b..b440e495 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java @@ -1106,7 +1106,7 @@ public class ElectrumServer { throw new CormorantBitcoindException("Legacy wallet configured"); } if(ElectrumServer.cormorant == null) { - ElectrumServer.cormorant = new Cormorant(); + ElectrumServer.cormorant = new Cormorant(subscribe); ElectrumServer.coreElectrumServer = cormorant.start(); } } catch(CormorantBitcoindException e) { diff --git a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/Cormorant.java b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/Cormorant.java index ac2dea3e..ceb59f65 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/Cormorant.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/Cormorant.java @@ -14,6 +14,7 @@ import com.sparrowwallet.sparrow.net.cormorant.electrum.ElectrumServerRunnable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; import java.util.Date; public class Cormorant { @@ -26,15 +27,20 @@ public class Cormorant { private BitcoindClient bitcoindClient; private ElectrumServerRunnable electrumServer; + private final boolean useWallets; private boolean running; + public Cormorant(boolean useWallets) { + this.useWallets = useWallets; + } + public Server start() throws CormorantBitcoindException { bitcoindClient = new BitcoindClient(); bitcoindClient.initialize(); Thread importThread = new Thread(() -> { try { - bitcoindClient.importWallets(AppServices.get().getOpenWallets().keySet()); + bitcoindClient.importWallets(useWallets ? AppServices.get().getOpenWallets().keySet() : Collections.emptySet()); } catch(ImportFailedException e) { log.debug("Failed to import wallets", e); } finally { 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 6b89dfc1..42daefd1 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 @@ -74,6 +74,8 @@ public class BitcoindClient { private final Condition initialImportCondition = initialImportLock.newCondition(); private boolean initialImportStarted; + private final List pruneWarnedDescriptors = new ArrayList<>(); + public BitcoindClient() { BitcoindTransport bitcoindTransport; @@ -149,9 +151,11 @@ public class BitcoindClient { } catch(ScanDateBeforePruneException e) { List prePruneWallets = wallets.stream() .filter(wallet -> wallet.getBirthDate() != null && wallet.getBirthDate().before(e.getPrunedDate()) && wallet.isValid() + && !pruneWarnedDescriptors.contains(e.getDescriptor()) && OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.RECEIVE).toString(false, true).equals(e.getDescriptor())) .sorted(Comparator.comparingLong(o -> o.getBirthDate().getTime())).collect(Collectors.toList()); if(!prePruneWallets.isEmpty()) { + pruneWarnedDescriptors.add(e.getDescriptor()); Platform.runLater(() -> EventManager.get().post(new CormorantPruneStatusEvent("Error: Wallet birthday earlier than Bitcoin Core prune date", prePruneWallets.get(0), e.getRescanSince(), e.getPrunedDate(), legacyWalletExists))); } throw new ImportFailedException("Wallet birthday earlier than prune date"); @@ -358,6 +362,7 @@ public class BitcoindClient { public void stop() { timer.cancel(); + pruneWarnedDescriptors.clear(); stopped = true; }