From 91be17195372f70cfe521662e3f5a4f5841e4ec8 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 31 Mar 2015 14:26:59 -0400 Subject: [PATCH 01/11] Added test for tx version bug. --- test/messages/commands/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index 73b812d..84df325 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -18,6 +18,13 @@ describe('Command Messages', function() { message.transaction.should.be.instanceof(bitcore.Transaction); }); + it('version should remain the same', function() { + var tx = new bitcore.Transaction(); + var version = Number(tx.version); + var message = messages.Transaction(tx); + message.transaction.version.should.equal(version); + }); + }); describe('Block', function() { From 24ffd3f5f6bc88c7618519439546274396c5d3cd Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 31 Mar 2015 17:52:36 -0400 Subject: [PATCH 02/11] 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); }; From f21e2439be8f9c8c4310d67c2425151a3bc327d2 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 31 Mar 2015 18:50:36 -0400 Subject: [PATCH 03/11] 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() { From 3921c46507f2936eed687d340cd49220592f9708 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 31 Mar 2015 19:04:45 -0400 Subject: [PATCH 04/11] Updated JSDocs for Message Commands --- lib/messages/commands/alert.js | 1 + lib/messages/commands/inv.js | 3 ++- lib/messages/commands/notfound.js | 2 +- lib/messages/commands/ping.js | 1 - lib/messages/commands/pong.js | 1 - lib/messages/commands/tx.js | 4 ++-- lib/messages/commands/version.js | 15 ++++++++------- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/messages/commands/alert.js b/lib/messages/commands/alert.js index 1e06986..021fa41 100644 --- a/lib/messages/commands/alert.js +++ b/lib/messages/commands/alert.js @@ -11,6 +11,7 @@ var BufferWriter = bitcore.encoding.BufferWriter; * @param {Object=} arg * @param {Buffer=} arg.payload * @param {Buffer=} arg.signature + * @param {Object} options * @param {Number} options.magicNumber * @extends Message * @constructor diff --git a/lib/messages/commands/inv.js b/lib/messages/commands/inv.js index f099634..0823584 100644 --- a/lib/messages/commands/inv.js +++ b/lib/messages/commands/inv.js @@ -9,7 +9,8 @@ var BufferWriter = bitcore.encoding.BufferWriter; var _ = bitcore.deps._; /** - * @param {Object|Array=} - options - If options is an array will use as "inventory" + * @param {Array=} arg - An array of inventory + * @param {Object} options * @param {Array=} options.inventory - An array of inventory items * @param {Number} options.magicNumber * @extends Message diff --git a/lib/messages/commands/notfound.js b/lib/messages/commands/notfound.js index e19787f..fb2dd59 100644 --- a/lib/messages/commands/notfound.js +++ b/lib/messages/commands/notfound.js @@ -9,7 +9,7 @@ var BufferWriter = bitcore.encoding.BufferWriter; var _ = bitcore.deps._; /** - * @param {Array} arg - If options is an array will use as "inventory" + * @param {Array} arg - An array of inventory * @param {Object} options * @param {Array=} options.inventory - An array of inventory items * @param {Number} options.magicNumber diff --git a/lib/messages/commands/ping.js b/lib/messages/commands/ping.js index 9c6e783..b87e432 100644 --- a/lib/messages/commands/ping.js +++ b/lib/messages/commands/ping.js @@ -10,7 +10,6 @@ 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 diff --git a/lib/messages/commands/pong.js b/lib/messages/commands/pong.js index 184986a..d447daf 100644 --- a/lib/messages/commands/pong.js +++ b/lib/messages/commands/pong.js @@ -10,7 +10,6 @@ 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 diff --git a/lib/messages/commands/tx.js b/lib/messages/commands/tx.js index 72e840c..a789bec 100644 --- a/lib/messages/commands/tx.js +++ b/lib/messages/commands/tx.js @@ -7,8 +7,8 @@ var $ = bitcore.util.preconditions; var _ = bitcore.deps._; /** - * @param {Object|Transaction=} options - If is an instance of Transaction will use as options.transaction - * @param {Transaction=} options.transaction - An instance of a Transaction + * @param {Transaction=} arg - An instance of Transaction + * @param {Object} options * @param {Number} options.magicNumber * @extends Message * @constructor diff --git a/lib/messages/commands/version.js b/lib/messages/commands/version.js index 3596605..5c2b599 100644 --- a/lib/messages/commands/version.js +++ b/lib/messages/commands/version.js @@ -17,13 +17,14 @@ var packageInfo = require('../../../package.json'); * communication is possible until both peers have exchanged their versions. * * @see https://en.bitcoin.it/wiki/Protocol_documentation#version - * @param {Object=} obj - properties for the version - * @param {Buffer=} obj.nonce - a random 8 byte buffer - * @param {String=} obj.subversion - version of the client - * @param {BN=} obj.services - * @param {Date=} obj.timestamp - * @param {Number=} obj.startHeight - * @param {Number} obj.magicNumber + * @param {Object=} arg - properties for the version message + * @param {Buffer=} arg.nonce - a random 8 byte buffer + * @param {String=} arg.subversion - version of the client + * @param {BN=} arg.services + * @param {Date=} arg.timestamp + * @param {Number=} arg.startHeight + * @param {Object} options + * @param {Number} options.magicNumber * @extends Message * @constructor */ From 101796f7e95cc32f902627e84b6d2fb4b67ce2ca Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 11:11:06 -0400 Subject: [PATCH 05/11] Improved inventory precondition checks to handle objects. --- lib/messages/utils.js | 10 +++++----- test/messages/commands/index.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/messages/utils.js b/lib/messages/utils.js index d1bd29e..eb3a975 100644 --- a/lib/messages/utils.js +++ b/lib/messages/utils.js @@ -7,12 +7,12 @@ var _ = bitcore.deps._; var utils; module.exports = utils = { - checkInventory: function(inventory) { + checkInventory: function(arg) { $.checkArgument( - _.isUndefined(inventory) || - inventory.length === 0 || - (!_.isUndefined(inventory[0].type) && !_.isUndefined(inventory[0].hash)), - 'Inventory must be an array of inventory objects' + _.isUndefined(arg) || + (Array.isArray(arg) && arg.length === 0) || + (Array.isArray(arg) && !_.isUndefined(arg[0].type) && !_.isUndefined(arg[0].hash)), + 'Argument is expected to be an array of inventory objects' ); }, checkFinished: function checkFinished(parser) { diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index ed28719..f4df5e6 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -70,6 +70,22 @@ describe('Command Messages', function() { }); + describe('Inventory', function() { + it('should error if arg is not an array', function() { + (function() { + var message = messages.Inventory({}); + }).should.throw('Argument is expected to be an array of inventory objects'); + }); + it('should not error if arg is an empty array', function() { + var message = messages.Inventory([]); + }); + it('should error if arg is not an array of inventory objects', function() { + (function() { + var message = messages.Inventory([Number(0)]); + }).should.throw('Argument is expected to be an array of inventory objects'); + }); + }); + describe('Transaction', function() { it('should be able to pass a custom Transaction', function(done) { From cf7d06baaa9f926e476836e7fc63ba0645f84db3 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 11:42:51 -0400 Subject: [PATCH 06/11] Create transaction if not supplied in transaction message. --- lib/messages/commands/tx.js | 3 +++ lib/messages/commands/version.js | 1 - test/messages/commands/index.js | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/messages/commands/tx.js b/lib/messages/commands/tx.js index a789bec..6dbd04f 100644 --- a/lib/messages/commands/tx.js +++ b/lib/messages/commands/tx.js @@ -23,6 +23,9 @@ function TransactionMessage(arg, options) { 'An instance of Transaction or undefined is expected' ); this.transaction = arg; + if (!this.transaction) { + this.transaction = new this.Transaction(); + } } inherits(TransactionMessage, Message); diff --git a/lib/messages/commands/version.js b/lib/messages/commands/version.js index 5c2b599..dc8ec9c 100644 --- a/lib/messages/commands/version.js +++ b/lib/messages/commands/version.js @@ -5,7 +5,6 @@ var inherits = require('util').inherits; var bitcore = require('bitcore'); var BufferWriter = bitcore.encoding.BufferWriter; var BufferReader = bitcore.encoding.BufferReader; -var _ = bitcore.deps._; var BN = bitcore.crypto.BN; var utils = require('../utils'); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index f4df5e6..a9db1de 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -31,6 +31,11 @@ describe('Command Messages', function() { message.transaction.should.be.instanceof(bitcore.Transaction); }); + it('should create a transaction instance', function() { + var message = messages.Transaction(); + message.transaction.should.be.instanceof(bitcore.Transaction); + }); + it('version should remain the same', function() { var tx = new bitcore.Transaction(); var version = Number(tx.version); From 79674a2d7b0647323d7212d036d1470d40216eec Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 11:52:35 -0400 Subject: [PATCH 07/11] Added preconditions to pong message. --- lib/messages/commands/pong.js | 10 ++++++++++ test/messages/commands/index.js | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/messages/commands/pong.js b/lib/messages/commands/pong.js index d447daf..f5ff9d6 100644 --- a/lib/messages/commands/pong.js +++ b/lib/messages/commands/pong.js @@ -4,6 +4,9 @@ var Message = require('../message'); var inherits = require('util').inherits; var bitcore = require('bitcore'); var utils = require('../utils'); +var $ = bitcore.util.preconditions; +var _ = bitcore.deps._; +var BufferUtil = bitcore.util.buffer; var BufferReader = bitcore.encoding.BufferReader; /** @@ -18,7 +21,14 @@ function PongMessage(arg, options) { Message.call(this, arg, options); this.command = 'pong'; this.magicNumber = options.magicNumber; + $.checkArgument( + _.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8), + 'First argument is expected to be an 8 byte buffer' + ); this.nonce = arg; + if (!this.nonce) { + this.nonce = utils.getNonce(); + } } inherits(PongMessage, Message); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index a9db1de..cbb276f 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -58,6 +58,28 @@ describe('Command Messages', function() { }); + describe('Pong', function() { + + it('should error if nonce is not a buffer', function() { + (function() { + var message = messages.Pong('not a buffer'); + }).should.throw('First argument is expected to be an 8 byte buffer'); + }); + + it('should error if nonce buffer has invalid length', function() { + (function() { + var message = messages.Pong(new Buffer(Array(9))); + }).should.throw('First argument is expected to be an 8 byte buffer'); + }); + + it('should set a nonce if not included', function() { + var message = messages.Pong(); + should.exist(message.nonce); + message.nonce.length.should.equal(8); + }); + + }); + describe('FilterLoad', function() { it('should return a null payload', function() { From 28b05e3cc154d0895de21b807caa22fdfc5cf80c Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 11:55:00 -0400 Subject: [PATCH 08/11] Added preconditions to ping message --- lib/messages/commands/ping.js | 7 +++++++ lib/messages/commands/pong.js | 5 +---- test/messages/commands/index.js | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/messages/commands/ping.js b/lib/messages/commands/ping.js index b87e432..8fa5703 100644 --- a/lib/messages/commands/ping.js +++ b/lib/messages/commands/ping.js @@ -4,6 +4,9 @@ var Message = require('../message'); var inherits = require('util').inherits; var bitcore = require('bitcore'); var utils = require('../utils'); +var $ = bitcore.util.preconditions; +var _ = bitcore.deps._; +var BufferUtil = bitcore.util.buffer; var BufferReader = bitcore.encoding.BufferReader; /** @@ -18,6 +21,10 @@ function PingMessage(arg, options) { Message.call(this, arg, options); this.command = 'ping'; this.magicNumber = options.magicNumber; + $.checkArgument( + _.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8), + 'First argument is expected to be an 8 byte buffer' + ); this.nonce = arg || utils.getNonce(); } inherits(PingMessage, Message); diff --git a/lib/messages/commands/pong.js b/lib/messages/commands/pong.js index f5ff9d6..5b248f5 100644 --- a/lib/messages/commands/pong.js +++ b/lib/messages/commands/pong.js @@ -25,10 +25,7 @@ function PongMessage(arg, options) { _.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8), 'First argument is expected to be an 8 byte buffer' ); - this.nonce = arg; - if (!this.nonce) { - this.nonce = utils.getNonce(); - } + this.nonce = arg || utils.getNonce(); } inherits(PongMessage, Message); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index cbb276f..98edd79 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -80,6 +80,28 @@ describe('Command Messages', function() { }); + describe('Ping', function() { + + it('should error if nonce is not a buffer', function() { + (function() { + var message = messages.Ping('not a buffer'); + }).should.throw('First argument is expected to be an 8 byte buffer'); + }); + + it('should error if nonce buffer has invalid length', function() { + (function() { + var message = messages.Ping(new Buffer(Array(9))); + }).should.throw('First argument is expected to be an 8 byte buffer'); + }); + + it('should set a nonce if not included', function() { + var message = messages.Ping(); + should.exist(message.nonce); + message.nonce.length.should.equal(8); + }); + + }); + describe('FilterLoad', function() { it('should return a null payload', function() { From 81047d73bde8f3e6846403827eb8658c5b7cfbce Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 11:59:17 -0400 Subject: [PATCH 09/11] Added preconditions to filteradd message. --- lib/messages/commands/filteradd.js | 5 +++++ test/messages/commands/index.js | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/messages/commands/filteradd.js b/lib/messages/commands/filteradd.js index 1bb9951..4237924 100644 --- a/lib/messages/commands/filteradd.js +++ b/lib/messages/commands/filteradd.js @@ -8,6 +8,7 @@ var BufferUtil = bitcore.util.buffer; var BufferWriter = bitcore.encoding.BufferWriter; var BufferReader = bitcore.encoding.BufferReader; var $ = bitcore.util.preconditions; +var _ = bitcore.deps._; /** * Request peer to add data to a bloom filter already set by 'filterload' @@ -21,6 +22,10 @@ function FilteraddMessage(arg, options) { Message.call(this, arg, options); this.magicNumber = options.magicNumber; this.command = 'filteradd'; + $.checkArgument( + _.isUndefined(arg) || BufferUtil.isBuffer(arg), + 'First argument is expected to be a Buffer or undefined' + ); this.data = arg || BufferUtil.EMPTY_BUFFER; } inherits(FilteraddMessage, Message); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index 98edd79..ce0eefd 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -102,6 +102,16 @@ describe('Command Messages', function() { }); + describe('FilterAdd', function() { + + it('should error if arg is not a buffer', function() { + (function() { + var message = messages.FilterAdd('not a buffer'); + }).should.throw('First argument is expected to be a Buffer or undefined'); + }); + + }); + describe('FilterLoad', function() { it('should return a null payload', function() { From 36709faaa9b1f45e7033090f54e7b9494096738b Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 12:13:26 -0400 Subject: [PATCH 10/11] Added preconditions to addr message. --- lib/messages/builder.js | 2 +- lib/messages/commands/addr.js | 10 ++++++++++ test/messages/commands/index.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/messages/builder.js b/lib/messages/builder.js index 3fdb612..590038c 100644 --- a/lib/messages/builder.js +++ b/lib/messages/builder.js @@ -48,7 +48,7 @@ function builder(options) { headers: 'Headers', notfound: 'NotFound', inv: 'Inventory', - addr: 'Address', + addr: 'Addresses', alert: 'Alert', reject: 'Reject', merkleblock: 'MerkleBlock', diff --git a/lib/messages/commands/addr.js b/lib/messages/commands/addr.js index 1c95950..7afa47c 100644 --- a/lib/messages/commands/addr.js +++ b/lib/messages/commands/addr.js @@ -4,6 +4,8 @@ var Message = require('../message'); var inherits = require('util').inherits; var bitcore = require('bitcore'); var utils = require('../utils'); +var $ = bitcore.util.preconditions; +var _ = bitcore.deps._; var BufferReader = bitcore.encoding.BufferReader; var BufferWriter = bitcore.encoding.BufferWriter; @@ -18,6 +20,14 @@ function AddrMessage(arg, options) { Message.call(this, arg, options); this.command = 'addr'; this.magicNumber = options.magicNumber; + $.checkArgument( + _.isUndefined(arg) || + (Array.isArray(arg) && + !_.isUndefined(arg[0].services) && + !_.isUndefined(arg[0].ip) && + !_.isUndefined(arg[0].port)), + 'First argument is expected to be an array of addrs' + ); this.addresses = arg; } inherits(AddrMessage, Message); diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index ce0eefd..741f368 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -10,6 +10,25 @@ describe('Command Messages', function() { var messages = new Messages(); + describe('Addr', function() { + + it('should error if arg is not an array of addrs', function() { + (function() { + var message = messages.Addresses(['not an addr']); + }).should.throw('First argument is expected to be an array of addrs'); + }); + + it('should instantiate with an array of addrs', function() { + var message = messages.Addresses([{ + ip: { + v4: 'localhost' + }, + services: 1, + port: 1234 + }]); + }); + }); + describe('Alert', function() { it('should accept a transaction instance as an argument', function() { From 81a9f1c1fb6c3b86e845659518d768e879884385 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 15:26:20 -0400 Subject: [PATCH 11/11] Removed `arg` argument from parent message constructor. --- lib/messages/commands/addr.js | 2 +- lib/messages/commands/alert.js | 2 +- lib/messages/commands/block.js | 2 +- lib/messages/commands/filteradd.js | 2 +- lib/messages/commands/filterclear.js | 2 +- lib/messages/commands/filterload.js | 2 +- lib/messages/commands/getaddr.js | 2 +- lib/messages/commands/getblocks.js | 2 +- lib/messages/commands/getdata.js | 2 +- lib/messages/commands/getheaders.js | 2 +- lib/messages/commands/headers.js | 2 +- lib/messages/commands/inv.js | 2 +- lib/messages/commands/mempool.js | 2 +- lib/messages/commands/merkleblock.js | 2 +- lib/messages/commands/notfound.js | 2 +- lib/messages/commands/ping.js | 2 +- lib/messages/commands/pong.js | 2 +- lib/messages/commands/reject.js | 2 +- lib/messages/commands/tx.js | 2 +- lib/messages/commands/verack.js | 2 +- lib/messages/commands/version.js | 2 +- lib/messages/message.js | 2 +- test/messages/message.js | 4 ++-- 23 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/messages/commands/addr.js b/lib/messages/commands/addr.js index 7afa47c..ed86b99 100644 --- a/lib/messages/commands/addr.js +++ b/lib/messages/commands/addr.js @@ -17,7 +17,7 @@ var BufferWriter = bitcore.encoding.BufferWriter; * @constructor */ function AddrMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'addr'; this.magicNumber = options.magicNumber; $.checkArgument( diff --git a/lib/messages/commands/alert.js b/lib/messages/commands/alert.js index 021fa41..28c12e1 100644 --- a/lib/messages/commands/alert.js +++ b/lib/messages/commands/alert.js @@ -17,7 +17,7 @@ var BufferWriter = bitcore.encoding.BufferWriter; * @constructor */ function AlertMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'alert'; if (!arg) { diff --git a/lib/messages/commands/block.js b/lib/messages/commands/block.js index 76fe212..9bcf9db 100644 --- a/lib/messages/commands/block.js +++ b/lib/messages/commands/block.js @@ -15,7 +15,7 @@ var _ = bitcore.deps._; * @constructor */ function BlockMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.Block = options.Block; this.command = 'block'; this.magicNumber = options.magicNumber; diff --git a/lib/messages/commands/filteradd.js b/lib/messages/commands/filteradd.js index 4237924..7cd82e1 100644 --- a/lib/messages/commands/filteradd.js +++ b/lib/messages/commands/filteradd.js @@ -19,7 +19,7 @@ var _ = bitcore.deps._; * @constructor */ function FilteraddMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'filteradd'; $.checkArgument( diff --git a/lib/messages/commands/filterclear.js b/lib/messages/commands/filterclear.js index 95c0e02..017ba20 100644 --- a/lib/messages/commands/filterclear.js +++ b/lib/messages/commands/filterclear.js @@ -12,7 +12,7 @@ var BufferUtil = bitcore.util.buffer; * @constructor */ function FilterclearMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'filterclear'; } diff --git a/lib/messages/commands/filterload.js b/lib/messages/commands/filterload.js index 6ed0159..63465e9 100644 --- a/lib/messages/commands/filterload.js +++ b/lib/messages/commands/filterload.js @@ -17,7 +17,7 @@ var _ = bitcore.deps._; * @constructor */ function FilterloadMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'filterload'; $.checkArgument( diff --git a/lib/messages/commands/getaddr.js b/lib/messages/commands/getaddr.js index 55d93a5..2beca5d 100644 --- a/lib/messages/commands/getaddr.js +++ b/lib/messages/commands/getaddr.js @@ -13,7 +13,7 @@ var BufferUtil = bitcore.util.buffer; * @constructor */ function GetaddrMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'getaddr'; } diff --git a/lib/messages/commands/getblocks.js b/lib/messages/commands/getblocks.js index eb7599d..771c5b2 100644 --- a/lib/messages/commands/getblocks.js +++ b/lib/messages/commands/getblocks.js @@ -21,7 +21,7 @@ var $ = bitcore.util.preconditions; * @constructor */ function GetblocksMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'getblocks'; this.version = options.protocolVersion; this.magicNumber = options.magicNumber; diff --git a/lib/messages/commands/getdata.js b/lib/messages/commands/getdata.js index dd0680b..cd9b7ef 100644 --- a/lib/messages/commands/getdata.js +++ b/lib/messages/commands/getdata.js @@ -16,7 +16,7 @@ var _ = bitcore.deps._; * @constructor */ function GetdataMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'getdata'; this.magicNumber = options.magicNumber; utils.checkInventory(arg); diff --git a/lib/messages/commands/getheaders.js b/lib/messages/commands/getheaders.js index 0dc933c..19b96bd 100644 --- a/lib/messages/commands/getheaders.js +++ b/lib/messages/commands/getheaders.js @@ -20,7 +20,7 @@ var $ = bitcore.util.preconditions; * @constructor */ function GetheadersMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'getheaders'; this.version = options.protocolVersion; this.magicNumber = options.magicNumber; diff --git a/lib/messages/commands/headers.js b/lib/messages/commands/headers.js index 14d600e..4fd1688 100644 --- a/lib/messages/commands/headers.js +++ b/lib/messages/commands/headers.js @@ -21,7 +21,7 @@ var $ = bitcore.util.preconditions; * @constructor */ function HeadersMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.BlockHeader = options.BlockHeader; this.magicNumber = options.magicNumber; this.command = 'headers'; diff --git a/lib/messages/commands/inv.js b/lib/messages/commands/inv.js index 0823584..073354b 100644 --- a/lib/messages/commands/inv.js +++ b/lib/messages/commands/inv.js @@ -17,7 +17,7 @@ var _ = bitcore.deps._; * @constructor */ function InvMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'inv'; this.magicNumber = options.magicNumber; utils.checkInventory(arg); diff --git a/lib/messages/commands/mempool.js b/lib/messages/commands/mempool.js index e70e4e5..00fad39 100644 --- a/lib/messages/commands/mempool.js +++ b/lib/messages/commands/mempool.js @@ -15,7 +15,7 @@ var BufferUtil = bitcore.util.buffer; * @constructor */ function MempoolMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'mempool'; } diff --git a/lib/messages/commands/merkleblock.js b/lib/messages/commands/merkleblock.js index f5856c9..e26f405 100644 --- a/lib/messages/commands/merkleblock.js +++ b/lib/messages/commands/merkleblock.js @@ -18,7 +18,7 @@ var _ = bitcore.deps._; * @constructor */ function MerkleblockMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.MerkleBlock = options.MerkleBlock; // constructor this.magicNumber = options.magicNumber; this.command = 'merkleblock'; diff --git a/lib/messages/commands/notfound.js b/lib/messages/commands/notfound.js index fb2dd59..a8c9f03 100644 --- a/lib/messages/commands/notfound.js +++ b/lib/messages/commands/notfound.js @@ -17,7 +17,7 @@ var _ = bitcore.deps._; * @constructor */ function NotfoundMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'notfound'; this.magicNumber = options.magicNumber; utils.checkInventory(arg); diff --git a/lib/messages/commands/ping.js b/lib/messages/commands/ping.js index 8fa5703..92ba3f3 100644 --- a/lib/messages/commands/ping.js +++ b/lib/messages/commands/ping.js @@ -18,7 +18,7 @@ var BufferReader = bitcore.encoding.BufferReader; * @constructor */ function PingMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'ping'; this.magicNumber = options.magicNumber; $.checkArgument( diff --git a/lib/messages/commands/pong.js b/lib/messages/commands/pong.js index 5b248f5..111c4cc 100644 --- a/lib/messages/commands/pong.js +++ b/lib/messages/commands/pong.js @@ -18,7 +18,7 @@ var BufferReader = bitcore.encoding.BufferReader; * @constructor */ function PongMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'pong'; this.magicNumber = options.magicNumber; $.checkArgument( diff --git a/lib/messages/commands/reject.js b/lib/messages/commands/reject.js index 363e6bc..ab73e36 100644 --- a/lib/messages/commands/reject.js +++ b/lib/messages/commands/reject.js @@ -7,7 +7,7 @@ var BufferUtil = bitcore.util.buffer; // todo: add payload: https://en.bitcoin.it/wiki/Protocol_documentation#reject function RejectMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'reject'; } diff --git a/lib/messages/commands/tx.js b/lib/messages/commands/tx.js index 6dbd04f..614c42b 100644 --- a/lib/messages/commands/tx.js +++ b/lib/messages/commands/tx.js @@ -14,7 +14,7 @@ var _ = bitcore.deps._; * @constructor */ function TransactionMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.command = 'tx'; this.magicNumber = options.magicNumber; this.Transaction = options.Transaction; diff --git a/lib/messages/commands/verack.js b/lib/messages/commands/verack.js index a33f91a..5c19751 100644 --- a/lib/messages/commands/verack.js +++ b/lib/messages/commands/verack.js @@ -12,7 +12,7 @@ var BufferUtil = bitcore.util.buffer; * @constructor */ function VerackMessage(arg, options) { - Message.call(this, arg, options); + Message.call(this, options); this.magicNumber = options.magicNumber; this.command = 'verack'; } diff --git a/lib/messages/commands/version.js b/lib/messages/commands/version.js index dc8ec9c..11dc5ad 100644 --- a/lib/messages/commands/version.js +++ b/lib/messages/commands/version.js @@ -32,7 +32,7 @@ function VersionMessage(arg, options) { if (!arg) { arg = {}; } - Message.call(this, arg, options); + Message.call(this, options); this.command = 'version'; this.magicNumber = options.magicNumber; this.version = arg.version || options.protocolVersion; diff --git a/lib/messages/message.js b/lib/messages/message.js index 341d4ed..4165914 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(arg, options) { +function Message(options) { this.command = options.command; this.magicNumber = options.magicNumber; } diff --git a/test/messages/message.js b/test/messages/message.js index 419071b..f93b0fd 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(null, {magicNumber: 0xd9b4bef9, command: 'command'}); + var message = new Message({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(null, {magicNumber: 0xd9b4bef9, command: 'command'}); + var message = new Message({magicNumber: 0xd9b4bef9, command: 'command'}); message.getPayload = function() { return new Buffer(0); };