Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client block header #862

Merged
merged 3 commits into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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