mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-25 05:06: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<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 ElectrumServerRpc electrumServerRpc = new SimpleElectrumServerRpc();
|
||||||
|
|
||||||
private static String bwtElectrumServer;
|
private static String bwtElectrumServer;
|
||||||
|
@ -93,6 +95,7 @@ public class ElectrumServer {
|
||||||
//If changing server, don't rely on previous transaction history
|
//If changing server, don't rely on previous transaction history
|
||||||
if(previousServerAddress != null && !electrumServer.equals(previousServerAddress)) {
|
if(previousServerAddress != null && !electrumServer.equals(previousServerAddress)) {
|
||||||
retrievedScriptHashes.clear();
|
retrievedScriptHashes.clear();
|
||||||
|
retrievedTransactions.clear();
|
||||||
}
|
}
|
||||||
previousServerAddress = electrumServer;
|
previousServerAddress = electrumServer;
|
||||||
|
|
||||||
|
@ -1229,8 +1232,30 @@ public class ElectrumServer {
|
||||||
protected Task<Map<Sha256Hash, BlockTransaction>> createTask() {
|
protected Task<Map<Sha256Hash, BlockTransaction>> createTask() {
|
||||||
return new Task<>() {
|
return new Task<>() {
|
||||||
protected Map<Sha256Hash, BlockTransaction> call() throws ServerException {
|
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();
|
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