Skip to content

Commit ad10033

Browse files
authored
Merge pull request #1 from twostack/1-2-0-SNAPSHOT
Fee calculation fixes.
2 parents cd4bf28 + bddb734 commit ad10033

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group 'org.twostack'
9-
version '1.1.0'
9+
version '1.2.0-SNAPSHOT'
1010

1111
repositories {
1212
mavenCentral()

src/main/java/org/twostack/bitcoin4j/transaction/TransactionBuilder.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class TransactionBuilder {
4040

4141
private final long DEFAULT_FEE_PER_KB = 512; //amount in satoshis
4242

43-
static final BigInteger DUST_AMOUNT = BigInteger.valueOf(546);
43+
static final BigInteger DUST_AMOUNT = BigInteger.valueOf(256);
4444

4545
/// Margin of error to allow fees in the vecinity of the expected value but doesn't allow a big difference
4646
private static final BigInteger FEE_SECURITY_MARGIN = BigInteger.valueOf(150);
@@ -264,6 +264,10 @@ public Transaction build(boolean performChecks) throws TransactionException {
264264
//add transaction outputs
265265
tx.addOutputs(outputs);
266266

267+
if (changeScriptBuilder != null) {
268+
tx.addOutput(getChangeOutput());
269+
}
270+
267271
tx.setLockTime(nLockTime);
268272

269273
return tx;
@@ -309,6 +313,12 @@ private void checkForDustErrors() throws TransactionException {
309313
throw new TransactionException("You have outputs with spending values below the dust limit of " + DUST_AMOUNT.toString());
310314
}
311315
}
316+
317+
//check for dust on change output
318+
if (getChangeOutput() != null && (getChangeOutput().getAmount().compareTo(DUST_AMOUNT) == -1)){
319+
throw new TransactionException("You have a change output with spending value below the dust limit of " + DUST_AMOUNT.toString());
320+
}
321+
312322
}
313323

314324

@@ -358,6 +368,9 @@ private void updateChangeOutput(){
358368
//spent amount equals input amount. No change generated. Return.
359369
if (calcRecipientTotals() == calcInputTotals()) return;
360370

371+
//clear change outputs
372+
changeOutput = null;
373+
361374
changeAmount = calculateChange();
362375
TransactionOutput output = getChangeOutput();
363376
output.setAmount(changeAmount);
@@ -399,6 +412,13 @@ private BigInteger estimateFee(){
399412

400413
BigInteger fee = BigInteger.valueOf(new Float(size / 1000 * feePerKb).longValue());
401414

415+
//if fee is less that 256, set fee at 256 satoshis
416+
//this is current minimum we set automatically if no explicit fee given
417+
//FIXME: Make this configurable
418+
if (fee.compareTo(BigInteger.valueOf(256)) == -1){
419+
fee = BigInteger.valueOf(256);
420+
}
421+
402422
return fee;
403423
}
404424

@@ -433,6 +453,12 @@ private BigInteger calcRecipientTotals() {
433453
amount = amount.add(output.getAmount());
434454
};
435455

456+
//deduct change output
457+
if (changeScriptBuilder != null){
458+
TransactionOutput changeOutput = getChangeOutput();
459+
amount = amount.add(changeOutput.getAmount());
460+
}
461+
436462
return amount;
437463
}
438464

src/test/java/org/twostack/bitcoin4j/transaction/TransactionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public void test_transaction_serialization_vectors() throws IOException, Transac
155155
//txHex
156156
String serializedTx = serializedTxNode.asText();
157157

158+
System.out.println(txId);
158159

159160
builder.spendFromUtxoMap(utxoMap, new P2PKHUnlockBuilder(privateKey.getPublicKey()));
160161
builder.withFeePerKb(100000);

0 commit comments

Comments
 (0)