mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-24 12:46:45 +00:00
add caching for verbose transaction lookups to avoid repeat server requests
This commit is contained in:
parent
be599fb003
commit
fc5d6ada36
1 changed files with 27 additions and 2 deletions
|
@ -50,6 +50,8 @@ public class ElectrumServer {
|
|||
|
||||
private static Map<String, String> retrievedScriptHashes = Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
private static Map<Sha256Hash, BlockTransaction> retrievedTransactions = Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
private static ElectrumServerRpc electrumServerRpc = new SimpleElectrumServerRpc();
|
||||
|
||||
private static String bwtElectrumServer;
|
||||
|
@ -93,6 +95,7 @@ public class ElectrumServer {
|
|||
//If changing server, don't rely on previous transaction history
|
||||
if(previousServerAddress != null && !electrumServer.equals(previousServerAddress)) {
|
||||
retrievedScriptHashes.clear();
|
||||
retrievedTransactions.clear();
|
||||
}
|
||||
previousServerAddress = electrumServer;
|
||||
|
||||
|
@ -1229,8 +1232,30 @@ public class ElectrumServer {
|
|||
protected Task<Map<Sha256Hash, BlockTransaction>> createTask() {
|
||||
return new Task<>() {
|
||||
protected Map<Sha256Hash, BlockTransaction> call() throws ServerException {
|
||||
Map<Sha256Hash, BlockTransaction> transactionMap = new HashMap<>();
|
||||
for(Sha256Hash ref : references) {
|
||||
if(retrievedTransactions.get(ref) != null) {
|
||||
transactionMap.put(ref, retrievedTransactions.get(ref));
|
||||
}
|
||||
}
|
||||
|
||||
Set<Sha256Hash> fetchReferences = new HashSet<>(references);
|
||||
fetchReferences.removeAll(transactionMap.keySet());
|
||||
|
||||
if(!fetchReferences.isEmpty()) {
|
||||
ElectrumServer electrumServer = new ElectrumServer();
|
||||
return electrumServer.getReferencedTransactions(references, scriptHash);
|
||||
Map<Sha256Hash, BlockTransaction> fetchedTransactions = electrumServer.getReferencedTransactions(fetchReferences, scriptHash);
|
||||
transactionMap.putAll(fetchedTransactions);
|
||||
|
||||
for(Map.Entry<Sha256Hash, BlockTransaction> fetchedEntry : fetchedTransactions.entrySet()) {
|
||||
if(fetchedEntry.getValue() != null && !Sha256Hash.ZERO_HASH.equals(fetchedEntry.getValue().getBlockHash()) &&
|
||||
AppServices.getCurrentBlockHeight() != null && fetchedEntry.getValue().getConfirmations(AppServices.getCurrentBlockHeight()) >= BlockTransactionHash.BLOCKS_TO_CONFIRM) {
|
||||
retrievedTransactions.put(fetchedEntry.getKey(), fetchedEntry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return transactionMap;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue