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';
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;
});

View File

@ -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;
});

View File

@ -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;

View File

@ -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() {

View File

@ -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() {