From dfefc7182ffa8229a47553d3f378045fd3459851 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 11 Dec 2016 18:57:15 -0800 Subject: [PATCH] serialization: more sizes. --- lib/coins/coinview.js | 25 +++++++ lib/hd/mnemonic.js | 16 +---- lib/mempool/fees.js | 13 +--- lib/net/bip151.js | 3 +- lib/net/bip152.js | 50 +++++++------ lib/net/packets.js | 122 +++++++++++++++++++++---------- lib/primitives/block.js | 16 ++--- lib/primitives/keyring.js | 6 +- lib/script/script.js | 2 +- lib/script/witness.js | 3 +- lib/utils/bloom.js | 6 +- lib/utils/encoding.js | 37 ++++++++++ lib/wallet/account.js | 9 +-- lib/wallet/masterkey.js | 14 ++-- lib/wallet/path.js | 6 +- lib/wallet/wallet.js | 11 +-- lib/workers/framer.js | 5 +- lib/workers/packets.js | 147 +++++++++++++++++++++++++++++++++++++- 18 files changed, 352 insertions(+), 139 deletions(-) diff --git a/lib/coins/coinview.js b/lib/coins/coinview.js index 2e0f5b3f..e64fd23f 100644 --- a/lib/coins/coinview.js +++ b/lib/coins/coinview.js @@ -12,6 +12,7 @@ var Coins = require('./coins'); var UndoCoins = require('./undocoins'); var BufferReader = require('../utils/reader'); var BufferWriter = require('../utils/writer'); +var encoding = require('../utils/encoding'); var CoinEntry = Coins.CoinEntry; /** @@ -381,6 +382,30 @@ CoinView.prototype.toArray = function toArray() { return out; }; +/** + * Calculate serialization size. + * @returns {Number} + */ + +CoinView.prototype.getFastSize = function getFastSize(tx) { + var size = 0; + var i, input, entry; + + size += tx.inputs.length; + + for (i = 0; i < tx.inputs.length; i++) { + input = tx.inputs[i]; + entry = this.getEntry(input); + + if (!entry) + continue; + + size += entry.getSize(); + } + + return size; +}; + /** * Write coin data to buffer writer * as it pertains to a transaction. diff --git a/lib/hd/mnemonic.js b/lib/hd/mnemonic.js index af9f331b..a1e5a4b4 100644 --- a/lib/hd/mnemonic.js +++ b/lib/hd/mnemonic.js @@ -421,20 +421,10 @@ Mnemonic.fromJSON = function fromJSON(json) { Mnemonic.prototype.getSize = function getSize() { var size = 0; - var len; - - size += 2; - size += 1; + size += 3; size += this.getEntropy().length; - - len = Buffer.byteLength(this.getPhrase(), 'utf8'); - size += encoding.sizeVarint(len); - size += len; - - len = Buffer.byteLength(this.passphrase, 'utf8'); - size += encoding.sizeVarint(len); - size += len; - + size += encoding.sizeVarString(this.getPhrase(), 'utf8'); + size += encoding.sizeVarString(this.passphrase, 'utf8'); return size; }; diff --git a/lib/mempool/fees.js b/lib/mempool/fees.js index 323c5d2a..43fec745 100644 --- a/lib/mempool/fees.js +++ b/lib/mempool/fees.js @@ -809,18 +809,9 @@ PolicyEstimator.prototype.estimatePriority = function estimatePriority(target, s PolicyEstimator.prototype.getSize = function getSize() { var size = 0; - var len; - size += 8; - - len = this.feeStats.getSize(); - size += encoding.sizeVarint(len); - size += len; - - len = this.priStats.getSize(); - size += encoding.sizeVarint(len); - size += len; - + size += encoding.sizeVarlen(this.feeStats.getSize()); + size += encoding.sizeVarlen(this.priStats.getSize()); return size; }; diff --git a/lib/net/bip151.js b/lib/net/bip151.js index ea6a1f65..b55b0aa5 100644 --- a/lib/net/bip151.js +++ b/lib/net/bip151.js @@ -528,8 +528,7 @@ BIP151.prototype.maybeRekey = function maybeRekey(data) { BIP151.prototype.packetSize = function packetSize(cmd, body) { var size = 0; size += 4; - size += encoding.sizeVarint(cmd.length); - size += cmd.length; + size += encoding.sizeVarString(cmd, 'ascii'); size += 4; size += body.length; size += 16; diff --git a/lib/net/bip152.js b/lib/net/bip152.js index d0d0ced0..c47c24ad 100644 --- a/lib/net/bip152.js +++ b/lib/net/bip152.js @@ -134,24 +134,24 @@ CompactBlock.fromRaw = function fromRaw(data, enc) { }; CompactBlock.prototype.toRaw = function toRaw() { - return this.frame(true); + return this.frameRaw(true); }; CompactBlock.prototype.toNormal = function toNormal() { - return this.frame(false); + return this.frameRaw(false); }; CompactBlock.prototype.toWriter = function toWriter(bw) { - return this.frameWriter(bw, true); + return this.writeRaw(bw, true); }; CompactBlock.prototype.toNormalWriter = function toNormalWriter(bw) { - return this.frameWriter(bw, false); + return this.writeRaw(bw, false); }; -CompactBlock.prototype.frame = function frame(witness) { +CompactBlock.prototype.frameRaw = function frameRaw(witness) { var size = this.getSize(); - return this.frameWriter(new StaticWriter(size), witness).render(); + return this.writeRaw(new StaticWriter(size), witness).render(); }; CompactBlock.prototype.getSize = function getSize(witness) { @@ -176,15 +176,10 @@ CompactBlock.prototype.getSize = function getSize(witness) { return size; }; -CompactBlock.prototype.frameWriter = function frameWriter(bw, witness) { +CompactBlock.prototype.writeRaw = function writeRaw(bw, witness) { var i, id, lo, hi, ptx; - bw.writeU32(this.version); - bw.writeHash(this.prevBlock); - bw.writeHash(this.merkleRoot); - bw.writeU32(this.ts); - bw.writeU32(this.bits); - bw.writeU32(this.nonce); + this.writeAbbr(bw); bw.writeBytes(this.keyNonce); @@ -527,6 +522,21 @@ TXRequest.fromRaw = function fromRaw(data) { return new TXRequest().fromRaw(data); }; +TXRequest.prototype.getSize = function getSize() { + var size = 0; + var i, index; + + size += 32; + size += encoding.sizeVarint(this.indexes.length); + + for (i = 0; i < this.indexes.length; i++) { + index = this.indexes[i] - (i === 0 ? 0 : this.indexes[i - 1] + 1); + size += encoding.sizeVarint(index); + } + + return size; +}; + TXRequest.prototype.toWriter = function toWriter(bw) { var i, index; @@ -621,19 +631,19 @@ TXResponse.fromBlock = function fromBlock(block, req) { }; TXResponse.prototype.toRaw = function toRaw() { - return this.frame(true); + return this.frameRaw(true); }; TXResponse.prototype.toNormal = function toNormal() { - return this.frame(false); + return this.frameRaw(false); }; TXResponse.prototype.toWriter = function toWriter(bw) { - return this.frameWriter(bw, true); + return this.writeRaw(bw, true); }; TXResponse.prototype.toNormalWriter = function toNormalWriter(bw) { - return this.frameWriter(bw, false); + return this.writeRaw(bw, false); }; TXResponse.prototype.getSize = function getSize(witness) { @@ -654,7 +664,7 @@ TXResponse.prototype.getSize = function getSize(witness) { return size; }; -TXResponse.prototype.frameWriter = function frameWriter(bw, witness) { +TXResponse.prototype.writeRaw = function writeRaw(bw, witness) { var i, tx; bw.writeHash(this.hash); @@ -672,9 +682,9 @@ TXResponse.prototype.frameWriter = function frameWriter(bw, witness) { return bw; }; -TXResponse.prototype.frame = function frame(witness) { +TXResponse.prototype.frameRaw = function frameRaw(witness) { var size = this.getSize(); - return this.frameWriter(new StaticWriter(size), witness).render(); + return this.writeRaw(new StaticWriter(size), witness).render(); }; /* diff --git a/lib/net/packets.js b/lib/net/packets.js index 5a494bef..d88a8bcd 100644 --- a/lib/net/packets.js +++ b/lib/net/packets.js @@ -189,19 +189,12 @@ VersionPacket.fromOptions = function fromOptions(options) { VersionPacket.prototype.getSize = function getSize() { var size = 0; - var len; - size += 20; size += this.recv.getSize(false); size += this.from.getSize(false); size += 8; - - len = Buffer.byteLength(this.agent, 'ascii'); - size += encoding.sizeVarint(len); - size += len; - + size += encoding.sizeVarString(this.agent, 'ascii'); size += 5; - return size; }; @@ -698,15 +691,8 @@ AlertPacket.prototype.verify = function verify(key) { AlertPacket.prototype.getSize = function getSize() { var size = 0; - var len; - - len = this.getPayloadSize(); - size += encoding.sizeVarint(len); - size += len; - - size += encoding.sizeVarint(this.signature.length); - size += this.signature.length; - + size += encoding.sizeVarlen(this.getPayloadSize()); + size += encoding.sizeVarBytes(this.signature); return size; }; @@ -747,19 +733,14 @@ AlertPacket.prototype.getPayloadSize = function getPayloadSize() { size += 8; size += encoding.sizeVarint(this.subVers.length); - for (i = 0; i < this.subVers.length; i++) { - size += encoding.sizeVarint(this.subVers[i].length); - size += this.subVers[i].length; - } + for (i = 0; i < this.subVers.length; i++) + size += encoding.sizeVarString(this.subVers[i], 'ascii'); size += 4; - size += encoding.sizeVarint(this.comment.length); - size += this.comment.length; - size += encoding.sizeVarint(this.statusBar.length); - size += this.statusBar.length; - size += encoding.sizeVarint(this.reserved.length); - size += this.reserved.length; + size += encoding.sizeVarString(this.comment, 'ascii'); + size += encoding.sizeVarString(this.statusBar, 'ascii'); + size += encoding.sizeVarString(this.reserved, 'ascii'); return size; }; @@ -1470,6 +1451,17 @@ util.inherits(BlockPacket, Packet); BlockPacket.prototype.cmd = 'block'; BlockPacket.prototype.type = exports.types.BLOCK; +/** + * Get serialization size. + * @returns {Number} + */ + +BlockPacket.prototype.getSize = function getSize() { + if (this.witness) + return this.block.getSize(); + return this.block.getBaseSize(); +}; + /** * Serialize block packet. * @returns {Buffer} @@ -1530,6 +1522,17 @@ util.inherits(TXPacket, Packet); TXPacket.prototype.cmd = 'tx'; TXPacket.prototype.type = exports.types.TX; +/** + * Get serialization size. + * @returns {Number} + */ + +TXPacket.prototype.getSize = function getSize() { + if (this.witness) + return this.tx.getSize(); + return this.tx.getBaseSize(); +}; + /** * Serialize tx packet. * @returns {Buffer} @@ -1645,11 +1648,9 @@ RejectPacket.fromOptions = function fromOptions(options) { RejectPacket.prototype.getSize = function getSize() { var size = 0; - size += encoding.sizeVarint(this.message.length); - size += this.message.length; + size += encoding.sizeVarString(this.message, 'ascii'); size += 1; - size += encoding.sizeVarint(this.reason.length); - size += this.reason.length; + size += encoding.sizeVarString(this.reason, 'ascii'); if (this.data) size += 32; @@ -1860,6 +1861,15 @@ util.inherits(FilterLoadPacket, Packet); FilterLoadPacket.prototype.cmd = 'filterload'; FilterLoadPacket.prototype.type = exports.types.FILTERLOAD; +/** + * Get serialization size. + * @returns {Number} + */ + +FilterLoadPacket.prototype.getSize = function getSize() { + return this.filter.getSize(); +}; + /** * Serialize filterload packet. * @returns {Buffer} @@ -1936,7 +1946,7 @@ FilterAddPacket.prototype.type = exports.types.FILTERADD; */ FilterAddPacket.prototype.getSize = function getSize() { - return encoding.sizeVarint(this.data.length) + this.data.length; + return encoding.sizeVarBytes(this.data); }; /** @@ -2057,6 +2067,15 @@ util.inherits(MerkleBlockPacket, Packet); MerkleBlockPacket.prototype.cmd = 'merkleblock'; MerkleBlockPacket.prototype.type = exports.types.MERKLEBLOCK; +/** + * Get serialization size. + * @returns {Number} + */ + +MerkleBlockPacket.prototype.getSize = function getSize() { + return this.block.getSize(); +}; + /** * Serialize merkleblock packet. * @returns {Buffer} @@ -2259,15 +2278,11 @@ UTXOsPacket.fromOptions = function fromOptions(options) { UTXOsPacket.prototype.getSize = function getSize() { var size = 0; - var i, len; + var i; size += 4; size += 32; - - len = (this.hits.length + 7) / 8 | 0; - size += encoding.sizeVarint(len); - size += len; - + size += encoding.sizeVarlen((this.hits.length + 7) / 8 | 0); size += encoding.sizeVarint(this.coins.length); for (i = 0; i < this.coins.length; i++) @@ -2594,6 +2609,17 @@ util.inherits(CmpctBlockPacket, Packet); CmpctBlockPacket.prototype.cmd = 'cmpctblock'; CmpctBlockPacket.prototype.type = exports.types.CMPCTBLOCK; +/** + * Serialize cmpctblock packet. + * @returns {Buffer} + */ + +CmpctBlockPacket.prototype.getSize = function getSize() { + if (this.witness) + return this.block.getSize(true); + return this.block.getSize(false); +}; + /** * Serialize cmpctblock packet. * @returns {Buffer} @@ -2651,6 +2677,15 @@ util.inherits(GetBlockTxnPacket, Packet); GetBlockTxnPacket.prototype.cmd = 'getblocktxn'; GetBlockTxnPacket.prototype.type = exports.types.GETBLOCKTXN; +/** + * Get serialization size. + * @returns {Number} + */ + +GetBlockTxnPacket.prototype.getSize = function getSize() { + return this.request.getSize(); +}; + /** * Serialize getblocktxn packet. * @returns {Buffer} @@ -2709,6 +2744,17 @@ util.inherits(BlockTxnPacket, Packet); BlockTxnPacket.prototype.cmd = 'blocktxn'; BlockTxnPacket.prototype.type = exports.types.BLOCKTXN; +/** + * Get serialization size. + * @returns {Number} + */ + +BlockTxnPacket.prototype.getSize = function getSize() { + if (this.witness) + return this.response.getSize(true); + return this.response.getSize(false); +}; + /** * Serialize blocktxn packet. * @returns {Buffer} diff --git a/lib/primitives/block.js b/lib/primitives/block.js index 61afa4ff..9aff63f2 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -834,11 +834,11 @@ Block.prototype.writeWitness = function writeWitness(bw) { */ Block.prototype.frameNormal = function frameNormal() { - var size = this.getNormalSizes(); - var bw = new StaticWriter(size.total); + var sizes = this.getNormalSizes(); + var bw = new StaticWriter(sizes.total); this.writeNormal(bw); - size.data = bw.render(); - return size; + sizes.data = bw.render(); + return sizes; }; /** @@ -849,11 +849,11 @@ Block.prototype.frameNormal = function frameNormal() { */ Block.prototype.frameWitness = function frameWitness() { - var size = this.getWitnessSizes(); - var bw = new StaticWriter(size.total); + var sizes = this.getWitnessSizes(); + var bw = new StaticWriter(sizes.total); this.writeWitness(bw); - size.data = bw.render(); - return size; + sizes.data = bw.render(); + return sizes; }; /** diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index 45e9868d..04f61f22 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -830,12 +830,10 @@ KeyRing.prototype.getSize = function getSize() { var size = 0; size += 1; if (this.privateKey) { - size += encoding.sizeVarint(this.privateKey.length); - size += this.privateKey.length; + size += encoding.sizeVarBytes(this.privateKey); size += 1; } else { - size += encoding.sizeVarint(this.publicKey.length); - size += this.publicKey.length; + size += encoding.sizeVarBytes(this.publicKey); } size += this.script ? this.script.getVarSize() : 1; return size; diff --git a/lib/script/script.js b/lib/script/script.js index 0c93ce54..d7e518ae 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -1961,7 +1961,7 @@ Script.prototype.getSize = function getSize() { */ Script.prototype.getVarSize = function getVarSize() { - return enc.sizeVarint(this.raw.length) + this.raw.length; + return enc.sizeVarBytes(this.raw); }; /** diff --git a/lib/script/witness.js b/lib/script/witness.js index b4651d07..b0a85160 100644 --- a/lib/script/witness.js +++ b/lib/script/witness.js @@ -316,8 +316,7 @@ Witness.prototype.getSize = function getSize() { for (i = 0; i < this.items.length; i++) { item = this.items[i]; - size += enc.sizeVarint(item.length); - size += item.length; + size += enc.sizeVarBytes(item); } return size; diff --git a/lib/utils/bloom.js b/lib/utils/bloom.js index d7b2302e..57f6fecc 100644 --- a/lib/utils/bloom.js +++ b/lib/utils/bloom.js @@ -180,11 +180,7 @@ Bloom.fromRate = function fromRate(items, rate, update) { */ Bloom.prototype.getSize = function getSize() { - var size = 0; - size += encoding.sizeVarint(this.filter.length); - size += this.filter.length; - size += 9; - return size; + return encoding.sizeVarBytes(this.filter) + 9; }; /** diff --git a/lib/utils/encoding.js b/lib/utils/encoding.js index 120464cf..60d6b4d3 100644 --- a/lib/utils/encoding.js +++ b/lib/utils/encoding.js @@ -851,6 +851,43 @@ encoding.U32 = function U32(num) { return data; }; +/** + * Get size of varint-prefixed bytes. + * @param {Buffer} data + * @returns {Number} + */ + +encoding.sizeVarBytes = function sizeVarBytes(data) { + return encoding.sizeVarint(data.length) + data.length; +}; + +/** + * Get size of varint-prefixed length. + * @param {Number} len + * @returns {Number} + */ + +encoding.sizeVarlen = function sizeVarlen(len) { + return encoding.sizeVarint(len) + len; +}; + +/** + * Get size of varint-prefixed string. + * @param {String} str + * @returns {Number} + */ + +encoding.sizeVarString = function sizeVarString(str, enc) { + var len; + + if (typeof str !== 'string') + return encoding.sizeVarBytes(str); + + len = Buffer.byteLength(str, enc); + + return encoding.sizeVarint(len) + len; +}; + /* * Helpers */ diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 76618489..25476219 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -913,13 +913,8 @@ Account.prototype.toJSON = function toJSON(minimal) { Account.prototype.getSize = function getSize() { var size = 0; - size += encoding.sizeVarint(this.name.length); - size += this.name.length; - size += 1 * 5; - size += 4 * 4; - size += 1; - size += 82; - size += 1; + size += encoding.sizeVarString(this.name, 'ascii'); + size += 105; size += this.keys.length * 82; return size; }; diff --git a/lib/wallet/masterkey.js b/lib/wallet/masterkey.js index 98203035..c5e8ee53 100644 --- a/lib/wallet/masterkey.js +++ b/lib/wallet/masterkey.js @@ -451,20 +451,14 @@ MasterKey.prototype.getSize = function getSize() { if (this.encrypted) { size += 1; - size += encoding.sizeVarint(this.iv.length); - size += this.iv.length; - size += encoding.sizeVarint(this.ciphertext.length); - size += this.ciphertext.length; - size += 1; - size += 4 * 3; + size += encoding.sizeVarBytes(this.iv); + size += encoding.sizeVarBytes(this.ciphertext); + size += 13; return size; } size += 1; - - len = this.key.getExtendedSize(); - size += encoding.sizeVarint(len); - size += len; + size += encoding.sizeVarlen(this.key.getExtendedSize()); return size; }; diff --git a/lib/wallet/path.js b/lib/wallet/path.js index ecce0f35..cb92532a 100644 --- a/lib/wallet/path.js +++ b/lib/wallet/path.js @@ -179,8 +179,7 @@ Path.fromRaw = function fromRaw(data) { Path.prototype.getSize = function getSize() { var size = 0; - size += 4; - size += 1; + size += 5; switch (this.keyType) { case Path.types.HD: @@ -188,8 +187,7 @@ Path.prototype.getSize = function getSize() { break; case Path.types.KEY: size += 1; - size += encoding.sizeVarint(this.data.length); - size += this.data.length; + size += encoding.sizeVarBytes(this.data); break; } diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index aa739694..01744d73 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -2541,16 +2541,9 @@ Wallet.prototype.toJSON = function toJSON(unsafe) { Wallet.prototype.getSize = function getSize() { var size = 0; - var len; - size += 50; - size += encoding.sizeVarint(this.id.length); - size += this.id.length; - - len = this.master.getSize(); - size += encoding.sizeVarint(len); - size += len; - + size += encoding.sizeVarString(this.id, 'ascii'); + size += encoding.sizeVarlen(this.master.getSize()); return size; }; diff --git a/lib/workers/framer.js b/lib/workers/framer.js index ad7f41f6..b4b3dbc0 100644 --- a/lib/workers/framer.js +++ b/lib/workers/framer.js @@ -9,7 +9,7 @@ var EventEmitter = require('events').EventEmitter; var util = require('../utils/util'); -var BufferWriter = require('../utils/writer'); +var StaticWriter = require('../utils/staticwriter'); /** * Framer @@ -26,7 +26,8 @@ function Framer() { util.inherits(Framer, EventEmitter); Framer.prototype.packet = function _packet(packet) { - var bw = new BufferWriter(); + var size = 10 + packet.getSize(); + var bw = new StaticWriter(size); var data; bw.writeU32(packet.id); diff --git a/lib/workers/packets.js b/lib/workers/packets.js index 616b9ef9..d08cb466 100644 --- a/lib/workers/packets.js +++ b/lib/workers/packets.js @@ -9,6 +9,7 @@ var assert = require('assert'); var util = require('../utils/util'); var BufferReader = require('../utils/reader'); +var encoding = require('../utils/encoding'); var Script = require('../script/script'); var Witness = require('../script/witness'); var Output = require('../primitives/output'); @@ -65,14 +66,19 @@ Packet.prototype.toWriter = function toWriter() { function EventPacket(items) { Packet.call(this); this.items = items || []; + this.json = JSON.stringify(this.items); } util.inherits(EventPacket, Packet); EventPacket.prototype.cmd = packetTypes.EVENT; +EventPacket.prototype.getSize = function getSize() { + return encoding.sizeVarString(this.json, 'utf8'); +}; + EventPacket.prototype.toWriter = function toWriter(bw) { - bw.writeVarString(JSON.stringify(this.items), 'utf8'); + bw.writeVarString(this.json, 'utf8'); }; EventPacket.fromRaw = function fromRaw(data) { @@ -96,6 +102,10 @@ util.inherits(LogPacket, Packet); LogPacket.prototype.cmd = packetTypes.LOG; +LogPacket.prototype.getSize = function getSize() { + return encoding.sizeVarString(this.text, 'utf8'); +}; + LogPacket.prototype.toWriter = function toWriter(bw) { bw.writeVarString(this.text, 'utf8'); }; @@ -121,6 +131,14 @@ util.inherits(ErrorPacket, Packet); ErrorPacket.prototype.cmd = packetTypes.ERROR; +ErrorPacket.prototype.getSize = function getSize() { + var size = 0; + size += encoding.sizeVarString(this.error.message + '', 'utf8'); + size += encoding.sizeVarString(this.error.stack + '', 'utf8'); + size += encoding.sizeVarString((this.error.type || ''), 'utf8'); + return size; +}; + ErrorPacket.prototype.toWriter = function toWriter(bw) { bw.writeVarString(this.error.message + '', 'utf8'); bw.writeVarString(this.error.stack + '', 'utf8'); @@ -150,6 +168,14 @@ util.inherits(ErrorResultPacket, Packet); ErrorResultPacket.prototype.cmd = packetTypes.ERRORRESULT; +ErrorResultPacket.prototype.getSize = function getSize() { + var size = 0; + size += encoding.sizeVarString(this.error.message + '', 'utf8'); + size += encoding.sizeVarString(this.error.stack + '', 'utf8'); + size += encoding.sizeVarString((this.error.type || ''), 'utf8'); + return size; +}; + ErrorResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeVarString(this.error.message + '', 'utf8'); bw.writeVarString(this.error.stack + '', 'utf8'); @@ -181,6 +207,10 @@ util.inherits(VerifyPacket, Packet); VerifyPacket.prototype.cmd = packetTypes.VERIFY; +VerifyPacket.prototype.getSize = function() { + return this.tx.getSize() + this.view.getFastSize(this.tx) + 4; +}; + VerifyPacket.prototype.toWriter = function(bw) { this.tx.toWriter(bw); this.view.toFast(bw, this.tx); @@ -216,6 +246,10 @@ util.inherits(VerifyResultPacket, Packet); VerifyResultPacket.prototype.cmd = packetTypes.VERIFYRESULT; +VerifyResultPacket.prototype.getSize = function getSize() { + return 1; +}; + VerifyResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeU8(this.value ? 1 : 0); }; @@ -243,6 +277,24 @@ util.inherits(SignPacket, Packet); SignPacket.prototype.cmd = packetTypes.SIGN; +SignPacket.prototype.getSize = function getSize() { + var size = 0; + var i, ring; + + size += this.tx.getSize(); + size += this.tx.view.getFastSize(this.tx); + size += encoding.sizeVarint(this.rings.length); + + for (i = 0; i < this.rings.length; i++) { + ring = this.rings[i]; + size += ring.getSize(); + } + + size += 1; + + return size; +}; + SignPacket.prototype.toWriter = function toWriter(bw) { var i, ring; @@ -308,6 +360,23 @@ SignResultPacket.fromTX = function fromTX(tx, total) { return packet; }; +SignResultPacket.prototype.getSize = function getSize() { + var size = 0; + var i, script, witness; + + size += encoding.sizeVarint(this.total); + size += encoding.sizeVarint(this.script.length); + + for (i = 0; i < this.script.length; i++) { + script = this.script[i]; + witness = this.witness[i]; + size += script.getVarSize(); + size += witness.getVarSize(); + } + + return size; +}; + SignResultPacket.prototype.toWriter = function toWriter(bw) { var i; @@ -369,6 +438,16 @@ util.inherits(VerifyInputPacket, Packet); VerifyInputPacket.prototype.cmd = packetTypes.VERIFYINPUT; +VerifyInputPacket.prototype.getSize = function getSize() { + var size = 0; + size += this.tx.getSize(); + size += encoding.sizeVarint(this.index); + size += encoding.sizeVarint(this.coin.value); + size += this.coin.script.getVarSize(); + size += 4; + return size; +}; + VerifyInputPacket.prototype.toWriter = function toWriter(bw) { this.tx.toWriter(bw); bw.writeVarint(this.index); @@ -410,6 +489,10 @@ util.inherits(VerifyInputResultPacket, Packet); VerifyInputResultPacket.prototype.cmd = packetTypes.VERIFYINPUTRESULT; +VerifyInputResultPacket.prototype.getSize = function getSize() { + return 1; +}; + VerifyInputResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeU8(this.value ? 1 : 0); }; @@ -439,6 +522,17 @@ util.inherits(SignInputPacket, Packet); SignInputPacket.prototype.cmd = packetTypes.SIGNINPUT; +SignInputPacket.prototype.getSize = function getSize() { + var size = 0; + size += this.tx.getSize(); + size += encoding.sizeVarint(this.index); + size += encoding.sizeVarint(this.coin.value); + size += this.coin.script.getVarSize(); + size += this.ring.getSize(); + size += 1; + return size; +}; + SignInputPacket.prototype.toWriter = function toWriter(bw) { this.tx.toWriter(bw); bw.writeVarint(this.index); @@ -493,6 +587,10 @@ SignInputResultPacket.fromTX = function fromTX(tx, i, value) { return packet; }; +SignInputResultPacket.prototype.getSize = function getSize() { + return 1 + this.script.getVarSize() + this.witness.getVarSize(); +}; + SignInputResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeU8(this.value ? 1 : 0); this.script.toWriter(bw); @@ -532,6 +630,14 @@ util.inherits(ECVerifyPacket, Packet); ECVerifyPacket.prototype.cmd = packetTypes.ECVERIFY; +ECVerifyPacket.prototype.getSize = function getSize() { + var size = 0; + size += encoding.sizeVarBytes(this.msg); + size += encoding.sizeVarBytes(this.sig); + size += encoding.sizeVarBytes(this.key); + return size; +}; + ECVerifyPacket.prototype.toWriter = function(bw) { bw.writeVarBytes(this.msg); bw.writeVarBytes(this.sig); @@ -561,6 +667,10 @@ util.inherits(ECVerifyResultPacket, Packet); ECVerifyResultPacket.prototype.cmd = packetTypes.ECVERIFYRESULT; +ECVerifyResultPacket.prototype.getSize = function getSize() { + return 1; +}; + ECVerifyResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeU8(this.value ? 1 : 0); }; @@ -587,7 +697,14 @@ util.inherits(ECSignPacket, Packet); ECSignPacket.prototype.cmd = packetTypes.ECSIGN; -ECSignPacket.prototype.toWriter = function(bw) { +ECSignPacket.prototype.getSize = function getSize() { + var size = 0; + size += encoding.sizeVarBytes(this.msg); + size += encoding.sizeVarBytes(this.key); + return size; +}; + +ECSignPacket.prototype.toWriter = function toWriter(bw) { bw.writeVarBytes(this.msg); bw.writeVarBytes(this.key); }; @@ -614,6 +731,10 @@ util.inherits(ECSignResultPacket, Packet); ECSignResultPacket.prototype.cmd = packetTypes.ECSIGNRESULT; +ECSignResultPacket.prototype.getSize = function getSize() { + return encoding.sizeVarBytes(this.sig); +}; + ECSignResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeVarBytes(this.sig); }; @@ -642,7 +763,11 @@ util.inherits(MinePacket, Packet); MinePacket.prototype.cmd = packetTypes.MINE; -MinePacket.prototype.toWriter = function(bw) { +MinePacket.prototype.getSize = function getSize() { + return 120; +}; + +MinePacket.prototype.toWriter = function toWriter(bw) { bw.writeBytes(this.data); bw.writeBytes(this.target); bw.writeU32(this.min); @@ -673,6 +798,10 @@ util.inherits(MineResultPacket, Packet); MineResultPacket.prototype.cmd = packetTypes.MINERESULT; +MineResultPacket.prototype.getSize = function getSize() { + return 4; +}; + MineResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeU32(this.nonce); }; @@ -705,6 +834,14 @@ util.inherits(ScryptPacket, Packet); ScryptPacket.prototype.cmd = packetTypes.SCRYPT; +ScryptPacket.prototype.getSize = function getSize() { + var size = 0; + size += encoding.sizeVarBytes(this.passwd); + size += encoding.sizeVarBytes(this.salt); + size += 16; + return size; +}; + ScryptPacket.prototype.toWriter = function(bw) { bw.writeVarBytes(this.passwd); bw.writeVarBytes(this.salt); @@ -740,6 +877,10 @@ util.inherits(ScryptResultPacket, Packet); ScryptResultPacket.prototype.cmd = packetTypes.SCRYPTRESULT; +ScryptResultPacket.prototype.getSize = function getSize() { + return encoding.sizeVarBytes(this.key); +}; + ScryptResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeVarBytes(this.key); };