Skip to content

Commit

Permalink
fix(Blockchain): sync chain when we receive new blocks in 'inv' messages
Browse files Browse the repository at this point in the history
This got broken somehow. Now chains should keep emitting 'block' events after the initial sync.
  • Loading branch information
mappum committed Jan 5, 2016
1 parent 60e5a79 commit 1ff6fba
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Blockchain.prototype.close = function (cb) {

Blockchain.prototype.sync = function (opts, cb) {
var self = this
cb = cb || noop
if (this.syncing) return cb(null)
if (typeof opts === 'function') {
cb = opts
Expand All @@ -113,6 +114,7 @@ Blockchain.prototype.sync = function (opts, cb) {

this.syncing = true
if (opts.to || this.to) this.syncHeight = opts.to || this.to
if (opts.peer) this._setDownloadPeer(opts.peer)

var syncDone = function () {
var done = false
Expand Down Expand Up @@ -321,6 +323,7 @@ Blockchain.prototype._setDownloadPeer = function (peer) {
}

this.downloadPeer = peer
this.syncing = !!peer
if (peer) {
peer.on('headers', this._onHeaders)
peer.on('disconnect', this._onDownloadPeerDisconnect)
Expand All @@ -342,7 +345,7 @@ Blockchain.prototype._getHeaders = function (opts) {

opts = opts || {}
this.getLocator(opts.from || this.tip.hash, function (err, locator) {
if (err) throw err
if (err) return self._error(err)
if (!self.downloadPeer) return
var message = self.downloadPeer.messages.GetHeaders({
starts: locator
Expand Down Expand Up @@ -378,7 +381,7 @@ Blockchain.prototype._listenForUpdates = function () {
item.type === Inventory.TYPE.FILTERED_BLOCK) {
if (self.lastInv && self.lastInv.compare(item.hash) === 0) return
self.lastInv = item.hash
self._getHeaders()
self.sync({ peer: peer })
}
})
})
Expand Down

0 comments on commit 1ff6fba

Please sign in to comment.