From fe61c633ae230c3425d52a0c9ac6264ea77e5041 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 20 Jan 2022 17:12:00 +0200 Subject: [PATCH] performance optimisations --- .../drongo/crypto/ChildNumber.java | 2 +- .../drongo/protocol/HashIndex.java | 47 ++++++++++++++++- .../drongo/wallet/BlockTransaction.java | 52 +++++++++---------- .../drongo/wallet/WalletNode.java | 2 +- 4 files changed, 73 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/crypto/ChildNumber.java b/src/main/java/com/sparrowwallet/drongo/crypto/ChildNumber.java index 0f83fbb..90d591c 100644 --- a/src/main/java/com/sparrowwallet/drongo/crypto/ChildNumber.java +++ b/src/main/java/com/sparrowwallet/drongo/crypto/ChildNumber.java @@ -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) { diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/HashIndex.java b/src/main/java/com/sparrowwallet/drongo/protocol/HashIndex.java index b192c4d..2559099 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/HashIndex.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/HashIndex.java @@ -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; + } +} diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java index 31b1c50..dec93b9 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java @@ -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 { private final Transaction transaction; private final Sha256Hash blockHash; + private final Set spending = new HashSet<>(); + private final Set 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 getSpending() { + return Collections.unmodifiableSet(spending); + } + + public Set 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 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 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()); - } } diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java index 7012b5b..d1fbc9d 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletNode.java @@ -175,7 +175,7 @@ public class WalletNode extends Persistable implements Comparable { @Override public int hashCode() { - return Objects.hash(derivationPath); + return derivationPath.hashCode(); } @Override