Skip to content

Commit

Permalink
wallets: Remove dependency to Bisq 2 NetworkUtils Class
Browse files Browse the repository at this point in the history
We can't depend on Bisq 2's NetworkUtils class to be able to reuse the wallets library in Bisq 1.
  • Loading branch information
alvasw committed Sep 11, 2024
1 parent d221ad0 commit 26a677f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@

package bisq.wallets.bitcoind;

import bisq.common.util.NetworkUtils;
import bisq.wallets.bitcoind.rpc.BitcoindDaemon;
import bisq.wallets.json_rpc.RpcConfig;
import bisq.wallets.json_rpc.RpcClientFactory;
import bisq.wallets.json_rpc.JsonRpcClient;
import bisq.wallets.json_rpc.RpcClientFactory;
import bisq.wallets.json_rpc.RpcConfig;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.util.Random;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class WalletNotRunningTest {
@Test
void notRunningTest() {
int freePort = NetworkUtils.findFreeSystemPort();
int freePort = findFreeSystemPort();

RpcConfig rpcConfig = RpcConfig.builder()
.hostname("127.0.0.1")
Expand All @@ -46,4 +48,15 @@ void notRunningTest() {
assertThatThrownBy(minerChainBackend::listWallets)
.hasCauseInstanceOf(ConnectException.class);
}

public static int findFreeSystemPort() {
try {
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
server.close();
return port;
} catch (IOException ignored) {
return new Random().nextInt(10000) + 50000;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.wallets.regtest.bitcoind;

import bisq.common.util.NetworkUtils;
import bisq.wallets.bitcoind.rpc.BitcoindDaemon;
import bisq.wallets.json_rpc.JsonRpcClient;
import bisq.wallets.json_rpc.RpcCallFailureException;
Expand Down Expand Up @@ -53,15 +52,15 @@ public BitcoindRegtestProcess(Path binaryPath, RpcConfig rpcConfig, Path dataDir

@Override
public ProcessConfig createProcessConfig() {
int zmqPort = NetworkUtils.findFreeSystemPort();
int zmqPort = BitcoindRegtestSetup.findFreeSystemPort();
return ProcessConfig.builder()
.name(binaryPath.toAbsolutePath().toString())
.args(List.of(
"-regtest",
"-datadir=" + dataDir.toAbsolutePath(),
"-debug=1",

"-bind=127.0.0.1:" + NetworkUtils.findFreeSystemPort(),
"-bind=127.0.0.1:" + BitcoindRegtestSetup.findFreeSystemPort(),
"-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 @@ -17,7 +17,6 @@

package bisq.wallets.regtest.bitcoind;

import bisq.common.util.NetworkUtils;
import bisq.wallets.bitcoind.rpc.BitcoindDaemon;
import bisq.wallets.bitcoind.rpc.BitcoindWallet;
import bisq.wallets.bitcoind.rpc.responses.BitcoindListUnspentResponse;
Expand All @@ -31,11 +30,13 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;

public class BitcoindRegtestSetup
extends AbstractRegtestSetup<MultiProcessCoordinator> {
Expand Down Expand Up @@ -101,7 +102,7 @@ public static RpcConfig createRpcConfig(String hostname, int port) {
}

private RpcConfig createRpcConfig() {
int port = NetworkUtils.findFreeSystemPort();
int port = findFreeSystemPort();
return createRpcConfig("127.0.0.1", port);
}

Expand Down Expand Up @@ -168,4 +169,15 @@ private Path installBitcoind(Path bitcoindBinaryDir) throws IOException {
return bitcoindPath;
}
}

public static int findFreeSystemPort() {
try {
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
server.close();
return port;
} catch (IOException ignored) {
return new Random().nextInt(10000) + 50000;
}
}
}

0 comments on commit 26a677f

Please sign in to comment.