http: refactor how height/hash are handled.

This commit is contained in:
Christopher Jeffrey 2016-11-16 12:08:45 -08:00
parent 51e271fe0d
commit 501eef858d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 24 additions and 15 deletions

View File

@ -344,12 +344,12 @@ HTTPClient.prototype.getTX = function getTX(hash) {
/** /**
* Retrieve a block from the chain database. * Retrieve a block from the chain database.
* @param {Hash} hash * @param {Hash|Number} block
* @returns {Promise} - Returns {@link Block}. * @returns {Promise} - Returns {@link Block}.
*/ */
HTTPClient.prototype.getBlock = function getBlock(hash) { HTTPClient.prototype.getBlock = function getBlock(block) {
return this._get('/block/' + hash); return this._get('/block/' + block);
}; };
/** /**
@ -377,12 +377,12 @@ HTTPClient.prototype.rescan = function rescan(height) {
/** /**
* Reset the chain. * Reset the chain.
* @param {Hash|Number} hash * @param {Hash|Number} block
* @returns {Promise} * @returns {Promise}
*/ */
HTTPClient.prototype.reset = function reset(hash) { HTTPClient.prototype.reset = function reset(block) {
var options = { hash: hash }; var options = { block: block };
return this._post('/reset', options); return this._post('/reset', options);
}; };

View File

@ -245,18 +245,27 @@ HTTPServer.prototype._init = function _init() {
options.id = params.id; options.id = params.id;
} }
if (utils.isUInt32(params.hash)) { if (params.block != null) {
options.height = params.hash; if (typeof params.block === 'number') {
} else if (params.hash) { assert(utils.isUInt32(params.block), 'Height must be a number.');
enforce(typeof params.hash === 'string', 'Hash must be a string.'); options.height = params.block;
if (params.hash.length !== 64) {
options.height = Number(params.hash);
enforce(utils.isUInt32(options.height), 'Height must be a number.');
} else { } else {
options.hash = utils.revHex(params.hash); enforce(typeof params.block === 'string', 'Hash must be a string.');
if (params.block.length !== 64) {
options.height = Number(params.block);
enforce(utils.isUInt32(options.height), 'Height must be a number.');
} else {
options.hash = utils.revHex(params.block);
}
} }
} }
if (params.hash) {
enforce(typeof params.hash === 'string', 'Hash must be a string.');
enforce(params.hash.length === 64, 'Hash must be a string.');
options.hash = utils.revHex(params.hash);
}
if (params.index != null) { if (params.index != null) {
options.index = Number(params.index); options.index = Number(params.index);
enforce(utils.isUInt32(options.index), 'Index must be a number.'); enforce(utils.isUInt32(options.index), 'Index must be a number.');
@ -681,7 +690,7 @@ HTTPServer.prototype._init = function _init() {
})); }));
// Block by hash/height // Block by hash/height
this.get('/block/:hash', con(function* (req, res, send, next) { this.get('/block/:block', con(function* (req, res, send, next) {
var hash = req.options.hash || req.options.height; var hash = req.options.hash || req.options.height;
var block; var block;