Skip to content

Commit

Permalink
Filter banned seednodes with wrong format
Browse files Browse the repository at this point in the history
  • Loading branch information
ripcurlx committed May 5, 2021
1 parent a486093 commit 0731804
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import org.jetbrains.annotations.Nullable;

// If a new BaseCurrencyNetwork type gets added we need to add the resource file for it as well!
@Slf4j
@Singleton
Expand Down Expand Up @@ -69,11 +72,12 @@ private void reload() {
List<NodeAddress> result = getSeedNodeAddressesFromPropertyFile(config.baseCurrencyNetwork.name().toLowerCase());
cache.addAll(result);

// filter
// let values configured by filter fail more gracefully
cache.removeAll(
config.bannedSeedNodes.stream()
.filter(n -> !n.isEmpty())
.map(NodeAddress::new)
.map(this::getNodeAddress)
.filter(Objects::nonNull)
.collect(Collectors.toSet()));

log.info("Seed nodes: {}", cache);
Expand Down Expand Up @@ -124,4 +128,14 @@ public boolean isSeedNode(NodeAddress nodeAddress) {
reload();
return cache.contains(nodeAddress);
}

@Nullable
private NodeAddress getNodeAddress(String n) {
try {
return new NodeAddress(n);
} catch (Throwable t) {
log.error("exception when filtering banned seednodes", t);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import bisq.common.config.Config;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -45,4 +46,21 @@ public void manualSeedNodes() {
Assert.assertTrue(DUT.getSeedNodeAddresses().contains(new NodeAddress(seed1)));
Assert.assertTrue(DUT.getSeedNodeAddresses().contains(new NodeAddress(seed2)));
}

@Test
public void ignoreBannedSeedNodesWithWrongFormat() {
String seed1 = "asdfbroken";
String seed2 = "localhost:2002";
String baseCurrencyNetwork = format("--%s=%s", Config.BASE_CURRENCY_NETWORK, "btc_regtest");
String bannedSeedNodesOption = format("--%s=%s,%s", Config.BANNED_SEED_NODES, seed1, seed2);
Config config = new Config(baseCurrencyNetwork, bannedSeedNodesOption);
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(config);
Assert.assertFalse(DUT.getSeedNodeAddresses().contains(new NodeAddress(seed2)));
}

@After
public void tearDown() {
//restore default Config
new Config();
}
}

0 comments on commit 0731804

Please sign in to comment.