From 81810fced5798becf29a3f0fc57fda5475c65c93 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 3 Mar 2022 12:45:44 +0200 Subject: [PATCH] buffer nodehistorychangedevents to avoid multiple simultaneous history refreshes --- .../sparrow/wallet/TransactionEntry.java | 4 ++-- .../sparrow/wallet/WalletForm.java | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionEntry.java b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionEntry.java index ecfb2a34..7ee0e4f4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionEntry.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionEntry.java @@ -113,7 +113,7 @@ public class TransactionEntry extends Entry implements Comparable ((HashIndexEntry)entry).getHashIndex().equals(ref.getSpentBy()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.INPUT))) { + if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().toString().equals(ref.getSpentBy().toString()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.INPUT))) { log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing child for input " + ref.getSpentBy() + " on output " + ref); return false; } @@ -123,7 +123,7 @@ public class TransactionEntry extends Entry implements Comparable ((HashIndexEntry)entry).getHashIndex().equals(ref) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.OUTPUT))) { + if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().toString().equals(ref.toString()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.OUTPUT))) { log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing child for output " + ref); return false; } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index fba748d3..94dde6ac 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -12,6 +12,8 @@ import com.sparrowwallet.sparrow.io.StorageException; import com.sparrowwallet.sparrow.net.AllHistoryChangedException; import com.sparrowwallet.sparrow.net.ElectrumServer; import com.sparrowwallet.sparrow.io.Storage; +import io.reactivex.rxjavafx.schedulers.JavaFxScheduler; +import io.reactivex.subjects.PublishSubject; import javafx.application.Platform; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; @@ -24,6 +26,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import static com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString; @@ -34,6 +37,8 @@ public class WalletForm { private final Storage storage; protected Wallet wallet; + private final PublishSubject refreshNodesSubject; + private final List nestedWalletForms = new ArrayList<>(); private WalletTransactionsEntry walletTransactionsEntry; @@ -54,6 +59,16 @@ public class WalletForm { this.storage = storage; this.wallet = currentWallet; + refreshNodesSubject = PublishSubject.create(); + refreshNodesSubject.buffer(1, TimeUnit.SECONDS) + .filter(walletNodes -> !walletNodes.isEmpty()) + .observeOn(JavaFxScheduler.platform()) + .subscribe(walletNodes -> { + refreshHistory(AppServices.getCurrentBlockHeight(), new HashSet<>(walletNodes)); + }, exception -> { + log.error("Error refreshing nodes", exception); + }); + if(refreshHistory && wallet.isValid()) { refreshHistory(AppServices.getCurrentBlockHeight()); } @@ -437,7 +452,7 @@ public class WalletForm { WalletNode walletNode = event.getWalletNode(wallet); if(walletNode != null) { log.debug(wallet.getFullName() + " history event for node " + walletNode + " (" + event.getScriptHash() + ")"); - refreshHistory(AppServices.getCurrentBlockHeight(), Set.of(walletNode)); + refreshNodesSubject.onNext(walletNode); } } }