pass size into block and tx for maxsize checks.

This commit is contained in:
Christopher Jeffrey 2015-12-18 15:41:34 -08:00
parent 7ffcc8e7f3
commit 5942209f41
3 changed files with 20 additions and 5 deletions

View File

@ -21,6 +21,7 @@ function Block(data, subtype) {
this.flags = data.flags || []; this.flags = data.flags || [];
this._network = data._network || false; this._network = data._network || false;
this._raw = data._raw || null; this._raw = data._raw || null;
this._size = data._size || 0;
// List of matched TXs // List of matched TXs
this.tx = []; this.tx = [];
@ -80,6 +81,10 @@ Block.prototype.render = function render() {
return bcoin.protocol.framer.block(this, this.subtype); 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) { Block.prototype.hasTX = function hasTX(hash) {
return this.tx.indexOf(hash) !== -1; return this.tx.indexOf(hash) !== -1;
}; };
@ -172,9 +177,11 @@ Block.prototype._checkBlock = function checkBlock() {
if (this.ts > (Date.now() / 1000) + 2 * 60 * 60) if (this.ts > (Date.now() / 1000) + 2 * 60 * 60)
return false; return false;
// Size of all txs cant be bigger than MAX_BLOCK_SIZE // Size can't be bigger than MAX_BLOCK_SIZE
if (this.txs.length > bcoin.protocol.constants.block.maxSize) if (this.txs.length > constants.block.maxSize
|| this.size() > constants.block.maxSize) {
return false; return false;
}
// First TX must be a coinbase // First TX must be a coinbase
if (!this.txs.length || if (!this.txs.length ||

View File

@ -222,7 +222,8 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
totalTX: readU32(p, 80), totalTX: readU32(p, 80),
hashes: hashes, hashes: hashes,
flags: flags, 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), nonce: readU32(p, 76),
totalTX: totalTX, totalTX: totalTX,
txs: txs, 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, inputs: txIn,
outputs: txOut, outputs: txOut,
lock: readU32(p, off), lock: readU32(p, off),
_off: off + 4 _off: off + 4,
_size: p.length
}; };
}; };

View File

@ -23,6 +23,7 @@ function TX(data, block) {
this._hash = null; this._hash = null;
this._raw = data._raw || null; this._raw = data._raw || null;
this._network = data._network || false; this._network = data._network || false;
this._size = data._size || 0;
this._lock = this.lock; this._lock = this.lock;
if (data.inputs) { if (data.inputs) {
@ -66,6 +67,10 @@ TX.prototype.render = function render() {
return bcoin.protocol.framer.tx(this); return bcoin.protocol.framer.tx(this);
}; };
TX.prototype.size = function size() {
return this._size || this.render().length;
};
TX.prototype.input = function input(i, index) { TX.prototype.input = function input(i, index) {
this._input(i, index); this._input(i, index);
return this; return this;