From 0372af663a41a8ded9f29c3b6581d4eb022c9beb Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 7 Feb 2023 14:07:26 -0500 Subject: [PATCH] synchronize access to account age witness maps to fix missing hash --- .../witness/AccountAgeWitnessService.java | 60 ++++++++++--------- .../core/btc/wallet/XmrWalletService.java | 4 +- .../bisq/core/payment/PaymentAccount.java | 4 +- .../tasks/ProcessPaymentReceivedMessage.java | 3 - 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java b/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java index 0840de1a41..9cc197a30b 100644 --- a/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java +++ b/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java @@ -244,7 +244,9 @@ public class AccountAgeWitnessService { @VisibleForTesting public void addToMap(AccountAgeWitness accountAgeWitness) { - accountAgeWitnessMap.putIfAbsent(accountAgeWitness.getHashAsByteArray(), accountAgeWitness); + synchronized (this) { + accountAgeWitnessMap.putIfAbsent(accountAgeWitness.getHashAsByteArray(), accountAgeWitness); + } } @@ -253,17 +255,19 @@ public class AccountAgeWitnessService { /////////////////////////////////////////////////////////////////////////////////////////// public void publishMyAccountAgeWitness(PaymentAccountPayload paymentAccountPayload) { - AccountAgeWitness accountAgeWitness = getMyWitness(paymentAccountPayload); - P2PDataStorage.ByteArray hash = accountAgeWitness.getHashAsByteArray(); - - // We use first our fast lookup cache. If its in accountAgeWitnessCache it is also in accountAgeWitnessMap - // and we do not publish. - if (accountAgeWitnessCache.containsKey(hash)) { - return; - } - - if (!accountAgeWitnessMap.containsKey(hash)) { - p2PService.addPersistableNetworkPayload(accountAgeWitness, false); + synchronized (this) { + AccountAgeWitness accountAgeWitness = getMyWitness(paymentAccountPayload); + P2PDataStorage.ByteArray hash = accountAgeWitness.getHashAsByteArray(); + + // We use first our fast lookup cache. If its in accountAgeWitnessCache it is also in accountAgeWitnessMap + // and we do not publish. + if (accountAgeWitnessCache.containsKey(hash)) { + return; + } + + if (!accountAgeWitnessMap.containsKey(hash)) { + p2PService.addPersistableNetworkPayload(accountAgeWitness, false); + } } } @@ -318,22 +322,24 @@ public class AccountAgeWitnessService { private Optional getWitnessByHash(byte[] hash) { P2PDataStorage.ByteArray hashAsByteArray = new P2PDataStorage.ByteArray(hash); + synchronized (this) { - // First we look up in our fast lookup cache - if (accountAgeWitnessCache.containsKey(hashAsByteArray)) { - return Optional.of(accountAgeWitnessCache.get(hashAsByteArray)); + // First we look up in our fast lookup cache + if (accountAgeWitnessCache.containsKey(hashAsByteArray)) { + return Optional.of(accountAgeWitnessCache.get(hashAsByteArray)); + } + + if (accountAgeWitnessMap.containsKey(hashAsByteArray)) { + AccountAgeWitness accountAgeWitness = accountAgeWitnessMap.get(hashAsByteArray); + + // We add it to our fast lookup cache + accountAgeWitnessCache.put(hashAsByteArray, accountAgeWitness); + + return Optional.of(accountAgeWitness); + } + + return Optional.empty(); } - - if (accountAgeWitnessMap.containsKey(hashAsByteArray)) { - AccountAgeWitness accountAgeWitness = accountAgeWitnessMap.get(hashAsByteArray); - - // We add it to our fast lookup cache - accountAgeWitnessCache.put(hashAsByteArray, accountAgeWitness); - - return Optional.of(accountAgeWitness); - } - - return Optional.empty(); } private Optional getWitnessByHashAsHex(String hashAsHex) { @@ -548,8 +554,8 @@ public class AccountAgeWitnessService { if (accountAgeWitnessOptional.isPresent()) { peersWitness = accountAgeWitnessOptional.get(); } else { - peersWitness = getNewWitness(peersPaymentAccountPayload, peersPubKeyRing); log.warn("We did not find the peers witness data. That is expected with peers using an older version."); + peersWitness = getNewWitness(peersPaymentAccountPayload, peersPubKeyRing); } // Check if date in witness is not older than the release date of that feature (was added in v0.6) diff --git a/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java b/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java index 3a1e64410e..7c81379b00 100644 --- a/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java +++ b/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java @@ -964,9 +964,7 @@ public class XmrWalletService { // -------------------------------- HELPERS ------------------------------- /** - * Processes internally before notifying external listeners. - * - * TODO: no longer neccessary to execute on user thread? + * Relays wallet notifications to external listeners. */ private class XmrWalletListener extends MoneroWalletListener { diff --git a/core/src/main/java/bisq/core/payment/PaymentAccount.java b/core/src/main/java/bisq/core/payment/PaymentAccount.java index c286f70a5f..d139984218 100644 --- a/core/src/main/java/bisq/core/payment/PaymentAccount.java +++ b/core/src/main/java/bisq/core/payment/PaymentAccount.java @@ -19,8 +19,6 @@ package bisq.core.payment; import bisq.core.api.model.PaymentAccountForm; import bisq.core.api.model.PaymentAccountFormField; -import bisq.core.api.model.PaymentAccountFormField.Component; -import bisq.core.api.model.PaymentAccountFormField.FieldId; import bisq.core.locale.BankUtil; import bisq.core.locale.Country; import bisq.core.locale.CountryUtil; @@ -335,7 +333,7 @@ public abstract class PaymentAccount implements PersistablePayload { * @param paymentAccountJsonString The json data representing a new payment account form. * @return A populated PaymentAccount subclass instance. */ - public static PaymentAccount fromJson(String paymentAccountJsonString) { + public static synchronized PaymentAccount fromJson(String paymentAccountJsonString) { Class clazz = getPaymentAccountClassFromJson(paymentAccountJsonString); Gson gson = gsonBuilder.registerTypeAdapter(clazz, new PaymentAccountTypeAdapter(clazz)).create(); return gson.fromJson(paymentAccountJsonString, clazz); diff --git a/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessPaymentReceivedMessage.java b/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessPaymentReceivedMessage.java index ea93871636..9a0f6e0b0b 100644 --- a/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessPaymentReceivedMessage.java +++ b/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessPaymentReceivedMessage.java @@ -79,9 +79,6 @@ public class ProcessPaymentReceivedMessage extends TradeTask { } } - // ensure connected to monero network - trade.checkWalletConnection(); - // process payout tx unless already unlocked if (!trade.isPayoutUnlocked()) processPayoutTx(message);