use cached fee rate estimates on initial server connection if available, and retrieve updates from fee rate source immediately afterwards

This commit is contained in:
Craig Raw 2024-03-28 14:14:32 +02:00
parent f0bfc44e72
commit a805d9e036
2 changed files with 37 additions and 15 deletions

View file

@ -117,6 +117,8 @@ public class AppServices {
private ElectrumServer.ConnectionService connectionService; private ElectrumServer.ConnectionService connectionService;
private ElectrumServer.FeeRatesService feeRatesService;
private Hwi.ScheduledEnumerateService deviceEnumerateService; private Hwi.ScheduledEnumerateService deviceEnumerateService;
private VersionCheckService versionCheckService; private VersionCheckService versionCheckService;
@ -188,6 +190,7 @@ public class AppServices {
public void start() { public void start() {
Config config = Config.get(); Config config = Config.get();
connectionService = createConnectionService(); connectionService = createConnectionService();
feeRatesService = createFeeRatesService();
ratesService = createRatesService(config.getExchangeSource(), config.getFiatCurrency()); ratesService = createRatesService(config.getExchangeSource(), config.getFiatCurrency());
versionCheckService = createVersionCheckService(); versionCheckService = createVersionCheckService();
torService = createTorService(); torService = createTorService();
@ -286,8 +289,13 @@ public class AppServices {
onlineProperty.setValue(true); onlineProperty.setValue(true);
onlineProperty.addListener(onlineServicesListener); onlineProperty.addListener(onlineServicesListener);
if(connectionService.getValue() != null) { FeeRatesUpdatedEvent event = connectionService.getValue();
EventManager.get().post(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 -> { connectionService.setOnFailed(failEvent -> {
@ -358,6 +366,15 @@ public class AppServices {
return connectionService; 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) { private ExchangeSource.RatesService createRatesService(ExchangeSource exchangeSource, Currency currency) {
ExchangeSource.RatesService ratesService = new ExchangeSource.RatesService( ExchangeSource.RatesService ratesService = new ExchangeSource.RatesService(
exchangeSource == null ? DEFAULT_EXCHANGE_SOURCE : exchangeSource, exchangeSource == null ? DEFAULT_EXCHANGE_SOURCE : exchangeSource,
@ -1110,12 +1127,11 @@ public class AppServices {
@Subscribe @Subscribe
public void feeRateSourceChanged(FeeRatesSourceChangedEvent event) { 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 //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 @Subscribe

View file

@ -812,13 +812,19 @@ public class ElectrumServer {
return transactionMap; return transactionMap;
} }
public Map<Integer, Double> getFeeEstimates(List<Integer> targetBlocks) throws ServerException { public Map<Integer, Double> getFeeEstimates(List<Integer> targetBlocks, boolean useCached) throws ServerException {
Map<Integer, Double> targetBlocksFeeRatesSats = getDefaultFeeEstimates(targetBlocks); Map<Integer, Double> targetBlocksFeeRatesSats = getDefaultFeeEstimates(targetBlocks);
FeeRatesSource feeRatesSource = Config.get().getFeeRatesSource(); if(useCached) {
feeRatesSource = (feeRatesSource == null ? FeeRatesSource.MEMPOOL_SPACE : feeRatesSource); if(AppServices.getTargetBlockFeeRates() != null) {
if(Network.get().equals(Network.MAINNET)) { targetBlocksFeeRatesSats.putAll(AppServices.getTargetBlockFeeRates());
targetBlocksFeeRatesSats.putAll(feeRatesSource.getBlockTargetFeeRates(targetBlocksFeeRatesSats)); }
} 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; return targetBlocksFeeRatesSats;
@ -1204,7 +1210,7 @@ public class ElectrumServer {
String banner = electrumServer.getServerBanner(); String banner = electrumServer.getServerBanner();
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE); Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, true);
Set<MempoolRateSize> mempoolRateSizes = electrumServer.getMempoolRateSizes(); Set<MempoolRateSize> mempoolRateSizes = electrumServer.getMempoolRateSizes();
feeRatesRetrievedAt = System.currentTimeMillis(); feeRatesRetrievedAt = System.currentTimeMillis();
@ -1220,7 +1226,7 @@ public class ElectrumServer {
long elapsed = System.currentTimeMillis() - feeRatesRetrievedAt; long elapsed = System.currentTimeMillis() - feeRatesRetrievedAt;
if(elapsed > FEE_RATES_PERIOD) { if(elapsed > FEE_RATES_PERIOD) {
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE); Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, false);
Set<MempoolRateSize> mempoolRateSizes = electrumServer.getMempoolRateSizes(); Set<MempoolRateSize> mempoolRateSizes = electrumServer.getMempoolRateSizes();
feeRatesRetrievedAt = System.currentTimeMillis(); feeRatesRetrievedAt = System.currentTimeMillis();
return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes); return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes);
@ -1679,7 +1685,7 @@ public class ElectrumServer {
return new Task<>() { return new Task<>() {
protected FeeRatesUpdatedEvent call() throws ServerException { protected FeeRatesUpdatedEvent call() throws ServerException {
ElectrumServer electrumServer = new ElectrumServer(); ElectrumServer electrumServer = new ElectrumServer();
Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE); Map<Integer, Double> blockTargetFeeRates = electrumServer.getFeeEstimates(AppServices.TARGET_BLOCKS_RANGE, false);
Set<MempoolRateSize> mempoolRateSizes = electrumServer.getMempoolRateSizes(); Set<MempoolRateSize> mempoolRateSizes = electrumServer.getMempoolRateSizes();
return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes); return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes);
} }