Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallets: Remove dependency to Bisq 2 NetworkUtils Class #2813

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package bisq.wallets.bitcoind.zmq;

import bisq.common.encoding.Hex;
import bisq.wallets.bitcoind.rpc.BitcoindDaemon;
import bisq.wallets.bitcoind.rpc.responses.BitcoindDecodeRawTransactionResponse;
import com.google.common.io.BaseEncoding;

public class BitcoindRawTxProcessor implements ZmqRawTxProcessor {

Expand All @@ -33,7 +33,7 @@ public BitcoindRawTxProcessor(BitcoindDaemon daemon, ZmqListeners listeners) {

@Override
public void processRawTx(byte[] serializedTx, byte[] sequenceNumber) {
String txInHex = Hex.encode(serializedTx);
String txInHex = BaseEncoding.base16().lowerCase().encode(serializedTx);
BitcoindDecodeRawTransactionResponse.Result rawTransaction = daemon.decodeRawTransaction(txInHex).getResult();
listeners.fireTxOutputAddressesListeners(rawTransaction);
listeners.fireTxIdInputListeners(rawTransaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package bisq.wallets.bitcoind.zmq;

import bisq.common.threading.ExecutorFactory;
import bisq.wallets.bitcoind.rpc.responses.BitcoindGetZmqNotificationsResponse;
import bisq.wallets.bitcoind.zmq.exceptions.CannotFindZmqAddressException;
import bisq.wallets.bitcoind.zmq.exceptions.CannotFindZmqTopicException;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.zeromq.SocketType;
Expand All @@ -33,6 +33,8 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.stream.Collectors;

@Slf4j
Expand All @@ -45,8 +47,8 @@ public class ZmqConnection implements AutoCloseable {
@Getter
private final ZmqListeners listeners;

private final ExecutorService executorService = ExecutorFactory
.newFixedThreadPool("wallet-zeromq-notification-thread-pool", 2);
private final ExecutorService executorService =
newFixedThreadPool("wallet-zeromq-notification-thread-pool", 2);

private ZContext context;

Expand Down Expand Up @@ -151,4 +153,12 @@ private boolean isSocketClosed(int errorCode) {
private boolean isZeroMqContextTerminated(int errorCode) {
return errorCode == ERROR_CODE_CONTEXT_TERMINATED;
}

public ExecutorService newFixedThreadPool(String name, int numThreads) {
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat(name + "-%d")
.setDaemon(true)
.build();
return Executors.newFixedThreadPool(numThreads, threadFactory);
}
}
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;
}
}
}
Loading