add _checkFinished

This commit is contained in:
Manuel Araoz 2015-02-06 15:06:45 -03:00
parent 7119fad398
commit 13361ea210
2 changed files with 16 additions and 8 deletions

View File

@ -159,7 +159,7 @@ describe('Integration with ' + network.name + ' bitcoind', function() {
});
var from = [blockHash[network.name]];
var stop = stopBlock[network.name];
it('gets headers', function(cb) {
it.only('gets headers', function(cb) {
connect(function(peer) {
peer.once('headers', function(message) {
(message instanceof Messages.Headers).should.equal(true);

View File

@ -169,6 +169,13 @@ Message.prototype.serialize = function(network) {
return message.buffer();
};
/**
* check if parser has no more extra data
*/
Message.prototype._checkFinished = function(parser) {
$.checkState(parser.finished(), 'data still available after parsing ' + this.constructor.name);
};
module.exports.Message = Message;
/**
@ -254,7 +261,7 @@ Version.prototype.fromBuffer = function(payload) {
*/
this.relay = !!parser.readUInt8();
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};
@ -336,7 +343,7 @@ Inventory.prototype.fromBuffer = function(payload) {
this.inventory.push(Inventory.forItem(type, hash));
}
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};
@ -420,7 +427,8 @@ util.inherits(Ping, Message);
Ping.prototype.fromBuffer = function(payload) {
var parser = new BufferReader(payload);
this.nonce = parser.read(8);
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};
@ -532,7 +540,7 @@ Addresses.prototype.fromBuffer = function(payload) {
this.addresses.push(addr);
}
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};
@ -607,7 +615,7 @@ Alert.prototype.fromBuffer = function(payload) {
var parser = new BufferReader(payload);
this.payload = parser.readVarLengthBuffer();
this.signature = parser.readVarLengthBuffer();
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};
@ -651,7 +659,7 @@ Headers.prototype.fromBuffer = function(payload) {
var header = BlockHeaderModel.fromBufferReader(parser);
this.headers.push(header);
}
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};
@ -777,7 +785,7 @@ GetBlocks.prototype.fromBuffer = function(payload) {
this.starts.push(parser.read(32));
}
this.stop = parser.read(32);
$.checkState(parser.finished());
this._checkFinished(parser);
return this;
};