let seller publish trade statistics only if peer is updated user. If not the peer will publish only.

This commit is contained in:
chimp1984 2020-10-05 17:43:35 -05:00
parent a522d0ade9
commit b2665bdd2c
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
2 changed files with 59 additions and 23 deletions

View file

@ -21,12 +21,13 @@ import bisq.core.offer.Offer;
import bisq.core.offer.OfferPayload; import bisq.core.offer.OfferPayload;
import bisq.core.trade.Trade; import bisq.core.trade.Trade;
import bisq.core.trade.protocol.tasks.TradeTask; 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.NodeAddress;
import bisq.network.p2p.network.NetworkNode; import bisq.network.p2p.network.NetworkNode;
import bisq.network.p2p.network.TorNetworkNode; import bisq.network.p2p.network.TorNetworkNode;
import bisq.common.app.Capability;
import bisq.common.taskrunner.TaskRunner; import bisq.common.taskrunner.TaskRunner;
import java.util.HashMap; import java.util.HashMap;
@ -49,31 +50,54 @@ public class SellerPublishesTradeStatistics extends TradeTask {
checkNotNull(trade.getDepositTx()); checkNotNull(trade.getDepositTx());
Map<String, String> extraDataMap = new HashMap<>(); processModel.getP2PService().findPeersCapabilities(trade.getTradingPeerNodeAddress())
if (processModel.getReferralIdService().getOptionalReferralId().isPresent()) { .filter(capabilities -> capabilities.containsAll(Capability.TRADE_STATISTICS_3))
extraDataMap.put(OfferPayload.REFERRAL_ID, processModel.getReferralIdService().getOptionalReferralId().get()); .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()); Map<String, String> extraDataMap = new HashMap<>();
// The first 4 chars are sufficient to identify a mediator. if (processModel.getReferralIdService().getOptionalReferralId().isPresent()) {
// For testing with regtest/localhost we use the full address as its localhost and would result in extraDataMap.put(OfferPayload.REFERRAL_ID, processModel.getReferralIdService().getOptionalReferralId().get());
// 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);
Offer offer = checkNotNull(trade.getOffer()); NodeAddress mediatorNodeAddress = checkNotNull(trade.getMediatorNodeAddress());
TradeStatistics2 tradeStatistics = new TradeStatistics2(offer.getOfferPayload(), // The first 4 chars are sufficient to identify a mediator.
trade.getTradePrice(), // For testing with regtest/localhost we use the full address as its localhost and would result in
checkNotNull(trade.getTradeAmount()), // same values for multiple mediators.
trade.getDate(), NetworkNode networkNode = model.getProcessModel().getP2PService().getNetworkNode();
trade.getDepositTxId(), String truncatedMediatorNodeAddress = networkNode instanceof TorNetworkNode ?
extraDataMap); mediatorNodeAddress.getFullAddress().substring(0, 4) :
processModel.getP2PService().addPersistableNetworkPayload(tradeStatistics, true); 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) { } catch (Throwable t) {
failed(t); failed(t);
} }

View file

@ -920,6 +920,18 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
return keyRing; return keyRing;
} }
public Optional<Capabilities> 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 @Value
public class MailboxItem { public class MailboxItem {