Skip to content

Commit

Permalink
BitcoinClient: cleanup in waitForServer, waitForBlock
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
msgilligan committed Sep 24, 2023
1 parent 9f99b10 commit 5fa1b71
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -432,19 +434,19 @@ 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) {
log.info("Server is at block " + block + " returning 'true'.");
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();
Expand All @@ -457,7 +459,6 @@ public Boolean waitForBlock(int blockHeight, int timeout) throws JsonRpcStatusEx
return false;
}


/**
* Tell the server to stop
* @return A status string
Expand Down

0 comments on commit 5fa1b71

Please sign in to comment.