added jsdocs for inventory

This commit is contained in:
Braydon Fuller 2015-03-16 10:53:32 -04:00
parent f17bbf5d6f
commit 42c829e49c

View File

@ -7,6 +7,13 @@ var BufferReader = bitcore.encoding.BufferReader;
var BufferWriter = bitcore.encoding.BufferWriter; var BufferWriter = bitcore.encoding.BufferWriter;
var _ = bitcore.deps._; var _ = bitcore.deps._;
/**
* A constructor for inventory related Bitcoin messages such as
* "getdata", "inv" and "notfound".
* @param {Object} - obj
* @param {Number} - obj.type - Inventory.TYPE
* @param {Buffer} - obj.hash - The hash for the inventory
*/
function Inventory(obj) { function Inventory(obj) {
this.type = obj.type; this.type = obj.type;
if (!BufferUtil.isBuffer(obj.hash)) { if (!BufferUtil.isBuffer(obj.hash)) {
@ -15,9 +22,14 @@ function Inventory(obj) {
this.hash = obj.hash; this.hash = obj.hash;
} }
/**
* A convenience constructor for Inventory.
* @param {Number} - type - Inventory.TYPE
* @param {Buffer|String} - hash - The hash for the inventory
* @returns {Inventory} - A new instance of Inventory
*/
Inventory.forItem = function(type, hash) { Inventory.forItem = function(type, hash) {
$.checkArgument(hash); $.checkArgument(hash);
//todo: is reversing expected behavior?
if (_.isString(hash)) { if (_.isString(hash)) {
hash = new Buffer(hash, 'hex'); hash = new Buffer(hash, 'hex');
hash = BufferUtil.reverse(hash); hash = BufferUtil.reverse(hash);
@ -25,18 +37,36 @@ Inventory.forItem = function(type, hash) {
return new Inventory({type: type, hash: hash}); return new Inventory({type: type, hash: hash});
}; };
/**
* A convenience constructor for Inventory for block inventory types.
* @param {Buffer|String} - hash - The hash for the block inventory
* @returns {Inventory} - A new instance of Inventory
*/
Inventory.forBlock = function(hash) { Inventory.forBlock = function(hash) {
return Inventory.forItem(Inventory.TYPE.BLOCK, hash); return Inventory.forItem(Inventory.TYPE.BLOCK, hash);
}; };
/**
* A convenience constructor for Inventory for filtered/merkle block inventory types.
* @param {Buffer|String} - hash - The hash for the filtered block inventory
* @returns {Inventory} - A new instance of Inventory
*/
Inventory.forFilteredBlock = function(hash) { Inventory.forFilteredBlock = function(hash) {
return Inventory.forItem(Inventory.TYPE.FILTERED_BLOCK, hash); return Inventory.forItem(Inventory.TYPE.FILTERED_BLOCK, hash);
}; };
/**
* A convenience constructor for Inventory for transaction inventory types.
* @param {Buffer|String} - hash - The hash for the transaction inventory
* @returns {Inventory} - A new instance of Inventory
*/
Inventory.forTransaction = function(hash) { Inventory.forTransaction = function(hash) {
return Inventory.forItem(Inventory.TYPE.TX, hash); return Inventory.forItem(Inventory.TYPE.TX, hash);
}; };
/**
* @returns {Buffer} - Serialized inventory
*/
Inventory.prototype.toBuffer = function() { Inventory.prototype.toBuffer = function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeUInt32LE(this.type); bw.writeUInt32LE(this.type);
@ -44,12 +74,18 @@ Inventory.prototype.toBuffer = function() {
return bw.concat(); return bw.concat();
}; };
/**
* @param {BufferWriter} - An instance of BufferWriter
*/
Inventory.prototype.toBufferWriter = function(bw) { Inventory.prototype.toBufferWriter = function(bw) {
bw.writeUInt32LE(this.type); bw.writeUInt32LE(this.type);
bw.write(this.hash); bw.write(this.hash);
return bw; return bw;
}; };
/**
* @param {Buffer} - Seralized buffer of the inventory
*/
Inventory.fromBuffer = function(payload) { Inventory.fromBuffer = function(payload) {
var parser = new BufferReader(payload); var parser = new BufferReader(payload);
var obj = {}; var obj = {};
@ -58,6 +94,9 @@ Inventory.fromBuffer = function(payload) {
return new Inventory(obj); return new Inventory(obj);
}; };
/**
* @param {BufferWriter} - An instance of BufferWriter
*/
Inventory.fromBufferReader = function(br) { Inventory.fromBufferReader = function(br) {
var obj = {}; var obj = {};
obj.type = br.readUInt32LE(); obj.type = br.readUInt32LE();