From 704c95c90dd3fd5aebca7de104dccb3a702bc02c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 20 Jan 2016 12:31:42 -0800 Subject: [PATCH] add tx.getPriority and tx.isFree. --- lib/bcoin/protocol/constants.js | 12 +++++----- lib/bcoin/tx.js | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 8f3012e3..d586a462 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -179,6 +179,10 @@ Object.keys(exports.opcodes).forEach(function(name) { exports.opcodesByVal[val] = name; }); +exports.coin = new bn(10000000).muln(10); +exports.cent = new bn(1000000); +exports.maxMoney = new bn(21000000).mul(exports.coin); + exports.hashType = { all: 1, none: 2, @@ -203,7 +207,9 @@ exports.tx = { maxSize: 100000, fee: 10000, dust: 5460, - bareMultisig: true + bareMultisig: true, + freeThreshold: exports.coin.muln(144).divn(250), + maxFreeSize: 1000 }; exports.script = { @@ -259,9 +265,5 @@ exports.zeroHash = utils.toArray( exports.userVersion = require('../../../package.json').version; exports.userAgent = '/bcoin:' + exports.userVersion + '/'; -exports.coin = new bn(10000000).muln(10); -exports.cent = new bn(1000000); -exports.maxMoney = new bn(21000000).mul(exports.coin); - exports.banTime = 24 * 60 * 60; exports.banScore = 100; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index e720cbb8..1fce32f8 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1326,6 +1326,45 @@ TX.prototype.isStandardInputs = function isStandardInputs(flags) { return true; }; +TX.prototype.getPriority = function getPriority() { + var size, value, i, input, output, age; + + size = this.maxSize(); + value = new bn(0); + + for (i = 0; i < this.inputs.length; i++) { + input = this.inputs[i]; + + if (!input.out.tx) + return constants.tx.freeThreshold.clone(); + + output = input.out.tx.outputs[input.out.index]; + age = input.out.tx.getConfirmations(); + + if (age === -1) + age = 0; + + if (age !== 0) + age += 1; + + value.iadd(output.value.muln(age)); + } + + return priority.divn(size); +}; + +TX.prototype.isFree = function isFree() { + var size = this.maxSize(); + var priority; + + if (size >= constants.tx.maxFreeSize) + return false; + + priority = this.getPriority(); + + return priority.cmp(constants.tx.freeThreshold) > 0; +}; + TX.prototype.getHeight = function getHeight() { if (!this.chain) return -1;