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 20, 2023
1 parent 62dfdbd commit bffb60a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 36 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
4 changes: 2 additions & 2 deletions bin/neutrino
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const node = new Neutrino({
argv: true,
env: true,
logFile: true,
logConsole: true,
logLevel: 'debug',
logConsole: true, // todo: remove
logLevel: 'debug', // todo: remove
db: 'leveldb',
memory: false,
workers: true,
Expand Down
3 changes: 2 additions & 1 deletion lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2701,7 +2701,8 @@ class ChainOptions {

fromOptions(options) {
if (!options.spv) {
assert(options.blocks && typeof options.blocks === 'object');
assert(options.blocks && typeof options.blocks === 'object',
'Chain requires a blockstore.');
}

this.blocks = options.blocks;
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
2 changes: 1 addition & 1 deletion lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ class Peer extends EventEmitter {
throw new Error('Peer does not support getheaders.');
}

if (this.options.spv && !this.options.neutrino) {
if (this.options.spv) {
if (!(this.services & services.BLOOM))
throw new Error('Peer does not support BIP37.');

Expand Down
34 changes: 9 additions & 25 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class Pool extends EventEmitter {
this.connected = false;
this.disconnecting = false;
this.syncing = false;
this.filterSyncing = false;
this.discovering = false;
this.spvFilter = null;
this.txFilter = null;
Expand All @@ -81,7 +80,6 @@ class Pool extends EventEmitter {
this.pendingRefill = null;

this.checkpoints = false;
this.neutrino = false;
this.headerChain = new List();
this.headerNext = null;
this.headerTip = null;
Expand All @@ -90,10 +88,9 @@ class Pool extends EventEmitter {
this.hosts = new HostList(this.options);
this.id = 0;

this.getcfheadersStopHash = null;
this.requestedFilterType = null;
this.getcfiltersStartHeight = null;
this.getcfiltersStopHash = null;
this.requestedStopHash = null;

if (this.options.spv) {
this.spvFilter = BloomFilter.fromRate(
Expand Down Expand Up @@ -740,7 +737,6 @@ class Pool extends EventEmitter {
if (!this.opened || !this.connected)
return;

this.filterSyncing = true;
const cFHeaderHeight = await this.chain.getCFHeaderHeight();
const startHeight = cFHeaderHeight
? cFHeaderHeight + 1 : 1;
Expand All @@ -749,7 +745,7 @@ class Pool extends EventEmitter {
? 2000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
this.requestedFilterType = common.FILTERS.BASIC;
this.getcfheadersStopHash = stopHash;
this.requestedStopHash = stopHash;
await this.peers.load.sendGetCFHeaders(
common.FILTERS.BASIC,
startHeight,
Expand All @@ -766,7 +762,6 @@ class Pool extends EventEmitter {
if (!this.opened || !this.connected)
return;

this.filterSyncing = true;
const cFilterHeight = await this.chain.getCFilterHeight();
const startHeight = cFilterHeight
? cFilterHeight + 1 : 1;
Expand All @@ -776,7 +771,7 @@ class Pool extends EventEmitter {
const stopHash = await this.chain.getHash(stopHeight);
this.requestedFilterType = common.FILTERS.BASIC;
this.getcfiltersStartHeight = startHeight;
this.getcfiltersStopHash = stopHash;
this.requestedStopHash = stopHash;
await this.peers.load.sendGetCFilters(
common.FILTERS.BASIC,
startHeight,
Expand Down Expand Up @@ -899,17 +894,6 @@ class Pool extends EventEmitter {
return this.sendLocator(locator, peer);
}

// /**
// * Sync the filter headers from peer.
// * @method
// * @param {Peer} peer
// * @returns {void}
// */
//
// syncCompactFiltersCheckPt(peer) {
// peer.sendGetCFCheckpt(common.FILTERS.BASIC, this.chain.tip.hash);
// }

/**
* Send a chain locator and start syncing from peer.
* @method
Expand Down Expand Up @@ -2184,7 +2168,7 @@ class Pool extends EventEmitter {
}

const stopHash = packet.stopHash;
if (!stopHash.equals(this.getcfheadersStopHash)) {
if (!stopHash.equals(this.requestedStopHash)) {
peer.ban();
return;
}
Expand Down Expand Up @@ -2220,7 +2204,7 @@ class Pool extends EventEmitter {
const nextStopHeight = stopHeight + 2000 < this.chain.height
? stopHeight + 2000 : this.chain.height;
const nextStopHash = await this.chain.getHash(nextStopHeight);
this.getcfheadersStopHash = nextStopHash;
this.requestedStopHash = nextStopHash;
peer.sendGetCFHeaders(filterType, stopHeight + 1, nextStopHash);
}
}
Expand All @@ -2244,7 +2228,7 @@ class Pool extends EventEmitter {
}

const blockHeight = await this.chain.getHeight(blockHash);
const stopHeight = await this.chain.getHeight(this.getcfiltersStopHash);
const stopHeight = await this.chain.getHeight(this.requestedStopHash);

if (!(blockHeight >= this.getcfiltersStartHeight
&& blockHeight <= stopHeight)) {
Expand All @@ -2271,7 +2255,7 @@ class Pool extends EventEmitter {
nextStopHeight = stopHeight + 1000;
const stopHash = await this.chain.getHash(nextStopHeight);
this.getcfiltersStartHeight = startHeight;
this.getcfiltersStopHash = stopHash;
this.requestedStopHash = stopHash;
this.peers.load.sendGetCFilters(
common.FILTERS.BASIC,
startHeight,
Expand All @@ -2281,7 +2265,7 @@ class Pool extends EventEmitter {
nextStopHeight = this.chain.height;
const stopHash = await this.chain.getHash(nextStopHeight);
this.getcfiltersStartHeight = startHeight;
this.getcfiltersStopHash = stopHash;
this.requestedStopHash = stopHash;
this.peers.load.sendGetCFilters(
common.FILTERS.BASIC,
startHeight,
Expand All @@ -2290,7 +2274,7 @@ class Pool extends EventEmitter {
return;
}
} else if (cFilterHeight === this.chain.height) {
this.logger.info('CFilters sync complete');
this.chain.emit('full');
}
}

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
4 changes: 1 addition & 3 deletions test/neutrino-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const FullNode = require('../lib/node/fullnode');
const NeutrinoNode = require('../lib/node/neutrino');
const {forValue} = require('./util/common');
const assert = require('bsert');
describe('neutrino', function () {
describe('Neutrino', function () {
this.timeout(100000);

const node1 = new NeutrinoNode({
Expand All @@ -13,8 +13,6 @@ describe('neutrino', function () {
port: 10000,
httpPort: 20000,
neutrino: true,
logConsole: true,
logLevel: 'debug',
only: '127.0.0.1'
});

Expand Down

0 comments on commit bffb60a

Please sign in to comment.