Skip to content

Commit

Permalink
fix<review>
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Jul 17, 2023
1 parent 46cd5d6 commit 7d2635c
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 112 deletions.
2 changes: 1 addition & 1 deletion bin/bcoin
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ for arg in "$@"; do
cmd='neutrino'
;;
--spv)
cmd='spvnode'
cmd='spvnode'
;;
esac
done
Expand Down
4 changes: 0 additions & 4 deletions bin/neutrino
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ if (!node.config.bool('no-wallet') && !node.has('walletdb')) {
await node.open();
await node.connect();
node.startSync();

node.on("full", () => {
console.log("Full node");
});
})().catch((err) => {
console.error(err.stack);
process.exit(1);
Expand Down
10 changes: 4 additions & 6 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ class Chain extends AsyncEmitter {
// UASF is now enforced (bip148) (mainnet-only).
if (this.options.bip148 && this.network === Network.main) {
if (witness !== thresholdStates.LOCKED_IN
&& witness !== thresholdStates.ACTIVE) {
&& witness !== thresholdStates.ACTIVE) {
// The BIP148 MTP check is nonsensical in
// that it includes the _current_ entry's
// timestamp. This requires some hackery,
Expand Down Expand Up @@ -1955,7 +1955,7 @@ class Chain extends AsyncEmitter {
return this.db.getBlock(hash);
}

async getBlockPeer(hash, filter) {
async getBlockPeer(hash) {
let block = await this.db.getBlock(hash);
if (block) {
const entry = await this.getEntry(hash);
Expand Down Expand Up @@ -2283,7 +2283,6 @@ class Chain extends AsyncEmitter {
return pow.bits;

while (prev.height !== 0
&& prev.height % pow.retargetInterval !== 0
&& prev.bits === pow.bits) {
const cache = this.getPrevCache(prev);

Expand Down Expand Up @@ -2517,7 +2516,7 @@ class Chain extends AsyncEmitter {
const state = await this.getState(prev, deployment);

if (state === thresholdStates.LOCKED_IN
|| state === thresholdStates.STARTED) {
|| state === thresholdStates.STARTED) {
version |= 1 << deployment.bit;
}
}
Expand Down Expand Up @@ -2701,8 +2700,7 @@ class ChainOptions {

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

this.blocks = options.blocks;
Expand Down
8 changes: 4 additions & 4 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,10 @@ class ChainDB {
const deployment = this.network.byBit(bit);

if (deployment
&& start === deployment.startTime
&& timeout === deployment.timeout
&& threshold === deployment.threshold
&& window === deployment.window) {
&& start === deployment.startTime
&& timeout === deployment.timeout
&& threshold === deployment.threshold
&& window === deployment.window) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ class NodeClient extends Client {
return this.get(`/filter/${filter}`);
}

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

/**
Expand Down
55 changes: 22 additions & 33 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Pool extends EventEmitter {
this.pendingRefill = null;

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

this.getcfheadersFilterType = null;
this.getcfheadersStopHash = null;
this.getcfiltersFilterType = null;
this.requestedFilterType = null;
this.getcfiltersStartHeight = null;
this.getcfiltersStopHash = null;

Expand Down Expand Up @@ -749,7 +748,7 @@ class Pool extends EventEmitter {
const stopHeight = chainHeight - startHeight + 1 > 2000
? 2000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
this.getcfheadersFilterType = common.FILTERS.BASIC;
this.requestedFilterType = common.FILTERS.BASIC;
this.getcfheadersStopHash = stopHash;
await this.peers.load.sendGetCFHeaders(
common.FILTERS.BASIC,
Expand All @@ -775,7 +774,7 @@ class Pool extends EventEmitter {
const stopHeight = chainHeight - startHeight + 1 > 1000
? 1000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
this.getcfiltersFilterType = common.FILTERS.BASIC;
this.requestedFilterType = common.FILTERS.BASIC;
this.getcfiltersStartHeight = startHeight;
this.getcfiltersStopHash = stopHash;
await this.peers.load.sendGetCFilters(
Expand Down Expand Up @@ -1419,7 +1418,7 @@ class Pool extends EventEmitter {
}

// We want compact blocks!
if (this.options.compact && !this.options.neutrino)
if (this.options.compact)
peer.sendCompact(this.options.blockMode);

// Find some more peers.
Expand Down Expand Up @@ -1762,7 +1761,7 @@ class Pool extends EventEmitter {
if (this.options.hasWitness() && !peer.hasWitness())
return;

if (this.neutrino) {
if (this.options.neutrino) {
this.startSync();
return;
}
Expand Down Expand Up @@ -2178,21 +2177,27 @@ class Pool extends EventEmitter {

const filterType = packet.filterType;

if (filterType !== this.getcfheadersFilterType) {
if (filterType !== this.requestedFilterType) {
peer.ban();
peer.destroy();
return;
}

const stopHash = packet.stopHash;
assert(stopHash.equals(this.getcfheadersStopHash));
if (!stopHash.equals(this.getcfheadersStopHash)) {
peer.ban();
return;
}
let previousFilterHeader = packet.previousFilterHeader;
const filterHashes = packet.filterHashes;
let blockHeight = await this.chain.getHeight(stopHash)
- filterHashes.length + 1;
const stopHeight = await this.chain.getHeight(stopHash);
for (const filterHash of filterHashes) {
assert(blockHeight <= stopHeight);
if (blockHeight > stopHeight) {
peer.ban();
return;
}
const basicFilter = new BasicFilter();
basicFilter._hash = filterHash;
const filterHeader = basicFilter.header(previousFilterHeader);
Expand Down Expand Up @@ -2232,7 +2237,7 @@ class Pool extends EventEmitter {
const filterType = packet.filterType;
const filter = packet.filterBytes;

if (filterType !== this.getcfheadersFilterType) {
if (filterType !== this.requestedFilterType) {
peer.ban();
peer.destroy();
return;
Expand All @@ -2241,8 +2246,11 @@ class Pool extends EventEmitter {
const blockHeight = await this.chain.getHeight(blockHash);
const stopHeight = await this.chain.getHeight(this.getcfiltersStopHash);

assert(blockHeight >= this.getcfiltersStartHeight
&& blockHeight <= stopHeight);
if (!(blockHeight >= this.getcfiltersStartHeight
&& blockHeight <= stopHeight)) {
peer.ban();
return;
}

const basicFilter = new BasicFilter();
const gcsFilter = basicFilter.fromNBytes(filter);
Expand Down Expand Up @@ -2543,11 +2551,6 @@ class Pool extends EventEmitter {
await this.addBlock(peer, packet.block, flags);
}

async handleFilter(peer, packet) {
const flags = chainCommon.flags.DEFAULT_FLAGS;
await this.addFilter(peer, packet.filter, flags);
}

/**
* Attempt to add block to chain.
* @method
Expand Down Expand Up @@ -2652,20 +2655,6 @@ class Pool extends EventEmitter {
await this.resolveChain(peer, hash);
}

async addFilter(peer, filter, flags) {
const hash = filter.hash();
const unlock = await this.locker.lock(hash);
try {
return await this._addFilter(peer, filter, flags);
} finally {
unlock();
}
}

async _addFilter(peer, filter, flags) {

}

/**
* Resolve header chain.
* @method
Expand Down Expand Up @@ -4086,13 +4075,13 @@ class PoolOptions {

if (options.spv != null) {
assert(typeof options.spv === 'boolean');
// assert(options.spv === this.chain.options.spv);
this.spv = options.spv;
} else {
this.spv = this.chain.options.spv;
}

if (options.neutrino != null) {
assert(options.compact !== true);
assert(typeof options.neutrino === 'boolean');
this.neutrino = options.neutrino;
}
Expand Down
44 changes: 0 additions & 44 deletions lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,50 +645,6 @@ class FullNode extends Node {

return false;
}

/**
* Retrieve compact filter by hash/height.
* @param {Hash | Number} hash
* @param {Number} type
* @returns {Promise} - Returns {@link Buffer}.
*/

async getBlockFilter(hash, filterType) {
const Indexer = this.filterIndexers.get(filterType);

if (!Indexer)
return null;

if (typeof hash === 'number')
hash = await this.chain.getHash(hash);

if (!hash)
return null;

return Indexer.getFilter(hash);
}

/**
* Retrieve compact filter by hash/height.
* @param {Hash | Number} hash
* @param {Number} type
* @returns {Promise} - Returns {@link Buffer}.
*/

async getBlockFilterHeader(hash, filterType) {
const Indexer = this.filterIndexers.get(filterType);

if (!Indexer)
return null;

if (typeof hash === 'number')
hash = await this.chain.getHash(hash);

if (!hash)
return null;

return Indexer.getFilterHeader(hash);
}
}

/*
Expand Down
3 changes: 1 addition & 2 deletions lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,7 @@ class HTTP extends Server {
socket.hook('get block peer', (...args) => {
const valid = new Validator(args);
const hash = valid.hash(0);
const filter = valid.buf(1);
return this.pool.getBlockPeer(hash, filter);
return this.chain.getBlockPeer(hash);
});

socket.hook('estimate fee', (...args) => {
Expand Down
9 changes: 2 additions & 7 deletions lib/node/neutrino.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* neutrino.js - spv node for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* Copyright (c) 2023, Manav Desai (MIT License)
* Copyright (c) 2023, Shaswat Gupta (MIT License).
* https://github.com/bcoin-org/bcoin
*/

Expand Down Expand Up @@ -134,18 +134,13 @@ class Neutrino extends Node {
*/

init() {
console.log('Initializing Neutrino Node.');
// Bind to errors
this.chain.on('error', err => this.error(err));
this.pool.on('error', err => this.error(err));

if (this.http)
this.http.on('error', err => this.error(err));

this.pool.on('tx', (tx) => {
this.emit('tx', tx);
});

this.chain.on('block', (block) => {
this.emit('block', block);
});
Expand Down
44 changes: 44 additions & 0 deletions lib/node/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,50 @@ class Node extends EventEmitter {
await plugin.close();
}
}

/**
* Retrieve compact filter by hash/height.
* @param {Hash | Number} hash
* @param {Number} type
* @returns {Promise} - Returns {@link Buffer}.
*/

async getBlockFilter(hash, filterType) {
const Indexer = this.filterIndexers.get(filterType);

if (!Indexer)
return null;

if (typeof hash === 'number')
hash = await this.chain.getHash(hash);

if (!hash)
return null;

return Indexer.getFilter(hash);
}

/**
* Retrieve compact filter header by hash/height.
* @param {Hash | Number} hash
* @param {Number} type
* @returns {Promise} - Returns {@link Buffer}.
*/

async getBlockFilterHeader(hash, filterType) {
const Indexer = this.filterIndexers.get(filterType);

if (!Indexer)
return null;

if (typeof hash === 'number')
hash = await this.chain.getHash(hash);

if (!hash)
return null;

return Indexer.getFilterHeader(hash);
}
}

/*
Expand Down
Loading

0 comments on commit 7d2635c

Please sign in to comment.