From 5fa1b712fe419111aae5d040d3c91d85e0c384b7 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Sun, 24 Sep 2023 12:16:59 -0700 Subject: [PATCH] BitcoinClient: cleanup in waitForServer, waitForBlock * Use Duration (and long) instead of int for retry, message delay * Move logging of status message out of try/catch block * Rename `ignored` local that wasn't ignored --- .../bitcoin/jsonrpc/BitcoinClient.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java b/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java index e543f4958..2f4eee818 100644 --- a/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java +++ b/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java @@ -57,6 +57,7 @@ import java.net.SocketException; import java.net.URI; import java.nio.ByteBuffer; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -103,9 +104,10 @@ public class BitcoinClient extends JsonRpcClientJavaNet implements ChainTipClien private static final int THREAD_POOL_SIZE = 5; - private static final int SECOND_IN_MSEC = 1000; - private static final int RETRY_SECONDS = 5; - private static final int MESSAGE_SECONDS = 30; + // Delay between retries in waitForServer and waitForBlock + private static final Duration RETRY_DELAY = Duration.ofSeconds(5); + // Delay between log messages in waitForBlock + private static final Duration MESSAGE_DELAY = Duration.ofSeconds(30); public static final int BITCOIN_CORE_VERSION_MIN = 200000; // Minimum Bitcoin Core supported (tested) version public static final int BITCOIN_CORE_VERSION_DESC_DEFAULT = 230000; // Bitcoin Core version that DEFAULTS to descriptor wallets @@ -363,7 +365,7 @@ public Boolean waitForServer(int timeout) throws JsonRpcException { String status; // Status message for logging String statusLast = null; - int seconds = 0; + long seconds = 0; while (seconds < timeout) { try { Integer block = this.getBlockCount(); @@ -383,10 +385,10 @@ public Boolean waitForServer(int timeout) throws JsonRpcException { throw new JsonRpcException("Unexpected exception in waitForServer", se) ; } - } catch (EOFException ignored) { + } catch (EOFException e) { /* Android exception, ignore */ // Expected exceptions on Android, RoboVM - status = ignored.getMessage(); + status = e.getMessage(); } catch (JsonRpcStatusException e) { // If server is in "warm-up" mode, e.g. validating/parsing the blockchain... if (e.jsonRpcCode == -28) { @@ -400,14 +402,14 @@ public Boolean waitForServer(int timeout) throws JsonRpcException { // Ignore all IOExceptions status = e.getMessage(); } + // Log status messages only once, if new or updated + if (!status.equals(statusLast)) { + log.info("Waiting for server: RPC Status: " + status); + statusLast = status; + } try { - // Log status messages only once, if new or updated - if (!status.equals(statusLast)) { - log.info("Waiting for server: RPC Status: " + status); - statusLast = status; - } - Thread.sleep(RETRY_SECONDS * SECOND_IN_MSEC); - seconds += RETRY_SECONDS; + Thread.sleep(RETRY_DELAY.toMillis()); + seconds += RETRY_DELAY.toSeconds(); } catch (InterruptedException e) { log.error(e.toString()); Thread.currentThread().interrupt(); @@ -432,7 +434,7 @@ public Boolean waitForBlock(int blockHeight, int timeout) throws JsonRpcStatusEx log.info("Waiting for server to reach block " + blockHeight); - int seconds = 0; + long seconds = 0; while (seconds < timeout) { Integer block = this.getBlockCount(); if (block >= blockHeight) { @@ -440,11 +442,11 @@ public Boolean waitForBlock(int blockHeight, int timeout) throws JsonRpcStatusEx return true; } else { try { - if (seconds % MESSAGE_SECONDS == 0) { + if (seconds % MESSAGE_DELAY.toSeconds() == 0) { log.debug("Server at block " + block); } - Thread.sleep(RETRY_SECONDS * SECOND_IN_MSEC); - seconds += RETRY_SECONDS; + Thread.sleep(RETRY_DELAY.toMillis()); + seconds += RETRY_DELAY.toSeconds(); } catch (InterruptedException e) { log.error(e.toString()); Thread.currentThread().interrupt(); @@ -457,7 +459,6 @@ public Boolean waitForBlock(int blockHeight, int timeout) throws JsonRpcStatusEx return false; } - /** * Tell the server to stop * @return A status string