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.
* @param {Hash} hash
* @param {Hash|Number} block
* @returns {Promise} - Returns {@link Block}.
*/
HTTPClient.prototype.getBlock = function getBlock(hash) {
return this._get('/block/' + hash);
HTTPClient.prototype.getBlock = function getBlock(block) {
return this._get('/block/' + block);
};
/**
@ -377,12 +377,12 @@ HTTPClient.prototype.rescan = function rescan(height) {
/**
* Reset the chain.
* @param {Hash|Number} hash
* @param {Hash|Number} block
* @returns {Promise}
*/
HTTPClient.prototype.reset = function reset(hash) {
var options = { hash: hash };
HTTPClient.prototype.reset = function reset(block) {
var options = { block: block };
return this._post('/reset', options);
};

View File

@ -245,18 +245,27 @@ HTTPServer.prototype._init = function _init() {
options.id = params.id;
}
if (utils.isUInt32(params.hash)) {
options.height = params.hash;
} else if (params.hash) {
enforce(typeof params.hash === 'string', 'Hash must be a string.');
if (params.hash.length !== 64) {
options.height = Number(params.hash);
enforce(utils.isUInt32(options.height), 'Height must be a number.');
if (params.block != null) {
if (typeof params.block === 'number') {
assert(utils.isUInt32(params.block), 'Height must be a number.');
options.height = params.block;
} 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) {
options.index = Number(params.index);
enforce(utils.isUInt32(options.index), 'Index must be a number.');
@ -681,7 +690,7 @@ HTTPServer.prototype._init = function _init() {
}));
// 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 block;