diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index 8e878c09..ac0ec31c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -294,8 +294,10 @@ public class AppServices { EventManager.get().post(event); } - if(event instanceof ConnectionEvent && Network.get().equals(Network.MAINNET)) { - EventManager.get().post(new FeeRatesSourceChangedEvent(Config.get().getFeeRatesSource())); + FeeRatesSource feeRatesSource = Config.get().getFeeRatesSource(); + feeRatesSource = (feeRatesSource == null ? FeeRatesSource.MEMPOOL_SPACE : feeRatesSource); + if(event instanceof ConnectionEvent && Network.get().equals(Network.MAINNET) && feeRatesSource.isExternal()) { + EventManager.get().post(new FeeRatesSourceChangedEvent(feeRatesSource)); } }); connectionService.setOnFailed(failEvent -> { @@ -1122,7 +1124,9 @@ public class AppServices { @Subscribe public void mempoolRateSizes(MempoolRateSizesUpdatedEvent event) { - addMempoolRateSizes(event.getMempoolRateSizes()); + if(event.getMempoolRateSizes() != null) { + addMempoolRateSizes(event.getMempoolRateSizes()); + } } @Subscribe diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java index 13c3622f..888bbd77 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java @@ -815,16 +815,16 @@ public class ElectrumServer { public Map getFeeEstimates(List targetBlocks, boolean useCached) throws ServerException { Map targetBlocksFeeRatesSats = getDefaultFeeEstimates(targetBlocks); - if(useCached) { + FeeRatesSource feeRatesSource = Config.get().getFeeRatesSource(); + feeRatesSource = (feeRatesSource == null ? FeeRatesSource.MEMPOOL_SPACE : feeRatesSource); + if(!feeRatesSource.isExternal()) { + targetBlocksFeeRatesSats.putAll(feeRatesSource.getBlockTargetFeeRates(targetBlocksFeeRatesSats)); + } else if(useCached) { if(AppServices.getTargetBlockFeeRates() != null) { targetBlocksFeeRatesSats.putAll(AppServices.getTargetBlockFeeRates()); } - } else { - FeeRatesSource feeRatesSource = Config.get().getFeeRatesSource(); - feeRatesSource = (feeRatesSource == null ? FeeRatesSource.MEMPOOL_SPACE : feeRatesSource); - if(Network.get().equals(Network.MAINNET)) { - targetBlocksFeeRatesSats.putAll(feeRatesSource.getBlockTargetFeeRates(targetBlocksFeeRatesSats)); - } + } else if(Network.get().equals(Network.MAINNET)) { + targetBlocksFeeRatesSats.putAll(feeRatesSource.getBlockTargetFeeRates(targetBlocksFeeRatesSats)); } return targetBlocksFeeRatesSats; @@ -1686,8 +1686,7 @@ public class ElectrumServer { protected FeeRatesUpdatedEvent call() throws ServerException { ElectrumServer electrumServer = new ElectrumServer(); Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, false); - Set mempoolRateSizes = electrumServer.getMempoolRateSizes(); - return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes); + return new FeeRatesUpdatedEvent(blockTargetFeeRates, null); } }; } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/FeeRatesSource.java b/src/main/java/com/sparrowwallet/sparrow/net/FeeRatesSource.java index e3f112b8..0a2fb5df 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/FeeRatesSource.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/FeeRatesSource.java @@ -9,27 +9,27 @@ import java.util.LinkedHashMap; import java.util.Map; public enum FeeRatesSource { - ELECTRUM_SERVER("Server") { + ELECTRUM_SERVER("Server", false) { @Override public Map getBlockTargetFeeRates(Map defaultblockTargetFeeRates) { return Collections.emptyMap(); } }, - MEMPOOL_SPACE("mempool.space") { + MEMPOOL_SPACE("mempool.space", true) { @Override public Map getBlockTargetFeeRates(Map defaultblockTargetFeeRates) { String url = AppServices.isUsingProxy() ? "http://mempoolhqx4isw62xs7abwphsq7ldayuidyx2v2oethdhhj6mlo2r6ad.onion/api/v1/fees/recommended" : "https://mempool.space/api/v1/fees/recommended"; return getThreeTierFeeRates(this, defaultblockTargetFeeRates, url); } }, - BITCOINFEES_EARN_COM("bitcoinfees.earn.com") { + BITCOINFEES_EARN_COM("bitcoinfees.earn.com", true) { @Override public Map getBlockTargetFeeRates(Map defaultblockTargetFeeRates) { String url = "https://bitcoinfees.earn.com/api/v1/fees/recommended"; return getThreeTierFeeRates(this, defaultblockTargetFeeRates, url); } }, - MINIMUM("Minimum (1 sat/vB)") { + MINIMUM("Minimum (1 sat/vB)", false) { @Override public Map getBlockTargetFeeRates(Map defaultblockTargetFeeRates) { Map blockTargetFeeRates = new LinkedHashMap<>(); @@ -40,7 +40,7 @@ public enum FeeRatesSource { return blockTargetFeeRates; } }, - OXT_ME("oxt.me") { + OXT_ME("oxt.me", true) { @Override public Map getBlockTargetFeeRates(Map defaultblockTargetFeeRates) { String url = AppServices.isUsingProxy() ? "http://oxtwshnfyktikbflierkwcxxksbonl6v73l5so5zky7ur72w52tktkid.onion/stats/global/mempool" : "https://api.oxt.me/stats/global/mempool"; @@ -63,9 +63,11 @@ public enum FeeRatesSource { public static final int BLOCKS_IN_TWO_HOURS = 12; private final String name; + private final boolean external; - FeeRatesSource(String name) { + FeeRatesSource(String name, boolean external) { this.name = name; + this.external = external; } public abstract Map getBlockTargetFeeRates(Map defaultblockTargetFeeRates); @@ -74,6 +76,10 @@ public enum FeeRatesSource { return name; } + public boolean isExternal() { + return external; + } + private static Map getThreeTierFeeRates(FeeRatesSource feeRatesSource, Map defaultblockTargetFeeRates, String url) { if(log.isInfoEnabled()) { log.info("Requesting fee rates from " + url);