From d17ba0747575ccae873401d3dcc22cc8dd81bfed Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 21 Aug 2016 11:04:47 -0700 Subject: [PATCH] mempool: mem usage estimation. --- lib/bcoin/mempool.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 75805fd6..76f83a51 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -1493,7 +1493,7 @@ Mempool.prototype._removeSpenders = function _removeSpenders(entry) { Mempool.prototype.memUsage = function memUsage(tx) { var mem = 0; - var i, input, output; + var i, j, input, output, coin, op; mem += 264; // tx mem += 80; // _hash @@ -1507,13 +1507,19 @@ Mempool.prototype.memUsage = function memUsage(tx) { mem += 144; // input if (input.coin) { + coin = input.coin; mem += 144; // coin mem += 88; // coin hash mem += 40; // script mem += 80; // script raw buffer mem += 32; // script code array - mem += input.coin.script.code.length * 40; // opcodes - mem += 80; // add a code buffer (assume p2pkh) + mem += coin.script.code.length * 40; // opcodes + + for (j = 0; j < coin.script.code.length; j++) { + op = coin.script.code[j]; + if (op.data) + mem += 80; // op buffers + } } mem += 104; // prevout @@ -1523,7 +1529,12 @@ Mempool.prototype.memUsage = function memUsage(tx) { mem += 80; // script raw buffer mem += 32; // script code array mem += input.script.code.length * 40; // opcodes - mem += 160; // add two code buffers (assume p2pkh) + + 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 @@ -1534,22 +1545,22 @@ Mempool.prototype.memUsage = function memUsage(tx) { for (i = 0; i < tx.outputs.length; i++) { output = tx.outputs[i]; + mem += 120; // output mem += 40; // script mem += 80; // script raw buffer mem += 32; // script code array mem += output.script.code.length * 40; // opcodes - mem += 80; // add a code buffer (assume p2pkh) + + 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 - // Note: normally we could add 176 (88 * 2) - // bytes for the spent and tx hash table - // entries, but v8 seems to store these - // strings off the heap, so it shouldn't - // be too much of a problem. - return mem; };