save stored block on event

This commit is contained in:
Craig Raw 2020-06-19 15:49:53 +02:00
parent 44be6e7203
commit b1e8e25837
2 changed files with 21 additions and 6 deletions

View file

@ -0,0 +1,16 @@
package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.drongo.wallet.Wallet;
public class WalletBlockHeightChangedEvent extends WalletChangedEvent {
private Integer blockHeight;
public WalletBlockHeightChangedEvent(Wallet wallet, Integer blockHeight) {
super(wallet);
this.blockHeight = blockHeight;
}
public Integer getBlockHeight() {
return blockHeight;
}
}

View file

@ -6,10 +6,7 @@ import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.AppController;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.ConnectionEvent;
import com.sparrowwallet.sparrow.event.NewBlockEvent;
import com.sparrowwallet.sparrow.event.WalletChangedEvent;
import com.sparrowwallet.sparrow.event.WalletHistoryChangedEvent;
import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.io.ElectrumServer;
import com.sparrowwallet.sparrow.io.Storage;
import javafx.application.Platform;
@ -62,7 +59,7 @@ public class WalletForm {
ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet);
historyService.setOnSucceeded(workerStateEvent -> {
wallet.setStoredBlockHeight(blockHeight);
notifyIfHistoryChanged(previousWallet);
notifyIfChanged(previousWallet, blockHeight);
});
historyService.setOnFailed(workerStateEvent -> {
workerStateEvent.getSource().getException().printStackTrace();
@ -71,13 +68,15 @@ public class WalletForm {
}
}
private void notifyIfHistoryChanged(Wallet previousWallet) {
private void notifyIfChanged(Wallet previousWallet, Integer blockHeight) {
List<WalletNode> historyChangedNodes = new ArrayList<>();
historyChangedNodes.addAll(getHistoryChangedNodes(previousWallet.getNode(KeyPurpose.RECEIVE).getChildren(), wallet.getNode(KeyPurpose.RECEIVE).getChildren()));
historyChangedNodes.addAll(getHistoryChangedNodes(previousWallet.getNode(KeyPurpose.CHANGE).getChildren(), wallet.getNode(KeyPurpose.CHANGE).getChildren()));
if(!historyChangedNodes.isEmpty()) {
Platform.runLater(() -> EventManager.get().post(new WalletHistoryChangedEvent(wallet, historyChangedNodes)));
} else if(blockHeight != null && !blockHeight.equals(previousWallet.getStoredBlockHeight())) {
Platform.runLater(() -> EventManager.get().post(new WalletBlockHeightChangedEvent(wallet, blockHeight)));
}
}