Skip to content

Commit 17d48d9

Browse files
committed
FederatedBtcNodeProvider: Compare banned node address and port
1 parent 0da22ab commit 17d48d9

File tree

2 files changed

+114
-8
lines changed

2 files changed

+114
-8
lines changed

core/src/main/java/bisq/core/btc/nodes/FederatedBtcNodeProvider.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@ static List<BtcNodes.BtcNode> getNodes(List<BtcNodes.BtcNode> hardcodedBtcNodes,
2525
.collect(Collectors.toSet());
2626
hardcodedBtcNodes.addAll(filterProvidedBtcNodes);
2727

28-
Set<String> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
28+
Set<NodeAddress> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
2929
.filter(n -> !n.isEmpty())
3030
.map(FederatedBtcNodeProvider::getNodeAddress)
3131
.filter(Objects::nonNull)
32-
.map(NodeAddress::getHostName)
3332
.collect(Collectors.toSet());
3433

3534
return hardcodedBtcNodes.stream()
3635
.filter(btcNode -> {
3736
String nodeAddress = btcNode.hasOnionAddress() ? btcNode.getOnionAddress() :
3837
btcNode.getHostNameOrAddress();
39-
return !bannedBtcNodeHostNames.contains(nodeAddress);
38+
Objects.requireNonNull(nodeAddress);
39+
40+
int port = btcNode.getPort();
41+
42+
for (NodeAddress bannedAddress : bannedBtcNodeHostNames) {
43+
boolean isBanned = nodeAddress.equals(bannedAddress.getHostName()) &&
44+
port == bannedAddress.getPort();
45+
if (isBanned) {
46+
return false;
47+
}
48+
}
49+
50+
return true;
4051
})
4152
.collect(Collectors.toList());
4253
}

core/src/test/java/bisq/core/btc/nodes/FederatedBtcNodeProviderTest.java

+100-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ void bannedIpV4Node() {
6060
assertIterableEquals(expected, selectedNodes);
6161
}
6262

63+
@Test
64+
void bannedIpV4NodeWrongPort() {
65+
String bannedAddress = "123.456.890.123";
66+
67+
var hardcodedNodes = List.of(
68+
new BtcNodes.BtcNode(null, "alice.onion", null,
69+
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
70+
new BtcNodes.BtcNode(null, null, bannedAddress, 4567, "@bob"),
71+
new BtcNodes.BtcNode(null, "charlie.onion", null,
72+
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
73+
);
74+
75+
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
76+
List<String> filterProvidedBtcNodes = Collections.emptyList();
77+
String bannedFullAddress = bannedAddress + ":" + 1234;
78+
List<String> bannedBtcNodes = List.of(bannedFullAddress);
79+
80+
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
81+
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
82+
83+
assertIterableEquals(hardcodedNodes, selectedNodes);
84+
}
85+
6386
@Test
6487
void bannedIpV6Node() {
6588
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
@@ -90,20 +113,45 @@ void bannedIpV6Node() {
90113
assertIterableEquals(expected, selectedNodes);
91114
}
92115

116+
@Test
117+
void bannedIpV6NodeWrongPort() {
118+
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
119+
120+
var hardcodedNodes = List.of(
121+
new BtcNodes.BtcNode(null, "alice.onion", null,
122+
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
123+
new BtcNodes.BtcNode(null, null, bannedAddress, 7348, "@bob"),
124+
new BtcNodes.BtcNode(null, "charlie.onion", null,
125+
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
126+
);
127+
128+
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
129+
List<String> filterProvidedBtcNodes = Collections.emptyList();
130+
String bannedFullAddress = "[" + bannedAddress + "]" + ":" + 1234;
131+
List<String> bannedBtcNodes = List.of(bannedFullAddress);
132+
133+
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
134+
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
135+
136+
assertIterableEquals(hardcodedNodes, selectedNodes);
137+
}
138+
93139
@Test
94140
void bannedHostNameNode() {
141+
String bannedHostName = "btc1.dnsalias.net";
142+
int port = 5678;
143+
95144
var hardcodedNodes = List.of(
96145
new BtcNodes.BtcNode(null, "alice.onion", null,
97146
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
98-
new BtcNodes.BtcNode(null, "btc1.dnsalias.net", null,
99-
5678, "@bob"),
147+
new BtcNodes.BtcNode(null, bannedHostName, null, port, "@bob"),
100148
new BtcNodes.BtcNode(null, "charlie.onion", null,
101149
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
102150
);
103151

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

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

168+
@Test
169+
void bannedHostNameNodeWrongPort() {
170+
String bannedHostName = "btc1.dnsalias.net";
171+
172+
var hardcodedNodes = List.of(
173+
new BtcNodes.BtcNode(null, "alice.onion", null,
174+
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
175+
new BtcNodes.BtcNode(null, bannedHostName, null, 5678, "@bob"),
176+
new BtcNodes.BtcNode(null, "charlie.onion", null,
177+
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
178+
);
179+
180+
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
181+
List<String> filterProvidedBtcNodes = Collections.emptyList();
182+
List<String> bannedBtcNodes = List.of(bannedHostName + ":" + 1234);
183+
184+
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
185+
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
186+
187+
assertIterableEquals(hardcodedNodes, selectedNodes);
188+
}
189+
120190
@Test
121191
void bannedOnionNode() {
192+
String bannedOnionAddress = "bob.onion";
193+
122194
var hardcodedNodes = List.of(
123195
new BtcNodes.BtcNode(null, "alice.onion", null,
124196
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
125-
new BtcNodes.BtcNode(null, "bob.onion", null,
197+
new BtcNodes.BtcNode(null, bannedOnionAddress, null,
126198
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
127199
new BtcNodes.BtcNode(null, "charlie.onion", null,
128200
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
129201
);
130202

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

135207
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
136208
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
@@ -143,4 +215,27 @@ void bannedOnionNode() {
143215
);
144216
assertIterableEquals(expected, selectedNodes);
145217
}
218+
219+
@Test
220+
void bannedOnionNodeWrongPort() {
221+
String bannedOnionAddress = "bob.onion";
222+
223+
var hardcodedNodes = List.of(
224+
new BtcNodes.BtcNode(null, "alice.onion", null,
225+
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
226+
new BtcNodes.BtcNode(null, bannedOnionAddress, null,
227+
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
228+
new BtcNodes.BtcNode(null, "charlie.onion", null,
229+
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
230+
);
231+
232+
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
233+
List<String> filterProvidedBtcNodes = Collections.emptyList();
234+
List<String> bannedBtcNodes = List.of(bannedOnionAddress + ":" + 1234);
235+
236+
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
237+
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
238+
239+
assertIterableEquals(hardcodedNodes, selectedNodes);
240+
}
146241
}

0 commit comments

Comments
 (0)