synchronize on wallet lock in XmrWalletService

This commit is contained in:
woodser 2023-07-25 11:15:16 -04:00
parent 426d25f78c
commit c39c5cf387

View File

@ -111,6 +111,7 @@ public class XmrWalletService {
private TradeManager tradeManager;
private MoneroWalletRpc wallet;
private Object walletLock = new Object();
private final Map<String, Optional<MoneroTx>> txCache = new HashMap<String, Optional<MoneroTx>>();
private boolean isShutDownStarted = false;
private ExecutorService syncWalletThreadPool = Executors.newFixedThreadPool(10); // TODO: adjust based on connection type
@ -299,16 +300,16 @@ public class XmrWalletService {
}
public MoneroTxWallet createTx(List<MoneroDestination> destinations) {
synchronized (walletLock) {
try {
synchronized (wallet) {
MoneroTxWallet tx = wallet.createTx(new MoneroTxConfig().setAccountIndex(0).setDestinations(destinations).setRelay(false).setCanSplit(false));
//printTxs("XmrWalletService.createTx", tx);
return tx;
}
} catch (Exception e) {
throw e;
}
}
}
/**
* Thaw the given outputs with a lock on the wallet.
@ -316,7 +317,7 @@ public class XmrWalletService {
* @param keyImages the key images to thaw
*/
public void thawOutputs(Collection<String> keyImages) {
synchronized (getWallet()) {
synchronized (walletLock) {
for (String keyImage : keyImages) wallet.thawOutput(keyImage);
}
}
@ -365,13 +366,7 @@ public class XmrWalletService {
* @return MoneroTxWallet the multisig deposit tx
*/
public MoneroTxWallet createDepositTx(Trade trade, boolean reserveExactAmount, Integer preferredSubaddressIndex) {
Offer offer = trade.getProcessModel().getOffer();
String multisigAddress = trade.getProcessModel().getMultisigAddress();
BigInteger tradeFee = trade instanceof MakerTrade ? trade.getOffer().getMakerFee() : trade.getTakerFee();
BigInteger sendAmount = trade instanceof BuyerTrade ? BigInteger.valueOf(0) : offer.getAmount();
BigInteger securityDeposit = trade instanceof BuyerTrade ? offer.getBuyerSecurityDeposit() : offer.getSellerSecurityDeposit();
MoneroWallet wallet = getWallet();
synchronized (wallet) {
synchronized (walletLock) {
// thaw reserved outputs
if (trade.getSelf().getReserveTxKeyImages() != null) {
@ -379,6 +374,11 @@ public class XmrWalletService {
}
// create deposit tx
Offer offer = trade.getProcessModel().getOffer();
String multisigAddress = trade.getProcessModel().getMultisigAddress();
BigInteger tradeFee = trade instanceof MakerTrade ? trade.getOffer().getMakerFee() : trade.getTakerFee();
BigInteger sendAmount = trade instanceof BuyerTrade ? BigInteger.valueOf(0) : offer.getAmount();
BigInteger securityDeposit = trade instanceof BuyerTrade ? offer.getBuyerSecurityDeposit() : offer.getSellerSecurityDeposit();
long time = System.currentTimeMillis();
log.info("Creating deposit tx with multisig address={}", multisigAddress);
MoneroTxWallet depositTx = createTradeTx(tradeFee, sendAmount, securityDeposit, multisigAddress, false, reserveExactAmount, preferredSubaddressIndex);
@ -388,8 +388,8 @@ public class XmrWalletService {
}
private MoneroTxWallet createTradeTx(BigInteger tradeFee, BigInteger sendAmount, BigInteger securityDeposit, String address, boolean isReserveTx, boolean reserveExactAmount, Integer preferredSubaddressIndex) {
synchronized (walletLock) {
MoneroWallet wallet = getWallet();
synchronized (wallet) {
// create a list of subaddresses to attempt spending from in preferred order
List<Integer> subaddressIndices = new ArrayList<Integer>();
@ -814,6 +814,7 @@ public class XmrWalletService {
}
private void onConnectionChanged(MoneroRpcConnection connection) {
synchronized (walletLock) {
if (isShutDownStarted) return;
if (wallet != null && HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return;
@ -823,10 +824,8 @@ public class XmrWalletService {
if (wallet == null) maybeInitMainWallet();
else if (wallet instanceof MoneroWalletRpc && !StringUtils.equals(oldProxyUri, newProxyUri)) {
log.info("Restarting main wallet since proxy URI has changed");
synchronized (wallet) {
closeMainWallet(true);
maybeInitMainWallet();
}
} else {
wallet.setDaemonConnection(connection);
if (connection != null) {
@ -844,6 +843,7 @@ public class XmrWalletService {
log.info("Done setting main wallet daemon connection: " + (connection == null ? null : connection.getUri()));
}
}
private void notifyBalanceListeners() {
for (XmrBalanceListener balanceListener : balanceListeners) {
@ -1110,27 +1110,27 @@ public class XmrWalletService {
}
public BigInteger getBalanceForSubaddress(int subaddressIndex) {
synchronized (wallet) {
synchronized (walletLock) {
return wallet.getBalance(0, subaddressIndex);
}
}
public BigInteger getAvailableBalanceForSubaddress(int subaddressIndex) {
synchronized (wallet) {
synchronized (walletLock) {
return wallet.getUnlockedBalance(0, subaddressIndex);
}
}
public BigInteger getBalance() {
if (wallet == null) return BigInteger.valueOf(0);
synchronized (wallet) {
synchronized (walletLock) {
return wallet.getBalance(0);
}
}
public BigInteger getAvailableBalance() {
if (wallet == null) return BigInteger.valueOf(0);
synchronized (wallet) {
synchronized (walletLock) {
return wallet.getUnlockedBalance(0);
}
}