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;
import com.sparrowwallet.drongo.wallet.Wallet;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
@ -7,19 +8,26 @@ import javafx.collections.ObservableList;
import java.util.List;
public abstract class Entry {
private final Wallet wallet;
private final SimpleStringProperty labelProperty;
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.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.children = children;
}
public Wallet getWallet() {
return wallet;
}
public String getLabel() {
return labelProperty.get();
}

View file

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

View file

@ -10,16 +10,14 @@ import com.sparrowwallet.sparrow.event.WalletEntryLabelChangedEvent;
import java.util.stream.Collectors;
public class NodeEntry extends Entry implements Comparable<NodeEntry> {
private final Wallet wallet;
private final WalletNode node;
public NodeEntry(Wallet wallet, WalletNode node) {
super(node.getLabel(),
super(wallet, node.getLabel(),
!node.getChildren().isEmpty() ?
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()));
this.wallet = wallet;
this.node = node;
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() {
return node;
}
public Address getAddress() {
return wallet.getAddress(node);
return getWallet().getAddress(node);
}
public Script getOutputScript() {
return wallet.getOutputScript(node);
return getWallet().getOutputScript(node);
}
public String getOutputDescriptor() {
return wallet.getOutputDescriptor(node);
return getWallet().getOutputDescriptor(node);
}
@Override

View file

@ -19,12 +19,10 @@ import java.util.*;
import java.util.stream.Collectors;
public class TransactionEntry extends Entry implements Comparable<TransactionEntry> {
private final Wallet wallet;
private final BlockTransaction blockTransaction;
public TransactionEntry(Wallet wallet, BlockTransaction blockTransaction, Map<BlockTransactionHashIndex, KeyPurpose> inputs, Map<BlockTransactionHashIndex, KeyPurpose> outputs) {
super(blockTransaction.getLabel(), createChildEntries(wallet, inputs, outputs));
this.wallet = wallet;
super(wallet, blockTransaction.getLabel(), createChildEntries(wallet, inputs, outputs));
this.blockTransaction = blockTransaction;
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() {
return blockTransaction;
}
@ -70,7 +64,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
}
public int calculateConfirmations() {
return blockTransaction.getConfirmations(wallet.getStoredBlockHeight());
return blockTransaction.getConfirmations(getWallet().getStoredBlockHeight());
}
public String getConfirmationsDescription() {
@ -86,7 +80,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
public boolean isComplete() {
int validEntries = 0;
Map<BlockTransactionHashIndex, WalletNode> walletTxos = wallet.getWalletTxos();
Map<BlockTransactionHashIndex, WalletNode> walletTxos = getWallet().getWalletTxos();
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();
if(optRef.isPresent()) {
@ -146,12 +140,12 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
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.
// 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
public int hashCode() {
return Objects.hash(wallet, blockTransaction, getChildren().size());
return Objects.hash(getWallet(), blockTransaction, getChildren().size());
}
@Override
@ -239,7 +233,7 @@ public class TransactionEntry extends Entry implements Comparable<TransactionEnt
@Subscribe
public void walletTabsClosed(WalletTabsClosedEvent event) {
for(WalletTabData tabData : event.getClosedWalletTabData()) {
if(tabData.getWalletForm().getWallet() == wallet) {
if(tabData.getWalletForm().getWallet() == getWallet()) {
try {
EventManager.get().unregister(this);
} catch(IllegalArgumentException e) {

View file

@ -18,18 +18,11 @@ import java.util.stream.Collectors;
public class WalletTransactionsEntry extends Entry {
private static final Logger log = LoggerFactory.getLogger(WalletTransactionsEntry.class);
private final Wallet wallet;
public WalletTransactionsEntry(Wallet wallet) {
super(wallet.getName(), getWalletTransactions(wallet).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList()));
this.wallet = wallet;
super(wallet, wallet.getName(), getWalletTransactions(wallet).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList()));
calculateBalances();
}
public Wallet getWallet() {
return wallet;
}
@Override
public Long getValue() {
return getBalance();
@ -58,7 +51,7 @@ public class WalletTransactionsEntry extends Entry {
}
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> 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());
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();
EventManager.get().post(new NewWalletTransactionsEvent(wallet, blockTransactions, totalBlockchainValue, totalMempoolValue));
EventManager.get().post(new NewWalletTransactionsEvent(getWallet(), blockTransactions, totalBlockchainValue, totalMempoolValue));
}
if(entriesAdded.size() > entriesComplete.size()) {

View file

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