Skip to content

Commit

Permalink
use cached fee rate estimates on initial server connection if availab…
Browse files Browse the repository at this point in the history
…le, and retrieve updates from fee rate source immediately afterwards
  • Loading branch information
craigraw committed Mar 28, 2024
1 parent f0bfc44 commit a805d9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
30 changes: 23 additions & 7 deletions src/main/java/com/sparrowwallet/sparrow/AppServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public class AppServices {

private ElectrumServer.ConnectionService connectionService;

private ElectrumServer.FeeRatesService feeRatesService;

private Hwi.ScheduledEnumerateService deviceEnumerateService;

private VersionCheckService versionCheckService;
Expand Down Expand Up @@ -188,6 +190,7 @@ private AppServices(Application application, InteractionServices interactionServ
public void start() {
Config config = Config.get();
connectionService = createConnectionService();
feeRatesService = createFeeRatesService();
ratesService = createRatesService(config.getExchangeSource(), config.getFiatCurrency());
versionCheckService = createVersionCheckService();
torService = createTorService();
Expand Down Expand Up @@ -286,8 +289,13 @@ private ElectrumServer.ConnectionService createConnectionService() {
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 -> {
Expand Down Expand Up @@ -358,6 +366,15 @@ private ElectrumServer.ConnectionService createConnectionService() {
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,
Expand Down Expand Up @@ -1110,12 +1127,11 @@ public void mempoolRateSizes(MempoolRateSizesUpdatedEvent event) {

@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
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,13 +812,19 @@ public Map<Sha256Hash, BlockTransaction> getReferencedTransactions(Set<Sha256Has
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);

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;
Expand Down Expand Up @@ -1204,7 +1210,7 @@ protected FeeRatesUpdatedEvent call() throws ServerException {

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();
feeRatesRetrievedAt = System.currentTimeMillis();

Expand All @@ -1220,7 +1226,7 @@ protected FeeRatesUpdatedEvent call() throws ServerException {

long elapsed = System.currentTimeMillis() - feeRatesRetrievedAt;
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();
feeRatesRetrievedAt = System.currentTimeMillis();
return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes);
Expand Down Expand Up @@ -1679,7 +1685,7 @@ protected Task<FeeRatesUpdatedEvent> createTask() {
return new Task<>() {
protected FeeRatesUpdatedEvent call() throws ServerException {
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();
return new FeeRatesUpdatedEvent(blockTargetFeeRates, mempoolRateSizes);
}
Expand Down

0 comments on commit a805d9e

Please sign in to comment.