diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkService.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkService.java index dae63b098ae52e1ff7a0330661732e8b161d7de3..69667304879a54b5db0939187c6aeaa04bb0dd86 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkService.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkService.java @@ -73,7 +73,7 @@ public interface NetworkService extends Service { List<Peer> getPeers(Peer mainPeer, Filter filter, Sort sort, ExecutorService pool); - CompletableFuture<List<CompletableFuture<Peer>>> asyncGetPeers(Peer mainPeer, ExecutorService pool) throws ExecutionException, InterruptedException; + CompletableFuture<List<CompletableFuture<Peer>>> asyncGetPeers(Peer mainPeer, List<String> filterEndpoints, ExecutorService pool) throws ExecutionException, InterruptedException; List<Peer> fillPeerStatsConsensus(final List<Peer> peers); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java index 8e5f28884aa8cc2de0c553d6c318085c909ef9b9..5323374f882740fc60f7f0fa0526643c821755eb 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java @@ -134,7 +134,7 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network public List<Peer> getPeers(final Peer mainPeer, Filter filter, Sort sort, ExecutorService executor) { try { - return asyncGetPeers(mainPeer, executor) + return asyncGetPeers(mainPeer, (filter != null ? filter.filterEndpoints : null), executor) .thenCompose(CompletableFutures::allOfToList) .thenApply(this::fillPeerStatsConsensus) .thenApply(peers -> peers.stream() @@ -162,14 +162,14 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network } @Override - public CompletableFuture<List<CompletableFuture<Peer>>> asyncGetPeers(final Peer mainPeer, ExecutorService executor) throws ExecutionException, InterruptedException { + public CompletableFuture<List<CompletableFuture<Peer>>> asyncGetPeers(final Peer mainPeer, List<String> filterEndpoints, ExecutorService executor) throws ExecutionException, InterruptedException { Preconditions.checkNotNull(mainPeer); log.debug("Loading network peers..."); final ExecutorService pool = (executor != null) ? executor : ForkJoinPool.commonPool(); - CompletableFuture<List<Peer>> peersFuture = CompletableFuture.supplyAsync(() -> loadPeerLeafs(mainPeer), pool); + CompletableFuture<List<Peer>> peersFuture = CompletableFuture.supplyAsync(() -> loadPeerLeafs(mainPeer, filterEndpoints), pool); CompletableFuture<Map<String, String>> memberUidsFuture = CompletableFuture.supplyAsync(() -> wotRemoteService.getMembersUids(mainPeer), pool); return CompletableFuture.allOf( @@ -184,6 +184,7 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network mainPeer.setCurrency(peer.getCurrency()); return asyncRefreshPeer(mainPeer, memberUids, pool); } + return asyncRefreshPeer(peer, memberUids, pool); }) .collect(Collectors.toList()); @@ -339,7 +340,7 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network } try { final List<Peer> newPeers = new ArrayList<>(); - addEndpointsAsPeers(bmaPeer, newPeers, null); + addEndpointsAsPeers(bmaPeer, newPeers, null, filter.filterEndpoints); CompletableFuture<List<CompletableFuture<Peer>>> jobs = CompletableFuture.supplyAsync(() -> wotRemoteService.getMembersUids(mainPeer), pool) @@ -423,7 +424,7 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network /* -- protected methods -- */ - protected List<Peer> loadPeerLeafs(Peer peer) { + protected List<Peer> loadPeerLeafs(Peer peer, List<String> filterEndpoints) { List<String> leaves = networkRemoteService.getPeersLeaves(peer); if (CollectionUtils.isEmpty(leaves)) return new ArrayList<>(); // should never occur @@ -444,7 +445,7 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network int count = Constants.Config.MAX_SAME_REQUEST_COUNT; while (offset < leaves.size()) { if (offset + count > leaves.size()) count = leaves.size() - offset; - loadPeerLeafs(peer, result, leaves, offset, count); + loadPeerLeafs(peer, result, leaves, offset, count, filterEndpoints); offset += count; try { Thread.sleep(1000); // wait 1 s @@ -458,13 +459,13 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network return result; } - protected void loadPeerLeafs(Peer requestedPeer, List<Peer> result, List<String> leaves, int offset, int count) { + protected void loadPeerLeafs(Peer requestedPeer, List<Peer> result, List<String> leaves, int offset, int count, List<String> filterEndpoints) { for (int i = offset; i< offset + count; i++) { String leaf = leaves.get(i); try { NetworkPeers.Peer peer = networkRemoteService.getPeerLeaf(requestedPeer, leaf); - addEndpointsAsPeers(peer, result, leaf); + addEndpointsAsPeers(peer, result, leaf, filterEndpoints); } catch(HttpNotFoundException | TechnicalException e) { log.warn("Peer not found for leaf=" + leaf); @@ -473,7 +474,7 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network } } - protected void addEndpointsAsPeers(NetworkPeers.Peer peer, List<Peer> result, String hash) { + protected void addEndpointsAsPeers(NetworkPeers.Peer peer, List<Peer> result, String hash, List<String> filterEndpoints) { if (CollectionUtils.isNotEmpty(peer.getEndpoints())) { for (NetworkPeering.Endpoint ep: peer.getEndpoints()) { if (ep != null && ep.getApi() != null) { @@ -483,7 +484,12 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network .setPubkey(peer.getPubkey()) .setEndpoint(ep) .build(); - result.add(peerEp); + // Filter on endpoints - fix #18 + if (CollectionUtils.isEmpty(filterEndpoints) + || StringUtils.isBlank(peerEp.getApi()) + || filterEndpoints.contains(peerEp.getApi())) { + result.add(peerEp); + } } } } diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/PeerService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/PeerService.java index 1313fe258d3eb8eee9524cc268555f511842af4e..4199e3fe56d10c18dfe75e4b47a1705241bdbbca 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/PeerService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/PeerService.java @@ -129,7 +129,7 @@ public class PeerService extends AbstractService { sortDef.sortType = null; try { - networkService.asyncGetPeers(firstPeer, threadPool.scheduler()) + networkService.asyncGetPeers(firstPeer, filterDef.filterEndpoints, threadPool.scheduler()) .thenCompose(CompletableFutures::allOfToList) .thenApply(networkService::fillPeerStatsConsensus) .thenApply(peers -> peers.stream()