node/net: refactor node bootstrapping.

This commit is contained in:
Christopher Jeffrey 2017-01-14 07:18:47 -08:00
parent 80d01b52c3
commit e8cc724488
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 43 additions and 17 deletions

View File

@ -4,10 +4,12 @@
process.title = 'bcoin'; process.title = 'bcoin';
var bcoin = require('../');
var assert = require('assert'); var assert = require('assert');
var bcoin = require('../');
var co = bcoin.co;
var options, node;
var options = bcoin.config({ options = bcoin.config({
config: true, config: true,
arg: true, arg: true,
env: true, env: true,
@ -19,7 +21,7 @@ var options = bcoin.config({
bcoin.set(options); bcoin.set(options);
var node = new bcoin.fullnode(options); node = new bcoin.fullnode(options);
node.on('error', function(err) { node.on('error', function(err) {
; ;
@ -31,7 +33,15 @@ process.on('uncaughtException', function(err) {
process.exit(1); process.exit(1);
}); });
node.open().then(function() { co.spawn(function *() {
node.pool.connect(); yield node.open();
if (options.listen)
yield node.listen();
yield node.connect();
node.startSync(); node.startSync();
}).catch(function(err) {
throw err;
}); });

View File

@ -4,11 +4,13 @@
process.title = 'bcoin'; process.title = 'bcoin';
var assert = require('assert');
var bcoin = require('../'); var bcoin = require('../');
var util = bcoin.util; var util = bcoin.util;
var assert = require('assert'); var co = bcoin.co;
var options, node;
var options = bcoin.config({ options = bcoin.config({
config: true, config: true,
arg: true, arg: true,
env: true, env: true,
@ -19,7 +21,7 @@ var options = bcoin.config({
bcoin.set(options); bcoin.set(options);
var node = bcoin.spvnode(options); node = bcoin.spvnode(options);
node.on('error', function(err) { node.on('error', function(err) {
; ;
@ -31,10 +33,13 @@ process.on('uncaughtException', function(err) {
process.exit(1); process.exit(1);
}); });
node.open().then(function() { co.spawn(function *() {
yield node.open();
yield node.connect();
if (process.argv.indexOf('--test') !== -1) { if (process.argv.indexOf('--test') !== -1) {
node.pool.watchAddress('1VayNert3x1KzbpzMGt2qdqrAThiRovi8'); node.pool.watchAddress('1VayNert3x1KzbpzMGt2qdqrAThiRovi8');
node.pool.watch(bcoin.outpoint().toRaw()); node.pool.watchOutpoint(new bcoin.outpoint());
node.on('block', function(block) { node.on('block', function(block) {
assert(block.txs.length >= 1); assert(block.txs.length >= 1);
if (block.txs.length > 1) if (block.txs.length > 1)
@ -43,4 +48,6 @@ node.open().then(function() {
} }
node.startSync(); node.startSync();
}).catch(function(err) {
throw err;
}); });

View File

@ -615,12 +615,15 @@ Pool.prototype.setLoader = function setLoader(peer) {
* Start the blockchain sync. * Start the blockchain sync.
*/ */
Pool.prototype.startSync = co(function* startSync() { Pool.prototype.startSync = function startSync() {
yield this.connect(); if (!this.loaded)
return;
assert(this.connected, 'Pool is not connected!');
this.syncing = true; this.syncing = true;
this.sync(); this.sync();
}); };
/** /**
* Send a sync to each peer. * Send a sync to each peer.
@ -630,6 +633,9 @@ Pool.prototype.startSync = co(function* startSync() {
Pool.prototype.sync = function sync() { Pool.prototype.sync = function sync() {
var peer; var peer;
if (!this.syncing)
return;
for (peer = this.peers.head(); peer; peer = peer.next) { for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.outbound) if (!peer.outbound)
continue; continue;
@ -645,6 +651,9 @@ Pool.prototype.sync = function sync() {
Pool.prototype.forceSync = function forceSync() { Pool.prototype.forceSync = function forceSync() {
var peer; var peer;
if (!this.syncing)
return;
for (peer = this.peers.head(); peer; peer = peer.next) { for (peer = this.peers.head(); peer; peer = peer.next) {
if (!peer.outbound) if (!peer.outbound)
continue; continue;

View File

@ -254,9 +254,6 @@ FullNode.prototype._open = co(function* open() {
// Ensure primary wallet. // Ensure primary wallet.
yield this.openWallet(); yield this.openWallet();
if (this.options.listen)
yield this.pool.listen();
this.logger.info('Node is loaded.'); this.logger.info('Node is loaded.');
}); });
@ -360,6 +357,7 @@ FullNode.prototype.listen = function listen() {
/** /**
* Connect to the network. * Connect to the network.
* @returns {Promise}
*/ */
FullNode.prototype.connect = function connect() { FullNode.prototype.connect = function connect() {

View File

@ -230,7 +230,8 @@ SPVNode.prototype.scan = co(function* scan(start, filter, iter) {
if (this.chain.height < height) { if (this.chain.height < height) {
// We need to somehow defer this. // We need to somehow defer this.
// yield this.pool.startSync(); // yield this.connect();
// this.startSync();
// yield this.watchUntil(height, iter); // yield this.watchUntil(height, iter);
} }
} finally { } finally {
@ -306,6 +307,7 @@ SPVNode.prototype.sendTX = function sendTX(tx) {
/** /**
* Connect to the network. * Connect to the network.
* @returns {Promise}
*/ */
SPVNode.prototype.connect = function connect() { SPVNode.prototype.connect = function connect() {