refactor wallet field push down to entry

This commit is contained in:
Craig Raw 2020-11-13 10:12:19 +02:00
parent 6460cb88a5
commit 0b9052dee9
6 changed files with 30 additions and 54 deletions

View file

@ -1,5 +1,6 @@
package com.sparrowwallet.sparrow.wallet; package com.sparrowwallet.sparrow.wallet;
import com.sparrowwallet.drongo.wallet.Wallet;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
@ -7,19 +8,26 @@ import javafx.collections.ObservableList;
import java.util.List; import java.util.List;
public abstract class Entry { public abstract class Entry {
private final Wallet wallet;
private final SimpleStringProperty labelProperty; private final SimpleStringProperty labelProperty;
private final ObservableList<Entry> children; private final ObservableList<Entry> children;
public Entry(String label, List<Entry> entries) { public Entry(Wallet wallet, String label, List<Entry> entries) {
this.wallet = wallet;
this.labelProperty = new SimpleStringProperty(this, "label", label); this.labelProperty = new SimpleStringProperty(this, "label", label);
this.children = FXCollections.observableList(entries); this.children = FXCollections.observableList(entries);
} }
public Entry(SimpleStringProperty labelProperty, ObservableList<Entry> children) { public Entry(Wallet wallet, SimpleStringProperty labelProperty, ObservableList<Entry> children) {
this.wallet = wallet;
this.labelProperty = labelProperty; this.labelProperty = labelProperty;
this.children = children; this.children = children;
} }
public Wallet getWallet() {
return wallet;
}
public String getLabel() { public String getLabel() {
return labelProperty.get(); return labelProperty.get();
} }

View file

@ -13,14 +13,12 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry> { public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry> {
private final Wallet wallet;
private final BlockTransactionHashIndex hashIndex; private final BlockTransactionHashIndex hashIndex;
private final Type type; private final Type type;
private final KeyPurpose keyPurpose; private final KeyPurpose keyPurpose;
public HashIndexEntry(Wallet wallet, BlockTransactionHashIndex hashIndex, Type type, KeyPurpose keyPurpose) { public HashIndexEntry(Wallet wallet, BlockTransactionHashIndex hashIndex, Type type, KeyPurpose keyPurpose) {
super(hashIndex.getLabel(), hashIndex.getSpentBy() != null ? List.of(new HashIndexEntry(wallet, hashIndex.getSpentBy(), Type.INPUT, keyPurpose)) : Collections.emptyList()); super(wallet, hashIndex.getLabel(), hashIndex.getSpentBy() != null ? List.of(new HashIndexEntry(wallet, hashIndex.getSpentBy(), Type.INPUT, keyPurpose)) : Collections.emptyList());
this.wallet = wallet;
this.hashIndex = hashIndex; this.hashIndex = hashIndex;
this.type = type; this.type = type;
this.keyPurpose = keyPurpose; this.keyPurpose = keyPurpose;
@ -31,10 +29,6 @@ public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry>
}); });
} }
public Wallet getWallet() {
return wallet;
}
public BlockTransactionHashIndex getHashIndex() { public BlockTransactionHashIndex getHashIndex() {
return hashIndex; return hashIndex;
} }
@ -48,7 +42,7 @@ public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry>
} }
public BlockTransaction getBlockTransaction() { public BlockTransaction getBlockTransaction() {
return wallet.getTransactions().get(hashIndex.getHash()); return getWallet().getTransactions().get(hashIndex.getHash());
} }
public String getDescription() { public String getDescription() {
@ -63,7 +57,7 @@ public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry>
} }
public boolean isSpendable() { public boolean isSpendable() {
return !isSpent() && (hashIndex.getHeight() > 0 || wallet.allInputsFromWallet(hashIndex.getHash())); return !isSpent() && (hashIndex.getHeight() > 0 || getWallet().allInputsFromWallet(hashIndex.getHash()));
} }
@Override @Override
@ -80,7 +74,7 @@ public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry>
if (this == o) return true; if (this == o) return true;
if (!(o instanceof HashIndexEntry)) return false; if (!(o instanceof HashIndexEntry)) return false;
HashIndexEntry that = (HashIndexEntry) o; HashIndexEntry that = (HashIndexEntry) o;
return wallet.equals(that.wallet) && return getWallet().equals(that.getWallet()) &&
hashIndex.equals(that.hashIndex) && hashIndex.equals(that.hashIndex) &&
type == that.type && type == that.type &&
keyPurpose == that.keyPurpose; keyPurpose == that.keyPurpose;
@ -88,7 +82,7 @@ public class HashIndexEntry extends Entry implements Comparable<HashIndexEntry>
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(wallet, hashIndex, type, keyPurpose); return Objects.hash(getWallet(), hashIndex, type, keyPurpose);
} }
@Override @Override

View file

@ -10,16 +10,14 @@ import com.sparrowwallet.sparrow.event.WalletEntryLabelChangedEvent;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class NodeEntry extends Entry implements Comparable<NodeEntry> { public class NodeEntry extends Entry implements Comparable<NodeEntry> {
private final Wallet wallet;
private final WalletNode node; private final WalletNode node;
public NodeEntry(Wallet wallet, WalletNode node) { public NodeEntry(Wallet wallet, WalletNode node) {
super(node.getLabel(), super(wallet, node.getLabel(),
!node.getChildren().isEmpty() ? !node.getChildren().isEmpty() ?
node.getChildren().stream().map(childNode -> new NodeEntry(wallet, childNode)).collect(Collectors.toList()) : node.getChildren().stream().map(childNode -> new NodeEntry(wallet, childNode)).collect(Collectors.toList()) :
node.getTransactionOutputs().stream().map(txo -> new HashIndexEntry(wallet, txo, HashIndexEntry.Type.OUTPUT, node.getKeyPurpose())).collect(Collectors.toList())); node.getTransactionOutputs().stream().map(txo -> new HashIndexEntry(wallet, txo, HashIndexEntry.Type.OUTPUT, node.getKeyPurpose())).collect(Collectors.toList()));
this.wallet = wallet;
this.node = node; this.node = node;
labelProperty().addListener((observable, oldValue, newValue) -> { labelProperty().addListener((observable, oldValue, newValue) -> {
@ -28,24 +26,20 @@ public class NodeEntry extends Entry implements Comparable<NodeEntry> {
}); });
} }
public Wallet getWallet() {
return wallet;
}
public WalletNode getNode() { public WalletNode getNode() {
return node; return node;
} }
public Address getAddress() { public Address getAddress() {
return wallet.getAddress(node); return getWallet().getAddress(node);
} }
public Script getOutputScript() { public Script getOutputScript() {
return wallet.getOutputScript(node); return getWallet().getOutputScript(node);
} }
public String getOutputDescriptor() { public String getOutputDescriptor() {
return wallet.getOutputDescriptor(node); return getWallet().getOutputDescriptor(node);
} }
@Override @Override

View file

@ -19,12 +19,10 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class TransactionEntry extends Entry implements Comparable<TransactionEntry> { public class TransactionEntry extends Entry implements Comparable<TransactionEntry> {
private final Wallet wallet;
private final BlockTransaction blockTransaction; private final BlockTransaction blockTransaction;
public TransactionEntry(Wallet wallet, BlockTransaction blockTransaction, Map<BlockTransactionHashIndex, KeyPurpose> inputs, Map<BlockTransactionHashIndex, KeyPurpose> outputs) { public TransactionEntry(Wallet wallet, BlockTransaction blockTransaction, Map<BlockTransactionHashIndex, KeyPurpose> inputs, Map<BlockTransactionHashIndex, KeyPurpose> outputs) {
super(blockTransaction.getLabel(), createChildEntries(wallet, inputs, outputs)); super(wallet, blockTransaction.getLabel(), createChildEntries(wallet, inputs, outputs));
this.wallet = wallet;
this.blockTransaction = blockTransaction; this.blockTransaction = blockTransaction;
labelProperty().addListener((observable, oldValue, newValue) -> { labelProperty().addListener((observable, oldValue, newValue) -> {
@ -38,10 +36,6 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
} }
} }
public Wallet getWallet() {
return wallet;
}
public BlockTransaction getBlockTransaction() { public BlockTransaction getBlockTransaction() {
return blockTransaction; return blockTransaction;
} }
@ -70,7 +64,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
} }
public int calculateConfirmations() { public int calculateConfirmations() {
return blockTransaction.getConfirmations(wallet.getStoredBlockHeight()); return blockTransaction.getConfirmations(getWallet().getStoredBlockHeight());
} }
public String getConfirmationsDescription() { public String getConfirmationsDescription() {
@ -86,7 +80,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
public boolean isComplete() { public boolean isComplete() {
int validEntries = 0; int validEntries = 0;
Map<BlockTransactionHashIndex, WalletNode> walletTxos = wallet.getWalletTxos(); Map<BlockTransactionHashIndex, WalletNode> walletTxos = getWallet().getWalletTxos();
for(TransactionInput txInput : blockTransaction.getTransaction().getInputs()) { for(TransactionInput txInput : blockTransaction.getTransaction().getInputs()) {
Optional<BlockTransactionHashIndex> optRef = walletTxos.keySet().stream().filter(ref -> ref.getHash().equals(txInput.getOutpoint().getHash()) && ref.getIndex() == txInput.getOutpoint().getIndex()).findFirst(); Optional<BlockTransactionHashIndex> optRef = walletTxos.keySet().stream().filter(ref -> ref.getHash().equals(txInput.getOutpoint().getHash()) && ref.getIndex() == txInput.getOutpoint().getIndex()).findFirst();
if(optRef.isPresent()) { if(optRef.isPresent()) {
@ -146,12 +140,12 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
TransactionEntry that = (TransactionEntry) o; TransactionEntry that = (TransactionEntry) o;
// Even though the txid identifies a transaction, receiving an incomplete set of script hash notifications can result in some inputs/outputs for a tx being missing. // Even though the txid identifies a transaction, receiving an incomplete set of script hash notifications can result in some inputs/outputs for a tx being missing.
// To resolve this we check the number of children, but not the children themselves (since we don't care here when they are spent) // To resolve this we check the number of children, but not the children themselves (since we don't care here when they are spent)
return wallet.equals(that.wallet) && blockTransaction.equals(that.blockTransaction) && getChildren().size() == that.getChildren().size(); return getWallet().equals(that.getWallet()) && blockTransaction.equals(that.blockTransaction) && getChildren().size() == that.getChildren().size();
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(wallet, blockTransaction, getChildren().size()); return Objects.hash(getWallet(), blockTransaction, getChildren().size());
} }
@Override @Override
@ -239,7 +233,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
@Subscribe @Subscribe
public void walletTabsClosed(WalletTabsClosedEvent event) { public void walletTabsClosed(WalletTabsClosedEvent event) {
for(WalletTabData tabData : event.getClosedWalletTabData()) { for(WalletTabData tabData : event.getClosedWalletTabData()) {
if(tabData.getWalletForm().getWallet() == wallet) { if(tabData.getWalletForm().getWallet() == getWallet()) {
try { try {
EventManager.get().unregister(this); EventManager.get().unregister(this);
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {

View file

@ -18,18 +18,11 @@ import java.util.stream.Collectors;
public class WalletTransactionsEntry extends Entry { public class WalletTransactionsEntry extends Entry {
private static final Logger log = LoggerFactory.getLogger(WalletTransactionsEntry.class); private static final Logger log = LoggerFactory.getLogger(WalletTransactionsEntry.class);
private final Wallet wallet;
public WalletTransactionsEntry(Wallet wallet) { public WalletTransactionsEntry(Wallet wallet) {
super(wallet.getName(), getWalletTransactions(wallet).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList())); super(wallet, wallet.getName(), getWalletTransactions(wallet).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList()));
this.wallet = wallet;
calculateBalances(); calculateBalances();
} }
public Wallet getWallet() {
return wallet;
}
@Override @Override
public Long getValue() { public Long getValue() {
return getBalance(); return getBalance();
@ -58,7 +51,7 @@ public class WalletTransactionsEntry extends Entry {
} }
public void updateTransactions() { public void updateTransactions() {
List<Entry> current = getWalletTransactions(wallet).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList()); List<Entry> current = getWalletTransactions(getWallet()).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList());
List<Entry> previous = new ArrayList<>(getChildren()); List<Entry> previous = new ArrayList<>(getChildren());
List<Entry> entriesAdded = new ArrayList<>(current); List<Entry> entriesAdded = new ArrayList<>(current);
@ -76,7 +69,7 @@ public class WalletTransactionsEntry extends Entry {
List<BlockTransaction> blockTransactions = entriesAdded.stream().map(txEntry -> ((TransactionEntry)txEntry).getBlockTransaction()).collect(Collectors.toList()); List<BlockTransaction> blockTransactions = entriesAdded.stream().map(txEntry -> ((TransactionEntry)txEntry).getBlockTransaction()).collect(Collectors.toList());
long totalBlockchainValue = entriesAdded.stream().filter(txEntry -> ((TransactionEntry)txEntry).getConfirmations() > 0).mapToLong(Entry::getValue).sum(); long totalBlockchainValue = entriesAdded.stream().filter(txEntry -> ((TransactionEntry)txEntry).getConfirmations() > 0).mapToLong(Entry::getValue).sum();
long totalMempoolValue = entriesAdded.stream().filter(txEntry -> ((TransactionEntry)txEntry).getConfirmations() == 0).mapToLong(Entry::getValue).sum(); long totalMempoolValue = entriesAdded.stream().filter(txEntry -> ((TransactionEntry)txEntry).getConfirmations() == 0).mapToLong(Entry::getValue).sum();
EventManager.get().post(new NewWalletTransactionsEvent(wallet, blockTransactions, totalBlockchainValue, totalMempoolValue)); EventManager.get().post(new NewWalletTransactionsEvent(getWallet(), blockTransactions, totalBlockchainValue, totalMempoolValue));
} }
if(entriesAdded.size() > entriesComplete.size()) { if(entriesAdded.size() > entriesComplete.size()) {

View file

@ -6,18 +6,11 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class WalletUtxosEntry extends Entry { public class WalletUtxosEntry extends Entry {
private final Wallet wallet;
public WalletUtxosEntry(Wallet wallet) { public WalletUtxosEntry(Wallet wallet) {
super(wallet.getName(), wallet.getWalletUtxos().entrySet().stream().map(entry -> new UtxoEntry(wallet, entry.getKey(), HashIndexEntry.Type.OUTPUT, entry.getValue())).collect(Collectors.toList())); super(wallet, wallet.getName(), wallet.getWalletUtxos().entrySet().stream().map(entry -> new UtxoEntry(wallet, entry.getKey(), HashIndexEntry.Type.OUTPUT, entry.getValue())).collect(Collectors.toList()));
this.wallet = wallet;
calculateDuplicates(); calculateDuplicates();
} }
public Wallet getWallet() {
return wallet;
}
@Override @Override
public Long getValue() { public Long getValue() {
return 0L; return 0L;
@ -42,7 +35,7 @@ public class WalletUtxosEntry extends Entry {
} }
public void updateUtxos() { public void updateUtxos() {
List<Entry> current = wallet.getWalletUtxos().entrySet().stream().map(entry -> new UtxoEntry(wallet, entry.getKey(), HashIndexEntry.Type.OUTPUT, entry.getValue())).collect(Collectors.toList()); List<Entry> current = getWallet().getWalletUtxos().entrySet().stream().map(entry -> new UtxoEntry(getWallet(), entry.getKey(), HashIndexEntry.Type.OUTPUT, entry.getValue())).collect(Collectors.toList());
List<Entry> previous = new ArrayList<>(getChildren()); List<Entry> previous = new ArrayList<>(getChildren());
List<Entry> entriesAdded = new ArrayList<>(current); List<Entry> entriesAdded = new ArrayList<>(current);