From 17d48d989aa09cb46a7211cfcf38459aedd5db05 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Mon, 18 Nov 2024 22:22:22 +0000 Subject: [PATCH] FederatedBtcNodeProvider: Compare banned node address and port --- .../btc/nodes/FederatedBtcNodeProvider.java | 17 ++- .../nodes/FederatedBtcNodeProviderTest.java | 105 +++++++++++++++++- 2 files changed, 114 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/bisq/core/btc/nodes/FederatedBtcNodeProvider.java b/core/src/main/java/bisq/core/btc/nodes/FederatedBtcNodeProvider.java index 5f2a4d4176..ed37f0a910 100644 --- a/core/src/main/java/bisq/core/btc/nodes/FederatedBtcNodeProvider.java +++ b/core/src/main/java/bisq/core/btc/nodes/FederatedBtcNodeProvider.java @@ -25,18 +25,29 @@ static List getNodes(List hardcodedBtcNodes, .collect(Collectors.toSet()); hardcodedBtcNodes.addAll(filterProvidedBtcNodes); - Set bannedBtcNodeHostNames = bannedBtcNodesConfig.stream() + Set 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()); } diff --git a/core/src/test/java/bisq/core/btc/nodes/FederatedBtcNodeProviderTest.java b/core/src/test/java/bisq/core/btc/nodes/FederatedBtcNodeProviderTest.java index 4d4b0d4cc1..5f42da3f2f 100644 --- a/core/src/test/java/bisq/core/btc/nodes/FederatedBtcNodeProviderTest.java +++ b/core/src/test/java/bisq/core/btc/nodes/FederatedBtcNodeProviderTest.java @@ -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 mutableHardcodedList = new ArrayList<>(hardcodedNodes); + List filterProvidedBtcNodes = Collections.emptyList(); + String bannedFullAddress = bannedAddress + ":" + 1234; + List bannedBtcNodes = List.of(bannedFullAddress); + + List selectedNodes = FederatedBtcNodeProvider + .getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes); + + assertIterableEquals(hardcodedNodes, selectedNodes); + } + @Test void bannedIpV6Node() { String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370"; @@ -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 mutableHardcodedList = new ArrayList<>(hardcodedNodes); + List filterProvidedBtcNodes = Collections.emptyList(); + String bannedFullAddress = "[" + bannedAddress + "]" + ":" + 1234; + List bannedBtcNodes = List.of(bannedFullAddress); + + List 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 mutableHardcodedList = new ArrayList<>(hardcodedNodes); List filterProvidedBtcNodes = Collections.emptyList(); - List bannedBtcNodes = List.of("btc1.dnsalias.net:5678"); + List bannedBtcNodes = List.of(bannedHostName + ":" + port); List selectedNodes = FederatedBtcNodeProvider .getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes); @@ -117,12 +165,36 @@ 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 mutableHardcodedList = new ArrayList<>(hardcodedNodes); + List filterProvidedBtcNodes = Collections.emptyList(); + List bannedBtcNodes = List.of(bannedHostName + ":" + 1234); + + List 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") @@ -130,7 +202,7 @@ void bannedOnionNode() { List mutableHardcodedList = new ArrayList<>(hardcodedNodes); List filterProvidedBtcNodes = Collections.emptyList(); - List bannedBtcNodes = List.of("bob.onion:8333"); + List bannedBtcNodes = List.of(bannedOnionAddress + ":" + BtcNodes.BtcNode.DEFAULT_PORT); List selectedNodes = FederatedBtcNodeProvider .getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes); @@ -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 mutableHardcodedList = new ArrayList<>(hardcodedNodes); + List filterProvidedBtcNodes = Collections.emptyList(); + List bannedBtcNodes = List.of(bannedOnionAddress + ":" + 1234); + + List selectedNodes = FederatedBtcNodeProvider + .getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes); + + assertIterableEquals(hardcodedNodes, selectedNodes); + } }