diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index d7a595bd..e4f9fd2f 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -21,6 +21,7 @@ function Block(data, subtype) { this.flags = data.flags || []; this._network = data._network || false; this._raw = data._raw || null; + this._size = data._size || 0; // List of matched TXs this.tx = []; @@ -80,6 +81,10 @@ Block.prototype.render = function render() { return bcoin.protocol.framer.block(this, this.subtype); }; +Block.prototype.size = function render() { + return this._size || this.render().length; +}; + Block.prototype.hasTX = function hasTX(hash) { return this.tx.indexOf(hash) !== -1; }; @@ -172,9 +177,11 @@ Block.prototype._checkBlock = function checkBlock() { if (this.ts > (Date.now() / 1000) + 2 * 60 * 60) return false; - // Size of all txs cant be bigger than MAX_BLOCK_SIZE - if (this.txs.length > bcoin.protocol.constants.block.maxSize) + // Size can't be bigger than MAX_BLOCK_SIZE + if (this.txs.length > constants.block.maxSize + || this.size() > constants.block.maxSize) { return false; + } // First TX must be a coinbase if (!this.txs.length || diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 160f2d6a..ebc58731 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -222,7 +222,8 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) { totalTX: readU32(p, 80), hashes: hashes, flags: flags, - _raw: p.slice(0, 80) + _raw: p.slice(0, 80), + _size: p.length }; }; @@ -289,7 +290,8 @@ Parser.prototype.parseBlock = function parseBlock(p) { nonce: readU32(p, 76), totalTX: totalTX, txs: txs, - _raw: p.slice(0, 80) + _raw: p.slice(0, 80), + _size: p.length }; }; @@ -381,7 +383,8 @@ Parser.prototype.parseTX = function parseTX(p) { inputs: txIn, outputs: txOut, lock: readU32(p, off), - _off: off + 4 + _off: off + 4, + _size: p.length }; }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 422e1148..cde33766 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -23,6 +23,7 @@ function TX(data, block) { this._hash = null; this._raw = data._raw || null; this._network = data._network || false; + this._size = data._size || 0; this._lock = this.lock; if (data.inputs) { @@ -66,6 +67,10 @@ TX.prototype.render = function render() { return bcoin.protocol.framer.tx(this); }; +TX.prototype.size = function size() { + return this._size || this.render().length; +}; + TX.prototype.input = function input(i, index) { this._input(i, index); return this;