From be88a05c9360e45473dd6b0a76a950c646a5b15c Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 16 Jun 2013 20:48:06 +0200 Subject: [PATCH] Make TransactionOutput.getMinNonDustOutput exact. --- core/src/main/java/com/google/bitcoin/core/Transaction.java | 2 +- .../java/com/google/bitcoin/core/TransactionOutput.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Transaction.java b/core/src/main/java/com/google/bitcoin/core/Transaction.java index 3210ac21..71ab1f20 100644 --- a/core/src/main/java/com/google/bitcoin/core/Transaction.java +++ b/core/src/main/java/com/google/bitcoin/core/Transaction.java @@ -65,7 +65,7 @@ public class Transaction extends ChildMessage implements Serializable { * This is calculated by assuming a standard output will be 34 bytes, and then using the formula used in * {@link TransactionOutput#getMinNonDustValue(BigInteger)}. */ - public static final BigInteger MIN_NONDUST_OUTPUT = BigInteger.valueOf(5461); + public static final BigInteger MIN_NONDUST_OUTPUT = BigInteger.valueOf(5460); // These are serialized in both bitcoin and java serialization. private long version; diff --git a/core/src/main/java/com/google/bitcoin/core/TransactionOutput.java b/core/src/main/java/com/google/bitcoin/core/TransactionOutput.java index 558a0bbd..a56ff8bc 100644 --- a/core/src/main/java/com/google/bitcoin/core/TransactionOutput.java +++ b/core/src/main/java/com/google/bitcoin/core/TransactionOutput.java @@ -180,9 +180,9 @@ public class TransactionOutput extends ChildMessage implements Serializable { * If you want a safe default, use {@link Transaction#REFERENCE_DEFAULT_MIN_TX_FEE}*3 */ public BigInteger getMinNonDustValue(BigInteger feePerKbRequired) { - // Note we skip the *3 as that should be considered in the parameter and we add one to account for loss of precision - // (1/1000 chance we require too much fee, 999/1000 chance we get the exact right value...) - return feePerKbRequired.multiply(BigInteger.valueOf(this.bitcoinSerialize().length + 148)).divide(BigInteger.valueOf(1000)).add(BigInteger.ONE); + // Note we skip the *3 as that should be considered in the parameter + BigInteger[] nonDustAndRemainder = feePerKbRequired.multiply(BigInteger.valueOf(this.bitcoinSerialize().length + 148)).divideAndRemainder(BigInteger.valueOf(1000)); + return nonDustAndRemainder[1].equals(BigInteger.ZERO) ? nonDustAndRemainder[0] : nonDustAndRemainder[0].add(BigInteger.ONE); } /**