From d92d6487b5376404291195dfbaf28da589668f8b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 28 Feb 2017 20:53:49 -0800 Subject: [PATCH] mempool: move memUsage method. --- lib/mempool/mempool.js | 74 +------------------------------------ lib/mempool/mempoolentry.js | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 88dc25cb..606074f5 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -1709,7 +1709,7 @@ Mempool.prototype.trackEntry = function trackEntry(entry, view) { this.indexEntry(entry, view); } - this.size += this.memUsage(tx); + this.size += entry.memUsage(); this.totalTX++; }; @@ -1738,7 +1738,7 @@ Mempool.prototype.untrackEntry = function untrackEntry(entry) { if (this.options.indexAddress) this.unindexEntry(entry); - this.size -= this.memUsage(tx); + this.size -= entry.memUsage(); this.totalTX--; }; @@ -1840,76 +1840,6 @@ Mempool.prototype.removeDoubleSpends = function removeDoubleSpends(tx) { } }; -/** - * Calculate the memory usage of a transaction. - * Note that this only calculates the JS heap - * size. Sizes of buffers are ignored (the v8 - * heap is what we care most about). All numbers - * are based on the output of v8 heap snapshots - * of TX objects. - * @param {TX} tx - * @returns {Number} Usage in bytes. - */ - -Mempool.prototype.memUsage = function memUsage(tx) { - var mem = 0; - var i, j, input, output, op; - - mem += 208; // tx - mem += 80; // _hash - mem += 88; // _hhash - mem += 80; // _raw - mem += 80; // _whash - mem += 48; // mutable - - mem += 32; // input array - - for (i = 0; i < tx.inputs.length; i++) { - input = tx.inputs[i]; - - mem += 120; // input - mem += 104; // prevout - mem += 88; // prevout hash - - mem += 40; // script - mem += 80; // script raw buffer - mem += 32; // script code array - mem += input.script.code.length * 40; // opcodes - - for (j = 0; j < input.script.code.length; j++) { - op = input.script.code[j]; - if (op.data) - mem += 80; // op buffers - } - - mem += 96; // witness - mem += 32; // witness items - mem += input.witness.items.length * 80; // witness buffers - } - - mem += 32; // output array - - for (i = 0; i < tx.outputs.length; i++) { - output = tx.outputs[i]; - - mem += 104; // output - mem += 40; // script - mem += 80; // script raw buffer - mem += 32; // script code array - mem += output.script.code.length * 40; // opcodes - - for (j = 0; j < output.script.code.length; j++) { - op = output.script.code[j]; - if (op.data) - mem += 80; // op buffers - } - } - - mem += 176; // mempool entry - - return mem; -}; - /** * Calculate the memory usage of the entire mempool. * @see DynamicMemoryUsage() diff --git a/lib/mempool/mempoolentry.js b/lib/mempool/mempoolentry.js index 4a53086f..c9a3832b 100644 --- a/lib/mempool/mempoolentry.js +++ b/lib/mempool/mempoolentry.js @@ -176,6 +176,77 @@ MempoolEntry.prototype.getDescRate = function getDescRate() { return policy.getRate(this.descSize, this.descFee); }; +/** + * Calculate the memory usage of a transaction. + * Note that this only calculates the JS heap + * size. Sizes of buffers are ignored (the v8 + * heap is what we care most about). All numbers + * are based on the output of v8 heap snapshots + * of TX objects. + * @returns {Number} Usage in bytes. + */ + +MempoolEntry.prototype.memUsage = function memUsage() { + var tx = this.tx; + var total = 0; + var i, j, input, output, op; + + total += 176; // mempool entry + total += 48; // dependencies + + total += 208; // tx + total += 80; // _hash + total += 88; // _hhash + total += 80; // _raw + total += 80; // _whash + total += 48; // mutable + + total += 32; // input array + + for (i = 0; i < tx.inputs.length; i++) { + input = tx.inputs[i]; + + total += 120; // input + total += 104; // prevout + total += 88; // prevout hash + + total += 40; // script + total += 80; // script raw buffer + total += 32; // script code array + total += input.script.code.length * 40; // opcodes + + for (j = 0; j < input.script.code.length; j++) { + op = input.script.code[j]; + if (op.data) + total += 80; // op buffers + } + + total += 96; // witness + total += 32; // witness items + total += input.witness.items.length * 80; // witness buffers + } + + total += 32; // output array + + for (i = 0; i < tx.outputs.length; i++) { + output = tx.outputs[i]; + + total += 104; // output + total += 40; // script + total += 80; // script raw buffer + total += 32; // script code array + total += output.script.code.length * 40; // opcodes + + for (j = 0; j < output.script.code.length; j++) { + op = output.script.code[j]; + if (op.data) + total += 80; // op buffers + } + } + + return total; +}; + /** * Test whether the entry is free with * the current priority (calculated by