compare txes better, clear nodes functionality

This commit is contained in:
Craig Raw 2020-06-21 17:14:56 +02:00
parent 81378b28b2
commit 18036268e5
2 changed files with 67 additions and 0 deletions

View file

@ -3,7 +3,11 @@ package com.sparrowwallet.drongo.wallet;
import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Sha256Hash;
import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.Transaction;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> { public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> {
private final Transaction transaction; private final Transaction transaction;
@ -29,6 +33,62 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
@Override @Override
public int compareTo(BlockTransaction blockchainTransaction) { public int compareTo(BlockTransaction blockchainTransaction) {
if(getHeight() != blockchainTransaction.getHeight()) {
return getHeight() - blockchainTransaction.getHeight();
}
if(getReferencedOutpoints(this).removeAll(getOutputs(blockchainTransaction))) {
return 1;
}
if(getReferencedOutpoints(blockchainTransaction).removeAll(getOutputs(this))) {
return -1;
}
return super.compareTo(blockchainTransaction); return super.compareTo(blockchainTransaction);
} }
private static List<HashIndex> getReferencedOutpoints(BlockTransaction blockchainTransaction) {
if(blockchainTransaction.getTransaction() == null) {
return Collections.emptyList();
}
return blockchainTransaction.getTransaction().getInputs().stream()
.map(txInput -> new HashIndex(txInput.getOutpoint().getHash(), (int)txInput.getOutpoint().getIndex()))
.collect(Collectors.toList());
}
private static List<HashIndex> getOutputs(BlockTransaction blockchainTransaction) {
if(blockchainTransaction.getTransaction() == null) {
return Collections.emptyList();
}
return blockchainTransaction.getTransaction().getOutputs().stream()
.map(txOutput -> new HashIndex(blockchainTransaction.getHash(), txOutput.getIndex()))
.collect(Collectors.toList());
}
private static class HashIndex {
public Sha256Hash hash;
public int index;
public HashIndex(Sha256Hash hash, int index) {
this.hash = hash;
this.index = index;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HashIndex hashIndex = (HashIndex) o;
return index == hashIndex.index &&
hash.equals(hashIndex.hash);
}
@Override
public int hashCode() {
return Objects.hash(hash, index);
}
}
} }

View file

@ -199,12 +199,19 @@ public class Wallet {
} }
} }
public void clearNodes() {
purposeNodes.clear();
transactions.clear();
storedBlockHeight = 0;
}
public void clearHistory() { public void clearHistory() {
for(WalletNode purposeNode : purposeNodes) { for(WalletNode purposeNode : purposeNodes) {
purposeNode.clearHistory(); purposeNode.clearHistory();
} }
transactions.clear(); transactions.clear();
storedBlockHeight = 0;
} }
public boolean isValid() { public boolean isValid() {