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 8e0bec0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 40 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
5 changes: 2 additions & 3 deletions bin/neutrino
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ console.log('Starting bcoin');
process.title = 'bcoin';
const Neutrino = require('../lib/node/neutrino');

// Doubt in db
const node = new Neutrino({
file: true,
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
11 changes: 5 additions & 6 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2060,14 +2060,12 @@ class Chain extends AsyncEmitter {
if (this.synced)
return;

if (this.options.neutrino && this.getProgress() < 1)
return;
if (this.options.checkpoints) {
if (this.height < this.network.lastCheckpoint)
return;
}
if (this.options.neutrino && this.tip.time < 1686851917)
// TODO change this later
return;
else if (!this.options.neutrino &&
} else if (!this.options.neutrino &&
this.tip.time < util.now() - this.network.block.maxTipAge)
return;

Expand Down Expand Up @@ -2701,7 +2699,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
44 changes: 18 additions & 26 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 @@ -1762,7 +1746,9 @@ class Pool extends EventEmitter {
return;

if (this.options.neutrino) {
this.startSync();
const filterHeight = await this.chain.getCFilterHeight();
if (filterHeight === this.chain.height)
this.startSync();
return;
}

Expand Down Expand Up @@ -1830,6 +1816,8 @@ class Pool extends EventEmitter {
*/

async handleTXInv(peer, hashes) {
if (this.options.neutrino)
return;
assert(hashes.length > 0);

if (this.syncing && !this.chain.synced)
Expand Down Expand Up @@ -2178,13 +2166,15 @@ class Pool extends EventEmitter {
const filterType = packet.filterType;

if (filterType !== this.requestedFilterType) {
this.logger.warning('Received CFHeaders packet with wrong filterType');
peer.ban();
peer.destroy();
return;
}

const stopHash = packet.stopHash;
if (!stopHash.equals(this.getcfheadersStopHash)) {
if (!stopHash.equals(this.requestedStopHash)) {
this.logger.warning('Received CFHeaders packet with wrong stopHash');
peer.ban();
return;
}
Expand Down Expand Up @@ -2220,7 +2210,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 @@ -2238,16 +2228,18 @@ class Pool extends EventEmitter {
const filter = packet.filterBytes;

if (filterType !== this.requestedFilterType) {
this.logger.warning('Received CFilter packet with wrong filterType');
peer.ban();
peer.destroy();
return;
}

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)) {
this.logger.warning('Received CFilter packet with wrong blockHeight');
peer.ban();
return;
}
Expand All @@ -2271,7 +2263,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 +2273,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 +2282,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

0 comments on commit 8e0bec0

Please sign in to comment.