diff --git a/lib/messages/builder.js b/lib/messages/builder.js index 33f1cfa..985775a 100644 --- a/lib/messages/builder.js +++ b/lib/messages/builder.js @@ -1,6 +1,7 @@ 'use strict'; var bitcore = require('bitcore'); +var Inventory = require('../inventory'); function builder(options) { /* jshint maxstatements: 20 */ @@ -31,6 +32,11 @@ function builder(options) { protocolVersion: options.protocolVersion, magicNumber: options.magicNumber }, + inventoryCommands: [ + 'getdata', + 'inv', + 'notfound' + ], commandsMap: { version: 'Version', verack: 'VerAck', @@ -61,6 +67,25 @@ function builder(options) { exported.commands[key] = require('./commands/' + key)(options); } + exported.inventoryCommands.forEach(function(command) { + + // add forTransaction methods + exported.commands[command].forTransaction = function forTransaction(hash) { + return new exported.commands[command]([Inventory.forTransaction(hash)]); + }; + + // add forBlock methods + exported.commands[command].forBlock = function forBlock(hash) { + return new exported.commands[command]([Inventory.forBlock(hash)]); + }; + + // add forFilteredBlock methods + exported.commands[command].forFilteredBlock = function forFilteredBlock(hash) { + return new exported.commands[command]([Inventory.forFilteredBlock(hash)]); + }; + + }); + return exported; } diff --git a/lib/messages/commands/getdata.js b/lib/messages/commands/getdata.js index 6bb3ce6..debdc18 100644 --- a/lib/messages/commands/getdata.js +++ b/lib/messages/commands/getdata.js @@ -40,30 +40,6 @@ function GetdataMessage(options) { } inherits(GetdataMessage, Message); -/** - * @param {Buffer|String} hash - The hash of the transaction inventory item - * @returns {GetdataMessage} - */ -GetdataMessage.forTransaction = function(hash) { - return new GetdataMessage([Inventory.forTransaction(hash)]); -}; - -/** - * @param {Buffer|String} hash - The hash of the block inventory item - * @returns {GetdataMessage} - */ -GetdataMessage.forBlock = function(hash) { - return new GetdataMessage([Inventory.forBlock(hash)]); -}; - -/** - * @param {Buffer|String} hash - The hash of the filtered block inventory item - * @returns {GetdataMessage} - */ -GetdataMessage.forFilteredBlock = function(hash) { - return new GetdataMessage([Inventory.forFilteredBlock(hash)]); -}; - GetdataMessage.fromBuffer = function(payload) { var obj = { inventory: [] diff --git a/lib/messages/commands/inv.js b/lib/messages/commands/inv.js index 200e9cf..2b2b73c 100644 --- a/lib/messages/commands/inv.js +++ b/lib/messages/commands/inv.js @@ -39,30 +39,6 @@ function InvMessage(options) { } inherits(InvMessage, Message); -/** - * @param {Buffer|String} hash - The hash of the transaction inventory item - * @returns {InvMessage} - */ -InvMessage.forTransaction = function(hash) { - return new InvMessage([Inventory.forTransaction(hash)]); -}; - -/** - * @param {Buffer|String} hash - The hash of the block inventory item - * @returns {InvMessage} - */ -InvMessage.forBlock = function(hash) { - return new InvMessage([Inventory.forBlock(hash)]); -}; - -/** - * @param {Buffer|String} hash - The hash of the filtered block inventory item - * @returns {InvMessage} - */ -InvMessage.forFilteredBlock = function(hash) { - return new InvMessage([Inventory.forFilteredBlock(hash)]); -}; - InvMessage.prototype.getPayload = function() { var bw = new BufferWriter(); utils.writeInventory(this.inventory, bw); diff --git a/lib/messages/commands/notfound.js b/lib/messages/commands/notfound.js index 2860dca..66b2776 100644 --- a/lib/messages/commands/notfound.js +++ b/lib/messages/commands/notfound.js @@ -39,30 +39,6 @@ function NotfoundMessage(options) { } inherits(NotfoundMessage, Message); -/** - * @param {Buffer|String} hash - The hash of the transaction inventory item - * @returns {NotfoundMessage} - */ -NotfoundMessage.forTransaction = function(hash) { - return new NotfoundMessage([Inventory.forTransaction(hash)]); -}; - -/** - * @param {Buffer|String} hash - The hash of the block inventory item - * @returns {NotfoundMessage} - */ -NotfoundMessage.forBlock = function(hash) { - return new NotfoundMessage([Inventory.forBlock(hash)]); -}; - -/** - * @param {Buffer|String} hash - The hash of the filtered block inventory item - * @returns {NotfoundMessage} - */ -NotfoundMessage.forFilteredBlock = function(hash) { - return new NotfoundMessage([Inventory.forFilteredBlock(hash)]); -}; - NotfoundMessage.fromBuffer = function(payload) { var obj = { inventory: [] diff --git a/test/messages/builder.js b/test/messages/builder.js index 4bd1094..89761c1 100644 --- a/test/messages/builder.js +++ b/test/messages/builder.js @@ -73,6 +73,42 @@ describe('Messages Builder', function() { }); - }); + describe('Inventory helpers for: ' + b.inventoryCommands.join(', '), function() { + var constructors = b.inventoryCommands; + var fakeHash = 'e2dfb8afe1575bfacae1a0b4afc49af7ddda69285857267bae0e22be15f74a3a'; + + describe('#forTransaction', function() { + constructors.forEach(function(name) { + it(name, function() { + should.exist(b.commands[name].forTransaction); + var message = b.commands[name].forTransaction(fakeHash); + should.exist(message); + message.should.be.instanceof(b.commands[name]); + }); + }); + }); + + describe('#forBlock', function() { + constructors.forEach(function(name) { + it(name, function() { + var message = b.commands[name].forBlock(fakeHash); + should.exist(message); + message.should.be.instanceof(b.commands[name]); + }); + }); + }); + + describe('#forFilteredBlock', function() { + constructors.forEach(function(name) { + it(name, function() { + var message = b.commands[name].forFilteredBlock(fakeHash); + should.exist(message); + message.should.be.instanceof(b.commands[name]); + }); + }); + }); + + }); + }); }); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index 8cae6d1..53bdebd 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -9,7 +9,6 @@ var bitcore = require('bitcore'); describe('Command Messages', function() { var messages = new Messages(); - var constructors = ['GetData', 'Inventory', 'NotFound']; var commandsMap = { version: 'Version', verack: 'VerAck', @@ -34,42 +33,6 @@ describe('Command Messages', function() { getaddr: 'GetAddr' }; - describe('Inventory helpers for: ' + constructors.join(', '), function() { - - var fakeHash = 'e2dfb8afe1575bfacae1a0b4afc49af7ddda69285857267bae0e22be15f74a3a'; - - describe('#forTransaction', function() { - constructors.forEach(function(name) { - it(name, function() { - var message = messages[name].forTransaction(fakeHash); - should.exist(message); - message.should.be.instanceof(messages[name]); - }); - }); - }); - - describe('#forBlock', function() { - constructors.forEach(function(name) { - it(name, function() { - var message = messages[name].forBlock(fakeHash); - should.exist(message); - message.should.be.instanceof(messages[name]); - }); - }); - }); - - describe('#forFilteredBlock', function() { - constructors.forEach(function(name) { - it(name, function() { - var message = messages[name].forFilteredBlock(fakeHash); - should.exist(message); - message.should.be.instanceof(messages[name]); - }); - }); - }); - - }); - describe('Transaction', function() { it('should accept a transaction instance as an argument', function() {