mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 18:03:12 +01:00
Add ArbitratorSelection based on tradestatistics
This commit is contained in:
parent
6873d03216
commit
f2e289d0a6
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.ArbitratorManager;
|
||||
import bisq.core.trade.statistics.TradeStatistics2;
|
||||
import bisq.core.trade.statistics.TradeStatisticsManager;
|
||||
|
||||
import bisq.common.util.Tuple2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class ArbitratorSelection {
|
||||
public static String getLeastUsedArbitrator(TradeStatisticsManager tradeStatisticsManager,
|
||||
ArbitratorManager arbitratorManager) {
|
||||
List<TradeStatistics2> list = new ArrayList<>(tradeStatisticsManager.getObservableTradeStatisticsSet());
|
||||
list.sort(Comparator.comparing(TradeStatistics2::getTradeDate));
|
||||
Collections.reverse(list);
|
||||
log.error("first " + list.get(0).getTradeDate());
|
||||
log.error("last " + list.get(list.size() - 1).getTradeDate());
|
||||
list = list.subList(0, 100);
|
||||
log.error("list post " + list);
|
||||
|
||||
List<String> lastAddressesUsedInTrades = list.stream()
|
||||
.filter(tradeStatistics2 -> tradeStatistics2.getExtraDataMap() != null)
|
||||
.map(tradeStatistics2 -> tradeStatistics2.getExtraDataMap().get(TradeStatistics2.ARBITRATOR_ADDRESS))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<String> arbitrators = arbitratorManager.getArbitratorsObservableMap().values().stream()
|
||||
.map(arbitrator -> arbitrator.getNodeAddress().getHostNameWithoutPostFix())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
String result = getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
log.error("result: " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static String getLeastUsedArbitrator(List<String> lastAddressesUsedInTrades, List<String> arbitrators) {
|
||||
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::equals)
|
||||
.mapToInt(e -> 1)
|
||||
.count();
|
||||
tuple.second.set(count);
|
||||
});
|
||||
|
||||
arbitratorTuples.sort(Comparator.comparing(e -> e.first));
|
||||
arbitratorTuples.sort(Comparator.comparingInt(e -> e.second.get()));
|
||||
|
||||
return arbitratorTuples.get(0).first;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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 java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ArbitratorSelectionTest {
|
||||
@Test
|
||||
public void testGetLeastUsedArbitrator() {
|
||||
// We get least used selected
|
||||
List<String> lastAddressesUsedInTrades = Arrays.asList("arb1", "arb2", "arb1");
|
||||
List<String> arbitrators = Arrays.asList("arb1", "arb2");
|
||||
String result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("arb2", result);
|
||||
|
||||
// if all are same we use first according to alphanumeric sorting
|
||||
lastAddressesUsedInTrades = Arrays.asList("arb1", "arb2", "arb3");
|
||||
arbitrators = Arrays.asList("arb1", "arb2", "arb3");
|
||||
result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("arb1", result);
|
||||
lastAddressesUsedInTrades = Arrays.asList("xxx", "ccc", "aaa");
|
||||
arbitrators = Arrays.asList("aaa", "ccc", "xxx");
|
||||
result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("aaa", result);
|
||||
lastAddressesUsedInTrades = Arrays.asList("333", "000", "111");
|
||||
arbitrators = Arrays.asList("111", "333", "000");
|
||||
result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("000", result);
|
||||
|
||||
// if winner is not in our arb list we use our arb from arbitrators even if never used in trades
|
||||
lastAddressesUsedInTrades = Arrays.asList("arb1", "arb2", "arb3");
|
||||
arbitrators = Arrays.asList("arb4");
|
||||
result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("arb4", result);
|
||||
|
||||
// if winner (arb2) is not in our arb list we use our arb from arbitrators
|
||||
lastAddressesUsedInTrades = Arrays.asList("arb1", "arb1", "arb1", "arb2");
|
||||
arbitrators = Arrays.asList("arb1");
|
||||
result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("arb1", result);
|
||||
|
||||
// arb1 is used least
|
||||
lastAddressesUsedInTrades = Arrays.asList("arb1", "arb2", "arb2", "arb2", "arb1", "arb1", "arb2");
|
||||
arbitrators = Arrays.asList("arb1", "arb2");
|
||||
result = ArbitratorSelection.getLeastUsedArbitrator(lastAddressesUsedInTrades, arbitrators);
|
||||
assertEquals("arb1", result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user