Skip to content

Commit

Permalink
fix<node,test>: headers-sync, refactor and test bip157
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Aug 4, 2023
1 parent 9750e82 commit d1d8e80
Show file tree
Hide file tree
Showing 20 changed files with 334 additions and 1,288 deletions.
20 changes: 0 additions & 20 deletions bin/bcoin-cli
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,6 @@ 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 @@ -262,9 +246,6 @@ class CLI {
case 'filter':
await this.getFilter();
break;
case 'filterheader':
await this.getFilterHeader();
break;
case 'fee':
await this.estimateFee();
break;
Expand All @@ -282,7 +263,6 @@ 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
31 changes: 6 additions & 25 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -1791,24 +1791,6 @@ class Chain extends AsyncEmitter {
return this.hasEntry(hash);
}

async getCFHeaderHeight() {
return await this.db.getCFHeaderHeight();
}

async saveCFHeaderHeight(height) {
this.db.neutrinoState.headerHeight = height;
await this.db.saveNeutrinoState();
}

async getCFilterHeight() {
return await this.db.getCFilterHeight();
}

async saveCFilterHeight(height) {
this.db.neutrinoState.filterHeight = height;
await this.db.saveNeutrinoState();
}

/**
* Find the corresponding block entry by hash or height.
* @param {Hash|Number} hash/height
Expand Down Expand Up @@ -2021,12 +2003,17 @@ class Chain extends AsyncEmitter {
if (this.synced)
return;

if (this.options.checkpoints)
if (this.options.checkpoints) {
if (this.height < this.network.lastCheckpoint)
return;
}

if (this.tip.time < util.now() - this.network.block.maxTipAge)
return;

if (!this.hasChainwork())
return;

this.synced = true;
this.emit('full');
}
Expand Down Expand Up @@ -2629,7 +2616,6 @@ class ChainOptions {
this.compression = true;

this.spv = false;
this.neutrino = false;
this.bip91 = false;
this.bip148 = false;
this.prune = false;
Expand Down Expand Up @@ -2676,11 +2662,6 @@ class ChainOptions {
this.spv = options.spv;
}

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

if (options.prefix != null) {
assert(typeof options.prefix === 'string');
this.prefix = options.prefix;
Expand Down
63 changes: 1 addition & 62 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ChainDB {
this.state = new ChainState();
this.pending = null;
this.current = null;
this.neutrinoState = null;

this.cacheHash = new LRU(this.options.entryCache, null, BufferMap);
this.cacheHeight = new LRU(this.options.entryCache);
Expand Down Expand Up @@ -91,11 +90,6 @@ class ChainDB {
this.logger.info('ChainDB successfully initialized.');
}

if (this.options.neutrino) {
if (!this.neutrinoState)
this.neutrinoState = await this.getNeutrinoState();
}

this.logger.info(
'Chain State: hash=%h tx=%d coin=%d value=%s.',
this.state.tip,
Expand Down Expand Up @@ -1007,7 +1001,7 @@ class ChainDB {
*/

async getRawBlock(block) {
if (this.options.spv && !this.options.neutrino)
if (this.options.spv)
return null;

const hash = await this.getHash(block);
Expand Down Expand Up @@ -1676,39 +1670,6 @@ class ChainDB {
b.put(layout.O.encode(), flags.toRaw());
return b.write();
}

/**
* Get Neutrino State
* @returns {Promise<NeutrinoState>} - Returns neutrino state
*/

async getNeutrinoState() {
const data = await this.db.get(layout.N.encode());
if (!data)
return new NeutrinoState();
return NeutrinoState.fromRaw(data);
}

async getCFHeaderHeight() {
const state = await this.getNeutrinoState();
return state.headerHeight;
}

async getCFilterHeight() {
const state = await this.getNeutrinoState();
return state.filterHeight;
}

/**
* Save Neutrino State
* @returns {void}
*/
async saveNeutrinoState() {
const state = this.neutrinoState.toRaw();
const b = this.db.batch();
b.put(layout.N.encode(), state);
return b.write();
}
}

/**
Expand Down Expand Up @@ -1991,28 +1952,6 @@ function fromU32(num) {
return data;
}

class NeutrinoState {
constructor() { // TODO: do we add support for multiple filters?
this.headerHeight = 0;
this.filterHeight = 0;
}

toRaw() {
const bw = bio.write(8);
bw.writeU32(this.headerHeight);
bw.writeU32(this.filterHeight);
return bw.render();
}

static fromRaw(data) {
const state = new NeutrinoState();
const br = bio.read(data);
state.headerHeight = br.readU32();
state.filterHeight = br.readU32();
return state;
}
}

/*
* Expose
*/
Expand Down
3 changes: 0 additions & 3 deletions lib/blockchain/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const bdb = require('bdb');
* O -> chain options
* R -> tip hash
* D -> versionbits deployments
* N -> neutrino state
* e[hash] -> entry
* h[hash] -> height
* H[height] -> hash
Expand All @@ -34,8 +33,6 @@ const layout = {
O: bdb.key('O'),
R: bdb.key('R'),
D: bdb.key('D'),
N: bdb.key('N'),
F: bdb.key('H', ['hash256']),
e: bdb.key('e', ['hash256']),
h: bdb.key('h', ['hash256']),
H: bdb.key('H', ['uint32']),
Expand Down
5 changes: 0 additions & 5 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ class NodeClient extends Client {
return this.get(`/filter/${block}`);

Check warning on line 169 in lib/client/node.js

View check run for this annotation

Codecov / codecov/patch

lib/client/node.js#L168-L169

Added lines #L168 - L169 were not covered by tests
}

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

getBlockPeer(hash) {
return this.call('get block peer', hash);

Check warning on line 173 in lib/client/node.js

View check run for this annotation

Codecov / codecov/patch

lib/client/node.js#L173

Added line #L173 was not covered by tests
}
Expand Down
28 changes: 5 additions & 23 deletions lib/indexer/filterindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,6 @@ 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
Expand All @@ -114,18 +93,21 @@ class FilterIndexer extends Indexer {
* @returns {Promise}
*/

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

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

this.batch = this.db.batch();

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

/**
Expand Down
7 changes: 7 additions & 0 deletions lib/indexer/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class Indexer extends EventEmitter {
this.height = 0;
}

async syncHeight(hash, height) {
const meta = new BlockMeta(hash, height);
await this._setTip(meta);
await this.commit();
this.height = height;
}

/**
* Bind to chain events and save listeners for removal on close
* @private
Expand Down
3 changes: 2 additions & 1 deletion lib/net/netaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ class NetAddress {
NetAddress.DEFAULT_SERVICES = 0
| common.services.NETWORK
| common.services.WITNESS
| common.services.BLOOM;
| common.services.BLOOM
| common.services.NODE_COMPACT_FILTERS;

/*
* Expose
Expand Down
24 changes: 5 additions & 19 deletions lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,11 @@ class Peer extends EventEmitter {
this.compactWitness = packet.version === 2;
}

sendSendHeaders() {
const packet = new packets.SendHeadersPacket();
this.send(packet);
}

/**
* Send `getheaders` to peer. Note that unlike
* `getblocks`, `getheaders` can have a null locator.
Expand Down Expand Up @@ -1840,25 +1845,6 @@ 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 d1d8e80

Please sign in to comment.