Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FederatedBtcNodeProvider: Compare banned node address and port #7312

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading