From 8c60eb1b918cc3095c511d14761c9175b06c35da Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Wed, 26 Jul 2023 15:32:23 -0700 Subject: [PATCH] Add necessary 2nd half of previous commit Now that bitcoinj Context no longer depends on Network, we can initialize the executorService before we know the network, but we still need the Network to allocate an RpcClientModule for the Jackson Mapper. --- .../bitcoin/jsonrpc/BitcoinClient.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 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 524297937..d8dc82a96 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 @@ -34,6 +34,8 @@ import org.consensusj.jsonrpc.JsonRpcErrorException; import org.consensusj.jsonrpc.JsonRpcException; import org.consensusj.jsonrpc.JsonRpcMessage; +import org.consensusj.jsonrpc.JsonRpcRequest; +import org.consensusj.jsonrpc.JsonRpcResponse; import org.consensusj.jsonrpc.JsonRpcStatusException; import org.consensusj.jsonrpc.JsonRpcClientHttpUrlConnection; import org.bitcoinj.base.Address; @@ -110,7 +112,7 @@ public class BitcoinClient extends JsonRpcClientHttpUrlConnection implements Cha // TODO: Replace NetworkParameters with Network/BitcoinNetwork once we upgrade to bitcoinj 0.17 (once it is released) private Network network; - private ExecutorService executorService; + private final ExecutorService executorService; private int serverVersion = 0; // 0 means unknown serverVersion private boolean isAddressIndexSuccessfullyTested = false; @@ -119,8 +121,13 @@ public class BitcoinClient extends JsonRpcClientHttpUrlConnection implements Cha public BitcoinClient(SSLSocketFactory sslSocketFactory, Network network, URI server, String rpcuser, String rpcpassword) { super(sslSocketFactory, JsonRpcMessage.Version.V2, server, rpcuser, rpcpassword); this.network = network; + ThreadFactory threadFactory = new BitcoinClientThreadFactory(new Context(), "Bitcoin RPC Client"); + // TODO: Tune and/or make configurable the thread pool size. + // Current pool size of 5 is chosen to minimize simultaneous active RPC + // calls in `bitcoind` -- which is not designed for serving multiple clients. + executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE, threadFactory); if (network != null) { - initExecutor(); + initMapper(); } } @@ -129,13 +136,11 @@ public BitcoinClient(SSLSocketFactory sslSocketFactory, NetworkParameters netPar this(sslSocketFactory, netParams.network(), server, rpcuser, rpcpassword); } - private void initExecutor() { + /** + * Initialize the Jackson Mapper (after we are sure we have the {@code Network}) + */ + private void initMapper() { mapper.registerModule(new RpcClientModule(network)); - ThreadFactory threadFactory = new BitcoinClientThreadFactory(new Context(), "Bitcoin RPC Client"); - // TODO: Tune and/or make configurable the thread pool size. - // Current pool size of 5 is chosen to minimize simultaneous active RPC - // calls in `bitcoind` -- which is not designed for serving multiple clients. - executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE, threadFactory); } // TODO: Reconcile this constructor mode with {@link #waitForServer(int)} @@ -212,7 +217,7 @@ public synchronized NetworkParameters getNetParams() { public synchronized Network getNetwork() { if (network == null) { network = getNetworkFromServer().join(); - initExecutor(); + initMapper(); } return network; }