From 1e70c70579bc814a9ec5e1cf5e534414e8d730a8 Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 23 Jul 2024 08:05:41 -0400 Subject: [PATCH] fix publishing duplicate trades after randomization --- core/src/main/java/haveno/core/trade/Trade.java | 2 +- .../core/trade/statistics/TradeStatistics3.java | 7 ++++--- .../trade/statistics/TradeStatisticsManager.java | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index 0f629136..6f1bcf46 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -2299,7 +2299,7 @@ public abstract class Trade implements Tradable, Model { private void doPublishTradeStatistics() { String referralId = processModel.getReferralIdService().getOptionalReferralId().orElse(null); boolean isTorNetworkNode = getProcessModel().getP2PService().getNetworkNode() instanceof TorNetworkNode; - TradeStatistics3 tradeStatistics = TradeStatistics3.from(this, referralId, isTorNetworkNode); + TradeStatistics3 tradeStatistics = TradeStatistics3.from(this, referralId, isTorNetworkNode, true); if (tradeStatistics.isValid()) { log.info("Publishing trade statistics for {} {}", getClass().getSimpleName(), getId()); processModel.getP2PService().addPersistableNetworkPayload(tradeStatistics, true); diff --git a/core/src/main/java/haveno/core/trade/statistics/TradeStatistics3.java b/core/src/main/java/haveno/core/trade/statistics/TradeStatistics3.java index 37bb9d7b..c60903c9 100644 --- a/core/src/main/java/haveno/core/trade/statistics/TradeStatistics3.java +++ b/core/src/main/java/haveno/core/trade/statistics/TradeStatistics3.java @@ -72,7 +72,8 @@ public final class TradeStatistics3 implements ProcessOncePersistableNetworkPayl public static TradeStatistics3 from(Trade trade, @Nullable String referralId, - boolean isTorNetworkNode) { + boolean isTorNetworkNode, + boolean isFuzzed) { Map extraDataMap = new HashMap<>(); if (referralId != null) { extraDataMap.put(OfferPayload.REFERRAL_ID, referralId); @@ -90,9 +91,9 @@ public final class TradeStatistics3 implements ProcessOncePersistableNetworkPayl Offer offer = checkNotNull(trade.getOffer()); return new TradeStatistics3(offer.getCurrencyCode(), trade.getPrice().getValue(), - fuzzTradeAmountReproducibly(trade), + isFuzzed ? fuzzTradeAmountReproducibly(trade) : trade.getAmount().longValueExact(), offer.getPaymentMethod().getId(), - fuzzTradeDateReproducibly(trade), + isFuzzed ? fuzzTradeDateReproducibly(trade) : trade.getTakeOfferDate().getTime(), truncatedArbitratorNodeAddress, extraDataMap); } diff --git a/core/src/main/java/haveno/core/trade/statistics/TradeStatisticsManager.java b/core/src/main/java/haveno/core/trade/statistics/TradeStatisticsManager.java index 34504f38..4bbd3153 100644 --- a/core/src/main/java/haveno/core/trade/statistics/TradeStatisticsManager.java +++ b/core/src/main/java/haveno/core/trade/statistics/TradeStatisticsManager.java @@ -206,13 +206,23 @@ public class TradeStatisticsManager { TradeStatistics3 tradeStatistics3 = null; try { - tradeStatistics3 = TradeStatistics3.from(trade, referralId, isTorNetworkNode); + tradeStatistics3 = TradeStatistics3.from(trade, referralId, isTorNetworkNode, false); } catch (Exception e) { log.warn("Error getting trade statistic for {} {}: {}", trade.getClass().getName(), trade.getId(), e.getMessage()); return; } + + TradeStatistics3 tradeStatistics3Fuzzed = null; + try { + tradeStatistics3Fuzzed = TradeStatistics3.from(trade, referralId, isTorNetworkNode, true); + } catch (Exception e) { + log.warn("Error getting trade statistic for {} {}: {}", trade.getClass().getName(), trade.getId(), e.getMessage()); + return; + } + boolean hasTradeStatistics3 = hashes.contains(new P2PDataStorage.ByteArray(tradeStatistics3.getHash())); - if (hasTradeStatistics3) { + boolean hasTradeStatistics3Fuzzed = hashes.contains(new P2PDataStorage.ByteArray(tradeStatistics3Fuzzed.getHash())); + if (hasTradeStatistics3 || hasTradeStatistics3Fuzzed) { log.debug("Trade: {}. We have already a tradeStatistics matching the hash of tradeStatistics3.", trade.getShortId()); return;