performance optimisations

This commit is contained in:
Craig Raw 2022-01-20 17:12:00 +02:00
parent 8dca2ee3f0
commit fe61c633ae
4 changed files with 73 additions and 30 deletions

View file

@ -50,7 +50,7 @@ public class ChildNumber {
}
public String toString(boolean useApostrophes) {
return String.format(Locale.US, "%d%s", num(), isHardened() ? (useApostrophes ? "'" : "h") : "");
return num() + (isHardened() ? (useApostrophes ? "'" : "h") : "");
}
public boolean equals(Object o) {

View file

@ -1,3 +1,48 @@
package com.sparrowwallet.drongo.protocol;
public record HashIndex(Sha256Hash hash, long index) {}
public class HashIndex {
private final Sha256Hash hash;
private final long index;
public HashIndex(Sha256Hash hash, long index) {
this.hash = hash;
this.index = index;
}
public Sha256Hash getHash() {
return hash;
}
public long getIndex() {
return index;
}
@Override
public String toString() {
return hash.toString() + ":" + index;
}
@Override
public boolean equals(Object o) {
if(this == o) {
return true;
}
if(o == null || getClass() != o.getClass()) {
return false;
}
HashIndex hashIndex = (HashIndex) o;
if(index != hashIndex.index) {
return false;
}
return hash.equals(hashIndex.hash);
}
@Override
public int hashCode() {
int result = hash.hashCode();
result = 31 * result + (int) (index ^ (index >>> 32));
return result;
}
}

View file

@ -1,18 +1,19 @@
package com.sparrowwallet.drongo.wallet;
import com.sparrowwallet.drongo.protocol.HashIndex;
import com.sparrowwallet.drongo.protocol.Sha256Hash;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.protocol.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.HashSet;
import java.util.Set;
public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> {
private final Transaction transaction;
private final Sha256Hash blockHash;
private final Set<HashIndex> spending = new HashSet<>();
private final Set<HashIndex> funding = new HashSet<>();
public BlockTransaction(Sha256Hash hash, int height, Date date, Long fee, Transaction transaction) {
this(hash, height, date, fee, transaction, null);
}
@ -25,6 +26,15 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
super(hash, height, date, fee, label);
this.transaction = transaction;
this.blockHash = blockHash;
if(transaction != null) {
for(TransactionInput txInput : transaction.getInputs()) {
spending.add(new HashIndex(txInput.getOutpoint().getHash(), txInput.getOutpoint().getIndex()));
}
for(TransactionOutput txOutput : transaction.getOutputs()) {
funding.add(new HashIndex(hash, txOutput.getIndex()));
}
}
}
public Transaction getTransaction() {
@ -35,6 +45,14 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
return blockHash;
}
public Set<HashIndex> getSpending() {
return Collections.unmodifiableSet(spending);
}
public Set<HashIndex> getFunding() {
return Collections.unmodifiableSet(funding);
}
@Override
public int compareTo(BlockTransaction blkTx) {
int blockOrder = compareBlockOrder(blkTx);
@ -50,34 +68,14 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
return getComparisonHeight() - blkTx.getComparisonHeight();
}
if(getReferencedOutpoints(this).removeAll(getOutputs(blkTx))) {
if(!Collections.disjoint(spending, blkTx.funding)) {
return 1;
}
if(getReferencedOutpoints(blkTx).removeAll(getOutputs(this))) {
if(!Collections.disjoint(blkTx.spending, funding)) {
return -1;
}
return 0;
}
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(), 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());
}
}

View file

@ -175,7 +175,7 @@ public class WalletNode extends Persistable implements Comparable<WalletNode> {
@Override
public int hashCode() {
return Objects.hash(derivationPath);
return derivationPath.hashCode();
}
@Override