synchronize on lock to prepare payment sent or received

This commit is contained in:
woodser 2024-10-25 11:35:55 -04:00
parent 5c4fa7a53f
commit 6db4812f06
2 changed files with 45 additions and 31 deletions

View File

@ -36,6 +36,7 @@ package haveno.core.trade.protocol.tasks;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import haveno.common.taskrunner.TaskRunner; import haveno.common.taskrunner.TaskRunner;
import haveno.core.trade.HavenoUtils;
import haveno.core.trade.Trade; import haveno.core.trade.Trade;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.wallet.MoneroWallet; import monero.wallet.MoneroWallet;
@ -79,15 +80,21 @@ public class BuyerPreparePaymentSentMessage extends TradeTask {
// create payout tx if we have seller's updated multisig hex // create payout tx if we have seller's updated multisig hex
if (trade.getSeller().getUpdatedMultisigHex() != null) { if (trade.getSeller().getUpdatedMultisigHex() != null) {
// import multisig hex // synchronize on lock for wallet operations
trade.importMultisigHex(); synchronized (trade.getWalletLock()) {
synchronized (HavenoUtils.getWalletFunctionLock()) {
// create payout tx // import multisig hex
log.info("Buyer creating unsigned payout tx for {} {} ", trade.getClass().getSimpleName(), trade.getShortId()); trade.importMultisigHex();
MoneroTxWallet payoutTx = trade.createPayoutTx();
trade.updatePayout(payoutTx); // create payout tx
trade.getSelf().setUnsignedPayoutTxHex(payoutTx.getTxSet().getMultisigTxHex()); log.info("Buyer creating unsigned payout tx for {} {} ", trade.getClass().getSimpleName(), trade.getShortId());
trade.requestPersistence(); MoneroTxWallet payoutTx = trade.createPayoutTx();
trade.updatePayout(payoutTx);
trade.getSelf().setUnsignedPayoutTxHex(payoutTx.getTxSet().getMultisigTxHex());
trade.requestPersistence();
}
}
} }
complete(); complete();

View File

@ -19,6 +19,7 @@ package haveno.core.trade.protocol.tasks;
import haveno.common.taskrunner.TaskRunner; import haveno.common.taskrunner.TaskRunner;
import haveno.core.support.dispute.Dispute; import haveno.core.support.dispute.Dispute;
import haveno.core.trade.HavenoUtils;
import haveno.core.trade.Trade; import haveno.core.trade.Trade;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import monero.wallet.model.MoneroTxWallet; import monero.wallet.model.MoneroTxWallet;
@ -49,33 +50,39 @@ public class SellerPreparePaymentReceivedMessage extends TradeTask {
trade.setPayoutTxHex(null); trade.setPayoutTxHex(null);
} }
// import multisig hex unless already signed // synchronize on lock for wallet operations
if (trade.getPayoutTxHex() == null) { synchronized (trade.getWalletLock()) {
trade.importMultisigHex(); synchronized (HavenoUtils.getWalletFunctionLock()) {
}
// verify, sign, and publish payout tx if given // import multisig hex unless already signed
if (trade.getBuyer().getPaymentSentMessage().getPayoutTxHex() != null) {
try {
if (trade.getPayoutTxHex() == null) { if (trade.getPayoutTxHex() == null) {
log.info("Seller verifying, signing, and publishing payout tx for trade {}", trade.getId()); trade.importMultisigHex();
trade.processPayoutTx(trade.getBuyer().getPaymentSentMessage().getPayoutTxHex(), true, true);
} else {
log.warn("Seller publishing previously signed payout tx for trade {}", trade.getId());
trade.processPayoutTx(trade.getPayoutTxHex(), false, true);
} }
} catch (IllegalArgumentException | IllegalStateException e) {
log.warn("Illegal state or argument verifying, signing, and publishing payout tx for {} {}: {}. Creating new unsigned payout tx", trade.getClass().getSimpleName(), trade.getId(), e.getMessage(), e);
createUnsignedPayoutTx();
} catch (Exception e) {
log.warn("Error verifying, signing, and publishing payout tx for trade {}: {}", trade.getId(), e.getMessage(), e);
throw e;
}
}
// otherwise create unsigned payout tx // verify, sign, and publish payout tx if given
else if (trade.getSelf().getUnsignedPayoutTxHex() == null) { if (trade.getBuyer().getPaymentSentMessage().getPayoutTxHex() != null) {
createUnsignedPayoutTx(); try {
if (trade.getPayoutTxHex() == null) {
log.info("Seller verifying, signing, and publishing payout tx for trade {}", trade.getId());
trade.processPayoutTx(trade.getBuyer().getPaymentSentMessage().getPayoutTxHex(), true, true);
} else {
log.warn("Seller publishing previously signed payout tx for trade {}", trade.getId());
trade.processPayoutTx(trade.getPayoutTxHex(), false, true);
}
} catch (IllegalArgumentException | IllegalStateException e) {
log.warn("Illegal state or argument verifying, signing, and publishing payout tx for {} {}. Creating new unsigned payout tx. error={}. ", trade.getClass().getSimpleName(), trade.getId(), e.getMessage(), e);
createUnsignedPayoutTx();
} catch (Exception e) {
log.warn("Error verifying, signing, and publishing payout tx for trade {}: {}", trade.getId(), e.getMessage(), e);
throw e;
}
}
// otherwise create unsigned payout tx
else if (trade.getSelf().getUnsignedPayoutTxHex() == null) {
createUnsignedPayoutTx();
}
}
} }
} else if (trade.getArbitrator().getPaymentReceivedMessage().getSignedPayoutTxHex() != null && !trade.isPayoutPublished()) { } else if (trade.getArbitrator().getPaymentReceivedMessage().getSignedPayoutTxHex() != null && !trade.isPayoutPublished()) {