avoid fetching wallet subaddresses individually by caching

This commit is contained in:
woodser 2024-01-08 09:59:38 -05:00
parent c28ffb70ff
commit 6c83fc4cf8
3 changed files with 41 additions and 24 deletions

View file

@ -32,6 +32,7 @@ import javafx.beans.property.StringProperty;
import javafx.scene.control.Tooltip;
import lombok.extern.slf4j.Slf4j;
import monero.daemon.model.MoneroTx;
import monero.wallet.model.MoneroSubaddress;
import monero.wallet.model.MoneroTxWallet;
import java.math.BigInteger;
@ -56,14 +57,14 @@ class DepositListItem {
return lazyFieldsSupplier.get();
}
DepositListItem(XmrAddressEntry addressEntry, XmrWalletService xmrWalletService, CoinFormatter formatter, List<MoneroTxWallet> cachedTxs) {
DepositListItem(XmrAddressEntry addressEntry, XmrWalletService xmrWalletService, CoinFormatter formatter, List<MoneroTxWallet> cachedTxs, List<MoneroSubaddress> cachedSubaddresses) {
this.xmrWalletService = xmrWalletService;
this.addressEntry = addressEntry;
balanceAsBI = xmrWalletService.getBalanceForSubaddress(addressEntry.getSubaddressIndex());
balanceAsBI = xmrWalletService.getBalanceForSubaddress(addressEntry.getSubaddressIndex(), cachedSubaddresses);
balance.set(HavenoUtils.formatXmr(balanceAsBI));
updateUsage(addressEntry.getSubaddressIndex(), cachedTxs);
updateUsage(addressEntry.getSubaddressIndex(), cachedTxs, cachedSubaddresses);
// confidence
lazyFieldsSupplier = Suppliers.memoize(() -> new LazyFields() {{
@ -82,8 +83,8 @@ class DepositListItem {
}});
}
private void updateUsage(int subaddressIndex, List<MoneroTxWallet> cachedTxs) {
numTxsWithOutputs = xmrWalletService.getNumTxsWithIncomingOutputs(addressEntry.getSubaddressIndex(), cachedTxs);
private void updateUsage(int subaddressIndex, List<MoneroTxWallet> cachedTxs, List<MoneroSubaddress> cachedSubaddresses) {
numTxsWithOutputs = xmrWalletService.getNumTxsWithIncomingOutputs(addressEntry.getSubaddressIndex(), cachedTxs, cachedSubaddresses);
switch (addressEntry.getContext()) {
case BASE_ADDRESS:
usage = Res.get("funds.deposit.baseAddress");

View file

@ -59,6 +59,7 @@ import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import monero.wallet.model.MoneroSubaddress;
import monero.wallet.model.MoneroTxConfig;
import monero.wallet.model.MoneroTxWallet;
import monero.wallet.model.MoneroWalletListener;
@ -136,11 +137,12 @@ public class DepositView extends ActivatableView<VBox, Void> {
// try to initialize with wallet txs
try {
// prefetch all incoming txs to avoid query per subaddress
// prefetch to avoid query per subaddress
txsWithIncomingOutputs = xmrWalletService.getTxsWithIncomingOutputs();
List<MoneroSubaddress> subaddresses = xmrWalletService.getWallet().getSubaddresses(0);
// trigger creation of at least 1 address
xmrWalletService.getFreshAddressEntry(txsWithIncomingOutputs);
xmrWalletService.getFreshAddressEntry(txsWithIncomingOutputs, subaddresses);
} catch (Exception e) {
log.warn("Failed to get wallet txs to initialize DepositView");
e.printStackTrace();
@ -318,13 +320,14 @@ public class DepositView extends ActivatableView<VBox, Void> {
// cache incoming txs
txsWithIncomingOutputs = xmrWalletService.getTxsWithIncomingOutputs();
List<MoneroSubaddress> subaddresses = xmrWalletService.getWallet().getSubaddresses(0);
// create deposit list items
List<XmrAddressEntry> addressEntries = xmrWalletService.getAddressEntries();
List<DepositListItem> items = new ArrayList<>();
for (XmrAddressEntry addressEntry : addressEntries) {
if (addressEntry.getContext().isReserved()) continue;
items.add(new DepositListItem(addressEntry, xmrWalletService, formatter, txsWithIncomingOutputs));
items.add(new DepositListItem(addressEntry, xmrWalletService, formatter, txsWithIncomingOutputs, subaddresses));
}
// update list