From f21e2439be8f9c8c4310d67c2425151a3bc327d2 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 31 Mar 2015 18:50:36 -0400 Subject: [PATCH] Added preconditions to the Headers message. --- lib/messages/commands/headers.js | 5 +++++ test/messages/commands/index.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/messages/commands/headers.js b/lib/messages/commands/headers.js index 5fe1207..14d600e 100644 --- a/lib/messages/commands/headers.js +++ b/lib/messages/commands/headers.js @@ -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); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index 84df325..ed28719 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -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() {