Skip to content

Commit

Permalink
Merge pull request #862 from tynes/client-block-header
Browse files Browse the repository at this point in the history
Client block header
  • Loading branch information
braydonf committed Sep 28, 2019
2 parents 67d444b + 40dac00 commit 99638ac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
20 changes: 20 additions & 0 deletions bin/bcoin-cli
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ class CLI {
this.log(block);
}

async getBlockHeader() {
let hash = this.config.str(0, '');

if (hash.length !== 64)
hash = parseInt(hash, 10);

const header = await this.client.getBlockHeader(hash);

if (!header) {
this.log('Header not found.');
return;
}

this.log(header);
}

async getFilter() {
let hash = this.config.str(0, '');

Expand Down Expand Up @@ -204,6 +220,9 @@ class CLI {
case 'block':
await this.getBlock();
break;
case 'header':
await this.getBlockHeader();
break;
case 'filter':
await this.getFilter();
break;
Expand All @@ -222,6 +241,7 @@ class CLI {
this.log(' $ tx [hash/address]: View transactions.');
this.log(' $ coin [hash+index/address]: View coins.');
this.log(' $ block [hash/height]: View block.');
this.log(' $ header [hash/height]: View block header.');
this.log(' $ filter [hash/height]: View filter');
this.log(' $ reset [height/hash]: Reset chain to desired block.');
this.log(' $ rpc [command] [args]: Execute RPC command.' +
Expand Down
11 changes: 11 additions & 0 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ class NodeClient extends Client {
return this.get(`/block/${block}`);
}

/**
* Retrieve a block header.
* @param {Hash|Number} block
* @returns {Promise}
*/

getBlockHeader(block) {
assert(typeof block === 'string' || typeof block === 'number');
return this.get(`/header/${block}`);
}

/**
* Retreive a filter from the filter indexer.
* @param {Hash|Number} filter
Expand Down
16 changes: 8 additions & 8 deletions test/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const {BloomFilter} = require('bfilter');
const assert = require('bsert');
const {NodeClient, WalletClient} = require('../lib/client');
const consensus = require('../lib/protocol/consensus');
const Address = require('../lib/primitives/address');
const Script = require('../lib/script/script');
Expand Down Expand Up @@ -35,9 +36,8 @@ const node = new FullNode({
httpPort: ports.node,
env: {
'BCOIN_WALLET_HTTP_PORT': ports.wallet.toString()
}});

const {NodeClient, WalletClient} = require('../lib/client');
}
});

const nclient = new NodeClient({
port: ports.node,
Expand Down Expand Up @@ -458,7 +458,7 @@ describe('HTTP', function() {
it('should fetch block header by height', async () => {
// fetch corresponding header and block
const height = 7;
const header = await nclient.get(`/header/${height}`);
const header = await nclient.getBlockHeader(height);
assert.equal(header.height, height);

const properties = [
Expand All @@ -485,23 +485,23 @@ describe('HTTP', function() {
it('should fetch block header by hash', async () => {
const info = await nclient.getInfo();

const headerByHash = await nclient.get(`/header/${info.chain.tip}`);
const headerByHeight = await nclient.get(`/header/${info.chain.height}`);
const headerByHash = await nclient.getBlockHeader(info.chain.tip);
const headerByHeight = await nclient.getBlockHeader(info.chain.height);

assert.deepEqual(headerByHash, headerByHeight);
});

it('should fetch null for block header that does not exist', async () => {
// Many blocks in the future.
const header = await nclient.get(`/header/${40000}`);
const header = await nclient.getBlockHeader(40000);
assert.equal(header, null);
});

it('should have valid header chain', async () => {
// Starting at the genesis block.
let prevBlock = '0000000000000000000000000000000000000000000000000000000000000000';
for (let i = 0; i < 10; i++) {
const header = await nclient.get(`/header/${i}`);
const header = await nclient.getBlockHeader(i);

assert.equal(prevBlock, header.prevBlock);
prevBlock = header.hash;
Expand Down

0 comments on commit 99638ac

Please sign in to comment.