FederatedBtcNodeProvider: Compare banned node address and port

This commit is contained in:
Alva Swanson 2024-11-18 22:22:22 +00:00
parent 0da22ab3b0
commit 17d48d989a
No known key found for this signature in database
GPG Key ID: 004760E77F753090
2 changed files with 116 additions and 10 deletions

View File

@ -25,18 +25,29 @@ public class FederatedBtcNodeProvider {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
hardcodedBtcNodes.addAll(filterProvidedBtcNodes); hardcodedBtcNodes.addAll(filterProvidedBtcNodes);
Set<String> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream() Set<NodeAddress> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
.filter(n -> !n.isEmpty()) .filter(n -> !n.isEmpty())
.map(FederatedBtcNodeProvider::getNodeAddress) .map(FederatedBtcNodeProvider::getNodeAddress)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(NodeAddress::getHostName)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
return hardcodedBtcNodes.stream() return hardcodedBtcNodes.stream()
.filter(btcNode -> { .filter(btcNode -> {
String nodeAddress = btcNode.hasOnionAddress() ? btcNode.getOnionAddress() : String nodeAddress = btcNode.hasOnionAddress() ? btcNode.getOnionAddress() :
btcNode.getHostNameOrAddress(); 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()); .collect(Collectors.toList());
} }

View File

@ -60,6 +60,29 @@ public class FederatedBtcNodeProviderTest {
assertIterableEquals(expected, selectedNodes); 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 @Test
void bannedIpV6Node() { void bannedIpV6Node() {
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370"; String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
@ -91,19 +114,44 @@ public class FederatedBtcNodeProviderTest {
} }
@Test @Test
void bannedHostNameNode() { void bannedIpV6NodeWrongPort() {
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
var hardcodedNodes = List.of( var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null, new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"), BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, "btc1.dnsalias.net", null, new BtcNodes.BtcNode(null, null, bannedAddress, 7348, "@bob"),
5678, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null, new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie") BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
); );
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes); List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList(); List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of("btc1.dnsalias.net:5678"); 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, 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(bannedHostName + ":" + port);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes); .getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
@ -118,11 +166,35 @@ public class FederatedBtcNodeProviderTest {
} }
@Test @Test
void bannedOnionNode() { void bannedHostNameNodeWrongPort() {
String bannedHostName = "btc1.dnsalias.net";
var hardcodedNodes = List.of( var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null, new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"), BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, "bob.onion", null, 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, bannedOnionAddress, null,
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"), BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null, new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie") BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
@ -130,7 +202,7 @@ public class FederatedBtcNodeProviderTest {
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes); List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList(); 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 List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes); .getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
@ -143,4 +215,27 @@ public class FederatedBtcNodeProviderTest {
); );
assertIterableEquals(expected, selectedNodes); 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);
}
} }