From 0d9e798bb7322ba79971a67e75068bfe9c285772 Mon Sep 17 00:00:00 2001 From: zeroleak Date: Tue, 19 Mar 2024 15:01:15 +0100 Subject: [PATCH] upgrade whirlpool-client 1.0.0-beta23 --- build.gradle | 4 +-- .../sparrow/net/HttpClientService.java | 3 +- .../sparrow/whirlpool/Whirlpool.java | 36 +++++++++++++------ .../SparrowUtxoConfigPersister.java | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 49da4d7f..a93ebeb1 100644 --- a/build.gradle +++ b/build.gradle @@ -124,8 +124,8 @@ dependencies { exclude group: 'org.slf4j' } implementation('com.sparrowwallet.bokmakierie:bokmakierie:1.0') - implementation('io.samourai.code.whirlpool:whirlpool-client:1.0.0-beta13') - implementation('io.samourai.code.wallet:java-http-client:2.0.0-beta4') + implementation('io.samourai.code.whirlpool:whirlpool-client:1.0.0-beta23') + implementation('io.samourai.code.wallet:java-http-client:2.0.0-beta8') implementation('io.reactivex.rxjava2:rxjava:2.2.15') implementation('io.reactivex.rxjava2:rxjavafx:2.2.2') implementation('org.apache.commons:commons-lang3:3.7') diff --git a/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java b/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java index 0eb37ef6..1eed8425 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java @@ -18,7 +18,7 @@ public class HttpClientService extends JettyHttpClientService { private static final int REQUEST_TIMEOUT = 120000; public HttpClientService(HostAndPort torProxy) { - super(REQUEST_TIMEOUT, ClientUtils.USER_AGENT, new HttpProxySupplier(torProxy)); + super(REQUEST_TIMEOUT, new HttpProxySupplier(torProxy)); } public T requestJson(String url, Class responseType, Map headers) throws Exception { @@ -60,7 +60,6 @@ public class HttpClientService extends JettyHttpClientService { protected Task createTask() { return new Task<>() { protected Boolean call() throws Exception { - ThreadUtil.getInstance().getExecutorService().shutdown(); httpClientService.stop(); return true; } diff --git a/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java b/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java index 6ec2e5dd..bfc7d20e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java +++ b/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java @@ -4,6 +4,7 @@ import com.google.common.eventbus.Subscribe; import com.samourai.soroban.client.SorobanConfig; import com.samourai.wallet.api.backend.beans.UnspentOutput; import com.samourai.wallet.bipFormat.BIP_FORMAT; +import com.samourai.wallet.bipWallet.WalletSupplier; import com.samourai.wallet.constants.BIP_WALLETS; import com.samourai.wallet.constants.SamouraiAccount; import com.samourai.wallet.constants.SamouraiNetwork; @@ -15,7 +16,7 @@ import com.samourai.whirlpool.client.event.*; import com.samourai.whirlpool.client.mix.handler.IPostmixHandler; import com.samourai.whirlpool.client.tx0.Tx0; import com.samourai.whirlpool.client.tx0.Tx0Config; -import com.samourai.whirlpool.client.tx0.Tx0PreviewService; +import com.samourai.whirlpool.client.tx0.Tx0Info; import com.samourai.whirlpool.client.tx0.Tx0Previews; import com.samourai.whirlpool.client.wallet.WhirlpoolEventService; import com.samourai.whirlpool.client.wallet.WhirlpoolWallet; @@ -45,6 +46,7 @@ import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowChainSupplier; import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowDataSource; import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowMinerFeeSupplier; import com.sparrowwallet.sparrow.whirlpool.dataSource.SparrowPostmixHandler; +import io.reactivex.Single; import io.reactivex.rxjavafx.schedulers.JavaFxScheduler; import io.reactivex.schedulers.Schedulers; import javafx.application.Platform; @@ -129,12 +131,17 @@ public class Whirlpool { } public Tx0Previews getTx0Previews(Collection utxos) throws Exception { + // TODO keep tx0Info for reusing it later on another TX0 preview or TX0 broadcast + // it should be refreshed after each successful TX0 broadcast + Tx0Info tx0Info = fetchTx0Info(); + // preview all pools - Tx0Config tx0Config = computeTx0Config(); - return AsyncUtil.getInstance().blockingGet(getWhirlpoolInfo().tx0Previews(tx0Config, utxos)); + Tx0Config tx0Config = computeTx0Config(tx0Info); + return tx0Info.tx0Previews(tx0Config, utxos); } public Tx0 broadcastTx0(Pool pool, Collection utxos) throws Exception { + // TODO no need to instanciate WhirlpoolWallet, you just need walletSupplier & utxoSupplier WhirlpoolWallet whirlpoolWallet = getWhirlpoolWallet(); whirlpoolWallet.startAsync().subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform()); UtxoSupplier utxoSupplier = whirlpoolWallet.getUtxoSupplier(); @@ -144,15 +151,24 @@ public class Whirlpool { throw new IllegalStateException("Failed to find UTXOs in Whirlpool wallet"); } - Tx0Config tx0Config = computeTx0Config(); - return whirlpoolWallet.tx0(whirlpoolUtxos, pool, tx0Config); + // TODO reuse tx0Info from TX0 preview to speed up the process + Tx0Info tx0Info = fetchTx0Info(); + + WalletSupplier walletSupplier = whirlpoolWallet.getWalletSupplier(); + Tx0Config tx0Config = computeTx0Config(tx0Info); + return tx0Info.tx0(walletSupplier, utxoSupplier, whirlpoolUtxos, pool, tx0Config); } - private Tx0Config computeTx0Config() { - CoordinatorSupplier coordinatorSupplier = getWhirlpoolInfo().getCoordinatorSupplier(); - Tx0PreviewService tx0PreviewService = getWhirlpoolInfo().getTx0PreviewService(); - Collection pools = coordinatorSupplier.getPools(); - return new Tx0Config(tx0PreviewService, pools, tx0FeeTarget, mixFeeTarget, SamouraiAccount.BADBANK); + private Tx0Info fetchTx0Info() throws Exception { + return AsyncUtil.getInstance().blockingGet( + Single.fromCallable(() -> getWhirlpoolInfo().fetchTx0Info(getScode())) + .subscribeOn(Schedulers.io()).observeOn(JavaFxScheduler.platform())); + } + + private Tx0Config computeTx0Config(Tx0Info tx0Info) { + Tx0Config tx0Config = tx0Info.getTx0Config(tx0FeeTarget, mixFeeTarget); + tx0Config.setChangeWallet(SamouraiAccount.BADBANK); + return tx0Config; } public void setHDWallet(String walletId, Wallet wallet) { diff --git a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataPersister/SparrowUtxoConfigPersister.java b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataPersister/SparrowUtxoConfigPersister.java index 050e461c..8458abfe 100644 --- a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataPersister/SparrowUtxoConfigPersister.java +++ b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataPersister/SparrowUtxoConfigPersister.java @@ -37,7 +37,7 @@ public class SparrowUtxoConfigPersister extends UtxoConfigPersisterFile { } Map utxoConfigs = wallet.getUtxoMixes().entrySet().stream() - .collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> new UtxoConfigPersisted(entry.getValue().getMixesDone(), entry.getValue().getExpired(), false, null), + .collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> new UtxoConfigPersisted(entry.getValue().getMixesDone(), entry.getValue().getExpired()), (u, v) -> { throw new IllegalStateException("Duplicate utxo config hashes"); }, ConcurrentHashMap::new));