From 24ffd3f5f6bc88c7618519439546274396c5d3cd Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 31 Mar 2015 17:52:36 -0400 Subject: [PATCH] Use factory options as a seperate argument for messages. --- lib/messages/builder.js | 12 +----------- lib/messages/commands/addr.js | 8 ++++---- lib/messages/commands/alert.js | 18 ++++++++++-------- lib/messages/commands/block.js | 25 ++++++++++++------------- lib/messages/commands/filteradd.js | 8 ++++---- lib/messages/commands/filterclear.js | 4 ++-- lib/messages/commands/filterload.js | 11 ++++++----- lib/messages/commands/getaddr.js | 5 +++-- lib/messages/commands/getblocks.js | 23 +++++++++++++---------- lib/messages/commands/getdata.js | 16 ++++------------ lib/messages/commands/getheaders.js | 16 +++++++++------- lib/messages/commands/headers.js | 7 ++++--- lib/messages/commands/inv.js | 15 ++++----------- lib/messages/commands/mempool.js | 5 +++-- lib/messages/commands/merkleblock.js | 10 +++++----- lib/messages/commands/notfound.js | 18 ++++++------------ lib/messages/commands/ping.js | 7 ++++--- lib/messages/commands/pong.js | 7 ++++--- lib/messages/commands/reject.js | 4 ++-- lib/messages/commands/tx.js | 21 ++++++++++----------- lib/messages/commands/verack.js | 4 ++-- lib/messages/commands/version.js | 24 +++++++++++++----------- lib/messages/message.js | 2 +- lib/peer.js | 2 +- test/messages/message.js | 4 ++-- 25 files changed, 129 insertions(+), 147 deletions(-) diff --git a/lib/messages/builder.js b/lib/messages/builder.js index 3b58903..3fdb612 100644 --- a/lib/messages/builder.js +++ b/lib/messages/builder.js @@ -68,17 +68,7 @@ function builder(options) { var Command = require('./commands/' + key); exported.commands[key] = function(obj) { - if (!obj) { - 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); + return new Command(obj, options); }; exported.commands[key]._constructor = Command; diff --git a/lib/messages/commands/addr.js b/lib/messages/commands/addr.js index 3d1bdcc..1c95950 100644 --- a/lib/messages/commands/addr.js +++ b/lib/messages/commands/addr.js @@ -8,17 +8,17 @@ var BufferReader = bitcore.encoding.BufferReader; var BufferWriter = bitcore.encoding.BufferWriter; /** + * @param {Array=} arg - An array of addrs * @param {Object=} options - * @param {Array=} options.addresses - An array of addrs * @param {Number} options.magicNumber * @extends Message * @constructor */ -function AddrMessage(options) { - Message.call(this, options); +function AddrMessage(arg, options) { + Message.call(this, arg, options); this.command = 'addr'; this.magicNumber = options.magicNumber; - this.addresses = options.addresses; + this.addresses = arg; } inherits(AddrMessage, Message); diff --git a/lib/messages/commands/alert.js b/lib/messages/commands/alert.js index 064e815..1e06986 100644 --- a/lib/messages/commands/alert.js +++ b/lib/messages/commands/alert.js @@ -8,20 +8,22 @@ var BufferReader = bitcore.encoding.BufferReader; var BufferWriter = bitcore.encoding.BufferWriter; /** - * @param {Object=} options - * @param {Buffer=} options.payload - * @param {Buffer=} options.signature + * @param {Object=} arg + * @param {Buffer=} arg.payload + * @param {Buffer=} arg.signature * @param {Number} options.magicNumber * @extends Message * @constructor */ -function AlertMessage(options) { - Message.call(this, options); +function AlertMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'alert'; - - this.payload = options.payload || new Buffer(32); - this.signature = options.signature || new Buffer(32); + if (!arg) { + arg = {}; + } + this.payload = arg.payload || new Buffer(32); + this.signature = arg.signature || new Buffer(32); } inherits(AlertMessage, Message); diff --git a/lib/messages/commands/block.js b/lib/messages/commands/block.js index 534d347..76fe212 100644 --- a/lib/messages/commands/block.js +++ b/lib/messages/commands/block.js @@ -2,29 +2,28 @@ var Message = require('../message'); 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=} options.block - An instance of a Block + * @param {Block=} arg - An instance of a Block + * @param {Object} options * @param {Number} options.magicNumber * @param {Function} options.Block - A block constructor * @extends Message * @constructor */ -function BlockMessage(options) { - Message.call(this, options); +function BlockMessage(arg, options) { + Message.call(this, arg, options); this.Block = options.Block; this.command = 'block'; this.magicNumber = options.magicNumber; - - var block; - if (options instanceof this.Block) { - block = options; - } else { - block = options.block; - } - - this.block = block; + $.checkArgument( + _.isUndefined(arg) || arg instanceof this.Block, + 'An instance of Block or undefined is expected' + ); + this.block = arg; } inherits(BlockMessage, Message); diff --git a/lib/messages/commands/filteradd.js b/lib/messages/commands/filteradd.js index 235cd0f..1bb9951 100644 --- a/lib/messages/commands/filteradd.js +++ b/lib/messages/commands/filteradd.js @@ -11,17 +11,17 @@ var $ = bitcore.util.preconditions; /** * 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 {Buffer=} options.data - Array of bytes representing bloom filter data * @param {Number} options.magicNumber * @extends Message * @constructor */ -function FilteraddMessage(options) { - Message.call(this, options); +function FilteraddMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'filteradd'; - this.data = options.data || BufferUtil.EMPTY_BUFFER; + this.data = arg || BufferUtil.EMPTY_BUFFER; } inherits(FilteraddMessage, Message); diff --git a/lib/messages/commands/filterclear.js b/lib/messages/commands/filterclear.js index 50dce6b..95c0e02 100644 --- a/lib/messages/commands/filterclear.js +++ b/lib/messages/commands/filterclear.js @@ -11,8 +11,8 @@ var BufferUtil = bitcore.util.buffer; * @param {Number} options.magicNumber * @constructor */ -function FilterclearMessage(options) { - Message.call(this, options); +function FilterclearMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'filterclear'; } diff --git a/lib/messages/commands/filterload.js b/lib/messages/commands/filterload.js index 6804347..6ed0159 100644 --- a/lib/messages/commands/filterload.js +++ b/lib/messages/commands/filterload.js @@ -10,20 +10,21 @@ var _ = bitcore.deps._; /** * 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 * @extends Message * @constructor */ -function FilterloadMessage(options) { - Message.call(this, options); +function FilterloadMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'filterload'; $.checkArgument( - _.isUndefined(options.filter) || options.filter instanceof BloomFilter, + _.isUndefined(arg) || arg instanceof BloomFilter, 'An instance of BloomFilter or undefined is expected' ); - this.filter = options.filter; + this.filter = arg; } inherits(FilterloadMessage, Message); diff --git a/lib/messages/commands/getaddr.js b/lib/messages/commands/getaddr.js index 9739075..55d93a5 100644 --- a/lib/messages/commands/getaddr.js +++ b/lib/messages/commands/getaddr.js @@ -8,11 +8,12 @@ var BufferUtil = bitcore.util.buffer; /** * Request information about active peers * @extends Message + * @param {Object} options * @param {Number} options.magicNumber * @constructor */ -function GetaddrMessage(options) { - Message.call(this, options); +function GetaddrMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'getaddr'; } diff --git a/lib/messages/commands/getblocks.js b/lib/messages/commands/getblocks.js index e674710..eb7599d 100644 --- a/lib/messages/commands/getblocks.js +++ b/lib/messages/commands/getblocks.js @@ -12,22 +12,25 @@ var $ = bitcore.util.preconditions; * 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 * hashes. - * @param {Object=} options - * @param {Array=} options.starts - Array of buffers or strings with the starting block hashes - * @param {Buffer=} options.stop - Hash of the last block + * @param {Object=} arg + * @param {Array=} arg.starts - Array of buffers or strings with the starting block hashes + * @param {Buffer=} arg.stop - Hash of the last block + * @param {Object} options * @param {Number} options.magicNumber * @extends Message * @constructor */ -function GetblocksMessage(options) { - Message.call(this, options); +function GetblocksMessage(arg, options) { + Message.call(this, arg, options); this.command = 'getblocks'; - this.version = options.version; + this.version = options.protocolVersion; this.magicNumber = options.magicNumber; - - options = utils.sanitizeStartStop(options); - this.starts = options.starts; - this.stop = options.stop; + if (!arg) { + arg = {}; + } + arg = utils.sanitizeStartStop(arg); + this.starts = arg.starts; + this.stop = arg.stop; } inherits(GetblocksMessage, Message); diff --git a/lib/messages/commands/getdata.js b/lib/messages/commands/getdata.js index 04ef7d2..dd0680b 100644 --- a/lib/messages/commands/getdata.js +++ b/lib/messages/commands/getdata.js @@ -15,20 +15,12 @@ var _ = bitcore.deps._; * @extends Message * @constructor */ -function GetdataMessage(options) { - Message.call(this, options); +function GetdataMessage(arg, options) { + Message.call(this, arg, options); this.command = 'getdata'; this.magicNumber = options.magicNumber; - - var inventory; - if (_.isArray(options)) { - inventory = options; - } else { - inventory = options.inventory; - } - - utils.checkInventory(inventory); - this.inventory = inventory; + utils.checkInventory(arg); + this.inventory = arg; } inherits(GetdataMessage, Message); diff --git a/lib/messages/commands/getheaders.js b/lib/messages/commands/getheaders.js index f0af1e5..0dc933c 100644 --- a/lib/messages/commands/getheaders.js +++ b/lib/messages/commands/getheaders.js @@ -19,15 +19,17 @@ var $ = bitcore.util.preconditions; * @extends Message * @constructor */ -function GetheadersMessage(options) { - Message.call(this, options); +function GetheadersMessage(arg, options) { + Message.call(this, arg, options); this.command = 'getheaders'; - this.version = options.version; + this.version = options.protocolVersion; this.magicNumber = options.magicNumber; - - options = utils.sanitizeStartStop(options); - this.starts = options.starts; - this.stop = options.stop; + if (!arg) { + arg = {}; + } + arg = utils.sanitizeStartStop(arg); + this.starts = arg.starts; + this.stop = arg.stop; } inherits(GetheadersMessage, Message); diff --git a/lib/messages/commands/headers.js b/lib/messages/commands/headers.js index d937609..5fe1207 100644 --- a/lib/messages/commands/headers.js +++ b/lib/messages/commands/headers.js @@ -11,6 +11,7 @@ var $ = bitcore.util.preconditions; /** * Sent in response to a `getheaders` message. It contains information about * block headers. + * @param {Array} arg - An array of BlockHeader instances * @param {Object=} options * @param {Array=} options.headers - array of block headers * @param {Number} options.magicNumber @@ -18,12 +19,12 @@ var $ = bitcore.util.preconditions; * @extends Message * @constructor */ -function HeadersMessage(options) { - Message.call(this, options); +function HeadersMessage(arg, options) { + Message.call(this, arg, options); this.BlockHeader = options.BlockHeader; this.magicNumber = options.magicNumber; this.command = 'headers'; - this.headers = options.headers; + this.headers = arg; } inherits(HeadersMessage, Message); diff --git a/lib/messages/commands/inv.js b/lib/messages/commands/inv.js index 370c0a9..f099634 100644 --- a/lib/messages/commands/inv.js +++ b/lib/messages/commands/inv.js @@ -15,19 +15,12 @@ var _ = bitcore.deps._; * @extends Message * @constructor */ -function InvMessage(options) { - Message.call(this, options); +function InvMessage(arg, options) { + Message.call(this, arg, options); this.command = 'inv'; this.magicNumber = options.magicNumber; - - var inventory; - if (_.isArray(options)) { - inventory = options; - } else { - inventory = options.inventory; - } - utils.checkInventory(inventory); - this.inventory = inventory; + utils.checkInventory(arg); + this.inventory = arg; } inherits(InvMessage, Message); diff --git a/lib/messages/commands/mempool.js b/lib/messages/commands/mempool.js index 2b3788f..e70e4e5 100644 --- a/lib/messages/commands/mempool.js +++ b/lib/messages/commands/mempool.js @@ -9,12 +9,13 @@ var BufferUtil = bitcore.util.buffer; * The mempool message sends a request to a node asking for information about * transactions it has verified but which have not yet confirmed. * @see https://en.bitcoin.it/wiki/Protocol_documentation#mempool + * @param {Object} options * @param {Number} options.magicNumber * @extends Message * @constructor */ -function MempoolMessage(options) { - Message.call(this, options); +function MempoolMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'mempool'; } diff --git a/lib/messages/commands/merkleblock.js b/lib/messages/commands/merkleblock.js index 3285048..f5856c9 100644 --- a/lib/messages/commands/merkleblock.js +++ b/lib/messages/commands/merkleblock.js @@ -10,23 +10,23 @@ var _ = bitcore.deps._; /** * Contains information about a MerkleBlock * @see https://en.bitcoin.it/wiki/Protocol_documentation + * @param {MerkleBlock} arg - An instance of MerkleBlock * @param {Object=} options - * @param {MerkleBlock=} options.merkleBlock * @param {Number} options.magicNumber * @param {Function} options.MerkleBlock - a MerkleBlock constructor * @extends Message * @constructor */ -function MerkleblockMessage(options) { - Message.call(this, options); +function MerkleblockMessage(arg, options) { + Message.call(this, arg, options); this.MerkleBlock = options.MerkleBlock; // constructor this.magicNumber = options.magicNumber; this.command = 'merkleblock'; $.checkArgument( - _.isUndefined(options.merkleBlock) || options.merkleBlock instanceof this.MerkleBlock, + _.isUndefined(arg) || arg instanceof this.MerkleBlock, 'An instance of MerkleBlock or undefined is expected' ); - this.merkleBlock = options.merkleBlock; + this.merkleBlock = arg; } inherits(MerkleblockMessage, Message); diff --git a/lib/messages/commands/notfound.js b/lib/messages/commands/notfound.js index 00a99ad..e19787f 100644 --- a/lib/messages/commands/notfound.js +++ b/lib/messages/commands/notfound.js @@ -9,25 +9,19 @@ var BufferWriter = bitcore.encoding.BufferWriter; 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 {Number} options.magicNumber * @extends Message * @constructor */ -function NotfoundMessage(options) { - Message.call(this, options); +function NotfoundMessage(arg, options) { + Message.call(this, arg, options); this.command = 'notfound'; this.magicNumber = options.magicNumber; - - var inventory; - if (_.isArray(options)) { - inventory = options; - } else { - inventory = options.inventory; - } - utils.checkInventory(inventory); - this.inventory = inventory; + utils.checkInventory(arg); + this.inventory = arg; } inherits(NotfoundMessage, Message); diff --git a/lib/messages/commands/ping.js b/lib/messages/commands/ping.js index 3f356c5..9c6e783 100644 --- a/lib/messages/commands/ping.js +++ b/lib/messages/commands/ping.js @@ -8,17 +8,18 @@ var BufferReader = bitcore.encoding.BufferReader; /** * A message to confirm that a connection is still valid. + * @param {Number} arg - A nonce for the Ping message * @param {Object=} options * @param {Buffer=} options.nonce * @param {Number} options.magicNumber * @extends Message * @constructor */ -function PingMessage(options) { - Message.call(this, options); +function PingMessage(arg, options) { + Message.call(this, arg, options); this.command = 'ping'; this.magicNumber = options.magicNumber; - this.nonce = options.nonce || utils.getNonce(); + this.nonce = arg || utils.getNonce(); } inherits(PingMessage, Message); diff --git a/lib/messages/commands/pong.js b/lib/messages/commands/pong.js index 40c7329..184986a 100644 --- a/lib/messages/commands/pong.js +++ b/lib/messages/commands/pong.js @@ -8,17 +8,18 @@ var BufferReader = bitcore.encoding.BufferReader; /** * A message in response to a ping message. + * @param {Number} arg - A nonce for the Pong message * @param {Object=} options * @param {Buffer=} options.nonce * @param {Number} options.magicNumber * @extends Message * @constructor */ -function PongMessage(options) { - Message.call(this, options); +function PongMessage(arg, options) { + Message.call(this, arg, options); this.command = 'pong'; this.magicNumber = options.magicNumber; - this.nonce = options.nonce; + this.nonce = arg; } inherits(PongMessage, Message); diff --git a/lib/messages/commands/reject.js b/lib/messages/commands/reject.js index 0fcfc43..363e6bc 100644 --- a/lib/messages/commands/reject.js +++ b/lib/messages/commands/reject.js @@ -6,8 +6,8 @@ var bitcore = require('bitcore'); var BufferUtil = bitcore.util.buffer; // todo: add payload: https://en.bitcoin.it/wiki/Protocol_documentation#reject -function RejectMessage(options) { - Message.call(this, options); +function RejectMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'reject'; } diff --git a/lib/messages/commands/tx.js b/lib/messages/commands/tx.js index 883e69b..72e840c 100644 --- a/lib/messages/commands/tx.js +++ b/lib/messages/commands/tx.js @@ -2,6 +2,9 @@ var Message = require('../message'); 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 @@ -10,20 +13,16 @@ var inherits = require('util').inherits; * @extends Message * @constructor */ -function TransactionMessage(options) { - Message.call(this, options); +function TransactionMessage(arg, options) { + Message.call(this, arg, options); this.command = 'tx'; this.magicNumber = options.magicNumber; this.Transaction = options.Transaction; - - var transaction; - if(options instanceof this.Transaction) { - transaction = options; - } else { - transaction = options.transaction; - } - - this.transaction = transaction; + $.checkArgument( + _.isUndefined(arg) || arg instanceof this.Transaction, + 'An instance of Transaction or undefined is expected' + ); + this.transaction = arg; } inherits(TransactionMessage, Message); diff --git a/lib/messages/commands/verack.js b/lib/messages/commands/verack.js index 55c847a..a33f91a 100644 --- a/lib/messages/commands/verack.js +++ b/lib/messages/commands/verack.js @@ -11,8 +11,8 @@ var BufferUtil = bitcore.util.buffer; * @extends Message * @constructor */ -function VerackMessage(options) { - Message.call(this, options); +function VerackMessage(arg, options) { + Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'verack'; } diff --git a/lib/messages/commands/version.js b/lib/messages/commands/version.js index 6d18c62..3596605 100644 --- a/lib/messages/commands/version.js +++ b/lib/messages/commands/version.js @@ -27,19 +27,21 @@ var packageInfo = require('../../../package.json'); * @extends Message * @constructor */ -function VersionMessage(obj) { +function VersionMessage(arg, options) { /* jshint maxcomplexity: 10 */ - Message.call(this, obj); + if (!arg) { + arg = {}; + } + Message.call(this, arg, options); this.command = 'version'; - _.assign(this, obj); - this.magicNumber = obj.magicNumber; - this.version = obj.version; - this.nonce = this.nonce || utils.getNonce(); - this.services = this.services || new BN(1, 10); - this.timestamp = this.timestamp || new Date(); - this.subversion = this.subversion || '/bitcore:' + packageInfo.version + '/'; - this.startHeight = this.startHeight || 0; - this.relay = this.relay === false ? false : true; + this.magicNumber = options.magicNumber; + this.version = arg.version || options.protocolVersion; + this.nonce = arg.nonce || utils.getNonce(); + this.services = arg.services || new BN(1, 10); + this.timestamp = arg.timestamp || new Date(); + this.subversion = arg.subversion || '/bitcore:' + packageInfo.version + '/'; + this.startHeight = arg.startHeight || 0; + this.relay = arg.relay === false ? false : true; } inherits(VersionMessage, Message); diff --git a/lib/messages/message.js b/lib/messages/message.js index 4165914..341d4ed 100644 --- a/lib/messages/message.js +++ b/lib/messages/message.js @@ -12,7 +12,7 @@ var Hash = bitcore.crypto.Hash; * @param {Number=} options.magicNumber * @constructor */ -function Message(options) { +function Message(arg, options) { this.command = options.command; this.magicNumber = options.magicNumber; } diff --git a/lib/peer.js b/lib/peer.js index 716a1ce..f81b510 100644 --- a/lib/peer.js +++ b/lib/peer.js @@ -208,7 +208,7 @@ Peer.prototype._sendVersion = function() { * Send a PONG message to the remote peer. */ Peer.prototype._sendPong = function(nonce) { - var message = this.messages.Pong({nonce: nonce}); + var message = this.messages.Pong(nonce); this.sendMessage(message); }; diff --git a/test/messages/message.js b/test/messages/message.js index f93b0fd..419071b 100644 --- a/test/messages/message.js +++ b/test/messages/message.js @@ -8,7 +8,7 @@ describe('Message', function() { describe('@constructor', 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.magicNumber.should.equal(0xd9b4bef9); }); @@ -16,7 +16,7 @@ describe('Message', function() { describe('#toBuffer', 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() { return new Buffer(0); };