improve multithreaded behaviour

This commit is contained in:
Craig Raw 2020-06-26 13:34:07 +02:00
parent 2e1012da8b
commit 24cde9d073
2 changed files with 30 additions and 14 deletions

View file

@ -75,12 +75,18 @@ public class Wallet {
return keystores; return keystores;
} }
private Set<WalletNode> getPurposeNodes() { public Map<Sha256Hash, BlockTransaction> getTransactions() {
return purposeNodes; return Collections.unmodifiableMap(transactions);
} }
public Map<Sha256Hash, BlockTransaction> getTransactions() { public synchronized void updateTransactions(Map<Sha256Hash, BlockTransaction> updatedTransactions) {
return transactions; for(BlockTransaction blockTx : updatedTransactions.values()) {
Optional<String> 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() { public Integer getStoredBlockHeight() {
@ -91,7 +97,7 @@ public class Wallet {
this.storedBlockHeight = storedBlockHeight; this.storedBlockHeight = storedBlockHeight;
} }
public WalletNode getNode(KeyPurpose keyPurpose) { public synchronized WalletNode getNode(KeyPurpose keyPurpose) {
WalletNode purposeNode; WalletNode purposeNode;
Optional<WalletNode> optionalPurposeNode = purposeNodes.stream().filter(node -> node.getKeyPurpose().equals(keyPurpose)).findFirst(); Optional<WalletNode> optionalPurposeNode = purposeNodes.stream().filter(node -> node.getKeyPurpose().equals(keyPurpose)).findFirst();
if(optionalPurposeNode.isEmpty()) { if(optionalPurposeNode.isEmpty()) {
@ -310,10 +316,10 @@ public class Wallet {
copy.getKeystores().add(keystore.copy()); copy.getKeystores().add(keystore.copy());
} }
for(WalletNode node : purposeNodes) { for(WalletNode node : purposeNodes) {
copy.getPurposeNodes().add(node.copy()); copy.purposeNodes.add(node.copy());
} }
for(Sha256Hash hash : transactions.keySet()) { for(Sha256Hash hash : transactions.keySet()) {
copy.getTransactions().put(hash, transactions.get(hash)); copy.transactions.put(hash, transactions.get(hash));
} }
return copy; return copy;

View file

@ -87,7 +87,7 @@ public class WalletNode implements Comparable<WalletNode> {
} }
public Set<WalletNode> getChildren() { public Set<WalletNode> getChildren() {
return children; return children == null ? null : Collections.unmodifiableSet(children);
} }
public void setChildren(Set<WalletNode> children) { public void setChildren(Set<WalletNode> children) {
@ -95,13 +95,23 @@ public class WalletNode implements Comparable<WalletNode> {
} }
public Set<BlockTransactionHashIndex> getTransactionOutputs() { public Set<BlockTransactionHashIndex> getTransactionOutputs() {
return transactionOutputs; return transactionOutputs == null ? null : Collections.unmodifiableSet(transactionOutputs);
} }
public void setTransactionOutputs(Set<BlockTransactionHashIndex> transactionOutputs) { public void setTransactionOutputs(Set<BlockTransactionHashIndex> transactionOutputs) {
this.transactionOutputs = transactionOutputs; this.transactionOutputs = transactionOutputs;
} }
public synchronized void updateTransactionOutputs(Set<BlockTransactionHashIndex> updatedOutputs) {
for(BlockTransactionHashIndex txo : updatedOutputs) {
Optional<String> 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<BlockTransactionHashIndex> getUnspentTransactionOutputs() { public Set<BlockTransactionHashIndex> getUnspentTransactionOutputs() {
Set<BlockTransactionHashIndex> unspentTXOs = new TreeSet<>(transactionOutputs); Set<BlockTransactionHashIndex> unspentTXOs = new TreeSet<>(transactionOutputs);
return unspentTXOs.stream().filter(txo -> !txo.isSpent()).collect(Collectors.toCollection(HashSet::new)); return unspentTXOs.stream().filter(txo -> !txo.isSpent()).collect(Collectors.toCollection(HashSet::new));
@ -116,10 +126,10 @@ public class WalletNode implements Comparable<WalletNode> {
return value; return value;
} }
public void fillToIndex(int index) { public synchronized void fillToIndex(int index) {
for(int i = 0; i <= index; i++) { for(int i = 0; i <= index; i++) {
WalletNode node = new WalletNode(getKeyPurpose(), i); WalletNode node = new WalletNode(getKeyPurpose(), i);
getChildren().add(node); children.add(node);
} }
} }
@ -172,7 +182,7 @@ public class WalletNode implements Comparable<WalletNode> {
return 0; return 0;
} }
public void clearHistory() { public synchronized void clearHistory() {
transactionOutputs.clear(); transactionOutputs.clear();
for(WalletNode childNode : getChildren()) { for(WalletNode childNode : getChildren()) {
childNode.clearHistory(); childNode.clearHistory();
@ -184,11 +194,11 @@ public class WalletNode implements Comparable<WalletNode> {
copy.setLabel(label); copy.setLabel(label);
for(WalletNode child : getChildren()) { for(WalletNode child : getChildren()) {
copy.getChildren().add(child.copy()); copy.children.add(child.copy());
} }
for(BlockTransactionHashIndex txo : getTransactionOutputs()) { for(BlockTransactionHashIndex txo : getTransactionOutputs()) {
copy.getTransactionOutputs().add(txo.copy()); copy.transactionOutputs.add(txo.copy());
} }
return copy; return copy;