From f0aa9491746cfa9d12630ff1f5ccdcf97cc4aa90 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 17 Aug 2020 16:47:55 +0200 Subject: [PATCH] fix compareTo bug with unconfirmed parents --- .../sparrowwallet/drongo/wallet/BlockTransaction.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java index bf866a9..586f843 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/BlockTransaction.java @@ -34,7 +34,7 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable @Override public int compareTo(BlockTransaction blkTx) { if(getHeight() != blkTx.getHeight()) { - return (getHeight() > 0 ? getHeight() : Integer.MAX_VALUE) - (blkTx.getHeight() > 0 ? blkTx.getHeight() : Integer.MAX_VALUE); + return getComparisonHeight() - blkTx.getComparisonHeight(); } if(getReferencedOutpoints(this).removeAll(getOutputs(blkTx))) { @@ -48,6 +48,15 @@ public class BlockTransaction extends BlockTransactionHash implements Comparable return super.compareTo(blkTx); } + /** + * Calculates a special height value that places txes with unconfirmed parents first, then normal unconfirmed txes, then confirmed txes + * + * @return the modified height value + */ + private int getComparisonHeight() { + return (getHeight() > 0 ? getHeight() : (getHeight() == -1 ? Integer.MAX_VALUE : Integer.MAX_VALUE - getHeight() - 1)); + } + private static List getReferencedOutpoints(BlockTransaction blockchainTransaction) { if(blockchainTransaction.getTransaction() == null) { return Collections.emptyList();