Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitration system improvements #1777

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/src/main/proto/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ message OfferAvailabilityResponse {
AvailabilityResult availability_result = 2;
repeated int32 supported_capabilities = 3;
string uid = 4;
NodeAddress arbitrator = 5;
}

message RefreshOfferMessage {
Expand Down Expand Up @@ -1050,6 +1051,7 @@ message OpenOffer {

Offer offer = 1;
State state = 2;
NodeAddress arbitrator_node_address = 3;
}

message Tradable {
Expand Down
39 changes: 15 additions & 24 deletions core/src/main/java/bisq/core/arbitration/ArbitratorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -229,30 +230,14 @@ public void updateArbitratorMap() {
);
});

if (preferences.isAutoSelectArbitrators()) {
arbitratorsObservableMap.values().stream()
.filter(user::hasMatchingLanguage)
.forEach(a -> {
user.addAcceptedArbitrator(a);
user.addAcceptedMediator(getMediator(a)
);
});
} else {
// if we don't have any arbitrator we set all matching
// we use a delay as we might get our matching arbitrator a bit delayed (first we get one we did not selected
// then we get our selected one - we don't want to activate the first in that case)
UserThread.runAfter(() -> {
if (user.getAcceptedArbitrators().isEmpty()) {
arbitratorsObservableMap.values().stream()
.filter(user::hasMatchingLanguage)
.forEach(a -> {
user.addAcceptedArbitrator(a);
user.addAcceptedMediator(getMediator(a)
);
});
}
}, 100, TimeUnit.MILLISECONDS);
}
// We keep the domain with storing the arbitrators in user as it might be still useful for mediators
arbitratorsObservableMap.values().forEach(a -> {
user.addAcceptedArbitrator(a);
user.addAcceptedMediator(getMediator(a)
);
});

log.info("Available arbitrators: {}", arbitratorsObservableMap.keySet());
}

// TODO we mirror arbitrator data for mediator as long we have not impl. it in the UI
Expand Down Expand Up @@ -381,4 +366,10 @@ private void stopRepublishArbitratorTimer() {
republishArbitratorTimer = null;
}
}

public Optional<Arbitrator> getArbitratorByNodeAddress(NodeAddress nodeAddress) {
return arbitratorsObservableMap.containsKey(nodeAddress) ?
Optional.of(arbitratorsObservableMap.get(nodeAddress)) :
Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class DefaultSeedNodeAddresses {
// 4. Rename the directory with your local onion address
// 5. Edit here your found onion address (new NodeAddress("YOUR_ONION.onion:8002")
new NodeAddress("rxdkppp3vicnbgqt.onion:8002"),
new NodeAddress("4ie52dse64kaarxw.onion:8002"),

// LTC mainnet
new NodeAddress("acyvotgewx46pebw.onion:8003"),
Expand Down
25 changes: 20 additions & 5 deletions core/src/main/java/bisq/core/offer/OpenOffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import bisq.core.trade.Tradable;
import bisq.core.trade.TradableList;

import bisq.network.p2p.NodeAddress;

import bisq.common.Timer;
import bisq.common.UserThread;
import bisq.common.proto.ProtoUtil;
Expand All @@ -28,11 +30,15 @@
import io.bisq.generated.protobuffer.PB;

import java.util.Date;
import java.util.Optional;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

@EqualsAndHashCode
@Slf4j
public final class OpenOffer implements Tradable {
Expand All @@ -52,6 +58,10 @@ public enum State {
private final Offer offer;
@Getter
private State state;
@Getter
@Setter
@Nullable
private NodeAddress arbitratorNodeAddress;

transient private Storage<TradableList<OpenOffer>> storage;

Expand All @@ -65,25 +75,30 @@ public OpenOffer(Offer offer, Storage<TradableList<OpenOffer>> storage) {
// PROTO BUFFER
///////////////////////////////////////////////////////////////////////////////////////////

private OpenOffer(Offer offer, State state) {
private OpenOffer(Offer offer, State state, @Nullable NodeAddress arbitratorNodeAddress) {
this.offer = offer;
this.state = state;
this.arbitratorNodeAddress = arbitratorNodeAddress;

if (this.state == State.RESERVED)
setState(State.AVAILABLE);
}

@Override
public PB.Tradable toProtoMessage() {
return PB.Tradable.newBuilder().setOpenOffer(PB.OpenOffer.newBuilder()
PB.OpenOffer.Builder builder = PB.OpenOffer.newBuilder()
.setOffer(offer.toProtoMessage())
.setState(PB.OpenOffer.State.valueOf(state.name())))
.build();
.setState(PB.OpenOffer.State.valueOf(state.name()));

Optional.ofNullable(arbitratorNodeAddress).ifPresent(nodeAddress -> builder.setArbitratorNodeAddress(nodeAddress.toProtoMessage()));

return PB.Tradable.newBuilder().setOpenOffer(builder).build();
}

public static Tradable fromProto(PB.OpenOffer proto) {
return new OpenOffer(Offer.fromProto(proto.getOffer()),
ProtoUtil.enumFromProto(OpenOffer.State.class, proto.getState().name()));
ProtoUtil.enumFromProto(OpenOffer.State.class, proto.getState().name()),
proto.hasArbitratorNodeAddress() ? NodeAddress.fromProto(proto.getArbitratorNodeAddress()) : null);
}


Expand Down
24 changes: 20 additions & 4 deletions core/src/main/java/bisq/core/offer/OpenOfferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package bisq.core.offer;

import bisq.core.arbitration.ArbitratorManager;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.TradeWalletService;
import bisq.core.exceptions.TradePriceOutOfToleranceException;
import bisq.core.offer.availability.ArbitratorSelection;
import bisq.core.offer.messages.OfferAvailabilityRequest;
import bisq.core.offer.messages.OfferAvailabilityResponse;
import bisq.core.offer.placeoffer.PlaceOfferModel;
Expand All @@ -29,6 +31,7 @@
import bisq.core.trade.TradableList;
import bisq.core.trade.closed.ClosedTradableManager;
import bisq.core.trade.handlers.TransactionResultHandler;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.Validator;
Expand Down Expand Up @@ -100,6 +103,8 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
private final ClosedTradableManager closedTradableManager;
private final PriceFeedService priceFeedService;
private final Preferences preferences;
private final TradeStatisticsManager tradeStatisticsManager;
private final ArbitratorManager arbitratorManager;
private final Storage<TradableList<OpenOffer>> openOfferTradableListStorage;
private final Map<String, OpenOffer> offersToBeEdited = new HashMap<>();
private boolean stopped;
Expand All @@ -124,6 +129,8 @@ public OpenOfferManager(KeyRing keyRing,
PriceFeedService priceFeedService,
Preferences preferences,
PersistenceProtoResolver persistenceProtoResolver,
TradeStatisticsManager tradeStatisticsManager,
ArbitratorManager arbitratorManager,
@Named(Storage.STORAGE_DIR) File storageDir) {
this.keyRing = keyRing;
this.user = user;
Expand All @@ -135,6 +142,8 @@ public OpenOfferManager(KeyRing keyRing,
this.closedTradableManager = closedTradableManager;
this.priceFeedService = priceFeedService;
this.preferences = preferences;
this.tradeStatisticsManager = tradeStatisticsManager;
this.arbitratorManager = arbitratorManager;

openOfferTradableListStorage = new Storage<>(storageDir, persistenceProtoResolver);

Expand Down Expand Up @@ -324,6 +333,8 @@ public void placeOffer(Offer offer,
tradeWalletService,
bsqWalletService,
offerBookService,
arbitratorManager,
tradeStatisticsManager,
user);
PlaceOfferProtocol placeOfferProtocol = new PlaceOfferProtocol(
model,
Expand Down Expand Up @@ -548,12 +559,17 @@ private void handleOfferAvailabilityRequest(OfferAvailabilityRequest request, No
try {
Optional<OpenOffer> openOfferOptional = getOpenOfferById(request.offerId);
AvailabilityResult availabilityResult;
NodeAddress arbitratorNodeAddress = null;
if (openOfferOptional.isPresent()) {
if (openOfferOptional.get().getState() == OpenOffer.State.AVAILABLE) {
final Offer offer = openOfferOptional.get().getOffer();
if (preferences.getIgnoreTradersList().stream().noneMatch(hostName -> hostName.equals(peer.getHostNameWithoutPostFix()))) {
OpenOffer openOffer = openOfferOptional.get();
if (openOffer.getState() == OpenOffer.State.AVAILABLE) {
final Offer offer = openOffer.getOffer();
if (preferences.getIgnoreTradersList().stream().noneMatch(hostName -> hostName.equals(peer.getHostName()))) {
availabilityResult = AvailabilityResult.AVAILABLE;

arbitratorNodeAddress = ArbitratorSelection.getLeastUsedArbitrator(tradeStatisticsManager, arbitratorManager).getNodeAddress();
openOffer.setArbitratorNodeAddress(arbitratorNodeAddress);

List<NodeAddress> acceptedArbitrators = user.getAcceptedArbitratorAddresses();
if (acceptedArbitrators != null && !acceptedArbitrators.isEmpty()) {
// Check also tradePrice to avoid failures after taker fee is paid caused by a too big difference
Expand Down Expand Up @@ -586,7 +602,7 @@ private void handleOfferAvailabilityRequest(OfferAvailabilityRequest request, No
availabilityResult = AvailabilityResult.OFFER_TAKEN;
}

OfferAvailabilityResponse offerAvailabilityResponse = new OfferAvailabilityResponse(request.offerId, availabilityResult);
OfferAvailabilityResponse offerAvailabilityResponse = new OfferAvailabilityResponse(request.offerId, availabilityResult, arbitratorNodeAddress);
log.info("Send {} with offerId {} and uid {} to peer {}",
offerAvailabilityResponse.getClass().getSimpleName(), offerAvailabilityResponse.getOfferId(),
offerAvailabilityResponse.getUid(), peer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.offer.availability;

import bisq.core.arbitration.Arbitrator;
import bisq.core.arbitration.ArbitratorManager;
import bisq.core.trade.statistics.TradeStatistics2;
import bisq.core.trade.statistics.TradeStatisticsManager;

import bisq.common.util.Tuple2;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import static com.google.common.base.Preconditions.checkArgument;

@Slf4j
public class ArbitratorSelection {
@Getter
private static boolean isNewRuleActivated;

static {
try {
//TODO set activation data to 1 month after release
Date activationDate = new SimpleDateFormat("dd/MM/yyyy").parse("20/11/2018");
isNewRuleActivated = new Date().after(activationDate);
} catch (ParseException e) {
e.printStackTrace();
}
}

public static Arbitrator getLeastUsedArbitrator(TradeStatisticsManager tradeStatisticsManager,
ArbitratorManager arbitratorManager) {
// We take last 100 entries from trade statistics
List<TradeStatistics2> list = new ArrayList<>(tradeStatisticsManager.getObservableTradeStatisticsSet());
list.sort(Comparator.comparing(TradeStatistics2::getTradeDate));
Collections.reverse(list);
if (!list.isEmpty()) {
int max = Math.min(list.size(), 100);
list = list.subList(0, max);
}

// We stored only first 4 chars of arbitrators onion address
List<String> lastAddressesUsedInTrades = list.stream()
.filter(tradeStatistics2 -> tradeStatistics2.getExtraDataMap() != null)
.map(tradeStatistics2 -> tradeStatistics2.getExtraDataMap().get(TradeStatistics2.ARBITRATOR_ADDRESS))
.collect(Collectors.toList());

Set<String> arbitrators = arbitratorManager.getArbitratorsObservableMap().values().stream()
.map(arbitrator -> arbitrator.getNodeAddress().getHostName())
.collect(Collectors.toSet());

String result = getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);

Optional<Arbitrator> optionalArbitrator = arbitratorManager.getArbitratorsObservableMap().values().stream()
.filter(e -> e.getNodeAddress().getHostName().equals(result))
.findAny();
checkArgument(optionalArbitrator.isPresent(), "optionalArbitrator has to be present");
return optionalArbitrator.get();
}

static String getLeastUsedArbitrator(List<String> lastAddressesUsedInTrades, Set<String> arbitrators) {
checkArgument(!arbitrators.isEmpty(), "arbitrators must nto be empty");
List<Tuple2<String, AtomicInteger>> arbitratorTuples = arbitrators.stream()
.map(e -> new Tuple2<>(e, new AtomicInteger(0)))
.collect(Collectors.toList());
arbitratorTuples.forEach(tuple -> {
int count = (int) lastAddressesUsedInTrades.stream()
.filter(tuple.first::startsWith) // we use only first 4 chars for comparing
.mapToInt(e -> 1)
.count();
tuple.second.set(count);
});
arbitratorTuples.sort(Comparator.comparingInt(e -> e.second.get()));
return arbitratorTuples.get(0).first;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import bisq.common.taskrunner.Model;

import lombok.Getter;
import lombok.Setter;

import javax.annotation.Nullable;

public class OfferAvailabilityModel implements Model {
@Getter
Expand All @@ -38,6 +41,10 @@ public class OfferAvailabilityModel implements Model {

private NodeAddress peerNodeAddress; // maker
private OfferAvailabilityResponse message;
@Nullable
@Setter
@Getter
private NodeAddress selectedArbitrator;

public OfferAvailabilityModel(Offer offer,
PubKeyRing pubKeyRing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

import bisq.core.offer.AvailabilityResult;
import bisq.core.offer.Offer;
import bisq.core.offer.availability.ArbitratorSelection;
import bisq.core.offer.availability.OfferAvailabilityModel;
import bisq.core.offer.messages.OfferAvailabilityResponse;

import bisq.network.p2p.NodeAddress;

import bisq.common.taskrunner.Task;
import bisq.common.taskrunner.TaskRunner;

Expand All @@ -39,6 +42,13 @@ protected void run() {
if (model.getOffer().getState() != Offer.State.REMOVED) {
if (offerAvailabilityResponse.getAvailabilityResult() == AvailabilityResult.AVAILABLE) {
model.getOffer().setState(Offer.State.AVAILABLE);
if (ArbitratorSelection.isNewRuleActivated()) {
NodeAddress selectedArbitrator = offerAvailabilityResponse.getArbitrator();
if (selectedArbitrator == null)
failed("You cannot take that offer because the offer maker is running an incompatible version.");
else
model.setSelectedArbitrator(selectedArbitrator);
}
} else {
model.getOffer().setState(Offer.State.NOT_AVAILABLE);
failed("Take offer attempt rejected because of: " + offerAvailabilityResponse.getAvailabilityResult());
Expand Down
Loading