Skip to content

Commit

Permalink
feat<node>: Filters-Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Jul 20, 2023
1 parent 0d84694 commit ed0ce73
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 29 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
15 changes: 12 additions & 3 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,18 @@ 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) {
return this.call('get block peer', hash);
}

/**
Expand Down
43 changes: 43 additions & 0 deletions lib/indexer/filterindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,49 @@ class FilterIndexer extends Indexer {
this.put(layout.f.encode(hash), gcsFilter.hash());
}

/**
* save filter header
* @param {Hash} blockHash
* @param {Hash} filterHeader
* @param {Hash} filterHash
* @returns {Promise}
*/

async saveFilterHeader(blockHash, filterHeader, filterHash) {
assert(blockHash);
assert(filterHeader);
assert(filterHash);

const filter = new Filter();
filter.header = filterHeader;

await this.blocks.writeFilter(blockHash, filter.toRaw(), this.filterType);
// console.log(layout.f.encode(blockHash));
this.put(layout.f.encode(blockHash), filterHash);
}

/**
* Save filter
* @param {Hash} blockHash
* @param {BasicFilter} basicFilter
* @param {Hash} filterHeader
* @returns {Promise}
*/

async saveFilter(blockHash, basicFilter, filterHeader) {
assert(blockHash);
assert(basicFilter);
assert(filterHeader);

const filter = new Filter();
filter.filter = basicFilter.toRaw();
filter.header = filterHeader;

await this.blocks.writeFilter(blockHash, filter.toRaw(), this.filterType);
// console.log(layout.f.encode(blockHash));
this.put(layout.f.encode(blockHash), basicFilter.hash());
}

/**
* Prune compact filters.
* @private
Expand Down
14 changes: 14 additions & 0 deletions lib/indexer/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Indexer extends EventEmitter {
this.blocks = this.options.blocks;
this.chain = this.options.chain;

this.neutrino = this.options.neutrino;

this.closing = false;
this.db = null;
this.batch = null;
Expand Down Expand Up @@ -292,6 +294,11 @@ class Indexer extends EventEmitter {
*/

async _syncBlock(meta, block, view) {
if (this.neutrino) {
if (!this.batch)
this.start();
return true;
}
// In the case that the next block is being
// connected or the current block disconnected
// use the block and view being passed directly,
Expand Down Expand Up @@ -636,6 +643,8 @@ class IndexOptions {
this.cacheSize = 16 << 20;
this.compression = true;

this.neutrino = false;

if (options)
this.fromOptions(options);
}
Expand Down Expand Up @@ -697,6 +706,11 @@ class IndexOptions {
this.compression = options.compression;
}

if (options.neutrino != null) {
assert(typeof options.neutrino === 'boolean');
this.neutrino = options.neutrino;
}

return this;
}

Expand Down
66 changes: 66 additions & 0 deletions lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,12 @@ class Peer extends EventEmitter {
case packetTypes.GETHEADERS:
this.request(packetTypes.HEADERS, timeout * 2);
break;
case packetTypes.GETCFHEADERS:
this.request(packetTypes.CFHEADERS, timeout);
break;
case packetTypes.GETCFILTERS:
this.request(packetTypes.CFILTER, timeout);
break;
case packetTypes.GETDATA:
this.request(packetTypes.DATA, timeout * 2);
break;
Expand Down Expand Up @@ -1751,6 +1757,26 @@ class Peer extends EventEmitter {
this.send(packet);
}

/**
* @param {Number} filterType - `0` = basic
* @param {Number} startHeight - Height to start at.
* @param {Hash} stopHash - Hash to stop at.
* @returns {void}
* @description Send `getcfilters` to peer.
*/
sendGetCFilters(filterType, startHeight, stopHash) {
const packet = new packets.GetCFiltersPacket(
filterType,
startHeight,
stopHash);

this.logger.debug(
'Sending getcfilters (type=%d, startHeight=%d, stopHash=%h).',
filterType, startHeight, stopHash);

this.send(packet);
}

/**
* Send `cfheaders` to peer.
* @param {Number} filterType
Expand All @@ -1773,6 +1799,27 @@ class Peer extends EventEmitter {
this.send(packet);
}

/**
* @param {Number} filterType
* @param {Number} startHeight
* @param {Hash} stopHash
* @returns {void}
* @description Send `getcfheaders` to peer.
*/

sendGetCFHeaders(filterType, startHeight, stopHash) {
const packet = new packets.GetCFHeadersPacket(
filterType,
startHeight,
stopHash);

this.logger.debug(
'Sending getcfheaders (type=%d, start=%h, stop=%h).',
filterType, startHeight, stopHash);

this.send(packet);
}

/**
* send `cfcheckpt` to peer.
* @param {Number} filterType
Expand All @@ -1793,6 +1840,25 @@ class Peer extends EventEmitter {
this.send(packet);
}

/**
* Send `getcfcheckpt` to peer.
* @param {Number} filterType
* @param {Hash} stopHash
* @returns {void}
*/

sendGetCFCheckpt(filterType, stopHash) {
const packet = new packets.GetCFCheckptPacket(
filterType,
stopHash);

this.logger.debug(
'Sending getcfcheckpt (type=%d, stop=%h).',
filterType, stopHash);

this.send(packet);
}

/**
* Send `mempool` to peer.
*/
Expand Down
Loading

0 comments on commit ed0ce73

Please sign in to comment.