rpc: add getblockbyheight call.

This commit is contained in:
Christopher Jeffrey 2017-04-30 07:18:16 -07:00
parent ed4400acb9
commit 12d3ee6f95
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -82,6 +82,7 @@ RPC.prototype.init = function init() {
this.add('getbestblockhash', this.getBestBlockHash);
this.add('getblockcount', this.getBlockCount);
this.add('getblock', this.getBlock);
this.add('getblockbyheight', this.getBlockByHeight);
this.add('getblockhash', this.getBlockHash);
this.add('getblockheader', this.getBlockHeader);
this.add('getchaintips', this.getChainTips);
@ -496,6 +497,7 @@ RPC.prototype.getBlock = co(function* getBlock(args, help) {
var valid = new Validator([args]);
var hash = valid.hash(0);
var verbose = valid.bool(1, true);
var details = valid.bool(2, false);
var entry, block;
if (help || args.length < 1 || args.length > 2)
@ -524,7 +526,43 @@ RPC.prototype.getBlock = co(function* getBlock(args, help) {
if (!verbose)
return block.toRaw().toString('hex');
return yield this.blockToJSON(entry, block, false);
return yield this.blockToJSON(entry, block, details);
});
RPC.prototype.getBlockByHeight = co(function* getBlockByHeight(args, help) {
var valid = new Validator([args]);
var height = valid.u32(0, -1);
var verbose = valid.bool(1, true);
var details = valid.bool(2, false);
var entry, block;
if (help || args.length < 1 || args.length > 3)
throw new RPCError(errs.MISC_ERROR, 'getblockbyheight "height" ( verbose )');
if (height === -1)
throw new RPCError(errs.TYPE_ERROR, 'Invalid block height.');
entry = yield this.chain.db.getEntry(height);
if (!entry)
throw new RPCError(errs.MISC_ERROR, 'Block not found');
block = yield this.chain.db.getBlock(entry.hash);
if (!block) {
if (this.chain.options.spv)
throw new RPCError(errs.MISC_ERROR, 'Block not available (spv mode)');
if (this.chain.options.prune)
throw new RPCError(errs.MISC_ERROR, 'Block not available (pruned data)');
throw new RPCError(errs.DATABASE_ERROR, 'Can\'t read block from disk');
}
if (!verbose)
return block.toRaw().toString('hex');
return yield this.blockToJSON(entry, block, details);
});
RPC.prototype.getBlockHash = co(function* getBlockHash(args, help) {