Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added headers first sync #1156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/bcoin
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ for arg in "$@"; do
--spv)
cmd='spvnode'
;;
--neutrino)
cmd='neutrino'
;;
esac
done

Expand Down
40 changes: 40 additions & 0 deletions bin/neutrino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node

'use strict';

process.title = 'bcoin';

const NeutrinoNode = require('../lib/node/neutrino');

const node = new NeutrinoNode({
file: true,
argv: true,
env: true,
logFile: true,
logConsole: true,
logLevel: 'debug',
db: 'leveldb',
memory: false,
workers: true,
listen: false,
loader: require
});

process.on('unhandledRejection', (err, promise) => {
throw err;
});

process.on('SIGINT', async () => {
await node.close();
});

(async () => {
await node.ensure();
await node.open();
await node.connect();

node.startSync();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});
17 changes: 17 additions & 0 deletions lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,12 @@ class Peer extends EventEmitter {
if (!(this.services & services.NETWORK))
throw new Error('Peer does not support network services.');

if (this.options.neutrino) {
if (!(this.services & services.NODE_COMPACT_FILTERS)) {
throw new Error('Peer does not support Compact Filters.');
}
}

if (this.options.headers) {
if (this.version < common.HEADERS_VERSION)
throw new Error('Peer does not support getheaders.');
Expand Down Expand Up @@ -1668,6 +1674,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 @@ -2080,6 +2091,7 @@ class PeerOptions {
this.agent = common.USER_AGENT;
this.noRelay = false;
this.spv = false;
this.neutrino = false;
this.compact = false;
this.headers = false;
this.banScore = common.BAN_SCORE;
Expand Down Expand Up @@ -2143,6 +2155,11 @@ class PeerOptions {
this.spv = options.spv;
}

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

if (options.compact != null) {
assert(typeof options.compact === 'boolean');
this.compact = options.compact;
Expand Down
59 changes: 51 additions & 8 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Pool extends EventEmitter {
this.pendingRefill = null;

this.checkpoints = false;
this.neutrino = false;
this.headerChain = new List();
this.headerNext = null;
this.headerTip = null;
Expand Down Expand Up @@ -807,14 +808,19 @@ class Pool extends EventEmitter {
return false;

// Ask for the mempool if we're synced.
if (this.network.requestMempool) {
if (this.network.requestMempool && !this.options.neutrino) {
if (peer.loader && this.chain.synced)
peer.sendMempool();
}

peer.syncing = true;
peer.blockTime = Date.now();

if (this.options.neutrino) {
peer.sendGetHeaders(locator);
return true;
}

if (this.checkpoints) {
peer.sendGetHeaders(locator, this.headerTip.hash);
return true;
Expand Down Expand Up @@ -861,7 +867,8 @@ class Pool extends EventEmitter {
break;
}

this.getBlock(peer, items);
if (!this.options.neutrino)
this.getBlock(peer, items);
}

/**
Expand Down Expand Up @@ -1290,6 +1297,9 @@ class Pool extends EventEmitter {
peer.send(new packets.AddrPacket([addr]));
}

if (this.options.neutrino)
peer.sendSendHeaders();

// We want compact blocks!
if (this.options.compact)
peer.sendCompact(this.options.blockMode);
Expand Down Expand Up @@ -2027,6 +2037,9 @@ class Pool extends EventEmitter {
if (this.options.selfish)
return;

if (this.options.neutrino)
return;

if (this.chain.options.spv)
return;

Expand Down Expand Up @@ -2139,7 +2152,7 @@ class Pool extends EventEmitter {
async _handleHeaders(peer, packet) {
const headers = packet.items;

if (!this.checkpoints)
if (!this.checkpoints && !this.options.neutrino)
return;

if (!this.syncing)
Expand All @@ -2156,14 +2169,22 @@ class Pool extends EventEmitter {
return;
}

assert(this.headerChain.size > 0);
if (this.checkpoints)
assert(this.headerChain.size > 0);

let checkpoint = false;
let node = null;
let hash = null;

for (const header of headers) {
hash = header.hash();

if (this.options.neutrino) {
await this._addBlock(peer, header, chainCommon.flags.VERIFY_POW);
continue;
}

const last = this.headerChain.tail;
const hash = header.hash();
const height = last.height + 1;

if (!header.verify()) {
Expand Down Expand Up @@ -2219,7 +2240,11 @@ class Pool extends EventEmitter {
}

// Request more headers.
peer.sendGetHeaders([node.hash], this.headerTip.hash);
// if (this.options.neutrino)
// peer.sendGetHeaders([hash]);
// else
if (this.checkpoints)
peer.sendGetHeaders([hash], this.headerTip.hash);
}

/**
Expand Down Expand Up @@ -2350,7 +2375,8 @@ class Pool extends EventEmitter {

this.logStatus(block);

await this.resolveChain(peer, hash);
if (!this.options.neutrino)
await this.resolveChain(peer, hash);
}

/**
Expand Down Expand Up @@ -3549,6 +3575,9 @@ class Pool extends EventEmitter {
*/

resolveBlock(peer, hash) {
if (this.options.neutrino)
return true;

if (!peer.blockMap.has(hash))
return false;

Expand Down Expand Up @@ -3690,6 +3719,7 @@ class PoolOptions {
this.prefix = null;
this.checkpoints = true;
this.spv = false;
this.neutrino = false;
this.bip37 = false;
this.bip157 = false;
this.listen = false;
Expand Down Expand Up @@ -3772,12 +3802,19 @@ 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(typeof options.neutrino === 'boolean');
this.neutrino = options.neutrino;
assert(
!options.neutrino || !options.compact,
'Neutrino cannot use compact blocks');
}

if (options.bip37 != null) {
assert(typeof options.bip37 === 'boolean');
this.bip37 = options.bip37;
Expand Down Expand Up @@ -3953,6 +3990,12 @@ class PoolOptions {
this.listen = false;
}

if (this.neutrino) {
this.requiredServices |= common.services.NODE_COMPACT_FILTERS;
this.checkpoints = true;
this.compact = false;
}

if (this.selfish) {
this.services &= ~common.services.NETWORK;
this.bip37 = false;
Expand Down
Loading