mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-31 20:11:23 +00:00
TransactionOutput: Fix fee calculation in getMinNonDustValue().
Adds a test, too.
This commit is contained in:
@@ -225,23 +225,21 @@ public class TransactionOutput extends ChildMessage {
|
||||
* <p>You probably should use {@link org.bitcoinj.core.TransactionOutput#getMinNonDustValue()} which uses
|
||||
* 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
|
||||
* If you want a safe default, use {@link Transaction#REFERENCE_DEFAULT_MIN_TX_FEE}*3
|
||||
* @param feePerKb The fee required per kilobyte. Note that this is the same as Bitcoin Core's -minrelaytxfee * 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
|
||||
// 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
|
||||
// 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.
|
||||
final long size = this.unsafeBitcoinSerialize().length + 148;
|
||||
Coin[] nonDustAndRemainder = feePerKbRequired.multiply(size).divideAndRemainder(1000);
|
||||
return nonDustAndRemainder[1].equals(Coin.ZERO) ? nonDustAndRemainder[0] : nonDustAndRemainder[0].add(Coin.SATOSHI);
|
||||
return feePerKb.multiply(size).divide(1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*/
|
||||
public Coin getMinNonDustValue() {
|
||||
|
@@ -64,4 +64,10 @@ public class TransactionOutputTest extends TestWithWallet {
|
||||
assertNull(tx.getOutput(0).getAddressFromP2SH(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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user