Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions test/fixtures/network.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"valid": [
{
"description": "when txSize < 1kb",
"network": "bitcoin",
"txSize": 1,
"fee": 10000
},
{
"description": "when txSize >= 1kb",
"network": "bitcoin",
"txSize": 1000,
"fee": 10000
},
{
"description": "rounding",
"network": "bitcoin",
"txSize": 2800,
"fee": 30000
},
{
"description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used",
"network": "dogecoin",
"txSize": 1000,
"outputs": [
{
"value": 100000000
}
],
"fee": 100000000
},
{
"description": "when not every outputs.value > DUST_SOFT_LIMIT",
"network": "dogecoin",
"txSize": 1000,
"outputs": [
{
"value": 99999999
},
{
"value": 99999999
}
],
"fee": 300000000
},
{
"description": "rounding",
"network": "dogecoin",
"txSize": 2800,
"fee": 300000000
},
{
"description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used",
"network": "litecoin",
"txSize": 1000,
"outputs": [
{
"value": 100000
}
],
"fee": 100000
},
{
"description": "when not every outputs.value > DUST_SOFT_LIMIT",
"network": "litecoin",
"txSize": 1000,
"outputs": [
{
"value": 99999
},
{
"value": 99999
}
],
"fee": 300000
},
{
"description": "rounding",
"network": "litecoin",
"txSize": 2800,
"fee": 300000
}
]
}
81 changes: 11 additions & 70 deletions test/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var networks = require('../src/networks')
var sinon = require('sinon')
var Transaction = require('../src/transaction')

var fixtures = require('./fixtures/network')

describe('networks', function() {
var txToBuffer
before(function(){
Expand All @@ -13,80 +15,19 @@ describe('networks', function() {
Transaction.prototype.toBuffer.restore()
})

describe('bitcoin', function() {
describe('estimateFee', function() {
var estimateFee = networks.bitcoin.estimateFee

it('works at boundry', function() {
txToBuffer.returns(new Buffer(1000))
var tx = new Transaction()
assert.equal(estimateFee(tx), 10000)
})

it('rounds up to the closest kb for estimation', function() {
txToBuffer.returns(new Buffer(2800))
var tx = new Transaction()
assert.equal(estimateFee(tx), 30000)
})
})
})

describe('dogecoin', function() {
describe('estimateFee', function() {
var estimateFee = networks.dogecoin.estimateFee

it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() {
txToBuffer.returns(new Buffer(1000))
var tx = new Transaction()
tx.outs[0] = { value: 100000000 }

assert.equal(estimateFee(tx), 100000000)
})

it('applies additional fee on every output with value below DUST_SOFT_LIMIT', function() {
txToBuffer.returns(new Buffer(1000))
var tx = new Transaction()
tx.outs[0] = { value: 99999999 }
tx.outs[1] = { value: 99999999 }

assert.equal(estimateFee(tx), 3 * 100000000)
})

it('rounds up to the closest kb for estimation', function() {
txToBuffer.returns(new Buffer(2800))
var tx = new Transaction()

assert.equal(estimateFee(tx), 300000000)
})
})
})

describe('litecoin', function() {
describe('estimateFee', function() {
var estimateFee = networks.litecoin.estimateFee

it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() {
txToBuffer.returns(new Buffer(1000))
var tx = new Transaction()
tx.outs[0] = { value: 100000 }

assert.equal(estimateFee(tx), 100000)
})

it('applies additional fee on every output with value below DUST_SOFT_LIMIT', function() {
txToBuffer.returns(new Buffer(1000))
var tx = new Transaction()
tx.outs[0] = { value: 99999 }
tx.outs[1] = { value: 99999 }
fixtures.valid.forEach(function(f) {
describe(f.network + ' estimateFee', function() {
var network = networks[f.network]

assert.equal(estimateFee(tx), 3 * 100000)
})
it('calculates the fee correctly for ' + f.description, function() {
var buffer = new Buffer(f.txSize)
txToBuffer.returns(buffer)

it('rounds up to the closest kb for estimation', function() {
txToBuffer.returns(new Buffer(2800))
var estimateFee = network.estimateFee
var tx = new Transaction()
tx.outs = f.outputs || []

assert.equal(estimateFee(tx), 300000)
assert.equal(estimateFee(tx), f.fee)
})
})
})
Expand Down