mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-26 01:56:44 +00:00
performance optimisations
This commit is contained in:
parent
8dca2ee3f0
commit
fe61c633ae
4 changed files with 73 additions and 30 deletions
|
@ -50,7 +50,7 @@ public class ChildNumber {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(boolean useApostrophes) {
|
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) {
|
public boolean equals(Object o) {
|
||||||
|
|
|
@ -1,3 +1,48 @@
|
||||||
package com.sparrowwallet.drongo.protocol;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package com.sparrowwallet.drongo.wallet;
|
package com.sparrowwallet.drongo.wallet;
|
||||||
|
|
||||||
import com.sparrowwallet.drongo.protocol.HashIndex;
|
import com.sparrowwallet.drongo.protocol.*;
|
||||||
import com.sparrowwallet.drongo.protocol.Sha256Hash;
|
|
||||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.HashSet;
|
||||||
import java.util.stream.Collectors;
|
import java.util.Set;
|
||||||
|
|
||||||
public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> {
|
public class BlockTransaction extends BlockTransactionHash implements Comparable<BlockTransaction> {
|
||||||
private final Transaction transaction;
|
private final Transaction transaction;
|
||||||
private final Sha256Hash blockHash;
|
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) {
|
public BlockTransaction(Sha256Hash hash, int height, Date date, Long fee, Transaction transaction) {
|
||||||
this(hash, height, date, fee, transaction, null);
|
this(hash, height, date, fee, transaction, null);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +26,15 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
|
||||||
super(hash, height, date, fee, label);
|
super(hash, height, date, fee, label);
|
||||||
this.transaction = transaction;
|
this.transaction = transaction;
|
||||||
this.blockHash = blockHash;
|
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() {
|
public Transaction getTransaction() {
|
||||||
|
@ -35,6 +45,14 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
|
||||||
return blockHash;
|
return blockHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<HashIndex> getSpending() {
|
||||||
|
return Collections.unmodifiableSet(spending);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<HashIndex> getFunding() {
|
||||||
|
return Collections.unmodifiableSet(funding);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(BlockTransaction blkTx) {
|
public int compareTo(BlockTransaction blkTx) {
|
||||||
int blockOrder = compareBlockOrder(blkTx);
|
int blockOrder = compareBlockOrder(blkTx);
|
||||||
|
@ -50,34 +68,14 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable
|
||||||
return getComparisonHeight() - blkTx.getComparisonHeight();
|
return getComparisonHeight() - blkTx.getComparisonHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(getReferencedOutpoints(this).removeAll(getOutputs(blkTx))) {
|
if(!Collections.disjoint(spending, blkTx.funding)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(getReferencedOutpoints(blkTx).removeAll(getOutputs(this))) {
|
if(!Collections.disjoint(blkTx.spending, funding)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,7 +175,7 @@ public class WalletNode extends Persistable implements Comparable<WalletNode> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(derivationPath);
|
return derivationPath.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue