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

node/http: get block header by hash/height #808

Merged
merged 5 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ class HTTP extends Server {
res.json(200, block.getJSON(this.network, view, height, depth));
});

// Block Header by hash/height
this.get('/header/:block', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.uintbrhash('block');

enforce(hash != null, 'Hash or height required.');

const entry = await this.chain.getEntry(hash);

if (!entry) {
res.json(404);
return;
}

res.json(200, entry.toJSON());
});

// Mempool snapshot
this.get('/mempool', async (req, res) => {
enforce(this.mempool, 'No mempool available.');
Expand Down
44 changes: 44 additions & 0 deletions test/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,50 @@ describe('HTTP', function() {
assert.strictEqual(blocks.length, 10);
});

// depends on the previous test to generate blocks
it('should fetch block header', async () => {
// fetch corresponding header and block
const header = await nclient.get(`/header/${7}`);
assert.equal(header.height, 7);

const properties = [
'hash', 'version', 'prevBlock',
'merkleRoot', 'time', 'bits',
'nonce', 'height', 'chainwork'
];

for (const property of properties)
assert(property in header);

const block = await nclient.getBlock(7);

assert.equal(block.hash, header.hash);
assert.equal(block.height, header.height);
assert.equal(block.version, header.version);
assert.equal(block.prevBlock, header.prevBlock);
assert.equal(block.merkleRoot, header.merkleRoot);
assert.equal(block.time, header.time);
assert.equal(block.bits, header.bits);
assert.equal(block.nonce, header.nonce);
});

it('should fetch null for block header that does not exist', async () => {
// many blocks in the future
const header = await nclient.get(`/header/${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}`);

assert.equal(prevBlock, header.prevBlock);
prevBlock = header.hash;
}
});

it('should initiate rescan from socket without a bloom filter', async () => {
// Rescan from height 5. Without a filter loaded = no response, but no error
const response = await nclient.call('rescan', 5);
Expand Down