Invalidate ElectrumX transactions cache when switching accounts.

This commit is contained in:
CalDescent 2022-02-05 10:23:25 +00:00
parent 8937b3ec86
commit 775e3c065e

View File

@ -1,12 +1,6 @@
package org.qortal.crosschain; package org.qortal.crosschain;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -57,6 +51,7 @@ public abstract class Bitcoiny implements ForeignBlockchain {
/** Cache recent transactions to speed up subsequent lookups */ /** Cache recent transactions to speed up subsequent lookups */
protected List<SimpleTransaction> transactionsCache; protected List<SimpleTransaction> transactionsCache;
protected Long transactionsCacheTimestamp; protected Long transactionsCacheTimestamp;
protected String transactionsCacheXpub;
protected static long TRANSACTIONS_CACHE_TIMEOUT = 2 * 60 * 1000L; // 2 minutes protected static long TRANSACTIONS_CACHE_TIMEOUT = 2 * 60 * 1000L; // 2 minutes
/** Keys that have been previously marked as fully spent,<br> /** Keys that have been previously marked as fully spent,<br>
@ -360,7 +355,8 @@ public abstract class Bitcoiny implements ForeignBlockchain {
public List<SimpleTransaction> getWalletTransactions(String key58) throws ForeignBlockchainException { public List<SimpleTransaction> getWalletTransactions(String key58) throws ForeignBlockchainException {
synchronized (this) { synchronized (this) {
// Serve from the cache if it's recent // Serve from the cache if it's recent, and matches this xpub
if (Objects.equals(transactionsCacheXpub, key58)) {
if (transactionsCache != null && transactionsCacheTimestamp != null) { if (transactionsCache != null && transactionsCacheTimestamp != null) {
Long now = NTP.getTime(); Long now = NTP.getTime();
boolean isCacheStale = (now != null && now - transactionsCacheTimestamp >= TRANSACTIONS_CACHE_TIMEOUT); boolean isCacheStale = (now != null && now - transactionsCacheTimestamp >= TRANSACTIONS_CACHE_TIMEOUT);
@ -368,6 +364,7 @@ public abstract class Bitcoiny implements ForeignBlockchain {
return transactionsCache; return transactionsCache;
} }
} }
}
Context.propagate(bitcoinjContext); Context.propagate(bitcoinjContext);
@ -432,6 +429,7 @@ public abstract class Bitcoiny implements ForeignBlockchain {
// Update cache and return // Update cache and return
transactionsCacheTimestamp = NTP.getTime(); transactionsCacheTimestamp = NTP.getTime();
transactionsCacheXpub = key58;
transactionsCache = walletTransactions.stream() transactionsCache = walletTransactions.stream()
.map(t -> convertToSimpleTransaction(t, keySet)) .map(t -> convertToSimpleTransaction(t, keySet))
.sorted(newestTimestampFirstComparator).collect(Collectors.toList()); .sorted(newestTimestampFirstComparator).collect(Collectors.toList());