From 808d8678a67c554b3a6d98946d1d67daa797e008 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 11 Dec 2016 10:33:00 -0800 Subject: [PATCH] tx: add sigops vsize calculation. --- lib/primitives/tx.js | 14 ++++++++++++++ lib/protocol/constants.js | 1 + 2 files changed, 15 insertions(+) diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 1a4832c1..4706525a 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -324,6 +324,20 @@ TX.prototype.getVirtualSize = function getVirtualSize() { return (this.getWeight() + scale - 1) / scale | 0; }; +/** + * Calculate the virtual size of the transaction + * (weighted against bytes per sigop cost). + * @param {Number} sigops - Sigops cost. + * @returns {Number} vsize + */ + +TX.prototype.getSigopsSize = function getSigopsSize(sigops) { + var scale = constants.WITNESS_SCALE_FACTOR; + var bytes = constants.tx.BYTES_PER_SIGOP; + var weight = Math.max(this.getWeight(), sigops * bytes); + return (weight + scale - 1) / scale | 0; +}; + /** * Calculate the weight of the transaction. * Note that this is cached. diff --git a/lib/protocol/constants.js b/lib/protocol/constants.js index 910a2afb..c066fbcd 100644 --- a/lib/protocol/constants.js +++ b/lib/protocol/constants.js @@ -422,6 +422,7 @@ exports.tx = { FREE_THRESHOLD: exports.COIN * 144 / 250, MAX_SIGOPS: exports.block.MAX_SIGOPS / 5, MAX_SIGOPS_WEIGHT: exports.block.MAX_SIGOPS_WEIGHT / 5, + BYTES_PER_SIGOP: 20, COINBASE_MATURITY: 100 };