Skip to content

Commit

Permalink
Merge pull request #7312 from alvasw/FederatedBtcNodeProvider_Compare…
Browse files Browse the repository at this point in the history
…_banned_node_address_and_port

FederatedBtcNodeProvider: Compare banned node address and port
  • Loading branch information
alejandrogarcia83 authored Nov 20, 2024
2 parents d67a234 + 17d48d9 commit dc2cd17
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ static List<BtcNodes.BtcNode> getNodes(List<BtcNodes.BtcNode> hardcodedBtcNodes,
.collect(Collectors.toSet());
hardcodedBtcNodes.addAll(filterProvidedBtcNodes);

Set<String> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
Set<NodeAddress> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
.filter(n -> !n.isEmpty())
.map(FederatedBtcNodeProvider::getNodeAddress)
.filter(Objects::nonNull)
.map(NodeAddress::getHostName)
.collect(Collectors.toSet());

return hardcodedBtcNodes.stream()
.filter(btcNode -> {
String nodeAddress = btcNode.hasOnionAddress() ? btcNode.getOnionAddress() :
btcNode.getHostNameOrAddress();
return !bannedBtcNodeHostNames.contains(nodeAddress);
Objects.requireNonNull(nodeAddress);

int port = btcNode.getPort();

for (NodeAddress bannedAddress : bannedBtcNodeHostNames) {
boolean isBanned = nodeAddress.equals(bannedAddress.getHostName()) &&
port == bannedAddress.getPort();
if (isBanned) {
return false;
}
}

return true;
})
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ void bannedIpV4Node() {
assertIterableEquals(expected, selectedNodes);
}

@Test
void bannedIpV4NodeWrongPort() {
String bannedAddress = "123.456.890.123";

var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, null, bannedAddress, 4567, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);

List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
String bannedFullAddress = bannedAddress + ":" + 1234;
List<String> bannedBtcNodes = List.of(bannedFullAddress);

List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);

assertIterableEquals(hardcodedNodes, selectedNodes);
}

@Test
void bannedIpV6Node() {
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
Expand Down Expand Up @@ -90,20 +113,45 @@ void bannedIpV6Node() {
assertIterableEquals(expected, selectedNodes);
}

@Test
void bannedIpV6NodeWrongPort() {
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";

var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, null, bannedAddress, 7348, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);

List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
String bannedFullAddress = "[" + bannedAddress + "]" + ":" + 1234;
List<String> bannedBtcNodes = List.of(bannedFullAddress);

List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);

assertIterableEquals(hardcodedNodes, selectedNodes);
}

@Test
void bannedHostNameNode() {
String bannedHostName = "btc1.dnsalias.net";
int port = 5678;

var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, "btc1.dnsalias.net", null,
5678, "@bob"),
new BtcNodes.BtcNode(null, bannedHostName, null, port, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);

List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of("btc1.dnsalias.net:5678");
List<String> bannedBtcNodes = List.of(bannedHostName + ":" + port);

List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
Expand All @@ -117,20 +165,44 @@ void bannedHostNameNode() {
assertIterableEquals(expected, selectedNodes);
}

@Test
void bannedHostNameNodeWrongPort() {
String bannedHostName = "btc1.dnsalias.net";

var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, bannedHostName, null, 5678, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);

List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of(bannedHostName + ":" + 1234);

List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);

assertIterableEquals(hardcodedNodes, selectedNodes);
}

@Test
void bannedOnionNode() {
String bannedOnionAddress = "bob.onion";

var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, "bob.onion", null,
new BtcNodes.BtcNode(null, bannedOnionAddress, null,
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);

List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of("bob.onion:8333");
List<String> bannedBtcNodes = List.of(bannedOnionAddress + ":" + BtcNodes.BtcNode.DEFAULT_PORT);

List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
Expand All @@ -143,4 +215,27 @@ void bannedOnionNode() {
);
assertIterableEquals(expected, selectedNodes);
}

@Test
void bannedOnionNodeWrongPort() {
String bannedOnionAddress = "bob.onion";

var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, bannedOnionAddress, null,
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);

List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of(bannedOnionAddress + ":" + 1234);

List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);

assertIterableEquals(hardcodedNodes, selectedNodes);
}
}

0 comments on commit dc2cd17

Please sign in to comment.