Skip to content

Commit

Permalink
feat<cli>: added filterheader
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Jul 17, 2023
1 parent aabe613 commit 934bdcc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
20 changes: 20 additions & 0 deletions bin/bcoin-cli
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ class CLI {
this.log(filter);
}

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

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

const filterHeader = await this.client.getFilterHeader(hash);

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

this.log(filterHeader);
}

async estimateFee() {
const blocks = this.config.uint(0, 1);

Expand Down Expand Up @@ -246,6 +262,9 @@ class CLI {
case 'filter':
await this.getFilter();
break;
case 'filterheader':
await this.getFilterHeader();
break;
case 'fee':
await this.estimateFee();
break;
Expand All @@ -263,6 +282,7 @@ class CLI {
this.log(' $ coin [hash+index/address]: View coins.');
this.log(' $ fee [target]: Estimate smart fee.');
this.log(' $ filter [hash/height]: View filter.');
this.log(' $ filterheader [hash/height]: View filter header.');
this.log(' $ header [hash/height]: View block header.');
this.log(' $ info: Get server info.');
this.log(' $ mempool: Get mempool snapshot.');
Expand Down
11 changes: 8 additions & 3 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,14 @@ class NodeClient extends Client {
* @returns {Promise}
*/

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

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

getBlockPeer(hash) {
Expand Down
21 changes: 20 additions & 1 deletion lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ class HTTP extends Server {

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

const filter = await this.node.getBlockFilter(hash);
const filterName = valid.str(1, 'BASIC').toUpperCase();
const filter = await this.node.getBlockFilter(hash, filterName);

if (!filter) {
res.json(404);
Expand All @@ -301,6 +302,24 @@ class HTTP extends Server {
res.json(200, filter.toJSON());
});

this.get('/filterheader/:block', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.uintbrhash('block');

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

const filterName = valid.str(1, 'BASIC').toUpperCase();
const filterHeader = await this.node.
getBlockFilterHeader(hash, filterName);

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

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

// Mempool snapshot
this.get('/mempool', async (req, res) => {
enforce(this.mempool, 'No mempool available.');
Expand Down

0 comments on commit 934bdcc

Please sign in to comment.