moved inventory helper functions to builder

This commit is contained in:
Braydon Fuller 2015-03-17 16:01:52 -04:00
parent 7cfe6d1865
commit 34c38466f7
6 changed files with 62 additions and 110 deletions

View File

@ -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;
}

View File

@ -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: []

View File

@ -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);

View File

@ -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: []

View File

@ -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]);
});
});
});
});
});
});

View File

@ -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() {