await external prices on startup

This commit is contained in:
woodser 2024-01-02 13:19:38 -05:00
parent f8d9c827ea
commit ba8a9ae21d
3 changed files with 27 additions and 14 deletions

View file

@ -403,7 +403,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
HavenoUtils.submitToThread(() -> { HavenoUtils.submitToThread(() -> {
// Wait for prices to be available // Wait for prices to be available
priceFeedService.awaitPrices(); priceFeedService.awaitExternalPrices();
// Republish means we send the complete offer object // Republish means we send the complete offer object
republishOffers(); republishOffers();

View file

@ -140,17 +140,24 @@ public class PriceFeedService {
/** /**
* Awaits prices to be available, but does not request them. * Awaits prices to be available, but does not request them.
*/ */
public void awaitPrices() { public void awaitExternalPrices() {
if (hasPrices()) return;
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { latch.countDown(); }; ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> {
if (hasExternalPrices()) latch.countDown();
};
updateCounter.addListener(listener); updateCounter.addListener(listener);
if (hasExternalPrices()) {
updateCounter.removeListener(listener);
return;
}
HavenoUtils.awaitLatch(latch); HavenoUtils.awaitLatch(latch);
updateCounter.removeListener(listener); updateCounter.removeListener(listener);
} }
public boolean hasPrices() { public boolean hasExternalPrices() {
return !cache.isEmpty(); synchronized (cache) {
return cache.values().stream().anyMatch(MarketPrice::isExternallyProvidedPrice);
}
} }
public void startRequestingPrices() { public void startRequestingPrices() {
@ -280,16 +287,20 @@ public class PriceFeedService {
@Nullable @Nullable
public MarketPrice getMarketPrice(String currencyCode) { public MarketPrice getMarketPrice(String currencyCode) {
synchronized (cache) {
return cache.getOrDefault(currencyCode, null); return cache.getOrDefault(currencyCode, null);
} }
}
private void setHavenoMarketPrice(String currencyCode, Price price) { private void setHavenoMarketPrice(String currencyCode, Price price) {
UserThread.await(() -> { UserThread.await(() -> {
synchronized (cache) {
if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) { if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) {
cache.put(currencyCode, new MarketPrice(currencyCode, cache.put(currencyCode, new MarketPrice(currencyCode,
MathUtils.scaleDownByPowerOf10(price.getValue(), CurrencyUtil.isCryptoCurrency(currencyCode) ? CryptoMoney.SMALLEST_UNIT_EXPONENT : TraditionalMoney.SMALLEST_UNIT_EXPONENT), MathUtils.scaleDownByPowerOf10(price.getValue(), CurrencyUtil.isCryptoCurrency(currencyCode) ? CryptoMoney.SMALLEST_UNIT_EXPONENT : TraditionalMoney.SMALLEST_UNIT_EXPONENT),
0, 0,
false)); false));
}
updateCounter.set(updateCounter.get() + 1); updateCounter.set(updateCounter.get() + 1);
} }
}); });
@ -456,7 +467,9 @@ public class PriceFeedService {
Map<String, MarketPrice> priceMap = result; Map<String, MarketPrice> priceMap = result;
synchronized (cache) {
cache.putAll(priceMap); cache.putAll(priceMap);
}
resultHandler.run(); resultHandler.run();
}); });

View file

@ -1022,7 +1022,7 @@ public abstract class DisputeManager<T extends DisputeList<Dispute>> extends Sup
// proposal). But if gain is larger than this loss he has economically an incentive to default in the trade. // proposal). But if gain is larger than this loss he has economically an incentive to default in the trade.
// We do all those calculations to give a hint to mediators to detect option trades. // We do all those calculations to give a hint to mediators to detect option trades.
protected void addPriceInfoMessage(Dispute dispute, int counter) { protected void addPriceInfoMessage(Dispute dispute, int counter) {
if (!priceFeedService.hasPrices()) { if (!priceFeedService.hasExternalPrices()) {
if (counter < 3) { if (counter < 3) {
log.info("Price provider has still no data. This is expected at startup. We try again in 10 sec."); log.info("Price provider has still no data. This is expected at startup. We try again in 10 sec.");
UserThread.runAfter(() -> addPriceInfoMessage(dispute, counter + 1), 10); UserThread.runAfter(() -> addPriceInfoMessage(dispute, counter + 1), 10);