From e42fc9a03351802b17f4e61f01ce3be46bd96b5a Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 14 Jul 2022 13:21:46 +0200 Subject: [PATCH] cache the wallet nodes for provided addresses during transaction construction --- drongo | 2 +- .../sparrow/wallet/SendController.java | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/drongo b/drongo index bd01cb87..6d0d5b7f 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit bd01cb87309bcc4a4739b63a6d7548ca1b25910e +Subproject commit 6d0d5b7f62781c15f16d4ce0b6e27d2298122d1e diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java index 78540f34..6f60cb24 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java @@ -10,10 +10,7 @@ import com.sparrowwallet.drongo.address.InvalidAddressException; import com.sparrowwallet.drongo.bip47.PaymentCode; import com.sparrowwallet.drongo.bip47.SecretPoint; import com.sparrowwallet.drongo.crypto.ECKey; -import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.protocol.Sha256Hash; -import com.sparrowwallet.drongo.protocol.Transaction; -import com.sparrowwallet.drongo.protocol.TransactionOutPoint; +import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.psbt.PSBT; import com.sparrowwallet.drongo.wallet.*; import com.sparrowwallet.sparrow.AppServices; @@ -178,6 +175,8 @@ public class SendController extends WalletFormController implements Initializabl private final Set excludedChangeNodes = new HashSet<>(); + private final Map> addressNodeMap = new HashMap<>(); + private final ChangeListener feeListener = new ChangeListener<>() { @Override public void changed(ObservableValue observable, String oldValue, String newValue) { @@ -578,7 +577,9 @@ public class SendController extends WalletFormController implements Initializabl boolean includeMempoolOutputs = Config.get().isIncludeMempoolOutputs(); boolean includeSpentMempoolOutputs = includeSpentMempoolOutputsProperty.get(); - walletTransactionService = new WalletTransactionService(wallet, getUtxoSelectors(payments), getUtxoFilters(), payments, opReturnsList, excludedChangeNodes, feeRate, getMinimumFeeRate(), userFee, currentBlockHeight, groupByAddress, includeMempoolOutputs, includeSpentMempoolOutputs); + walletTransactionService = new WalletTransactionService(addressNodeMap, wallet, getUtxoSelectors(payments), getUtxoFilters(), + payments, opReturnsList, excludedChangeNodes, + feeRate, getMinimumFeeRate(), userFee, currentBlockHeight, groupByAddress, includeMempoolOutputs, includeSpentMempoolOutputs); walletTransactionService.setOnSucceeded(event -> { if(!walletTransactionService.isIgnoreResult()) { walletTransactionProperty.setValue(walletTransactionService.getValue()); @@ -636,6 +637,7 @@ public class SendController extends WalletFormController implements Initializabl } private static class WalletTransactionService extends Service { + private final Map> addressNodeMap; private final Wallet wallet; private final List utxoSelectors; private final List utxoFilters; @@ -651,7 +653,11 @@ public class SendController extends WalletFormController implements Initializabl private final boolean includeSpentMempoolOutputs; private boolean ignoreResult; - public WalletTransactionService(Wallet wallet, List utxoSelectors, List utxoFilters, List payments, List opReturns, Set excludedChangeNodes, double feeRate, double longTermFeeRate, Long fee, Integer currentBlockHeight, boolean groupByAddress, boolean includeMempoolOutputs, boolean includeSpentMempoolOutputs) { + public WalletTransactionService(Map> addressNodeMap, + Wallet wallet, List utxoSelectors, List utxoFilters, + List payments, List opReturns, Set excludedChangeNodes, + double feeRate, double longTermFeeRate, Long fee, Integer currentBlockHeight, boolean groupByAddress, boolean includeMempoolOutputs, boolean includeSpentMempoolOutputs) { + this.addressNodeMap = addressNodeMap; this.wallet = wallet; this.utxoSelectors = utxoSelectors; this.utxoFilters = utxoFilters; @@ -671,7 +677,10 @@ public class SendController extends WalletFormController implements Initializabl protected Task createTask() { return new Task<>() { protected WalletTransaction call() throws InsufficientFundsException { - return wallet.createWalletTransaction(utxoSelectors, utxoFilters, payments, opReturns, excludedChangeNodes, feeRate, longTermFeeRate, fee, currentBlockHeight, groupByAddress, includeMempoolOutputs, includeSpentMempoolOutputs); + WalletTransaction walletTransaction = wallet.createWalletTransaction(utxoSelectors, utxoFilters, payments, opReturns, excludedChangeNodes, + feeRate, longTermFeeRate, fee, currentBlockHeight, groupByAddress, includeMempoolOutputs, includeSpentMempoolOutputs); + walletTransaction.updateAddressNodeMap(addressNodeMap, walletTransaction.getWallet()); + return walletTransaction; } }; } @@ -1078,6 +1087,8 @@ public class SendController extends WalletFormController implements Initializabl whirlpoolProperty.set(null); paymentCodeProperty.set(null); + + addressNodeMap.clear(); } public UtxoSelector getUtxoSelector() { @@ -1155,8 +1166,8 @@ public class SendController extends WalletFormController implements Initializabl WalletTransaction walletTransaction = walletTransactionProperty.get(); Set nodes = new LinkedHashSet<>(walletTransaction.getSelectedUtxos().values()); nodes.addAll(walletTransaction.getChangeMap().keySet()); - List consolidationNodes = walletTransaction.getConsolidationSendNodes(); - nodes.addAll(consolidationNodes); + Map addressNodeMap = walletTransaction.getAddressNodeMap(walletTransaction.getWallet()); + nodes.addAll(addressNodeMap.values().stream().filter(Objects::nonNull).collect(Collectors.toList())); //All wallet nodes applicable to this transaction are stored so when the subscription status for one is updated, the history for all can be fetched in one atomic update walletForm.addWalletTransactionNodes(nodes);