buffer nodehistorychangedevents to avoid multiple simultaneous history refreshes

This commit is contained in:
Craig Raw 2022-03-03 12:45:44 +02:00
parent 416fc83b4d
commit 81810fced5
2 changed files with 18 additions and 3 deletions

View file

@ -113,7 +113,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
BlockTransactionHashIndex ref = walletTxos.get(new HashIndex(txInput.getOutpoint().getHash(), txInput.getOutpoint().getIndex())); BlockTransactionHashIndex ref = walletTxos.get(new HashIndex(txInput.getOutpoint().getHash(), txInput.getOutpoint().getIndex()));
if(ref != null) { if(ref != null) {
validEntries++; validEntries++;
if(getChildren().stream().noneMatch(entry -> ((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); log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing child for input " + ref.getSpentBy() + " on output " + ref);
return false; return false;
} }
@ -123,7 +123,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
BlockTransactionHashIndex ref = walletTxos.get(new HashIndex(txOutput.getHash(), txOutput.getIndex())); BlockTransactionHashIndex ref = walletTxos.get(new HashIndex(txOutput.getHash(), txOutput.getIndex()));
if(ref != null) { if(ref != null) {
validEntries++; validEntries++;
if(getChildren().stream().noneMatch(entry -> ((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); log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing child for output " + ref);
return false; return false;
} }

View file

@ -12,6 +12,8 @@ import com.sparrowwallet.sparrow.io.StorageException;
import com.sparrowwallet.sparrow.net.AllHistoryChangedException; import com.sparrowwallet.sparrow.net.AllHistoryChangedException;
import com.sparrowwallet.sparrow.net.ElectrumServer; import com.sparrowwallet.sparrow.net.ElectrumServer;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
import io.reactivex.rxjavafx.schedulers.JavaFxScheduler;
import io.reactivex.subjects.PublishSubject;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
@ -24,6 +26,7 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString; import static com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString;
@ -34,6 +37,8 @@ public class WalletForm {
private final Storage storage; private final Storage storage;
protected Wallet wallet; protected Wallet wallet;
private final PublishSubject<WalletNode> refreshNodesSubject;
private final List<WalletForm> nestedWalletForms = new ArrayList<>(); private final List<WalletForm> nestedWalletForms = new ArrayList<>();
private WalletTransactionsEntry walletTransactionsEntry; private WalletTransactionsEntry walletTransactionsEntry;
@ -54,6 +59,16 @@ public class WalletForm {
this.storage = storage; this.storage = storage;
this.wallet = currentWallet; 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()) { if(refreshHistory && wallet.isValid()) {
refreshHistory(AppServices.getCurrentBlockHeight()); refreshHistory(AppServices.getCurrentBlockHeight());
} }
@ -437,7 +452,7 @@ public class WalletForm {
WalletNode walletNode = event.getWalletNode(wallet); WalletNode walletNode = event.getWalletNode(wallet);
if(walletNode != null) { if(walletNode != null) {
log.debug(wallet.getFullName() + " history event for node " + walletNode + " (" + event.getScriptHash() + ")"); log.debug(wallet.getFullName() + " history event for node " + walletNode + " (" + event.getScriptHash() + ")");
refreshHistory(AppServices.getCurrentBlockHeight(), Set.of(walletNode)); refreshNodesSubject.onNext(walletNode);
} }
} }
} }