catching the synced state better

simpler connect handling
This commit is contained in:
Justin Langston 2017-08-09 13:24:24 -04:00
parent 6ad7d35963
commit ccf51ec337
No known key found for this signature in database
GPG Key ID: EBB3714C72F9FE5D
2 changed files with 16 additions and 18 deletions

View File

@ -10,7 +10,7 @@ var Bcoin = function(options) {
this.emitter = new EE();
};
Bcoin.prototype.start = function(callback) {
Bcoin.prototype.start = function(done) {
var self = this;
self._bcoin = bcoin.fullnode(self._config);
@ -18,11 +18,14 @@ Bcoin.prototype.start = function(callback) {
self._bcoin.open().then(function() {
self._bcoin.connect().then(function() {
self._bcoin.pool.once('full', function() {
callback();
});
self._bcoin.startSync();
log.info('Waiting for Bcoin to sync');
self._bcoin.startSync();
if (self._bcoin.chain.synced){
return done();
}
self._bcoin.chain.once('full', function() {
done();
});
});
});
};

View File

@ -171,17 +171,14 @@ P2P.prototype._broadcast = function(subscribers, name, entity) {
P2P.prototype._connect = function() {
var self = this;
self._connectCalled = self._connectCalled > 0 ? 2 : 1;
if(self._connectCalled > 1 || !self._bcoin) {
log.info('Connecting to p2p network.');
log.info('Connecting to p2p network.');
self._pool.connect();
var retryInterval = setInterval(function() {
self._pool.connect();
var retryInterval = setInterval(function() {
self._pool.connect();
}, 5000);
self._pool.once('peerready', function() {
clearInterval(retryInterval);
});
}
}, 5000);
self._pool.once('peerready', function() {
clearInterval(retryInterval);
});
};
P2P.prototype._getBestHeight = function() {
@ -315,9 +312,7 @@ P2P.prototype._setListeners = function() {
self._pool.on('peertx', self._onPeerTx.bind(self));
self._pool.on('peerblock', self._onPeerBlock.bind(self));
self._pool.on('peerheaders', self._onPeerHeaders.bind(self));
if (!self._bcoin) {
self.node.on('ready', self._connect.bind(self));
}
self.node.on('ready', self._connect.bind(self));
};
P2P.prototype._setResourceFilter = function(filter, resource) {