From 24cde9d073da636fbc2150b7abbd50b48342e040 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 26 Jun 2020 13:34:07 +0200 Subject: [PATCH] improve multithreaded behaviour --- .../sparrowwallet/drongo/wallet/Wallet.java | 20 ++++++++++------ .../drongo/wallet/WalletNode.java | 24 +++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index d65ab46..26c6f92 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -75,12 +75,18 @@ public class Wallet { return keystores; } - private Set getPurposeNodes() { - return purposeNodes; + public Map getTransactions() { + return Collections.unmodifiableMap(transactions); } - public Map getTransactions() { - return transactions; + public synchronized void updateTransactions(Map updatedTransactions) { + for(BlockTransaction blockTx : updatedTransactions.values()) { + Optional optionalLabel = transactions.values().stream().filter(oldBlTx -> oldBlTx.getHash().equals(blockTx.getHash())).map(BlockTransaction::getLabel).filter(Objects::nonNull).findFirst(); + optionalLabel.ifPresent(blockTx::setLabel); + } + + transactions.clear(); + transactions.putAll(updatedTransactions); } public Integer getStoredBlockHeight() { @@ -91,7 +97,7 @@ public class Wallet { this.storedBlockHeight = storedBlockHeight; } - public WalletNode getNode(KeyPurpose keyPurpose) { + public synchronized WalletNode getNode(KeyPurpose keyPurpose) { WalletNode purposeNode; Optional optionalPurposeNode = purposeNodes.stream().filter(node -> node.getKeyPurpose().equals(keyPurpose)).findFirst(); if(optionalPurposeNode.isEmpty()) { @@ -310,10 +316,10 @@ public class Wallet { copy.getKeystores().add(keystore.copy()); } for(WalletNode node : purposeNodes) { - copy.getPurposeNodes().add(node.copy()); + copy.purposeNodes.add(node.copy()); } for(Sha256Hash hash : transactions.keySet()) { - copy.getTransactions().put(hash, transactions.get(hash)); + copy.transactions.put(hash, transactions.get(hash)); } return copy; diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java index e74da7a..21941d6 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java @@ -87,7 +87,7 @@ public class WalletNode implements Comparable { } public Set getChildren() { - return children; + return children == null ? null : Collections.unmodifiableSet(children); } public void setChildren(Set children) { @@ -95,13 +95,23 @@ public class WalletNode implements Comparable { } public Set getTransactionOutputs() { - return transactionOutputs; + return transactionOutputs == null ? null : Collections.unmodifiableSet(transactionOutputs); } public void setTransactionOutputs(Set transactionOutputs) { this.transactionOutputs = transactionOutputs; } + public synchronized void updateTransactionOutputs(Set updatedOutputs) { + for(BlockTransactionHashIndex txo : updatedOutputs) { + Optional optionalLabel = transactionOutputs.stream().filter(oldTxo -> oldTxo.getHash().equals(txo.getHash()) && oldTxo.getIndex() == txo.getIndex()).map(BlockTransactionHash::getLabel).filter(Objects::nonNull).findFirst(); + optionalLabel.ifPresent(txo::setLabel); + } + + transactionOutputs.clear(); + transactionOutputs.addAll(updatedOutputs); + } + public Set getUnspentTransactionOutputs() { Set unspentTXOs = new TreeSet<>(transactionOutputs); return unspentTXOs.stream().filter(txo -> !txo.isSpent()).collect(Collectors.toCollection(HashSet::new)); @@ -116,10 +126,10 @@ public class WalletNode implements Comparable { return value; } - public void fillToIndex(int index) { + public synchronized void fillToIndex(int index) { for(int i = 0; i <= index; i++) { WalletNode node = new WalletNode(getKeyPurpose(), i); - getChildren().add(node); + children.add(node); } } @@ -172,7 +182,7 @@ public class WalletNode implements Comparable { return 0; } - public void clearHistory() { + public synchronized void clearHistory() { transactionOutputs.clear(); for(WalletNode childNode : getChildren()) { childNode.clearHistory(); @@ -184,11 +194,11 @@ public class WalletNode implements Comparable { copy.setLabel(label); for(WalletNode child : getChildren()) { - copy.getChildren().add(child.copy()); + copy.children.add(child.copy()); } for(BlockTransactionHashIndex txo : getTransactionOutputs()) { - copy.getTransactionOutputs().add(txo.copy()); + copy.transactionOutputs.add(txo.copy()); } return copy;