Skip to content

Commit

Permalink
bitcoind: Use project bitcoind in tests
Browse files Browse the repository at this point in the history
The tests use the installed bitcoind binary. To make the test failures
reproducible it's better to run the tests with the officially supported
Bitcoin Core version.
  • Loading branch information
alvasw committed Sep 3, 2024
1 parent b35ece2 commit 679fd81
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
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(elementsdConfig.elementsdRpcConfig(), dataDir);
super(null, elementsdConfig.elementsdRpcConfig(), dataDir);
this.elementsdConfig = elementsdConfig;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
@Slf4j
public class BitcoindRegtestProcess extends DaemonProcess {

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

public BitcoindRegtestProcess(RpcConfig rpcConfig, Path dataDir) {
public BitcoindRegtestProcess(Path binaryPath, RpcConfig rpcConfig, Path dataDir) {
super(dataDir);
this.binaryPath = binaryPath;
this.rpcConfig = rpcConfig;
JsonRpcClient rpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig);
bitcoindDaemon = new BitcoindDaemon(rpcClient);
Expand All @@ -53,7 +55,7 @@ public BitcoindRegtestProcess(RpcConfig rpcConfig, Path dataDir) {
public ProcessConfig createProcessConfig() {
int zmqPort = NetworkUtils.findFreeSystemPort();
return ProcessConfig.builder()
.name("bitcoind")
.name(binaryPath.toAbsolutePath().toString())
.args(List.of(
"-regtest",
"-datadir=" + dataDir.toAbsolutePath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.wallets.regtest.bitcoind;

import bisq.common.platform.OS;
import bisq.common.util.NetworkUtils;
import bisq.wallets.bitcoind.rpc.BitcoindDaemon;
import bisq.wallets.bitcoind.rpc.BitcoindWallet;
Expand All @@ -27,7 +28,10 @@
import bisq.wallets.regtest.process.MultiProcessCoordinator;
import lombok.Getter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -101,9 +105,13 @@ private RpcConfig createRpcConfig() {
return createRpcConfig("127.0.0.1", port);
}

private BitcoindRegtestProcess createBitcoindProcess() {
private BitcoindRegtestProcess createBitcoindProcess() throws IOException {
Path bitcoindBinaryDir = tmpDirPath.resolve("bitcoind_binary");
Path bitcoindPath = installBitcoind(bitcoindBinaryDir);

Path bitcoindDataDir = tmpDirPath.resolve("bitcoind_data_dir");
return new BitcoindRegtestProcess(
bitcoindPath,
rpcConfig,
bitcoindDataDir
);
Expand All @@ -124,4 +132,40 @@ public BitcoindWallet getMinerWallet() {
public ZmqListeners getZmqListeners() {
return remoteBitcoind.getZmqListeners();
}

private Path installBitcoind(Path bitcoindBinaryDir) throws IOException {
File bitcoindBinaryDirFile = bitcoindBinaryDir.toFile();
boolean isSuccess = bitcoindBinaryDirFile.mkdirs();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create " + bitcoindBinaryDir.toAbsolutePath() + " for " +
"bitcoind installation.");
}

InputStream inputStream = getClass()
.getClassLoader()
.getResourceAsStream("bitcoind");

if (inputStream == null) {
throw new IllegalStateException("Can't read bitcoind binary from resources.");
}

Path bitcoindPath = bitcoindBinaryDir.resolve("bitcoind");
try (inputStream) {
Files.copy(inputStream, bitcoindPath);

boolean endOfStreamReached = inputStream.available() == 0;
if (!endOfStreamReached) {
throw new IllegalStateException("Couldn't extract bitcoind binary.");
}

if (OS.isLinux() || OS.isMacOs()) {
isSuccess = bitcoindPath.toFile().setExecutable(true);
if (!isSuccess) {
throw new IllegalStateException("Couldn't set executable bit on bitcoind binary.");
}
}

return bitcoindPath;
}
}
}

0 comments on commit 679fd81

Please sign in to comment.