From 368b24ea3b39748aece19ea3b094f9a064d70223 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 9 Mar 2023 10:09:52 +0200 Subject: [PATCH] cormorant: handle empty (0 block only) chains --- .../net/cormorant/bitcoind/BitcoindClient.java | 6 +++++- .../net/cormorant/bitcoind/BitcoindTransport.java | 11 ++++++----- .../net/cormorant/bitcoind/VerboseBlockHeader.java | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) 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 37d5e1a5..dd785d0e 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 @@ -539,7 +539,7 @@ public class BitcoindClient { try { if(syncing) { BlockchainInfo blockchainInfo = getBitcoindService().getBlockchainInfo(); - if(blockchainInfo.initialblockdownload()) { + if(blockchainInfo.initialblockdownload() && !isEmptyBlockchain(blockchainInfo)) { int percent = blockchainInfo.getProgressPercent(); Date tipDate = blockchainInfo.getTip(); Platform.runLater(() -> EventManager.get().post(new CormorantSyncStatusEvent("Syncing" + (percent < 100 ? " (" + percent + "%)" : ""), percent, tipDate))); @@ -615,6 +615,10 @@ public class BitcoindClient { return scanningWallets; } + private boolean isEmptyBlockchain(BlockchainInfo blockchainInfo) { + return blockchainInfo.blocks() == 0 && blockchainInfo.getProgressPercent() == 100; + } + private record ScanDate(Date rescanSince, Integer range, boolean forceRescan) { public Object getTimestamp() { return rescanSince == null ? "now" : rescanSince.getTime() / 1000; diff --git a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java index 76317f80..e5b844ed 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java @@ -16,6 +16,7 @@ import java.net.Proxy; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Base64; @@ -75,7 +76,7 @@ public class BitcoindTransport implements Transport { connection.setDoOutput(true); - log.trace("> " + request); + log.debug("> " + request); try(OutputStream os = connection.getOutputStream()) { byte[] jsonBytes = request.getBytes(StandardCharsets.UTF_8); @@ -101,7 +102,7 @@ public class BitcoindTransport implements Transport { } String response = res.toString(); - log.trace("< " + response); + log.debug("< " + response); return response; } @@ -127,11 +128,11 @@ public class BitcoindTransport implements Transport { } private static File getCookieDir(File bitcoindDir) { - if(Network.get() == Network.TESTNET && !bitcoindDir.getName().contains("testnet")) { + if(Network.get() == Network.TESTNET && Files.exists(Path.of(bitcoindDir.getAbsolutePath(), "testnet3", COOKIE_FILENAME))) { return new File(bitcoindDir, "testnet3"); - } else if(Network.get() == Network.REGTEST && !bitcoindDir.getName().contains("regtest")) { + } else if(Network.get() == Network.REGTEST && Files.exists(Path.of(bitcoindDir.getAbsolutePath(), "regtest", COOKIE_FILENAME))) { return new File(bitcoindDir, "regtest"); - } else if(Network.get() == Network.SIGNET && !bitcoindDir.getName().contains("signet")) { + } else if(Network.get() == Network.SIGNET && Files.exists(Path.of(bitcoindDir.getAbsolutePath(), "signet", COOKIE_FILENAME))) { return new File(bitcoindDir, "signet"); } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/VerboseBlockHeader.java b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/VerboseBlockHeader.java index f1095348..178b524a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/VerboseBlockHeader.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/VerboseBlockHeader.java @@ -13,7 +13,7 @@ public record VerboseBlockHeader(String hash, int confirmations, int height, int String bits, double difficulty, String chainwork, int nTx, String previousblockhash) { public ElectrumBlockHeader getBlockHeader() { BigInteger nBits = new BigInteger(bits, 16); - BlockHeader blockHeader = new BlockHeader(version, Sha256Hash.wrap(previousblockhash), Sha256Hash.wrap(merkleroot), null, time, nBits.longValue(), nonce); + BlockHeader blockHeader = new BlockHeader(version, previousblockhash == null ? Sha256Hash.ZERO_HASH : Sha256Hash.wrap(previousblockhash), Sha256Hash.wrap(merkleroot), null, time, nBits.longValue(), nonce); return new ElectrumBlockHeader(height, Utils.bytesToHex(blockHeader.bitcoinSerialize())); } }