cache incoming txs to improve performance of funds tab

This commit is contained in:
woodser 2022-12-23 12:20:19 +00:00
parent a21628ad0d
commit 1c6d4cec83
3 changed files with 18 additions and 9 deletions

View file

@ -914,16 +914,21 @@ public class XmrWalletService {
return getNumTxOutputsForSubaddress(subaddressIndex, null);
}
private int getNumTxOutputsForSubaddress(int subaddressIndex, List<MoneroTxWallet> incomingTxs) {
public int getNumTxOutputsForSubaddress(int subaddressIndex, List<MoneroTxWallet> incomingTxs) {
if (incomingTxs == null) incomingTxs = getIncomingTxs(subaddressIndex);
int numUnspentOutputs = 0;
for (MoneroTxWallet tx : incomingTxs) {
if (tx.getTransfers(new MoneroTransferQuery().setSubaddressIndex(subaddressIndex)).isEmpty()) continue;
numUnspentOutputs += tx.isConfirmed() ? tx.getOutputsWallet(new MoneroOutputQuery().setSubaddressIndex(subaddressIndex)).size() : 1; // TODO: monero-project does not provide outputs for unconfirmed txs
}
return numUnspentOutputs;
}
private List<MoneroTxWallet> getIncomingTxs(Integer subaddressIndex) {
public List<MoneroTxWallet> getIncomingTxs() {
return getIncomingTxs(null);
}
public List<MoneroTxWallet> getIncomingTxs(Integer subaddressIndex) {
return wallet.getTxs(new MoneroTxQuery()
.setTransferQuery((new MoneroTransferQuery()
.setAccountIndex(0)
@ -970,7 +975,7 @@ public class XmrWalletService {
// TODO (woodser): update balance and other listening
public void addBalanceListener(XmrBalanceListener listener) {
balanceListeners.add(listener);
if (!balanceListeners.contains(listener)) balanceListeners.add(listener);
}
public void removeBalanceListener(XmrBalanceListener listener) {

View file

@ -59,7 +59,7 @@ class DepositListItem {
return lazyFieldsSupplier.get();
}
DepositListItem(XmrAddressEntry addressEntry, XmrWalletService xmrWalletService, CoinFormatter formatter) {
DepositListItem(XmrAddressEntry addressEntry, XmrWalletService xmrWalletService, CoinFormatter formatter, List<MoneroTxWallet> cachedTxs) {
this.xmrWalletService = xmrWalletService;
this.addressEntry = addressEntry;
@ -68,7 +68,7 @@ class DepositListItem {
public void onBalanceChanged(BigInteger balance) {
DepositListItem.this.balanceAsCoin = HavenoUtils.atomicUnitsToCoin(balance);
DepositListItem.this.balance.set(formatter.formatCoin(balanceAsCoin));
updateUsage(addressEntry.getSubaddressIndex());
updateUsage(addressEntry.getSubaddressIndex(), null);
}
};
xmrWalletService.addBalanceListener(balanceListener);
@ -76,7 +76,7 @@ class DepositListItem {
balanceAsCoin = xmrWalletService.getBalanceForSubaddress(addressEntry.getSubaddressIndex());
balance.set(formatter.formatCoin(balanceAsCoin));
updateUsage(addressEntry.getSubaddressIndex());
updateUsage(addressEntry.getSubaddressIndex(), cachedTxs);
// confidence
lazyFieldsSupplier = Suppliers.memoize(() -> new LazyFields() {{
@ -95,8 +95,8 @@ class DepositListItem {
}});
}
private void updateUsage(int subaddressIndex) {
numTxOutputs = xmrWalletService.getNumTxOutputsForSubaddress(addressEntry.getSubaddressIndex());
private void updateUsage(int subaddressIndex, List<MoneroTxWallet> cachedTxs) {
numTxOutputs = xmrWalletService.getNumTxOutputsForSubaddress(addressEntry.getSubaddressIndex(), cachedTxs);
usage = numTxOutputs == 0 ? Res.get("funds.deposit.unused") : Res.get("funds.deposit.usedInTx", numTxOutputs);
}

View file

@ -52,6 +52,7 @@ import net.glxn.qrgen.image.ImageType;
import javax.inject.Inject;
import javax.inject.Named;
import monero.wallet.model.MoneroTxConfig;
import monero.wallet.model.MoneroTxWallet;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
@ -83,6 +84,7 @@ import javafx.util.Callback;
import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
@ -298,8 +300,10 @@ public class DepositView extends ActivatableView<VBox, Void> {
private void updateList() {
observableList.forEach(DepositListItem::cleanup);
observableList.clear();
List<MoneroTxWallet> incomingTxs = xmrWalletService.getIncomingTxs(); // cache incoming txs for performance
xmrWalletService.getAvailableAddressEntries()
.forEach(e -> observableList.add(new DepositListItem(e, xmrWalletService, formatter)));
.forEach(e -> observableList.add(new DepositListItem(e, xmrWalletService, formatter, incomingTxs)));
}
private Coin getAmountAsCoin() {