Added preconditions to the Headers message.

This commit is contained in:
Braydon Fuller 2015-03-31 18:50:36 -04:00
parent 24ffd3f5f6
commit f21e2439be
2 changed files with 36 additions and 0 deletions

View File

@ -6,6 +6,7 @@ var bitcore = require('bitcore');
var utils = require('../utils');
var BufferReader = bitcore.encoding.BufferReader;
var BufferWriter = bitcore.encoding.BufferWriter;
var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
/**
@ -24,6 +25,10 @@ function HeadersMessage(arg, options) {
this.BlockHeader = options.BlockHeader;
this.magicNumber = options.magicNumber;
this.command = 'headers';
$.checkArgument(
_.isUndefined(arg) || (Array.isArray(arg) && arg[0] instanceof this.BlockHeader),
'First argument is expected to be an array of BlockHeader instances'
);
this.headers = arg;
}
inherits(HeadersMessage, Message);

View File

@ -10,6 +10,19 @@ describe('Command Messages', function() {
var messages = new Messages();
describe('Alert', function() {
it('should accept a transaction instance as an argument', function() {
var message = messages.Alert({
payload: new Buffer('abcdef', 'hex'),
signature: new Buffer('123456', 'hex')
});
message.payload.should.deep.equal(new Buffer('abcdef', 'hex'));
message.signature.should.deep.equal(new Buffer('123456', 'hex'));
});
});
describe('Transaction', function() {
it('should accept a transaction instance as an argument', function() {
@ -123,6 +136,24 @@ describe('Command Messages', function() {
});
describe('Headers', function() {
it('should error if arg is not an array', function() {
(function() {
var message = messages.Headers({});
}).should.throw('First argument is expected to be an array');
});
it('should error if arg is an empty array', function() {
(function() {
var message = messages.Headers([]);
}).should.throw('First argument is expected to be an array');
});
it('should error if arg is not an array of BlockHeaders', function() {
(function() {
var message = messages.Headers([Number(0)]);
}).should.throw('First argument is expected to be an array');
});
});
describe('MerkleBlock', function() {
it('should return null buffer for payload', function() {