Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
improve Transaction._estimateFeePerKb() precision
Browse files Browse the repository at this point in the history
Use the binary interpretation of kilobyte (1024 bytes) and return the
correct amount of satoshis.
  • Loading branch information
lpinca committed Dec 17, 2018
1 parent 46c6501 commit 49cd527
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,11 +985,11 @@ Transaction.prototype._clearSignatures = function() {
};

Transaction._estimateFee = function(size, amountAvailable, feePerKb) {
var fee = Math.ceil(size / 1000) * (feePerKb || Transaction.FEE_PER_KB);
var fee = Math.ceil((size / 1024) * (feePerKb || Transaction.FEE_PER_KB));
if (amountAvailable > fee) {
size += Transaction.CHANGE_OUTPUT_MAX_SIZE;
}
return Math.ceil(size / 1000) * (feePerKb || Transaction.FEE_PER_KB);
return Math.ceil((size / 1024) * (feePerKb || Transaction.FEE_PER_KB));
};

Transaction.prototype._estimateSize = function() {
Expand Down
2 changes: 1 addition & 1 deletion test/data/tx_creation.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@
"change", ["3BazTqvkvEBcWk7J4sbgRnxUw6rjYrogf9"],
"sign", ["L2U9m5My3cdyN5qX1PH4B7XstGDZFWwyukdX8gj8vsJ3fkrqArQo"],
"sign", ["L4jFVcDaqZCkknP5KQWjCBgiLFxKxRxywNGTucm3jC3ozByZcbZv"],
"serialize", "010000000220c24f763536edb05ce8df2a4816d971be4f20b58451d71589db434aca98bfaf00000000fdfd0000473044022024b955f8bf6aaf0741da011e3214eaec7040cd12694303471cefc6ba0cc4ec290220124738015033a465636dec1524a6f956a229e69d31aef6c7a98b2a291f3cfc6701483045022100e6ae6c43240e8a11a6de2d034501c2a366c0ccdf069c7828de0791f05e68e787022028b80bd36c2b2ae63fe7afb491da6c0ce23fbbb982450962c817b20f0bb24075014c695221020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2210271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b2103a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd853aeffffffffa0644cd1606e081c59eb65fe69d4a83a3a822da423bc392c91712fb77a192edc00000000fc00483045022100ae7f136cf906dc37d34d5035b8d2001c6a783773b74507ba83080e73e903623f0220023baf7738395268f7097e5586130f682b911fd49b83b265f8fa481f2a6b1ee90146304302201d60f512a8b37663d85c123933053e0354f13d89daf699ca600defa03d4a1dab021f41042b6e4ba30311fc3a68c228c3725f3b0f05a4453ef19408e6a4ae30a2b0014c695221020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2210271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b2103a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd853aeffffffff03f04902000000000017a9144de752833233fe69a20064f29b2ca0f6399c8af387007102000000000017a9144de752833233fe69a20064f29b2ca0f6399c8af387ab2f03000000000017a9146c8d8b04c6a1e664b1ec20ec932760760c97688e8700000000"
"serialize", "010000000220c24f763536edb05ce8df2a4816d971be4f20b58451d71589db434aca98bfaf00000000fc0047304402203afdadf56a6b8a327d69d358ff23c15f8be79a0da079f2029c799c7219001d3302200265c701d4746ba59c1cad7402f85d1a11a81ff151a422f05e135d806fc7fce80147304402206f7f16cdd778caa10b4cadf39151ac5e52c9b1a1d6fe25fbad3afb5abe79135e0220474b975fa2f21199cca4e6763de16511efa501ad79e1ad9a4cdb98e54c05edf2014c695221020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2210271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b2103a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd853aeffffffffa0644cd1606e081c59eb65fe69d4a83a3a822da423bc392c91712fb77a192edc00000000fdfd0000473044022003b21d29a9d8505b34377f426d43aeed4fd8bf69910791315420271ad84ba1a3022003a973fcaed688684c60f4633748cd0391dc9a203f395970fde5f321c6a5c20901483045022100f05598380b0ac6f4ef4c91cefcdf9c0a98965245db1026c757868150d7858857022008d36141e8f7b9d729466cc4df3b0215c5665be45084be8f8291aa6f335a4b8d014c695221020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2210271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b2103a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd853aeffffffff03f04902000000000017a9144de752833233fe69a20064f29b2ca0f6399c8af387007102000000000017a9144de752833233fe69a20064f29b2ca0f6399c8af3873bb803000000000017a9146c8d8b04c6a1e664b1ec20ec932760760c97688e8700000000"
]
]
10 changes: 5 additions & 5 deletions test/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('Transaction', function() {
.change(changeAddress)
.sign(privateKey);
transaction.outputs.length.should.equal(2);
transaction.outputs[1].satoshis.should.equal(400000);
transaction.outputs[1].satoshis.should.equal(477636);
transaction.outputs[1].script.toString()
.should.equal(Script.fromAddress(changeAddress).toString());
var actual = transaction.getChangeOutput().script.toString();
Expand Down Expand Up @@ -384,7 +384,7 @@ describe('Transaction', function() {
.sign(privateKey);
transaction._estimateSize().should.be.within(1000, 1999);
transaction.outputs.length.should.equal(2);
transaction.outputs[1].satoshis.should.equal(34000);
transaction.outputs[1].satoshis.should.equal(40687);
});
it('if satoshis are invalid', function() {
var transaction = new Transaction()
Expand Down Expand Up @@ -935,7 +935,7 @@ describe('Transaction', function() {
.change(changeAddress)
.to(toAddress, 1000);
transaction.inputAmount.should.equal(100000000);
transaction.outputAmount.should.equal(99900000);
transaction.outputAmount.should.equal(99977636);
});
it('returns correct values for coinjoin transaction', function() {
// see livenet tx c16467eea05f1f30d50ed6dbc06a38539d9bb15110e4b7dc6653046a3678a718
Expand Down Expand Up @@ -1007,7 +1007,7 @@ describe('Transaction', function() {
expect(function() {
tx.shuffleOutputs();
}).to.not.throw(errors.Transaction.InvalidSorting);
})
});
});

describe('clearOutputs', function() {
Expand All @@ -1027,7 +1027,7 @@ describe('Transaction', function() {
tx.outputs.length.should.equal(2);
tx.outputs[0].satoshis.should.equal(10000000);
tx.outputs[0].script.toAddress().toString().should.equal(toAddress);
tx.outputs[1].satoshis.should.equal(89900000);
tx.outputs[1].satoshis.should.equal(89977636);
tx.outputs[1].script.toAddress().toString().should.equal(changeAddress);
});

Expand Down

0 comments on commit 49cd527

Please sign in to comment.