prefer local price provider, preserve provider when banned nodes applied

This commit is contained in:
woodser 2024-11-10 11:15:59 -05:00
parent 6730d023d6
commit 01aea56087

View file

@ -37,6 +37,7 @@ package haveno.core.provider;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named; import com.google.inject.name.Named;
import haveno.common.config.Config; import haveno.common.config.Config;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -47,6 +48,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ProvidersRepository { public class ProvidersRepository {
private static final String DEFAULT_LOCAL_NODE = "http://localhost:8078/";
private static final List<String> DEFAULT_NODES = Arrays.asList( private static final List<String> DEFAULT_NODES = Arrays.asList(
"http://elaxlgigphpicy5q7pi5wkz2ko2vgjbq4576vic7febmx4xcxvk6deqd.onion/", // Haveno "http://elaxlgigphpicy5q7pi5wkz2ko2vgjbq4576vic7febmx4xcxvk6deqd.onion/", // Haveno
"http://lrrgpezvdrbpoqvkavzobmj7dr2otxc5x6wgktrw337bk6mxsvfp5yid.onion/" // Cake "http://lrrgpezvdrbpoqvkavzobmj7dr2otxc5x6wgktrw337bk6mxsvfp5yid.onion/" // Cake
@ -78,19 +81,22 @@ public class ProvidersRepository {
this.providersFromProgramArgs = providers; this.providersFromProgramArgs = providers;
this.useLocalhostForP2P = useLocalhostForP2P; this.useLocalhostForP2P = useLocalhostForP2P;
Collections.shuffle(DEFAULT_NODES); Collections.shuffle(DEFAULT_NODES); // randomize order of default nodes
applyBannedNodes(config.bannedPriceRelayNodes); applyBannedNodes(config.bannedPriceRelayNodes);
} }
public void applyBannedNodes(@Nullable List<String> bannedNodes) { public void applyBannedNodes(@Nullable List<String> bannedNodes) {
this.bannedNodes = bannedNodes; this.bannedNodes = bannedNodes;
// fill provider list
fillProviderList(); fillProviderList();
selectNextProviderBaseUrl();
// select next provider if current provider is null or banned
if (baseUrl == null || isBanned(baseUrl)) selectNextProviderBaseUrl();
if (bannedNodes != null && !bannedNodes.isEmpty()) { if (bannedNodes != null && !bannedNodes.isEmpty()) {
log.info("Excluded provider nodes from filter: nodes={}, selected provider baseUrl={}, providerList={}", log.info("Excluded provider nodes from filter: nodes={}, selected provider baseUrl={}, providerList={}", bannedNodes, baseUrl, providerList);
bannedNodes, baseUrl, providerList);
} }
} }
@ -129,22 +135,30 @@ public class ProvidersRepository {
// If we run in localhost mode we don't have the tor node running, so we need a clearnet host // If we run in localhost mode we don't have the tor node running, so we need a clearnet host
// Use localhost for using a locally running provider // Use localhost for using a locally running provider
providers = List.of( providers = List.of(
"http://localhost:8078/", DEFAULT_LOCAL_NODE,
"https://price.haveno.network/", "https://price.haveno.network/",
"http://173.230.142.36:8078/"); "http://173.230.142.36:8078/");
} else { } else {
providers = DEFAULT_NODES; providers = new ArrayList<String>();
providers.add(DEFAULT_LOCAL_NODE); // try local provider first
providers.addAll(DEFAULT_NODES);
} }
} else { } else {
providers = providersFromProgramArgs; providers = providersFromProgramArgs;
} }
providerList = providers.stream() providerList = providers.stream()
.filter(e -> bannedNodes == null || .filter(e -> !isBanned(e))
!bannedNodes.contains(e.replace("http://", "")
.replace("/", "")
.replace(".onion", "")))
.map(e -> e.endsWith("/") ? e : e + "/") .map(e -> e.endsWith("/") ? e : e + "/")
.map(e -> e.startsWith("http") ? e : "http://" + e) .map(e -> e.startsWith("http") ? e : "http://" + e)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private boolean isBanned(String provider) {
if (bannedNodes == null) return false;
return bannedNodes.stream()
.anyMatch(e -> provider.replace("http://", "")
.replace("/", "")
.replace(".onion", "")
.equals(e));
}
} }