cormorant: handle empty (0 block only) chains

This commit is contained in:
Craig Raw 2023-03-09 10:09:52 +02:00
parent 107b5ba36c
commit 368b24ea3b
3 changed files with 12 additions and 7 deletions

View file

@ -539,7 +539,7 @@ public class BitcoindClient {
try { try {
if(syncing) { if(syncing) {
BlockchainInfo blockchainInfo = getBitcoindService().getBlockchainInfo(); BlockchainInfo blockchainInfo = getBitcoindService().getBlockchainInfo();
if(blockchainInfo.initialblockdownload()) { if(blockchainInfo.initialblockdownload() && !isEmptyBlockchain(blockchainInfo)) {
int percent = blockchainInfo.getProgressPercent(); int percent = blockchainInfo.getProgressPercent();
Date tipDate = blockchainInfo.getTip(); Date tipDate = blockchainInfo.getTip();
Platform.runLater(() -> EventManager.get().post(new CormorantSyncStatusEvent("Syncing" + (percent < 100 ? " (" + percent + "%)" : ""), percent, tipDate))); Platform.runLater(() -> EventManager.get().post(new CormorantSyncStatusEvent("Syncing" + (percent < 100 ? " (" + percent + "%)" : ""), percent, tipDate)));
@ -615,6 +615,10 @@ public class BitcoindClient {
return scanningWallets; return scanningWallets;
} }
private boolean isEmptyBlockchain(BlockchainInfo blockchainInfo) {
return blockchainInfo.blocks() == 0 && blockchainInfo.getProgressPercent() == 100;
}
private record ScanDate(Date rescanSince, Integer range, boolean forceRescan) { private record ScanDate(Date rescanSince, Integer range, boolean forceRescan) {
public Object getTimestamp() { public Object getTimestamp() {
return rescanSince == null ? "now" : rescanSince.getTime() / 1000; return rescanSince == null ? "now" : rescanSince.getTime() / 1000;

View file

@ -16,6 +16,7 @@ import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Base64; import java.util.Base64;
@ -75,7 +76,7 @@ public class BitcoindTransport implements Transport {
connection.setDoOutput(true); connection.setDoOutput(true);
log.trace("> " + request); log.debug("> " + request);
try(OutputStream os = connection.getOutputStream()) { try(OutputStream os = connection.getOutputStream()) {
byte[] jsonBytes = request.getBytes(StandardCharsets.UTF_8); byte[] jsonBytes = request.getBytes(StandardCharsets.UTF_8);
@ -101,7 +102,7 @@ public class BitcoindTransport implements Transport {
} }
String response = res.toString(); String response = res.toString();
log.trace("< " + response); log.debug("< " + response);
return response; return response;
} }
@ -127,11 +128,11 @@ public class BitcoindTransport implements Transport {
} }
private static File getCookieDir(File bitcoindDir) { 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"); 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"); 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"); return new File(bitcoindDir, "signet");
} }

View file

@ -13,7 +13,7 @@ public record VerboseBlockHeader(String hash, int confirmations, int height, int
String bits, double difficulty, String chainwork, int nTx, String previousblockhash) { String bits, double difficulty, String chainwork, int nTx, String previousblockhash) {
public ElectrumBlockHeader getBlockHeader() { public ElectrumBlockHeader getBlockHeader() {
BigInteger nBits = new BigInteger(bits, 16); 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())); return new ElectrumBlockHeader(height, Utils.bytesToHex(blockHeader.bitcoinSerialize()));
} }
} }