networks: remove estimateFee entirely

This commit is contained in:
Daniel Cousens 2015-03-16 12:35:49 +11:00
parent 7d2e1cd3ba
commit bd464d7cb5
3 changed files with 41 additions and 193 deletions

View file

@ -1,7 +1,7 @@
// https://en.bitcoin.it/wiki/List_of_address_prefixes // https://en.bitcoin.it/wiki/List_of_address_prefixes
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731 // Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
var networks = { module.exports = {
bitcoin: { bitcoin: {
magic: 0xd9b4bef9, magic: 0xd9b4bef9,
messagePrefix: '\x18Bitcoin Signed Message:\n', messagePrefix: '\x18Bitcoin Signed Message:\n',
@ -12,8 +12,7 @@ var networks = {
pubKeyHash: 0x00, pubKeyHash: 0x00,
scriptHash: 0x05, scriptHash: 0x05,
wif: 0x80, wif: 0x80,
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162 dustThreshold: 546 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162
feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
}, },
testnet: { testnet: {
magic: 0xd9b4bef9, magic: 0xd9b4bef9,
@ -25,8 +24,7 @@ var networks = {
pubKeyHash: 0x6f, pubKeyHash: 0x6f,
scriptHash: 0xc4, scriptHash: 0xc4,
wif: 0xef, wif: 0xef,
dustThreshold: 546, dustThreshold: 546
feePerKb: 10000
}, },
litecoin: { litecoin: {
magic: 0xd9b4bef9, magic: 0xd9b4bef9,
@ -38,9 +36,7 @@ var networks = {
pubKeyHash: 0x30, pubKeyHash: 0x30,
scriptHash: 0x05, scriptHash: 0x05,
wif: 0xb0, wif: 0xb0,
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365 dustThreshold: 0 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53
feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
}, },
dogecoin: { dogecoin: {
messagePrefix: '\x19Dogecoin Signed Message:\n', messagePrefix: '\x19Dogecoin Signed Message:\n',
@ -51,37 +47,6 @@ var networks = {
pubKeyHash: 0x1e, pubKeyHash: 0x1e,
scriptHash: 0x16, scriptHash: 0x16,
wif: 0x9e, wif: 0x9e,
dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160 dustThreshold: 0 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160
dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62
feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
} }
} }
function estimateFee (tx, network) {
var baseFee = network.feePerKb
var byteSize = tx.byteLength()
var fee = baseFee * Math.ceil(byteSize / 1000)
if (network.dustSoftThreshold === undefined) return fee
tx.outs.forEach(function (output) {
if (output.value < network.dustSoftThreshold) {
fee += baseFee
}
})
return fee
}
// FIXME: 1.5.3 compatibility patch(s)
function patchEstimateFee (network, tx) {
return estimateFee(tx, network)
}
for (var networkName in networks) {
var network = networks[networkName]
network.estimateFee = patchEstimateFee.bind(null, network)
}
module.exports = networks

View file

@ -1,6 +1,4 @@
{ [
"valid": {
"constants": [
{ {
"network": "bitcoin", "network": "bitcoin",
"bip32": { "bip32": {
@ -29,88 +27,4 @@
"public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP" "public": "dgub8kXBZ7ymNWy2S8Q3jNgVjFUm5ZJ3QLLaSTdAA89ukSv7Q6MSXwE14b7Nv6eDpE9JJXinTKc8LeLVu19uDPrm5uJuhpKNzV2kAgncwo6bNpP"
} }
} }
],
"estimateFee": [
{
"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
}
] ]
}
}

View file

@ -1,53 +1,22 @@
/* global describe, it, before, after */ /* global describe, it */
var assert = require('assert') var assert = require('assert')
var networks = require('../src/networks') var networks = require('../src/networks')
var sinon = require('sinon')
var HDNode = require('../src/hdnode') var HDNode = require('../src/hdnode')
var Transaction = require('../src/transaction')
var fixtures = require('./fixtures/network') var fixtures = require('./fixtures/network')
describe('networks', function () { describe('networks', function () {
var txByteLength fixtures.forEach(function (f) {
before(function () {
txByteLength = sinon.stub(Transaction.prototype, 'byteLength')
})
after(function () {
Transaction.prototype.byteLength.restore()
})
describe('constants', function () {
fixtures.valid.constants.forEach(function (f) {
var network = networks[f.network] var network = networks[f.network]
Object.keys(f.bip32).forEach(function (name) { Object.keys(f.bip32).forEach(function (name) {
var extb58 = f.bip32[name] var extb58 = f.bip32[name]
it('resolves ' + extb58 + ' to ' + f.network, function () { it(extb58 + ' auto-detects ' + f.network, function () {
assert.equal(HDNode.fromBase58(extb58).network, network) assert.equal(HDNode.fromBase58(extb58).network, network)
}) })
}) })
}) })
}) })
describe('estimateFee', function () {
fixtures.valid.estimateFee.forEach(function (f) {
describe('(' + f.network + ')', function () {
var network = networks[f.network]
it('calculates the fee correctly for ' + f.description, function () {
txByteLength.returns(f.txSize)
var estimateFee = network.estimateFee
var tx = new Transaction()
tx.outs = f.outputs || []
assert.equal(estimateFee(tx), f.fee)
})
})
})
})
})