Skip to content

Commit

Permalink
node/net: refactor node bootstrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jan 14, 2017
1 parent 80d01b5 commit e8cc724
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
20 changes: 15 additions & 5 deletions bin/node
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
;
Expand All @@ -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;
});
17 changes: 12 additions & 5 deletions bin/spvnode
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
;
Expand All @@ -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)
Expand All @@ -43,4 +48,6 @@ node.open().then(function() {
}

node.startSync();
}).catch(function(err) {
throw err;
});
15 changes: 12 additions & 3 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});

Expand Down Expand Up @@ -360,6 +357,7 @@ FullNode.prototype.listen = function listen() {

/**
* Connect to the network.
* @returns {Promise}
*/

FullNode.prototype.connect = function connect() {
Expand Down
4 changes: 3 additions & 1 deletion lib/node/spvnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -306,6 +307,7 @@ SPVNode.prototype.sendTX = function sendTX(tx) {

/**
* Connect to the network.
* @returns {Promise}
*/

SPVNode.prototype.connect = function connect() {
Expand Down

0 comments on commit e8cc724

Please sign in to comment.