Commit a507259f authored by zeroleak's avatar zeroleak
Browse files

use Observable

parent 928941ee
package com.samourai.whirlpool.cli;
import com.samourai.whirlpool.client.mix.listener.MixFailReason;
import com.samourai.whirlpool.client.mix.listener.MixStep;
import com.samourai.whirlpool.client.mix.listener.MixSuccess;
import com.samourai.whirlpool.client.whirlpool.listener.WhirlpoolClientListener;
public class CliListener implements WhirlpoolClientListener {
private boolean done;
public CliListener() {
super();
}
public void waitDone() throws InterruptedException {
synchronized (this) {
while (!done) {
wait(1000);
}
}
}
@Override
public void success(MixSuccess mixSuccess) {
done = true;
// override with custom code here: success
}
@Override
public void fail(MixFailReason reason, String notifiableError) {
done = true;
// override with custom code here: failure
}
@Override
public void progress(MixStep step) {
// override with custom code here: mix progress
}
}
......@@ -4,7 +4,7 @@ import com.samourai.whirlpool.cli.api.controllers.AbstractRestController;
import com.samourai.whirlpool.cli.api.protocol.CliApiEndpoint;
import com.samourai.whirlpool.cli.api.protocol.rest.ApiWalletStateResponse;
import com.samourai.whirlpool.cli.services.CliWalletService;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolWalletState;
import com.samourai.whirlpool.client.wallet.beans.MixingState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestHeader;
......@@ -19,8 +19,8 @@ public class MixController extends AbstractRestController {
@RequestMapping(value = CliApiEndpoint.REST_MIX, method = RequestMethod.GET)
public ApiWalletStateResponse wallet(@RequestHeader HttpHeaders headers) throws Exception {
checkHeaders(headers);
WhirlpoolWalletState whirlpoolWalletState = cliWalletService.getSessionWallet().getState();
return new ApiWalletStateResponse(whirlpoolWalletState);
MixingState mixingState = cliWalletService.getSessionWallet().getMixingState();
return new ApiWalletStateResponse(mixingState);
}
@RequestMapping(value = CliApiEndpoint.REST_MIX_START, method = RequestMethod.POST)
......
......@@ -2,10 +2,7 @@ package com.samourai.whirlpool.cli.api.protocol.beans;
import com.samourai.wallet.api.backend.beans.UnspentResponse;
import com.samourai.whirlpool.client.mix.listener.MixStep;
import com.samourai.whirlpool.client.wallet.beans.MixableStatus;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolAccount;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxo;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxoStatus;
import com.samourai.whirlpool.client.wallet.beans.*;
public class ApiUtxo {
private String hash;
......@@ -35,18 +32,22 @@ public class ApiUtxo {
this.path = utxo.xpub.path;
this.account = whirlpoolUtxo.getAccount();
this.status = whirlpoolUtxo.getStatus();
this.mixStep = whirlpoolUtxo.getMixStep();
this.mixableStatus = whirlpoolUtxo.getMixableStatus();
this.progressPercent = whirlpoolUtxo.getProgressPercent();
this.poolId = whirlpoolUtxo.getUtxoConfig().getPoolId();
this.mixsTarget = whirlpoolUtxo.getUtxoConfig().getMixsTarget();
this.mixsDone = whirlpoolUtxo.getUtxoConfig().getMixsDone();
this.message = whirlpoolUtxo.getMessage();
this.error = whirlpoolUtxo.getError();
WhirlpoolUtxoConfig utxoConfig = whirlpoolUtxo.getUtxoConfig();
WhirlpoolUtxoState utxoState = whirlpoolUtxo.getUtxoState();
this.status = utxoState.getStatus();
this.mixStep =
utxoState.getMixProgress() != null ? utxoState.getMixProgress().getMixStep() : null;
this.mixableStatus = utxoState.getMixableStatus();
this.progressPercent =
utxoState.getMixProgress() != null ? utxoState.getMixProgress().getProgressPercent() : null;
this.poolId = utxoConfig.getPoolId();
this.mixsTarget = utxoConfig.getMixsTarget();
this.mixsDone = utxoConfig.getMixsDone();
this.message = utxoState.getMessage();
this.error = utxoState.getError();
this.lastActivityElapsed =
whirlpoolUtxo.getLastActivity() != null
? System.currentTimeMillis() - whirlpoolUtxo.getLastActivity()
utxoState.getLastActivity() != null
? System.currentTimeMillis() - utxoState.getLastActivity()
: null;
}
......
package com.samourai.whirlpool.cli.api.protocol.rest;
import com.samourai.whirlpool.cli.api.protocol.beans.ApiUtxo;
import com.samourai.whirlpool.client.wallet.beans.MixOrchestratorState;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolWalletState;
import com.samourai.whirlpool.client.wallet.beans.MixingState;
import java.util.Collection;
import java.util.stream.Collectors;
......@@ -13,14 +12,12 @@ public class ApiWalletStateResponse {
private int nbQueued;
private Collection<ApiUtxo> threads;
public ApiWalletStateResponse(WhirlpoolWalletState whirlpoolWalletState) {
this.started = whirlpoolWalletState.isStarted();
MixOrchestratorState mixState = whirlpoolWalletState.getMixState();
this.nbMixing = mixState.getNbMixing();
this.nbQueued = mixState.getNbQueued();
public ApiWalletStateResponse(MixingState mixingState) {
this.started = mixingState.isStarted();
this.nbMixing = mixingState.getNbMixing();
this.nbQueued = mixingState.getNbQueued();
this.threads =
mixState
mixingState
.getUtxosMixing()
.stream()
.map(whirlpoolUtxo -> new ApiUtxo(whirlpoolUtxo))
......
......@@ -4,6 +4,7 @@ import com.google.common.primitives.Ints;
import com.samourai.whirlpool.cli.api.protocol.beans.ApiWallet;
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxo;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxoState;
import java.util.Comparator;
import java8.lang.Longs;
......@@ -16,14 +17,16 @@ public class ApiWalletUtxosResponse {
Comparator<WhirlpoolUtxo> comparator =
(o1, o2) -> {
// last activity first
if (o1.getLastActivity() != null || o2.getLastActivity() != null) {
if (o1.getLastActivity() != null && o2.getLastActivity() == null) {
WhirlpoolUtxoState s1 = o1.getUtxoState();
WhirlpoolUtxoState s2 = o2.getUtxoState();
if (s1.getLastActivity() != null || s2.getLastActivity() != null) {
if (s1.getLastActivity() != null && s2.getLastActivity() == null) {
return -1;
}
if (o2.getLastActivity() != null && o1.getLastActivity() == null) {
if (s2.getLastActivity() != null && s1.getLastActivity() == null) {
return 1;
}
int compare = Longs.compare(o2.getLastActivity(), o1.getLastActivity());
int compare = Longs.compare(s2.getLastActivity(), s1.getLastActivity());
if (compare != 0) {
return compare;
}
......
......@@ -7,9 +7,8 @@ import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.client.utils.ClientUtils;
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletConfig;
import com.samourai.whirlpool.client.wallet.beans.MixOrchestratorState;
import com.samourai.whirlpool.client.wallet.beans.MixingState;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxo;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolWalletState;
import com.samourai.whirlpool.client.wallet.orchestrator.AbstractOrchestrator;
import java.util.Collection;
import org.slf4j.Logger;
......@@ -30,8 +29,7 @@ public class CliStatusOrchestrator extends AbstractOrchestrator {
@Override
protected void runOrchestrator() {
// log CLI status
printState();
interactive();
}
public void interactive() {
......@@ -67,22 +65,20 @@ public class CliStatusOrchestrator extends AbstractOrchestrator {
private void printState() {
try {
WhirlpoolWallet whirlpoolWallet = cliWalletService.getSessionWallet();
WhirlpoolWalletState whirlpoolWalletState = whirlpoolWallet.getState();
MixOrchestratorState mixState = whirlpoolWalletState.getMixState();
MixingState mixingState = whirlpoolWallet.getMixingState();
WhirlpoolWalletConfig walletConfig = whirlpoolWallet.getConfig();
System.out.print(
System.out.println(
"⣿ Wallet OPENED, mix "
+ (whirlpoolWallet.isStarted() ? "STARTED" : "STOPPED")
+ (mixingState.isStarted() ? "STARTED" : "STOPPED")
+ (walletConfig.isAutoTx0() ? " +autoTx0=" + walletConfig.getAutoTx0PoolId() : "")
+ (walletConfig.isAutoMix() ? " +autoMix" : "")
+ (cliConfig.getTor() ? " +Tor" : "")
+ (cliConfig.isDojoEnabled() ? " +Dojo" : "")
+ ", "
+ mixState.getNbMixing()
+ mixingState.getNbMixing()
+ " mixing, "
+ mixState.getNbQueued()
+ mixingState.getNbQueued()
+ " queued. Commands: [T]hreads, [D]eposit, [P]remix, P[O]stmix\r");
} catch (NoSessionWalletException e) {
System.out.print("⣿ Wallet CLOSED");
......@@ -94,12 +90,11 @@ public class CliStatusOrchestrator extends AbstractOrchestrator {
private void printThreads() {
try {
WhirlpoolWallet whirlpoolWallet = cliWalletService.getSessionWallet();
WhirlpoolWalletState whirlpoolWalletState = whirlpoolWallet.getState();
MixOrchestratorState mixState = whirlpoolWalletState.getMixState();
MixingState mixingState = whirlpoolWallet.getMixingState();
log.info("⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿");
log.info("⣿ THREADS:");
int i = 0;
for (WhirlpoolUtxo whirlpoolUtxo : mixState.getUtxosMixing()) {
for (WhirlpoolUtxo whirlpoolUtxo : mixingState.getUtxosMixing()) {
log.info(
"⣿ Thread #"
+ (i + 1)
......
......@@ -133,8 +133,7 @@ public class CliWalletService extends WhirlpoolWalletService {
cliConfig,
cliConfigService,
walletAggregateService,
cliTorClientService,
this);
cliTorClientService);
return (CliWallet) openWallet(cliWallet);
}
......
......@@ -2,30 +2,26 @@ package com.samourai.whirlpool.cli.wallet;
import com.samourai.wallet.client.Bip84ApiWallet;
import com.samourai.whirlpool.cli.config.CliConfig;
import com.samourai.whirlpool.cli.run.CliStatusOrchestrator;
import com.samourai.whirlpool.cli.services.CliConfigService;
import com.samourai.whirlpool.cli.services.CliTorClientService;
import com.samourai.whirlpool.cli.services.CliWalletService;
import com.samourai.whirlpool.cli.services.WalletAggregateService;
import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.client.WhirlpoolClient;
import com.samourai.whirlpool.client.exception.EmptyWalletException;
import com.samourai.whirlpool.client.exception.NotifiableException;
import com.samourai.whirlpool.client.mix.listener.MixSuccess;
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
import com.samourai.whirlpool.client.wallet.beans.MixProgress;
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxo;
import com.samourai.whirlpool.client.whirlpool.listener.WhirlpoolClientListener;
import io.reactivex.Observable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CliWallet extends WhirlpoolWallet {
private static final Logger log = LoggerFactory.getLogger(CliWallet.class);
private static final int CLI_STATUS_DELAY = 5000;
private CliConfig cliConfig;
private CliConfigService cliConfigService;
private WalletAggregateService walletAggregateService;
private CliStatusOrchestrator cliStatusOrchestrator;
private CliTorClientService cliTorClientService;
public CliWallet(
......@@ -33,17 +29,12 @@ public class CliWallet extends WhirlpoolWallet {
CliConfig cliConfig,
CliConfigService cliConfigService,
WalletAggregateService walletAggregateService,
CliTorClientService cliTorClientService,
CliWalletService cliWalletService) {
CliTorClientService cliTorClientService) {
super(whirlpoolWallet);
this.cliConfig = cliConfig;
this.cliConfigService = cliConfigService;
this.walletAggregateService = walletAggregateService;
this.cliTorClientService = cliTorClientService;
// log status
this.cliStatusOrchestrator =
new CliStatusOrchestrator(CLI_STATUS_DELAY, cliWalletService, cliConfig);
}
@Override
......@@ -54,21 +45,18 @@ public class CliWallet extends WhirlpoolWallet {
}
// start wallet
super.start();
this.cliStatusOrchestrator.start();
}
@Override
public void stop() {
super.stop();
this.cliStatusOrchestrator.stop();
}
@Override
public WhirlpoolClient mix(WhirlpoolUtxo whirlpoolUtxo, WhirlpoolClientListener notifyListener)
throws NotifiableException {
public Observable<MixProgress> mix(WhirlpoolUtxo whirlpoolUtxo) throws NotifiableException {
// get Tor ready before mixing
cliTorClientService.waitReady();
return super.mix(whirlpoolUtxo, notifyListener);
return super.mix(whirlpoolUtxo);
}
@Override
......@@ -156,10 +144,6 @@ public class CliWallet extends WhirlpoolWallet {
CliUtils.notifyError(message);
}
public void interactive() {
cliStatusOrchestrator.interactive();
}
// make public
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment