diff --git a/bin/node b/bin/node index 8a96f26ec..4d8e85fd3 100755 --- a/bin/node +++ b/bin/node @@ -4,10 +4,12 @@ process.title = 'bcoin'; -var bcoin = require('../'); var assert = require('assert'); +var bcoin = require('../'); +var co = bcoin.co; +var options, node; -var options = bcoin.config({ +options = bcoin.config({ config: true, arg: true, env: true, @@ -19,7 +21,7 @@ var options = bcoin.config({ bcoin.set(options); -var node = new bcoin.fullnode(options); +node = new bcoin.fullnode(options); node.on('error', function(err) { ; @@ -31,7 +33,15 @@ process.on('uncaughtException', function(err) { process.exit(1); }); -node.open().then(function() { - node.pool.connect(); +co.spawn(function *() { + yield node.open(); + + if (options.listen) + yield node.listen(); + + yield node.connect(); + node.startSync(); +}).catch(function(err) { + throw err; }); diff --git a/bin/spvnode b/bin/spvnode index e68e3802c..793f8a17b 100755 --- a/bin/spvnode +++ b/bin/spvnode @@ -4,11 +4,13 @@ process.title = 'bcoin'; +var assert = require('assert'); var bcoin = require('../'); var util = bcoin.util; -var assert = require('assert'); +var co = bcoin.co; +var options, node; -var options = bcoin.config({ +options = bcoin.config({ config: true, arg: true, env: true, @@ -19,7 +21,7 @@ var options = bcoin.config({ bcoin.set(options); -var node = bcoin.spvnode(options); +node = bcoin.spvnode(options); node.on('error', function(err) { ; @@ -31,10 +33,13 @@ process.on('uncaughtException', function(err) { process.exit(1); }); -node.open().then(function() { +co.spawn(function *() { + yield node.open(); + yield node.connect(); + if (process.argv.indexOf('--test') !== -1) { node.pool.watchAddress('1VayNert3x1KzbpzMGt2qdqrAThiRovi8'); - node.pool.watch(bcoin.outpoint().toRaw()); + node.pool.watchOutpoint(new bcoin.outpoint()); node.on('block', function(block) { assert(block.txs.length >= 1); if (block.txs.length > 1) @@ -43,4 +48,6 @@ node.open().then(function() { } node.startSync(); +}).catch(function(err) { + throw err; }); diff --git a/lib/net/pool.js b/lib/net/pool.js index 1f038bbd4..c296199bc 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -615,12 +615,15 @@ Pool.prototype.setLoader = function setLoader(peer) { * Start the blockchain sync. */ -Pool.prototype.startSync = co(function* startSync() { - yield this.connect(); +Pool.prototype.startSync = function startSync() { + if (!this.loaded) + return; + + assert(this.connected, 'Pool is not connected!'); this.syncing = true; this.sync(); -}); +}; /** * Send a sync to each peer. @@ -630,6 +633,9 @@ Pool.prototype.startSync = co(function* startSync() { Pool.prototype.sync = function sync() { var peer; + if (!this.syncing) + return; + for (peer = this.peers.head(); peer; peer = peer.next) { if (!peer.outbound) continue; @@ -645,6 +651,9 @@ Pool.prototype.sync = function sync() { Pool.prototype.forceSync = function forceSync() { var peer; + if (!this.syncing) + return; + for (peer = this.peers.head(); peer; peer = peer.next) { if (!peer.outbound) continue; diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index 8916cc212..655accfc2 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -254,9 +254,6 @@ FullNode.prototype._open = co(function* open() { // Ensure primary wallet. yield this.openWallet(); - if (this.options.listen) - yield this.pool.listen(); - this.logger.info('Node is loaded.'); }); @@ -360,6 +357,7 @@ FullNode.prototype.listen = function listen() { /** * Connect to the network. + * @returns {Promise} */ FullNode.prototype.connect = function connect() { diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 4d374eaf2..59e53acfe 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -230,7 +230,8 @@ SPVNode.prototype.scan = co(function* scan(start, filter, iter) { if (this.chain.height < height) { // We need to somehow defer this. - // yield this.pool.startSync(); + // yield this.connect(); + // this.startSync(); // yield this.watchUntil(height, iter); } } finally { @@ -306,6 +307,7 @@ SPVNode.prototype.sendTX = function sendTX(tx) { /** * Connect to the network. + * @returns {Promise} */ SPVNode.prototype.connect = function connect() {