Skip to content

Commit

Permalink
BitcoindRegtestProcess: Expose P2P port number
Browse files Browse the repository at this point in the history
BitcoinJ connects to Bitcoin Core's P2P port. Up to now, we selected a
random port without saving the p2p port number.
  • Loading branch information
alvasw committed Oct 9, 2024
1 parent c3dd750 commit 523ee17
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
public class BitcoindRegtestProcess extends DaemonProcess {

private final Path binaryPath;
protected final int p2pPort;
@Getter
protected final RpcConfig rpcConfig;
private final BitcoindDaemon bitcoindDaemon;

public BitcoindRegtestProcess(Path binaryPath, RpcConfig rpcConfig, Path dataDir) {
public BitcoindRegtestProcess(Path binaryPath, int p2pPort, RpcConfig rpcConfig, Path dataDir) {
super(dataDir);
this.binaryPath = binaryPath;
this.p2pPort = p2pPort;
this.rpcConfig = rpcConfig;
JsonRpcClient rpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig);
bitcoindDaemon = new BitcoindDaemon(rpcClient);
Expand All @@ -60,7 +62,7 @@ public ProcessConfig createProcessConfig() {
"-datadir=" + dataDir.toAbsolutePath(),
"-debug=1",

"-bind=127.0.0.1:" + BitcoindRegtestSetup.findFreeSystemPort(),
"-bind=127.0.0.1:" + p2pPort,
"-whitelist=127.0.0.1",

"-rpcbind=127.0.0.1:" + rpcConfig.getPort(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
public class BitcoindRegtestSetup
extends AbstractRegtestSetup<MultiProcessCoordinator> {

@Getter
private final int p2pPort;
@Getter
private final RpcConfig rpcConfig;
private final BitcoindRegtestProcess bitcoindProcess;
Expand All @@ -49,6 +51,7 @@ public class BitcoindRegtestSetup

public BitcoindRegtestSetup() throws IOException {
super();
p2pPort = BitcoindRegtestSetup.findFreeSystemPort();
rpcConfig = createRpcConfig();
bitcoindProcess = createBitcoindProcess();
remoteBitcoind = new RemoteBitcoind(rpcConfig);
Expand Down Expand Up @@ -113,6 +116,7 @@ private BitcoindRegtestProcess createBitcoindProcess() throws IOException {
Path bitcoindDataDir = tmpDirPath.resolve("bitcoind_data_dir");
return new BitcoindRegtestProcess(
bitcoindPath,
p2pPort,
rpcConfig,
bitcoindDataDir
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ElementsdRegtestProcess extends BitcoindRegtestProcess {
private final ElementsdConfig elementsdConfig;

public ElementsdRegtestProcess(ElementsdConfig elementsdConfig, Path dataDir) {
super(null, elementsdConfig.elementsdRpcConfig(), dataDir);
super(null, NetworkUtils.findFreeSystemPort(), elementsdConfig.elementsdRpcConfig(), dataDir);
this.elementsdConfig = elementsdConfig;
}

Expand All @@ -52,7 +52,7 @@ public ProcessConfig createProcessConfig() {
"-datadir=" + dataDir.toAbsolutePath(),
"-debug=1",

"-bind=127.0.0.1:" + NetworkUtils.findFreeSystemPort(),
"-bind=127.0.0.1:" + p2pPort,
"-whitelist=127.0.0.1",

"-rpcbind=127.0.0.1:" + rpcConfig.getPort(),
Expand Down

0 comments on commit 523ee17

Please sign in to comment.