From 79021dc9c6294481458742c26956c169cf7bd7bb Mon Sep 17 00:00:00 2001 From: woodser Date: Sun, 2 Oct 2022 17:04:21 -0400 Subject: [PATCH] revert getters for peer and arbitrator node addresses to avoid NPEs --- core/src/main/java/bisq/core/api/model/TradeInfo.java | 8 ++++---- core/src/main/java/bisq/core/btc/Balances.java | 2 +- .../java/bisq/core/trade/ClosedTradableManager.java | 6 ++---- core/src/main/java/bisq/core/trade/Tradable.java | 2 +- core/src/main/java/bisq/core/trade/Trade.java | 10 +++++++++- core/src/main/java/bisq/core/trade/TradeManager.java | 1 - .../bisq/core/trade/statistics/TradeStatistics2.java | 2 +- .../main/overlays/windows/TradeDetailsWindow.java | 8 ++++---- .../main/portfolio/closedtrades/ClosedTradesView.java | 2 +- .../portfolio/pendingtrades/PendingTradesView.java | 2 +- .../pendingtrades/PendingTradesViewModel.java | 6 +++--- 11 files changed, 27 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/bisq/core/api/model/TradeInfo.java b/core/src/main/java/bisq/core/api/model/TradeInfo.java index 74a69cfb95..17e25daee7 100644 --- a/core/src/main/java/bisq/core/api/model/TradeInfo.java +++ b/core/src/main/java/bisq/core/api/model/TradeInfo.java @@ -42,14 +42,14 @@ public class TradeInfo implements Payload { // view and interact with trades. private static final Function toPeerNodeAddress = (trade) -> - trade.getTradingPeer() == null || trade.getTradingPeer().getNodeAddress() == null + trade.getTradingPeerNodeAddress() == null ? "" - : trade.getTradingPeer().getNodeAddress().getFullAddress(); + : trade.getTradingPeerNodeAddress().getFullAddress(); private static final Function toArbitratorNodeAddress = (trade) -> - trade.getArbitrator() == null || trade.getArbitrator().getNodeAddress() == null + trade.getArbitratorNodeAddress() == null ? "" - : trade.getArbitrator().getNodeAddress().getFullAddress(); + : trade.getArbitratorNodeAddress().getFullAddress(); private static final Function toRoundedVolume = (trade) -> trade.getVolume() == null diff --git a/core/src/main/java/bisq/core/btc/Balances.java b/core/src/main/java/bisq/core/btc/Balances.java index 21c1a329e9..d22c0a59d5 100644 --- a/core/src/main/java/bisq/core/btc/Balances.java +++ b/core/src/main/java/bisq/core/btc/Balances.java @@ -139,7 +139,7 @@ public class Balances { if (trade.getContract() == null) continue; Long reservedAmt; OfferPayload offerPayload = trade.getContract().getOfferPayload(); - if (trade.getArbitrator().getNodeAddress().equals(P2PService.getMyNodeAddress())) { // TODO (woodser): this only works if node address does not change + if (trade.getArbitratorNodeAddress().equals(P2PService.getMyNodeAddress())) { // TODO (woodser): this only works if node address does not change reservedAmt = offerPayload.getAmount() + offerPayload.getBuyerSecurityDeposit() + offerPayload.getSellerSecurityDeposit(); // arbitrator reserved balance is sum of amounts sent to multisig } else { reservedAmt = trade.getContract().isMyRoleBuyer(tradeManager.getKeyRing().getPubKeyRing()) ? offerPayload.getBuyerSecurityDeposit() : offerPayload.getAmount() + offerPayload.getSellerSecurityDeposit(); diff --git a/core/src/main/java/bisq/core/trade/ClosedTradableManager.java b/core/src/main/java/bisq/core/trade/ClosedTradableManager.java index 35ded1c2ef..3eed4b4c7a 100644 --- a/core/src/main/java/bisq/core/trade/ClosedTradableManager.java +++ b/core/src/main/java/bisq/core/trade/ClosedTradableManager.java @@ -22,7 +22,6 @@ import bisq.core.monetary.Volume; import bisq.core.offer.Offer; import bisq.core.offer.OpenOffer; import bisq.core.provider.price.PriceFeedService; -import bisq.core.trade.protocol.TradingPeer; import bisq.core.trade.statistics.TradeStatisticsManager; import bisq.core.user.Preferences; @@ -192,10 +191,9 @@ public class ClosedTradableManager implements PersistedDataHost { if (isOpenOffer(tradable)) { return 0; } - NodeAddress addressInTrade = castToTradeModel(tradable).getTradingPeer().getNodeAddress(); + NodeAddress addressInTrade = castToTradeModel(tradable).getTradingPeerNodeAddress(); return (int) getTradeModelStream() - .map(Trade::getTradingPeer) - .map(TradingPeer::getNodeAddress) + .map(Trade::getTradingPeerNodeAddress) .filter(Objects::nonNull) .filter(address -> address.equals(addressInTrade)) .count(); diff --git a/core/src/main/java/bisq/core/trade/Tradable.java b/core/src/main/java/bisq/core/trade/Tradable.java index 771362ba85..32186be469 100644 --- a/core/src/main/java/bisq/core/trade/Tradable.java +++ b/core/src/main/java/bisq/core/trade/Tradable.java @@ -76,6 +76,6 @@ public interface Tradable extends PersistablePayload { } default Optional getOptionalTradingPeerNodeAddress() { - return asTradeModel().map(Trade::getTradingPeer).map(TradingPeer::getNodeAddress); + return asTradeModel().map(Trade::getTradingPeerNodeAddress); } } diff --git a/core/src/main/java/bisq/core/trade/Trade.java b/core/src/main/java/bisq/core/trade/Trade.java index 8fb9defeb2..f5608a3a78 100644 --- a/core/src/main/java/bisq/core/trade/Trade.java +++ b/core/src/main/java/bisq/core/trade/Trade.java @@ -600,7 +600,7 @@ public abstract class Trade implements Tradable, Model { } public void initialize(ProcessModelServiceProvider serviceProvider) { - serviceProvider.getArbitratorManager().getDisputeAgentByNodeAddress(getArbitrator().getNodeAddress()).ifPresent(arbitrator -> { + serviceProvider.getArbitratorManager().getDisputeAgentByNodeAddress(getArbitratorNodeAddress()).ifPresent(arbitrator -> { getArbitrator().setPubKeyRing(arbitrator.getPubKeyRing()); }); @@ -616,6 +616,14 @@ public abstract class Trade implements Tradable, Model { getSelf().setNodeAddress(P2PService.getMyNodeAddress()); } + public NodeAddress getTradingPeerNodeAddress() { + return getTradingPeer() == null ? null : getTradingPeer().getNodeAddress(); + } + + public NodeAddress getArbitratorNodeAddress() { + return getArbitrator() == null ? null : getArbitrator().getNodeAddress(); + } + /** * Create a contract based on the current state. * diff --git a/core/src/main/java/bisq/core/trade/TradeManager.java b/core/src/main/java/bisq/core/trade/TradeManager.java index 793613f811..82265a4475 100644 --- a/core/src/main/java/bisq/core/trade/TradeManager.java +++ b/core/src/main/java/bisq/core/trade/TradeManager.java @@ -837,7 +837,6 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi // If trade was completed (closed without fault but might be closed by a dispute) we move it to the closed trades public void onTradeCompleted(Trade trade) { - if (trade.getState() == Trade.State.WITHDRAW_COMPLETED) return; closedTradableManager.add(trade); trade.setState(Trade.State.WITHDRAW_COMPLETED); maybeRemoveTrade(trade); diff --git a/core/src/main/java/bisq/core/trade/statistics/TradeStatistics2.java b/core/src/main/java/bisq/core/trade/statistics/TradeStatistics2.java index 6ca6f6f17f..8ab7915c44 100644 --- a/core/src/main/java/bisq/core/trade/statistics/TradeStatistics2.java +++ b/core/src/main/java/bisq/core/trade/statistics/TradeStatistics2.java @@ -81,7 +81,7 @@ public final class TradeStatistics2 implements ProcessOncePersistableNetworkPayl extraDataMap.put(OfferPayload.REFERRAL_ID, referralId); } - NodeAddress arbitratorNodeAddress = trade.getArbitrator().getNodeAddress(); + NodeAddress arbitratorNodeAddress = trade.getArbitratorNodeAddress(); if (arbitratorNodeAddress != null) { // The first 4 chars are sufficient to identify a arbitrator. // For testing with regtest/localhost we use the full address as its localhost and would result in diff --git a/desktop/src/main/java/bisq/desktop/main/overlays/windows/TradeDetailsWindow.java b/desktop/src/main/java/bisq/desktop/main/overlays/windows/TradeDetailsWindow.java index 9d0c63923b..643ad89c32 100644 --- a/desktop/src/main/java/bisq/desktop/main/overlays/windows/TradeDetailsWindow.java +++ b/desktop/src/main/java/bisq/desktop/main/overlays/windows/TradeDetailsWindow.java @@ -208,7 +208,7 @@ public class TradeDetailsWindow extends Overlay { rows++; if (trade.hasFailed()) rows += 2; - if (trade.getTradingPeer().getNodeAddress() != null) + if (trade.getTradingPeerNodeAddress() != null) rows++; if (showXmrProofResult) rows++; @@ -232,16 +232,16 @@ public class TradeDetailsWindow extends Overlay { Res.get("shared.takerTxFee", formatter.formatCoinWithCode(trade.getTxFee().multiply(3))); addConfirmationLabelTextField(gridPane, ++rowIndex, Res.get("tradeDetailsWindow.txFee"), txFee); - NodeAddress arbitratorNodeAddress = trade.getArbitrator().getNodeAddress(); + NodeAddress arbitratorNodeAddress = trade.getArbitratorNodeAddress(); if (arbitratorNodeAddress != null) { addConfirmationLabelTextField(gridPane, ++rowIndex, Res.get("tradeDetailsWindow.agentAddresses"), arbitratorNodeAddress.getFullAddress()); } - if (trade.getTradingPeer().getNodeAddress() != null) + if (trade.getTradingPeerNodeAddress() != null) addConfirmationLabelTextField(gridPane, ++rowIndex, Res.get("tradeDetailsWindow.tradingPeersOnion"), - trade.getTradingPeer().getNodeAddress().getFullAddress()); + trade.getTradingPeerNodeAddress().getFullAddress()); if (showXmrProofResult) { // As the window is already overloaded we replace the tradingPeersPubKeyHash field with the auto-conf state diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java b/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java index 070be3a55b..d36beb5ee3 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/closedtrades/ClosedTradesView.java @@ -501,7 +501,7 @@ public class ClosedTradesView extends ActivatableViewAndModel { if (e instanceof Trade) { Trade t = (Trade) e; - return t.getTradingPeer().getNodeAddress() != null && - trade.getTradingPeer().getNodeAddress() != null && - t.getTradingPeer().getNodeAddress().getFullAddress().equals(trade.getTradingPeer().getNodeAddress().getFullAddress()); + return t.getTradingPeerNodeAddress() != null && + trade.getTradingPeerNodeAddress() != null && + t.getTradingPeerNodeAddress().getFullAddress().equals(trade.getTradingPeerNodeAddress().getFullAddress()); } else return false;