diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index 4c68c86d..8e878c09 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -117,6 +117,8 @@ public class AppServices { private ElectrumServer.ConnectionService connectionService; + private ElectrumServer.FeeRatesService feeRatesService; + private Hwi.ScheduledEnumerateService deviceEnumerateService; private VersionCheckService versionCheckService; @@ -188,6 +190,7 @@ public class AppServices { public void start() { Config config = Config.get(); connectionService = createConnectionService(); + feeRatesService = createFeeRatesService(); ratesService = createRatesService(config.getExchangeSource(), config.getFiatCurrency()); versionCheckService = createVersionCheckService(); torService = createTorService(); @@ -286,8 +289,13 @@ public class AppServices { onlineProperty.setValue(true); onlineProperty.addListener(onlineServicesListener); - if(connectionService.getValue() != null) { - EventManager.get().post(connectionService.getValue()); + FeeRatesUpdatedEvent event = connectionService.getValue(); + if(event != null) { + EventManager.get().post(event); + } + + if(event instanceof ConnectionEvent && Network.get().equals(Network.MAINNET)) { + EventManager.get().post(new FeeRatesSourceChangedEvent(Config.get().getFeeRatesSource())); } }); connectionService.setOnFailed(failEvent -> { @@ -358,6 +366,15 @@ public class AppServices { return connectionService; } + private ElectrumServer.FeeRatesService createFeeRatesService() { + ElectrumServer.FeeRatesService feeRatesService = new ElectrumServer.FeeRatesService(); + feeRatesService.setOnSucceeded(workerStateEvent -> { + EventManager.get().post(feeRatesService.getValue()); + }); + + return feeRatesService; + } + private ExchangeSource.RatesService createRatesService(ExchangeSource exchangeSource, Currency currency) { ExchangeSource.RatesService ratesService = new ExchangeSource.RatesService( exchangeSource == null ? DEFAULT_EXCHANGE_SOURCE : exchangeSource, @@ -1110,12 +1127,11 @@ public class AppServices { @Subscribe public void feeRateSourceChanged(FeeRatesSourceChangedEvent event) { - ElectrumServer.FeeRatesService feeRatesService = new ElectrumServer.FeeRatesService(); - feeRatesService.setOnSucceeded(workerStateEvent -> { - EventManager.get().post(feeRatesService.getValue()); - }); //Perform once-off fee rates retrieval to immediately change displayed rates - feeRatesService.start(); + if(feeRatesService != null && !feeRatesService.isRunning() && Config.get().getMode() != Mode.OFFLINE) { + feeRatesService = createFeeRatesService(); + feeRatesService.start(); + } } @Subscribe diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java index 0424ed9b..13c3622f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java @@ -812,13 +812,19 @@ public class ElectrumServer { return transactionMap; } - public Map getFeeEstimates(List targetBlocks) throws ServerException { + public Map getFeeEstimates(List targetBlocks, boolean useCached) throws ServerException { Map targetBlocksFeeRatesSats = getDefaultFeeEstimates(targetBlocks); - FeeRatesSource feeRatesSource = Config.get().getFeeRatesSource(); - feeRatesSource = (feeRatesSource == null ? FeeRatesSource.MEMPOOL_SPACE : feeRatesSource); - if(Network.get().equals(Network.MAINNET)) { - targetBlocksFeeRatesSats.putAll(feeRatesSource.getBlockTargetFeeRates(targetBlocksFeeRatesSats)); + 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)); + } } return targetBlocksFeeRatesSats; @@ -1204,7 +1210,7 @@ public class ElectrumServer { String banner = electrumServer.getServerBanner(); - Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE); + Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, true); Set mempoolRateSizes = electrumServer.getMempoolRateSizes(); feeRatesRetrievedAt = System.currentTimeMillis(); @@ -1220,7 +1226,7 @@ public class ElectrumServer { long elapsed = System.currentTimeMillis() - feeRatesRetrievedAt; if(elapsed > FEE_RATES_PERIOD) { - Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE); + Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, false); Set mempoolRateSizes = electrumServer.getMempoolRateSizes(); feeRatesRetrievedAt = System.currentTimeMillis(); return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes); @@ -1679,7 +1685,7 @@ public class ElectrumServer { return new Task<>() { protected FeeRatesUpdatedEvent call() throws ServerException { ElectrumServer electrumServer = new ElectrumServer(); - Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE); + Map blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, false); Set mempoolRateSizes = electrumServer.getMempoolRateSizes(); return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes); }