Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for local monerod #757

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import haveno.common.ThreadUtils;
import haveno.common.UserThread;
import haveno.common.app.DevEnv;
import haveno.common.config.BaseCurrencyNetwork;
import haveno.common.config.Config;
import haveno.core.trade.HavenoUtils;
import haveno.core.user.Preferences;
Expand Down Expand Up @@ -612,8 +611,7 @@ private void pollDaemonInfo() {
chainHeight.set(lastInfo.getHeight());

// update sync progress
boolean isTestnet = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL;
if (lastInfo.isSynchronized() || isTestnet) doneDownload(); // TODO: skipping synchronized check for testnet because tests cannot sync 3rd local node, see "Can manage Monero daemon connections"
if (lastInfo.isSynchronized()) doneDownload();
else if (lastInfo.isBusySyncing()) {
long targetHeight = lastInfo.getTargetHeight();
long blocksLeft = targetHeight - lastInfo.getHeight();
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/haveno/core/api/XmrLocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class XmrLocalNode {
MONEROD_ARGS.add(MONEROD_PATH);
MONEROD_ARGS.add("--no-igd");
MONEROD_ARGS.add("--hide-my-port");
MONEROD_ARGS.add("--p2p-bind-ip");
MONEROD_ARGS.add(HavenoUtils.LOOPBACK_HOST);
if (!Config.baseCurrencyNetwork().isMainnet()) MONEROD_ARGS.add("--" + Config.baseCurrencyNetwork().getNetwork().toLowerCase());
}

Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/haveno/core/offer/OpenOfferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public void shutDown(@Nullable Runnable completeHandler) {
// Force broadcaster to send out immediately, otherwise we could have a 2 sec delay until the
// bundled messages sent out.
broadcaster.flush();
shutDownThreadPool();

if (completeHandler != null) {
// For typical number of offers we are tolerant with delay to give enough time to broadcast.
Expand All @@ -335,12 +336,18 @@ public void shutDown(@Nullable Runnable completeHandler) {
}, THREAD_ID);
} else {
broadcaster.flush();
shutDownThreadPool();
if (completeHandler != null)
completeHandler.run();
}
}

// shut down pool
ThreadUtils.shutDown(THREAD_ID, SHUTDOWN_TIMEOUT_MS);
private void shutDownThreadPool() {
try {
ThreadUtils.shutDown(THREAD_ID, SHUTDOWN_TIMEOUT_MS);
} catch (Exception e) {
log.error("Error shutting down OpenOfferManager thread pool", e);
}
}

public void removeAllOpenOffers(@Nullable Runnable completeHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void requestPrices() {
public void awaitExternalPrices() {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> {
if (hasExternalPrices()) latch.countDown();
if (hasExternalPrices() && latch.getCount() != 0) latch.countDown();
};
updateCounter.addListener(listener);
if (hasExternalPrices()) {
Expand Down Expand Up @@ -277,12 +277,14 @@ private void retryWithNewProvider() {

// returns true if provider selection loops back to beginning
private boolean setNewPriceProvider() {
httpClient.cancelPendingRequest();
boolean looped = providersRepository.selectNextProviderBaseUrl();
if (!providersRepository.getBaseUrl().isEmpty())
if (!providersRepository.getBaseUrl().isEmpty()) {
priceProvider = new PriceProvider(httpClient, providersRepository.getBaseUrl());
else
} else {
log.warn("We cannot create a new priceProvider because new base url is empty.");
return looped;
}
return looped;
}

@Nullable
Expand All @@ -293,7 +295,7 @@ public MarketPrice getMarketPrice(String currencyCode) {
}

private void setHavenoMarketPrice(String currencyCode, Price price) {
UserThread.await(() -> {
UserThread.execute(() -> {
synchronized (cache) {
if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) {
cache.put(currencyCode, new MarketPrice(currencyCode,
Expand Down Expand Up @@ -374,7 +376,7 @@ public void applyLatestHavenoMarketPrice(Set<TradeStatistics3> tradeStatisticsSe
*/
public synchronized Map<String, MarketPrice> requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { latch.countDown(); };
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { if (latch.getCount() != 0) latch.countDown(); };
updateCounter.addListener(listener);
requestAllPricesError = null;
requestPrices();
Expand Down Expand Up @@ -442,7 +444,7 @@ private boolean applyPriceToConsumer() {
faultHandler.handleFault(errorMessage, new PriceRequestException(errorMessage));
}

UserThread.await(() -> updateCounter.set(updateCounter.get() + 1));
UserThread.execute(() -> updateCounter.set(updateCounter.get() + 1));

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/haveno/core/xmr/XmrNodeSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static XmrNodeSettings fromProto(protobuf.XmrNodeSettings proto) {
public protobuf.XmrNodeSettings toProtoMessage() {
protobuf.XmrNodeSettings.Builder builder = protobuf.XmrNodeSettings.newBuilder();
Optional.ofNullable(blockchainPath).ifPresent(e -> builder.setBlockchainPath(blockchainPath));
Optional.ofNullable(bootstrapUrl).ifPresent(e -> builder.setBlockchainPath(bootstrapUrl));
Optional.ofNullable(bootstrapUrl).ifPresent(e -> builder.setBootstrapUrl(bootstrapUrl));
Optional.ofNullable(startupFlags).ifPresent(e -> builder.addAllStartupFlags(startupFlags));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public abstract class MutableOfferDataModel extends OfferDataModel {
private final CoinFormatter btcFormatter;
private final Navigation navigation;
private final String offerId;
private final XmrBalanceListener xmrBalanceListener;
private final SetChangeListener<PaymentAccount> paymentAccountsChangeListener;

protected OfferDirection direction;
Expand Down Expand Up @@ -128,6 +127,7 @@ public abstract class MutableOfferDataModel extends OfferDataModel {
protected long triggerPrice;
@Getter
protected boolean reserveExactAmount;
private XmrBalanceListener xmrBalanceListener;


///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -163,20 +163,12 @@ public MutableOfferDataModel(CreateOfferService createOfferService,

offerId = OfferUtil.getRandomOfferId();
shortOfferId = Utilities.getShortId(offerId);
addressEntry = xmrWalletService.getOrCreateAddressEntry(offerId, XmrAddressEntry.Context.OFFER_FUNDING);

reserveExactAmount = preferences.getSplitOfferOutput();

useMarketBasedPrice.set(preferences.isUsePercentageBasedPrice());
buyerSecurityDepositPct.set(Restrictions.getMinBuyerSecurityDepositAsPercent());

xmrBalanceListener = new XmrBalanceListener(getAddressEntry().getSubaddressIndex()) {
@Override
public void onBalanceChanged(BigInteger balance) {
updateBalance();
}
};

paymentAccountsChangeListener = change -> fillPaymentAccounts();
}

Expand Down Expand Up @@ -212,6 +204,14 @@ private void removeListeners() {

// called before activate()
public boolean initWithData(OfferDirection direction, TradeCurrency tradeCurrency) {
addressEntry = xmrWalletService.getOrCreateAddressEntry(offerId, XmrAddressEntry.Context.OFFER_FUNDING);
xmrBalanceListener = new XmrBalanceListener(getAddressEntry().getSubaddressIndex()) {
@Override
public void onBalanceChanged(BigInteger balance) {
updateBalance();
}
};

this.direction = direction;
this.tradeCurrency = tradeCurrency;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ public MutableOfferViewModel(M dataModel,

paymentLabel = Res.get("createOffer.fundsBox.paymentLabel", dataModel.shortOfferId);

if (dataModel.getAddressEntry() != null) {
addressAsString = dataModel.getAddressEntry().getAddressString();
}
createListeners();
}

Expand Down Expand Up @@ -568,6 +565,9 @@ private void removeListeners() {

boolean initWithData(OfferDirection direction, TradeCurrency tradeCurrency) {
boolean result = dataModel.initWithData(direction, tradeCurrency);
if (dataModel.getAddressEntry() != null) {
addressAsString = dataModel.getAddressEntry().getAddressString();
}
if (dataModel.paymentAccount != null)
xmrValidator.setMaxValue(dataModel.paymentAccount.getPaymentMethod().getMaxTradeLimit(dataModel.getTradeCurrencyCode().get()));
xmrValidator.setMaxTradeLimit(BigInteger.valueOf(dataModel.getMaxTradeLimit()));
Expand Down
2 changes: 2 additions & 0 deletions p2p/src/main/java/haveno/network/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ String post(String param,

boolean hasPendingRequest();

void cancelPendingRequest();

void shutDown();
}
18 changes: 18 additions & 0 deletions p2p/src/main/java/haveno/network/http/HttpClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ private String doRequest(String param,
}
}

public void cancelPendingRequest() {
if (!hasPendingRequest) return;
try {
if (connection != null) {
connection.getInputStream().close();
connection.disconnect();
connection = null;
}
if (closeableHttpClient != null) {
closeableHttpClient.close();
closeableHttpClient = null;
}
} catch (IOException err) {
// igbnore
}
hasPendingRequest = false;
}

private String requestWithoutProxy(String baseUrl,
String param,
HttpMethod httpMethod,
Expand Down
Loading