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;
}
private Set<WalletNode> getPurposeNodes() {
return purposeNodes;
public Map<Sha256Hash, BlockTransaction> getTransactions() {
return Collections.unmodifiableMap(transactions);
}
public Map<Sha256Hash, BlockTransaction> getTransactions() {
return transactions;
public synchronized void updateTransactions(Map<Sha256Hash, BlockTransaction> updatedTransactions) {
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() {
@ -91,7 +97,7 @@ public class Wallet {
this.storedBlockHeight = storedBlockHeight;
}
public WalletNode getNode(KeyPurpose keyPurpose) {
public synchronized WalletNode getNode(KeyPurpose keyPurpose) {
WalletNode purposeNode;
Optional<WalletNode> 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;

View file

@ -87,7 +87,7 @@ public class WalletNode implements Comparable<WalletNode> {
}
public Set<WalletNode> getChildren() {
return children;
return children == null ? null : Collections.unmodifiableSet(children);
}
public void setChildren(Set<WalletNode> children) {
@ -95,13 +95,23 @@ public class WalletNode implements Comparable<WalletNode> {
}
public Set<BlockTransactionHashIndex> getTransactionOutputs() {
return transactionOutputs;
return transactionOutputs == null ? null : Collections.unmodifiableSet(transactionOutputs);
}
public void setTransactionOutputs(Set<BlockTransactionHashIndex> 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() {
Set<BlockTransactionHashIndex> 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<WalletNode> {
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<WalletNode> {
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<WalletNode> {
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;