TransactionOutput: Fix fee calculation in getMinNonDustValue().

Adds a test, too.
This commit is contained in:
Andreas Schildbach
2016-03-12 21:07:45 +01:00
parent 90d8c151be
commit 0bc6c04d5f
2 changed files with 10 additions and 6 deletions

View File

@@ -225,23 +225,21 @@ public class TransactionOutput extends ChildMessage {
* <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses * <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses
* a safe fee-per-kb by default.</p> * a safe fee-per-kb by default.</p>
* *
* @param feePerKbRequired The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 3 * @param feePerKb The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 3
* If you want a safe default, use {@link Transaction#REFERENCE_DEFAULT_MIN_TX_FEE}*3
*/ */
public Coin getMinNonDustValue(Coin feePerKbRequired) { public Coin getMinNonDustValue(Coin feePerKb) {
// A typical output is 33 bytes (pubkey hash + opcodes) and requires an input of 148 bytes to spend so we add // A typical output is 33 bytes (pubkey hash + opcodes) and requires an input of 148 bytes to spend so we add
// that together to find out the total amount of data used to transfer this amount of value. Note that this // that together to find out the total amount of data used to transfer this amount of value. Note that this
// formula is wrong for anything that's not a pay-to-address output, unfortunately, we must follow Bitcoin Core's // formula is wrong for anything that's not a pay-to-address output, unfortunately, we must follow Bitcoin Core's
// wrongness in order to ensure we're considered standard. A better formula would either estimate the // wrongness in order to ensure we're considered standard. A better formula would either estimate the
// size of data needed to satisfy all different script types, or just hard code 33 below. // size of data needed to satisfy all different script types, or just hard code 33 below.
final long size = this.unsafeBitcoinSerialize().length + 148; final long size = this.unsafeBitcoinSerialize().length + 148;
Coin[] nonDustAndRemainder = feePerKbRequired.multiply(size).divideAndRemainder(1000); return feePerKb.multiply(size).divide(1000);
return nonDustAndRemainder[1].equals(Coin.ZERO) ? nonDustAndRemainder[0] : nonDustAndRemainder[0].add(Coin.SATOSHI);
} }
/** /**
* Returns the minimum value for this output to be considered "not dust", i.e. the transaction will be relayable * Returns the minimum value for this output to be considered "not dust", i.e. the transaction will be relayable
* and mined by default miners. For normal pay to address outputs, this is 546 satoshis, the same as * and mined by default miners. For normal pay to address outputs, this is 2730 satoshis, the same as
* {@link Transaction#MIN_NONDUST_OUTPUT}. * {@link Transaction#MIN_NONDUST_OUTPUT}.
*/ */
public Coin getMinNonDustValue() { public Coin getMinNonDustValue() {

View File

@@ -64,4 +64,10 @@ public class TransactionOutputTest extends TestWithWallet {
assertNull(tx.getOutput(0).getAddressFromP2SH(PARAMS)); assertNull(tx.getOutput(0).getAddressFromP2SH(PARAMS));
assertNull(tx.getOutput(0).getAddressFromP2PKHScript(PARAMS)); assertNull(tx.getOutput(0).getAddressFromP2PKHScript(PARAMS));
} }
@Test
public void getMinNonDustValue() throws Exception {
TransactionOutput payToAddressOutput = new TransactionOutput(PARAMS, null, Coin.COIN, myAddress);
assertEquals(Transaction.MIN_NONDUST_OUTPUT, payToAddressOutput.getMinNonDustValue());
}
} }