Skip to content

Commit

Permalink
Merge pull request #521 from alvasw/electrum_migrate_from_cli_to_json…
Browse files Browse the repository at this point in the history
…_rpc

Electrum: Migrate from CLI to JSON-RPC
  • Loading branch information
alvasw authored Oct 26, 2022
2 parents e665745 + eda4774 commit 9468d90
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@

package bisq.wallets.electrum;

import bisq.wallets.core.RpcConfig;
import bisq.wallets.core.rpc.DaemonRpcClient;
import bisq.wallets.core.rpc.RpcClientFactory;
import bisq.wallets.electrum.rpc.ElectrumDaemon;
import bisq.wallets.electrum.rpc.ElectrumProcessConfig;
import bisq.wallets.process.BisqProcess;
import bisq.wallets.process.DaemonProcess;
import lombok.Getter;

import java.nio.file.Path;
Expand All @@ -37,8 +33,6 @@ public class ElectrumProcess implements BisqProcess {
@Getter
private Optional<Path> binaryPath = Optional.empty();
private Optional<ElectrumRegtestProcess> electrumRegtestProcess = Optional.empty();
@Getter
private Optional<ElectrumDaemon> electrumDaemon = Optional.empty();
private Optional<String> electrumVersion = Optional.empty();


Expand All @@ -54,13 +48,11 @@ public void start() {
makeBinaryExecutable();
}
createAndStartProcess();

electrumDaemon = Optional.of(createElectrumDaemon());
}

@Override
public void shutdown() {
electrumRegtestProcess.ifPresent(DaemonProcess::shutdown);
electrumRegtestProcess.ifPresent(ElectrumRegtestProcess::invokeStopRpcCall);
}

private void unpackArchive() {
Expand Down Expand Up @@ -94,12 +86,6 @@ private void createAndStartProcess() {
electrumRegtestProcess = Optional.of(process);
}

private ElectrumDaemon createElectrumDaemon() {
RpcConfig rpcConfig = electrumRegtestProcess.orElseThrow().getRpcConfig();
DaemonRpcClient daemonRpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig);
return new ElectrumDaemon(daemonRpcClient);
}

public Optional<String> getElectrumVersion() {
if (electrumVersion.isPresent()) {
return electrumVersion;
Expand Down Expand Up @@ -131,4 +117,8 @@ private String getBinarySuffix() {
public Path getDataDir() {
return electrumRegtestProcess.orElseThrow().getDataDir();
}

public ElectrumDaemon getElectrumDaemon() {
return electrumRegtestProcess.orElseThrow().getElectrumDaemon();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

import bisq.common.util.FileUtils;
import bisq.wallets.core.RpcConfig;
import bisq.wallets.core.rpc.DaemonRpcClient;
import bisq.wallets.core.rpc.RpcClientFactory;
import bisq.wallets.electrum.rpc.ElectrumDaemon;
import bisq.wallets.electrum.rpc.ElectrumProcessConfig;
import bisq.wallets.electrum.rpc.cli.ElectrumCli;
import bisq.wallets.electrum.rpc.cli.ElectrumCliFacade;
import bisq.wallets.process.DaemonProcess;
import bisq.wallets.process.ProcessConfig;
import bisq.wallets.process.scanner.FileScanner;
Expand All @@ -46,7 +47,8 @@ public class ElectrumRegtestProcess extends DaemonProcess {

private final Path binaryPath;
private final ElectrumProcessConfig electrumProcessConfig;
private final ElectrumCliFacade electrumCliFacade;
@Getter
private final ElectrumDaemon electrumDaemon;

@Getter
private RpcConfig rpcConfig;
Expand All @@ -56,9 +58,7 @@ public ElectrumRegtestProcess(Path binaryPath, ElectrumProcessConfig electrumPro
super(electrumProcessConfig.getDataDir());
this.binaryPath = binaryPath;
this.electrumProcessConfig = electrumProcessConfig;

var electrumCli = new ElectrumCli(binaryPath, dataDir);
electrumCliFacade = new ElectrumCliFacade(electrumCli);
this.electrumDaemon = createElectrumDaemon();
}

@Override
Expand All @@ -75,14 +75,14 @@ public ProcessConfig createProcessConfig() {
return ProcessConfig.builder()
.name(binaryPath.toAbsolutePath().toString())
.args(List.of(
ElectrumCli.ELECTRUM_REGTEST_ARG,
"--regtest",
"daemon",

"-s",
electrumProcessConfig.getElectrumXServerHost() + ":" +
electrumProcessConfig.getElectrumXServerPort() + ":t",

ElectrumCli.ELECTRUM_DATA_DIR_ARG,
"--dir",
dataDir.toAbsolutePath().toString(),

"-v" // Enable logging (only works on Mac and Linux)
Expand All @@ -93,7 +93,7 @@ public ProcessConfig createProcessConfig() {

@Override
public void invokeStopRpcCall() {
electrumCliFacade.stop();
electrumDaemon.stop();
}

@Override
Expand All @@ -112,6 +112,12 @@ protected Set<String> getIsSuccessfulStartUpLogLines() {
);
}

private ElectrumDaemon createElectrumDaemon() {
RpcConfig rpcConfig = electrumProcessConfig.getElectrumConfig().toRpcConfig();
DaemonRpcClient daemonRpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig);
return new ElectrumDaemon(daemonRpcClient);
}

private Future<Path> findNewLogFile() {
try {
Path logsDirPath = dataDir.resolve("regtest").resolve("logs");
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ElectrumRegtest(RemoteBitcoind remoteBitcoind, String electrumXHost, int
public void start() {
electrumProcess.start();

electrumDaemon = electrumProcess.getElectrumDaemon().orElseThrow();
electrumDaemon = electrumProcess.getElectrumDaemon();
if (doCreateWallet) {
walletInfo = electrumDaemon.create(AbstractRegtestSetup.WALLET_PASSPHRASE);
electrumDaemon.loadWallet(AbstractRegtestSetup.WALLET_PASSPHRASE);
Expand Down

0 comments on commit 9468d90

Please sign in to comment.