From bee93bf45fa659b3d5db9ca9d7284284f7797a56 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 18 May 2024 09:48:22 -0400 Subject: [PATCH] reset internal state and popup warning if main wallet swapped --- .../java/haveno/core/app/HavenoSetup.java | 6 ++++++ .../core/xmr/model/XmrAddressEntryList.java | 5 +++++ .../core/xmr/wallet/XmrWalletService.java | 20 +++++++++++++++++++ .../java/haveno/desktop/main/MainView.java | 7 +++++++ .../haveno/desktop/main/MainViewModel.java | 4 ++++ .../openoffer/OpenOffersDataModel.java | 2 +- 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/haveno/core/app/HavenoSetup.java b/core/src/main/java/haveno/core/app/HavenoSetup.java index e90d25df..799d688c 100644 --- a/core/src/main/java/haveno/core/app/HavenoSetup.java +++ b/core/src/main/java/haveno/core/app/HavenoSetup.java @@ -97,6 +97,7 @@ import javafx.beans.property.BooleanProperty; import javafx.beans.property.DoubleProperty; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.collections.SetChangeListener; @@ -139,6 +140,7 @@ public class HavenoSetup { private final MediationManager mediationManager; private final RefundManager refundManager; private final ArbitrationManager arbitrationManager; + private final StringProperty topErrorMsg = new SimpleStringProperty(); @Setter @Nullable private Consumer displayTacHandler; @@ -717,6 +719,10 @@ public class HavenoSetup { return walletAppSetup.getWalletServiceErrorMsg(); } + public StringProperty getTopErrorMsg() { + return topErrorMsg; + } + public StringProperty getXmrSplashSyncIconId() { return walletAppSetup.getXmrSplashSyncIconId(); } diff --git a/core/src/main/java/haveno/core/xmr/model/XmrAddressEntryList.java b/core/src/main/java/haveno/core/xmr/model/XmrAddressEntryList.java index 1796b7d1..73f7379d 100644 --- a/core/src/main/java/haveno/core/xmr/model/XmrAddressEntryList.java +++ b/core/src/main/java/haveno/core/xmr/model/XmrAddressEntryList.java @@ -140,6 +140,11 @@ public final class XmrAddressEntryList implements PersistableEnvelope, Persisted return newAddressEntry; } + public void clear() { + entrySet.clear(); + requestPersistence(); + } + public void requestPersistence() { persistenceManager.requestPersistence(); } diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index 933b2590..3ecd75da 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1372,6 +1372,9 @@ public class XmrWalletService { // reapply connection after wallet synced onConnectionChanged(xmrConnectionService.getConnection()); + // reset internal state if main wallet was swapped + resetIfWalletChanged(); + // signal that main wallet is synced doneDownload(); @@ -1409,6 +1412,23 @@ public class XmrWalletService { } } + private void resetIfWalletChanged() { + getAddressEntryListAsImmutableList(); // TODO: using getter to create base address if necessary + List baseAddresses = getAddressEntries(XmrAddressEntry.Context.BASE_ADDRESS); + if (baseAddresses.size() > 1 || (baseAddresses.size() == 1 && !baseAddresses.get(0).getAddressString().equals(wallet.getPrimaryAddress()))) { + String warningMsg = "New Monero wallet detected. Resetting internal state."; + if (!tradeManager.getOpenTrades().isEmpty()) warningMsg += "\n\nWARNING: Your open trades will settle to the payout address in the OLD wallet!"; // TODO: allow payout address to be updated in PaymentSentMessage, PaymentReceivedMessage, and DisputeOpenedMessage? + HavenoUtils.havenoSetup.getTopErrorMsg().set(warningMsg); + + // reset address entries + xmrAddressEntryList.clear(); + getAddressEntryListAsImmutableList(); // recreate base address + + // cancel offers + tradeManager.getOpenOfferManager().removeAllOpenOffers(null); + } + } + private void syncWithProgress() { // show sync progress diff --git a/desktop/src/main/java/haveno/desktop/main/MainView.java b/desktop/src/main/java/haveno/desktop/main/MainView.java index 07a26486..5466f0f7 100644 --- a/desktop/src/main/java/haveno/desktop/main/MainView.java +++ b/desktop/src/main/java/haveno/desktop/main/MainView.java @@ -678,6 +678,13 @@ public class MainView extends InitializableView { } }); + model.getTopErrorMsg().addListener((ov, oldValue, newValue) -> { + log.warn("top level warning has been set! " + newValue); + if (newValue != null) { + new Popup().warning(newValue).show(); + } + }); + // temporarily disabled due to high CPU usage (per issue #4649) //model.getCombinedSyncProgress().addListener((ov, oldValue, newValue) -> { // if ((double) newValue >= 1) { diff --git a/desktop/src/main/java/haveno/desktop/main/MainViewModel.java b/desktop/src/main/java/haveno/desktop/main/MainViewModel.java index 6d2496fa..f2a7ab13 100644 --- a/desktop/src/main/java/haveno/desktop/main/MainViewModel.java +++ b/desktop/src/main/java/haveno/desktop/main/MainViewModel.java @@ -641,6 +641,10 @@ public class MainViewModel implements ViewModel, HavenoSetup.HavenoSetupListener return havenoSetup.getWalletServiceErrorMsg(); } + StringProperty getTopErrorMsg() { + return havenoSetup.getTopErrorMsg(); + } + StringProperty getXmrSplashSyncIconId() { return havenoSetup.getXmrSplashSyncIconId(); } diff --git a/desktop/src/main/java/haveno/desktop/main/portfolio/openoffer/OpenOffersDataModel.java b/desktop/src/main/java/haveno/desktop/main/portfolio/openoffer/OpenOffersDataModel.java index 99bd3541..14dccd6b 100644 --- a/desktop/src/main/java/haveno/desktop/main/portfolio/openoffer/OpenOffersDataModel.java +++ b/desktop/src/main/java/haveno/desktop/main/portfolio/openoffer/OpenOffersDataModel.java @@ -88,7 +88,7 @@ class OpenOffersDataModel extends ActivatableDataModel { private void applyList() { list.clear(); - list.addAll(openOfferManager.getObservableList().stream().map(OpenOfferListItem::new).collect(Collectors.toList())); + list.addAll(openOfferManager.getOpenOffers().stream().map(OpenOfferListItem::new).collect(Collectors.toList())); // we sort by date, earliest first list.sort((o1, o2) -> o2.getOffer().getDate().compareTo(o1.getOffer().getDate()));