Use factory options as a seperate argument for messages.

This commit is contained in:
Braydon Fuller 2015-03-31 17:52:36 -04:00
parent 91be171953
commit 24ffd3f5f6
25 changed files with 129 additions and 147 deletions

View File

@ -68,17 +68,7 @@ function builder(options) {
var Command = require('./commands/' + key); var Command = require('./commands/' + key);
exported.commands[key] = function(obj) { exported.commands[key] = function(obj) {
if (!obj) { return new Command(obj, options);
obj = {};
}
// pass factory options
obj.Block = options.Block;
obj.BlockHeader = options.BlockHeader;
obj.Transaction = options.Transaction;
obj.MerkleBlock = options.MerkleBlock;
obj.magicNumber = options.magicNumber;
obj.version = options.protocolVersion;
return new Command(obj);
}; };
exported.commands[key]._constructor = Command; exported.commands[key]._constructor = Command;

View File

@ -8,17 +8,17 @@ var BufferReader = bitcore.encoding.BufferReader;
var BufferWriter = bitcore.encoding.BufferWriter; var BufferWriter = bitcore.encoding.BufferWriter;
/** /**
* @param {Array=} arg - An array of addrs
* @param {Object=} options * @param {Object=} options
* @param {Array=} options.addresses - An array of addrs
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function AddrMessage(options) { function AddrMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'addr'; this.command = 'addr';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.addresses = options.addresses; this.addresses = arg;
} }
inherits(AddrMessage, Message); inherits(AddrMessage, Message);

View File

@ -8,20 +8,22 @@ var BufferReader = bitcore.encoding.BufferReader;
var BufferWriter = bitcore.encoding.BufferWriter; var BufferWriter = bitcore.encoding.BufferWriter;
/** /**
* @param {Object=} options * @param {Object=} arg
* @param {Buffer=} options.payload * @param {Buffer=} arg.payload
* @param {Buffer=} options.signature * @param {Buffer=} arg.signature
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function AlertMessage(options) { function AlertMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'alert'; this.command = 'alert';
if (!arg) {
this.payload = options.payload || new Buffer(32); arg = {};
this.signature = options.signature || new Buffer(32); }
this.payload = arg.payload || new Buffer(32);
this.signature = arg.signature || new Buffer(32);
} }
inherits(AlertMessage, Message); inherits(AlertMessage, Message);

View File

@ -2,29 +2,28 @@
var Message = require('../message'); var Message = require('../message');
var inherits = require('util').inherits; var inherits = require('util').inherits;
var bitcore = require('bitcore');
var $ = bitcore.util.preconditions;
var _ = bitcore.deps._;
/** /**
* @param {Object|Block=} options - If is an instance of Block will use as options.block * @param {Block=} arg - An instance of a Block
* @param {Block=} options.block - An instance of a Block * @param {Object} options
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @param {Function} options.Block - A block constructor * @param {Function} options.Block - A block constructor
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function BlockMessage(options) { function BlockMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.Block = options.Block; this.Block = options.Block;
this.command = 'block'; this.command = 'block';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
$.checkArgument(
var block; _.isUndefined(arg) || arg instanceof this.Block,
if (options instanceof this.Block) { 'An instance of Block or undefined is expected'
block = options; );
} else { this.block = arg;
block = options.block;
}
this.block = block;
} }
inherits(BlockMessage, Message); inherits(BlockMessage, Message);

View File

@ -11,17 +11,17 @@ var $ = bitcore.util.preconditions;
/** /**
* Request peer to add data to a bloom filter already set by 'filterload' * Request peer to add data to a bloom filter already set by 'filterload'
* @param {Buffer=} data - Array of bytes representing bloom filter data
* @param {Object=} options * @param {Object=} options
* @param {Buffer=} options.data - Array of bytes representing bloom filter data
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function FilteraddMessage(options) { function FilteraddMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'filteradd'; this.command = 'filteradd';
this.data = options.data || BufferUtil.EMPTY_BUFFER; this.data = arg || BufferUtil.EMPTY_BUFFER;
} }
inherits(FilteraddMessage, Message); inherits(FilteraddMessage, Message);

View File

@ -11,8 +11,8 @@ var BufferUtil = bitcore.util.buffer;
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @constructor * @constructor
*/ */
function FilterclearMessage(options) { function FilterclearMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'filterclear'; this.command = 'filterclear';
} }

View File

@ -10,20 +10,21 @@ var _ = bitcore.deps._;
/** /**
* Request peer to send inv messages based on a bloom filter * Request peer to send inv messages based on a bloom filter
* @param {BloomFilter=} options.filter - An instance of BloomFilter * @param {BloomFilter=} arg - An instance of BloomFilter
* @param {Object} options
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function FilterloadMessage(options) { function FilterloadMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'filterload'; this.command = 'filterload';
$.checkArgument( $.checkArgument(
_.isUndefined(options.filter) || options.filter instanceof BloomFilter, _.isUndefined(arg) || arg instanceof BloomFilter,
'An instance of BloomFilter or undefined is expected' 'An instance of BloomFilter or undefined is expected'
); );
this.filter = options.filter; this.filter = arg;
} }
inherits(FilterloadMessage, Message); inherits(FilterloadMessage, Message);

View File

@ -8,11 +8,12 @@ var BufferUtil = bitcore.util.buffer;
/** /**
* Request information about active peers * Request information about active peers
* @extends Message * @extends Message
* @param {Object} options
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @constructor * @constructor
*/ */
function GetaddrMessage(options) { function GetaddrMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'getaddr'; this.command = 'getaddr';
} }

View File

@ -12,22 +12,25 @@ var $ = bitcore.util.preconditions;
* Query another peer about blocks. It can query for multiple block hashes, * Query another peer about blocks. It can query for multiple block hashes,
* and the response will contain all the chains of blocks starting from those * and the response will contain all the chains of blocks starting from those
* hashes. * hashes.
* @param {Object=} options * @param {Object=} arg
* @param {Array=} options.starts - Array of buffers or strings with the starting block hashes * @param {Array=} arg.starts - Array of buffers or strings with the starting block hashes
* @param {Buffer=} options.stop - Hash of the last block * @param {Buffer=} arg.stop - Hash of the last block
* @param {Object} options
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function GetblocksMessage(options) { function GetblocksMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'getblocks'; this.command = 'getblocks';
this.version = options.version; this.version = options.protocolVersion;
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
if (!arg) {
options = utils.sanitizeStartStop(options); arg = {};
this.starts = options.starts; }
this.stop = options.stop; arg = utils.sanitizeStartStop(arg);
this.starts = arg.starts;
this.stop = arg.stop;
} }
inherits(GetblocksMessage, Message); inherits(GetblocksMessage, Message);

View File

@ -15,20 +15,12 @@ var _ = bitcore.deps._;
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function GetdataMessage(options) { function GetdataMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'getdata'; this.command = 'getdata';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
utils.checkInventory(arg);
var inventory; this.inventory = arg;
if (_.isArray(options)) {
inventory = options;
} else {
inventory = options.inventory;
}
utils.checkInventory(inventory);
this.inventory = inventory;
} }
inherits(GetdataMessage, Message); inherits(GetdataMessage, Message);

View File

@ -19,15 +19,17 @@ var $ = bitcore.util.preconditions;
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function GetheadersMessage(options) { function GetheadersMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'getheaders'; this.command = 'getheaders';
this.version = options.version; this.version = options.protocolVersion;
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
if (!arg) {
options = utils.sanitizeStartStop(options); arg = {};
this.starts = options.starts; }
this.stop = options.stop; arg = utils.sanitizeStartStop(arg);
this.starts = arg.starts;
this.stop = arg.stop;
} }
inherits(GetheadersMessage, Message); inherits(GetheadersMessage, Message);

View File

@ -11,6 +11,7 @@ var $ = bitcore.util.preconditions;
/** /**
* Sent in response to a `getheaders` message. It contains information about * Sent in response to a `getheaders` message. It contains information about
* block headers. * block headers.
* @param {Array} arg - An array of BlockHeader instances
* @param {Object=} options * @param {Object=} options
* @param {Array=} options.headers - array of block headers * @param {Array=} options.headers - array of block headers
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
@ -18,12 +19,12 @@ var $ = bitcore.util.preconditions;
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function HeadersMessage(options) { function HeadersMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.BlockHeader = options.BlockHeader; this.BlockHeader = options.BlockHeader;
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'headers'; this.command = 'headers';
this.headers = options.headers; this.headers = arg;
} }
inherits(HeadersMessage, Message); inherits(HeadersMessage, Message);

View File

@ -15,19 +15,12 @@ var _ = bitcore.deps._;
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function InvMessage(options) { function InvMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'inv'; this.command = 'inv';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
utils.checkInventory(arg);
var inventory; this.inventory = arg;
if (_.isArray(options)) {
inventory = options;
} else {
inventory = options.inventory;
}
utils.checkInventory(inventory);
this.inventory = inventory;
} }
inherits(InvMessage, Message); inherits(InvMessage, Message);

View File

@ -9,12 +9,13 @@ var BufferUtil = bitcore.util.buffer;
* The mempool message sends a request to a node asking for information about * The mempool message sends a request to a node asking for information about
* transactions it has verified but which have not yet confirmed. * transactions it has verified but which have not yet confirmed.
* @see https://en.bitcoin.it/wiki/Protocol_documentation#mempool * @see https://en.bitcoin.it/wiki/Protocol_documentation#mempool
* @param {Object} options
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function MempoolMessage(options) { function MempoolMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'mempool'; this.command = 'mempool';
} }

View File

@ -10,23 +10,23 @@ var _ = bitcore.deps._;
/** /**
* Contains information about a MerkleBlock * Contains information about a MerkleBlock
* @see https://en.bitcoin.it/wiki/Protocol_documentation * @see https://en.bitcoin.it/wiki/Protocol_documentation
* @param {MerkleBlock} arg - An instance of MerkleBlock
* @param {Object=} options * @param {Object=} options
* @param {MerkleBlock=} options.merkleBlock
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @param {Function} options.MerkleBlock - a MerkleBlock constructor * @param {Function} options.MerkleBlock - a MerkleBlock constructor
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function MerkleblockMessage(options) { function MerkleblockMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.MerkleBlock = options.MerkleBlock; // constructor this.MerkleBlock = options.MerkleBlock; // constructor
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'merkleblock'; this.command = 'merkleblock';
$.checkArgument( $.checkArgument(
_.isUndefined(options.merkleBlock) || options.merkleBlock instanceof this.MerkleBlock, _.isUndefined(arg) || arg instanceof this.MerkleBlock,
'An instance of MerkleBlock or undefined is expected' 'An instance of MerkleBlock or undefined is expected'
); );
this.merkleBlock = options.merkleBlock; this.merkleBlock = arg;
} }
inherits(MerkleblockMessage, Message); inherits(MerkleblockMessage, Message);

View File

@ -9,25 +9,19 @@ var BufferWriter = bitcore.encoding.BufferWriter;
var _ = bitcore.deps._; var _ = bitcore.deps._;
/** /**
* @param {Object|Array=} options - If options is an array will use as "inventory" * @param {Array} arg - If options is an array will use as "inventory"
* @param {Object} options
* @param {Array=} options.inventory - An array of inventory items * @param {Array=} options.inventory - An array of inventory items
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function NotfoundMessage(options) { function NotfoundMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'notfound'; this.command = 'notfound';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
utils.checkInventory(arg);
var inventory; this.inventory = arg;
if (_.isArray(options)) {
inventory = options;
} else {
inventory = options.inventory;
}
utils.checkInventory(inventory);
this.inventory = inventory;
} }
inherits(NotfoundMessage, Message); inherits(NotfoundMessage, Message);

View File

@ -8,17 +8,18 @@ var BufferReader = bitcore.encoding.BufferReader;
/** /**
* A message to confirm that a connection is still valid. * A message to confirm that a connection is still valid.
* @param {Number} arg - A nonce for the Ping message
* @param {Object=} options * @param {Object=} options
* @param {Buffer=} options.nonce * @param {Buffer=} options.nonce
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function PingMessage(options) { function PingMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'ping'; this.command = 'ping';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.nonce = options.nonce || utils.getNonce(); this.nonce = arg || utils.getNonce();
} }
inherits(PingMessage, Message); inherits(PingMessage, Message);

View File

@ -8,17 +8,18 @@ var BufferReader = bitcore.encoding.BufferReader;
/** /**
* A message in response to a ping message. * A message in response to a ping message.
* @param {Number} arg - A nonce for the Pong message
* @param {Object=} options * @param {Object=} options
* @param {Buffer=} options.nonce * @param {Buffer=} options.nonce
* @param {Number} options.magicNumber * @param {Number} options.magicNumber
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function PongMessage(options) { function PongMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'pong'; this.command = 'pong';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.nonce = options.nonce; this.nonce = arg;
} }
inherits(PongMessage, Message); inherits(PongMessage, Message);

View File

@ -6,8 +6,8 @@ var bitcore = require('bitcore');
var BufferUtil = bitcore.util.buffer; var BufferUtil = bitcore.util.buffer;
// todo: add payload: https://en.bitcoin.it/wiki/Protocol_documentation#reject // todo: add payload: https://en.bitcoin.it/wiki/Protocol_documentation#reject
function RejectMessage(options) { function RejectMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'reject'; this.command = 'reject';
} }

View File

@ -2,6 +2,9 @@
var Message = require('../message'); var Message = require('../message');
var inherits = require('util').inherits; var inherits = require('util').inherits;
var bitcore = require('bitcore');
var $ = bitcore.util.preconditions;
var _ = bitcore.deps._;
/** /**
* @param {Object|Transaction=} options - If is an instance of Transaction will use as options.transaction * @param {Object|Transaction=} options - If is an instance of Transaction will use as options.transaction
@ -10,20 +13,16 @@ var inherits = require('util').inherits;
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function TransactionMessage(options) { function TransactionMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.command = 'tx'; this.command = 'tx';
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.Transaction = options.Transaction; this.Transaction = options.Transaction;
$.checkArgument(
var transaction; _.isUndefined(arg) || arg instanceof this.Transaction,
if(options instanceof this.Transaction) { 'An instance of Transaction or undefined is expected'
transaction = options; );
} else { this.transaction = arg;
transaction = options.transaction;
}
this.transaction = transaction;
} }
inherits(TransactionMessage, Message); inherits(TransactionMessage, Message);

View File

@ -11,8 +11,8 @@ var BufferUtil = bitcore.util.buffer;
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function VerackMessage(options) { function VerackMessage(arg, options) {
Message.call(this, options); Message.call(this, arg, options);
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
this.command = 'verack'; this.command = 'verack';
} }

View File

@ -27,19 +27,21 @@ var packageInfo = require('../../../package.json');
* @extends Message * @extends Message
* @constructor * @constructor
*/ */
function VersionMessage(obj) { function VersionMessage(arg, options) {
/* jshint maxcomplexity: 10 */ /* jshint maxcomplexity: 10 */
Message.call(this, obj); if (!arg) {
arg = {};
}
Message.call(this, arg, options);
this.command = 'version'; this.command = 'version';
_.assign(this, obj); this.magicNumber = options.magicNumber;
this.magicNumber = obj.magicNumber; this.version = arg.version || options.protocolVersion;
this.version = obj.version; this.nonce = arg.nonce || utils.getNonce();
this.nonce = this.nonce || utils.getNonce(); this.services = arg.services || new BN(1, 10);
this.services = this.services || new BN(1, 10); this.timestamp = arg.timestamp || new Date();
this.timestamp = this.timestamp || new Date(); this.subversion = arg.subversion || '/bitcore:' + packageInfo.version + '/';
this.subversion = this.subversion || '/bitcore:' + packageInfo.version + '/'; this.startHeight = arg.startHeight || 0;
this.startHeight = this.startHeight || 0; this.relay = arg.relay === false ? false : true;
this.relay = this.relay === false ? false : true;
} }
inherits(VersionMessage, Message); inherits(VersionMessage, Message);

View File

@ -12,7 +12,7 @@ var Hash = bitcore.crypto.Hash;
* @param {Number=} options.magicNumber * @param {Number=} options.magicNumber
* @constructor * @constructor
*/ */
function Message(options) { function Message(arg, options) {
this.command = options.command; this.command = options.command;
this.magicNumber = options.magicNumber; this.magicNumber = options.magicNumber;
} }

View File

@ -208,7 +208,7 @@ Peer.prototype._sendVersion = function() {
* Send a PONG message to the remote peer. * Send a PONG message to the remote peer.
*/ */
Peer.prototype._sendPong = function(nonce) { Peer.prototype._sendPong = function(nonce) {
var message = this.messages.Pong({nonce: nonce}); var message = this.messages.Pong(nonce);
this.sendMessage(message); this.sendMessage(message);
}; };

View File

@ -8,7 +8,7 @@ describe('Message', function() {
describe('@constructor', function() { describe('@constructor', function() {
it('construct with magic number and command', function() { it('construct with magic number and command', function() {
var message = new Message({magicNumber: 0xd9b4bef9, command: 'command'}); var message = new Message(null, {magicNumber: 0xd9b4bef9, command: 'command'});
message.command.should.equal('command'); message.command.should.equal('command');
message.magicNumber.should.equal(0xd9b4bef9); message.magicNumber.should.equal(0xd9b4bef9);
}); });
@ -16,7 +16,7 @@ describe('Message', function() {
describe('#toBuffer', function() { describe('#toBuffer', function() {
it('serialize to a buffer', function() { it('serialize to a buffer', function() {
var message = new Message({magicNumber: 0xd9b4bef9, command: 'command'}); var message = new Message(null, {magicNumber: 0xd9b4bef9, command: 'command'});
message.getPayload = function() { message.getPayload = function() {
return new Buffer(0); return new Buffer(0);
}; };