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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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