From b2665bdd2cf73659e41fa763f05abb19654560d8 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Mon, 5 Oct 2020 17:43:35 -0500 Subject: [PATCH] let seller publish trade statistics only if peer is updated user. If not the peer will publish only. --- .../SellerPublishesTradeStatistics.java | 70 +++++++++++++------ .../java/bisq/network/p2p/P2PService.java | 12 ++++ 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/bisq/core/trade/protocol/tasks/seller/SellerPublishesTradeStatistics.java b/core/src/main/java/bisq/core/trade/protocol/tasks/seller/SellerPublishesTradeStatistics.java index 2dfb52e44f..0489ac0e32 100644 --- a/core/src/main/java/bisq/core/trade/protocol/tasks/seller/SellerPublishesTradeStatistics.java +++ b/core/src/main/java/bisq/core/trade/protocol/tasks/seller/SellerPublishesTradeStatistics.java @@ -21,12 +21,13 @@ import bisq.core.offer.Offer; import bisq.core.offer.OfferPayload; import bisq.core.trade.Trade; import bisq.core.trade.protocol.tasks.TradeTask; -import bisq.core.trade.statistics.TradeStatistics2; +import bisq.core.trade.statistics.TradeStatistics3; import bisq.network.p2p.NodeAddress; import bisq.network.p2p.network.NetworkNode; import bisq.network.p2p.network.TorNetworkNode; +import bisq.common.app.Capability; import bisq.common.taskrunner.TaskRunner; import java.util.HashMap; @@ -49,31 +50,54 @@ public class SellerPublishesTradeStatistics extends TradeTask { checkNotNull(trade.getDepositTx()); - Map extraDataMap = new HashMap<>(); - if (processModel.getReferralIdService().getOptionalReferralId().isPresent()) { - extraDataMap.put(OfferPayload.REFERRAL_ID, processModel.getReferralIdService().getOptionalReferralId().get()); - } + processModel.getP2PService().findPeersCapabilities(trade.getTradingPeerNodeAddress()) + .filter(capabilities -> capabilities.containsAll(Capability.TRADE_STATISTICS_3)) + .ifPresentOrElse(capabilities -> { + // Our peer has updated, so as we are the seller we will publish the trade statistics. + // The peer as buyer does not publish anymore with v.1.4.0 (where Capability.TRADE_STATISTICS_3 was added) - NodeAddress mediatorNodeAddress = checkNotNull(trade.getMediatorNodeAddress()); - // The first 4 chars are sufficient to identify a mediator. - // For testing with regtest/localhost we use the full address as its localhost and would result in - // same values for multiple mediators. - NetworkNode networkNode = model.getProcessModel().getP2PService().getNetworkNode(); - String address = networkNode instanceof TorNetworkNode ? - mediatorNodeAddress.getFullAddress().substring(0, 4) : - mediatorNodeAddress.getFullAddress(); - extraDataMap.put(TradeStatistics2.MEDIATOR_ADDRESS, address); + Map extraDataMap = new HashMap<>(); + if (processModel.getReferralIdService().getOptionalReferralId().isPresent()) { + extraDataMap.put(OfferPayload.REFERRAL_ID, processModel.getReferralIdService().getOptionalReferralId().get()); + } - Offer offer = checkNotNull(trade.getOffer()); - TradeStatistics2 tradeStatistics = new TradeStatistics2(offer.getOfferPayload(), - trade.getTradePrice(), - checkNotNull(trade.getTradeAmount()), - trade.getDate(), - trade.getDepositTxId(), - extraDataMap); - processModel.getP2PService().addPersistableNetworkPayload(tradeStatistics, true); + NodeAddress mediatorNodeAddress = checkNotNull(trade.getMediatorNodeAddress()); + // The first 4 chars are sufficient to identify a mediator. + // For testing with regtest/localhost we use the full address as its localhost and would result in + // same values for multiple mediators. + NetworkNode networkNode = model.getProcessModel().getP2PService().getNetworkNode(); + String truncatedMediatorNodeAddress = networkNode instanceof TorNetworkNode ? + mediatorNodeAddress.getFullAddress().substring(0, 4) : + mediatorNodeAddress.getFullAddress(); - complete(); + NodeAddress refundAgentNodeAddress = checkNotNull(trade.getRefundAgentNodeAddress()); + String truncatedRefundAgentNodeAddress = networkNode instanceof TorNetworkNode ? + refundAgentNodeAddress.getFullAddress().substring(0, 4) : + refundAgentNodeAddress.getFullAddress(); + + Offer offer = checkNotNull(trade.getOffer()); + TradeStatistics3 tradeStatistics = new TradeStatistics3(offer.getCurrencyCode(), + trade.getTradePrice().getValue(), + trade.getTradeAmountAsLong(), + offer.getPaymentMethod().getId(), + trade.getTakeOfferDate().getTime(), + truncatedMediatorNodeAddress, + truncatedRefundAgentNodeAddress, + extraDataMap); + if (tradeStatistics.isValid()) { + log.info("Publishing trade statistics"); + processModel.getP2PService().addPersistableNetworkPayload(tradeStatistics, true); + } else { + log.warn("Trade statistics are invalid. We do not publish. {}", tradeStatistics); + } + + complete(); + }, + () -> { + log.info("Our peer does not has updated yet, so they will publish the trade statistics. " + + "To avoid duplicates we do not publish from our side."); + complete(); + }); } catch (Throwable t) { failed(t); } diff --git a/p2p/src/main/java/bisq/network/p2p/P2PService.java b/p2p/src/main/java/bisq/network/p2p/P2PService.java index 95b68f0c61..bf594ecf6f 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PService.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PService.java @@ -920,6 +920,18 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis return keyRing; } + public Optional findPeersCapabilities(NodeAddress peer) { + return networkNode.getConfirmedConnections().stream() + .filter(e -> e.getPeersNodeAddressOptional().isPresent()) + .filter(e -> e.getPeersNodeAddressOptional().get().equals(peer)) + .map(Connection::getCapabilities) + .findAny(); + } + + + /////////////////////////////////////////////////////////////////////////////////////////// + // Private + /////////////////////////////////////////////////////////////////////////////////////////// @Value public class MailboxItem {