From 00722129cbffeec4ad926c864dc9dff1c4da5ee3 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Tue, 8 Jan 2019 18:56:04 +0100 Subject: [PATCH] Add validation for trade statistics We got a trade statistics object with price and amount 0. Unclear why that can happen (maybe some dev was debugging things or it was created with the API). We need to add validation when creating and publishing a trade statistics object as well. This is just a fast fix for getting a new release out. Fixes #2222 and #2223 --- .../trade/statistics/TradeStatistics2.java | 4 +++ .../statistics/TradeStatisticsManager.java | 26 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) 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 8e1529ffb07..f2b77c60f2e 100644 --- a/core/src/main/java/bisq/core/trade/statistics/TradeStatistics2.java +++ b/core/src/main/java/bisq/core/trade/statistics/TradeStatistics2.java @@ -263,6 +263,10 @@ public Volume getTradeVolume() { } } + public boolean isValid() { + return tradeAmount > 0 && tradePrice > 0; + } + @Override public String toString() { return "TradeStatistics2{" + diff --git a/core/src/main/java/bisq/core/trade/statistics/TradeStatisticsManager.java b/core/src/main/java/bisq/core/trade/statistics/TradeStatisticsManager.java index f736ce8c7a6..6410032375d 100644 --- a/core/src/main/java/bisq/core/trade/statistics/TradeStatisticsManager.java +++ b/core/src/main/java/bisq/core/trade/statistics/TradeStatisticsManager.java @@ -103,7 +103,9 @@ public void onAllServicesInitialized() { Map map = new HashMap<>(); p2PService.getP2PDataStorage().getAppendOnlyDataStoreMap().values().stream() .filter(e -> e instanceof TradeStatistics2) - .forEach(e -> addToMap((TradeStatistics2) e, map)); + .map(e -> (TradeStatistics2) e) + .filter(TradeStatistics2::isValid) + .forEach(e -> addToMap(e, map)); observableTradeStatisticsSet.addAll(map.values()); priceFeedService.applyLatestBisqMarketPrice(observableTradeStatisticsSet); @@ -149,16 +151,18 @@ public ObservableSet getObservableTradeStatisticsSet() { private void addToMap(TradeStatistics2 tradeStatistics, boolean storeLocally) { if (!observableTradeStatisticsSet.contains(tradeStatistics)) { - boolean itemAlreadyAdded = observableTradeStatisticsSet.stream() - .anyMatch(e -> (e.getOfferId().equals(tradeStatistics.getOfferId()))); - if (!itemAlreadyAdded) { - observableTradeStatisticsSet.add(tradeStatistics); - if (storeLocally) { - priceFeedService.applyLatestBisqMarketPrice(observableTradeStatisticsSet); - dump(); - } - } else { - log.debug("We have already an item with the same offer ID. That might happen if both the maker and the taker published the tradeStatistics"); + + if (observableTradeStatisticsSet.stream() + .anyMatch(e -> (e.getOfferId().equals(tradeStatistics.getOfferId())))) + return; + + if (!tradeStatistics.isValid()) + return; + + observableTradeStatisticsSet.add(tradeStatistics); + if (storeLocally) { + priceFeedService.applyLatestBisqMarketPrice(observableTradeStatisticsSet); + dump(); } } }