Skip to content

Commit

Permalink
Merge pull request #7306 from alvasw/Move_federated_BTC_node_selectio…
Browse files Browse the repository at this point in the history
…n_to_FederatedBtcNodeProvider

Move federated BTC node selection to FederatedBtcNodeProvider
  • Loading branch information
alejandrogarcia83 authored Nov 20, 2024
2 parents 0ddbd30 + b90b7b5 commit 1ba9d84
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,16 @@
import bisq.core.btc.nodes.BtcNodes.BtcNode;
import bisq.core.user.Preferences;

import bisq.network.p2p.NodeAddress;

import bisq.common.config.Config;
import bisq.common.util.Utilities;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.jetbrains.annotations.Nullable;


public class BtcNodesSetupPreferences {
private static final Logger log = LoggerFactory.getLogger(BtcNodesSetupPreferences.class);

Expand Down Expand Up @@ -74,24 +66,7 @@ public List<BtcNode> selectPreferredNodes(BtcNodes btcNodes) {
break;
case PROVIDED:
default:
Set<BtcNode> providedBtcNodes = new HashSet<>(btcNodes.getProvidedBtcNodes());
Set<BtcNode> filterProvidedBtcNodes = config.filterProvidedBtcNodes.stream()
.filter(n -> !n.isEmpty())
.map(this::getNodeAddress)
.filter(Objects::nonNull)
.map(nodeAddress -> new BtcNode(null, nodeAddress.getHostName(), null, nodeAddress.getPort(), "Provided by filter"))
.collect(Collectors.toSet());
providedBtcNodes.addAll(filterProvidedBtcNodes);

Set<String> bannedBtcNodeHostNames = config.bannedBtcNodes.stream()
.filter(n -> !n.isEmpty())
.map(this::getNodeAddress)
.filter(Objects::nonNull)
.map(NodeAddress::getHostName)
.collect(Collectors.toSet());
result = providedBtcNodes.stream()
.filter(e -> !bannedBtcNodeHostNames.contains(e.getHostName()))
.collect(Collectors.toList());
result = FederatedBtcNodeProvider.getNodes(btcNodes, config);
break;
}

Expand Down Expand Up @@ -125,14 +100,4 @@ public int calculateMinBroadcastConnections(List<BtcNode> nodes) {
}
return result;
}

@Nullable
private NodeAddress getNodeAddress(String address) {
try {
return new NodeAddress(address);
} catch (Throwable t) {
log.error("exception when filtering banned seednodes", t);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package bisq.core.btc.nodes;

import bisq.network.p2p.NodeAddress;

import bisq.common.config.Config;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import org.jetbrains.annotations.Nullable;

@Slf4j
public class FederatedBtcNodeProvider {

static List<BtcNodes.BtcNode> getNodes(BtcNodes btcNodes, Config config) {
Set<BtcNodes.BtcNode> providedBtcNodes = new HashSet<>(btcNodes.getProvidedBtcNodes());
Set<BtcNodes.BtcNode> filterProvidedBtcNodes = config.filterProvidedBtcNodes.stream()
.filter(n -> !n.isEmpty())
.map(FederatedBtcNodeProvider::getNodeAddress)
.filter(Objects::nonNull)
.map(nodeAddress -> new BtcNodes.BtcNode(null, nodeAddress.getHostName(), null, nodeAddress.getPort(), "Provided by filter"))
.collect(Collectors.toSet());
providedBtcNodes.addAll(filterProvidedBtcNodes);

Set<String> bannedBtcNodeHostNames = config.bannedBtcNodes.stream()
.filter(n -> !n.isEmpty())
.map(FederatedBtcNodeProvider::getNodeAddress)
.filter(Objects::nonNull)
.map(NodeAddress::getHostName)
.collect(Collectors.toSet());
return providedBtcNodes.stream()
.filter(e -> !bannedBtcNodeHostNames.contains(e.getHostName()))
.collect(Collectors.toList());
}

@Nullable
private static NodeAddress getNodeAddress(String address) {
try {
return new NodeAddress(address);
} catch (Throwable t) {
log.error("exception when filtering banned seednodes", t);
}
return null;
}
}

0 comments on commit 1ba9d84

Please sign in to comment.