Skip to content

Commit

Permalink
Merge pull request #18 from braydonf/optional-sync
Browse files Browse the repository at this point in the history
Enable optional syncing.
  • Loading branch information
pnagurny committed Jul 13, 2015
2 parents 63b316c + 6412116 commit e56948a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function P2P(options) {
this.maxBlocks = options.maxBlocks || P2P.MAX_BLOCKS;
this.staleTipAge = options.staleTipAge || P2P.STALE_TIP_AGE;
this.syncTimeout = options.syncTimeout || P2P.SYNC_TIMEOUT;
this.disableSync = options.disableSync || false;
this.ready = false;
this.synced = false;
this.ignoreTransactions = true; // Ignore p2p transactions while syncing
Expand Down Expand Up @@ -76,6 +77,12 @@ P2P.prototype.initialize = function() {
this.emit('initialized');
};

P2P.prototype.startSync = function() {
log.info('Starting P2P Sync');
this.disableSync = false;
this._sync();
};

P2P.prototype.sendMessage = function(message) {
this.pool.sendMessage(message);
};
Expand Down Expand Up @@ -192,7 +199,7 @@ P2P.prototype._onChainQueueProcessed = function() {
if(!this.synced) {
this._sync();
}
}
};

P2P.prototype._onMempoolTransaction = function(transaction) {
var message = this.messages.Inventory.forTransaction(transaction.hash);
Expand All @@ -211,6 +218,10 @@ P2P.prototype._hashToBuffer = function(hash) {
P2P.prototype._sync = function(peer) {
var self = this;

if (this.disableSync) {
return false;
}

var tipHash = self.chain.tip.hash;

if((self.chain.tip.timestamp.getTime() > (Date.now() - self.staleTipAge)) && !self.synced) {
Expand Down
19 changes: 17 additions & 2 deletions test/p2p.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ describe('P2P', function() {
p2p.chain.lastSavedMetadataThreshold.should.equal(0);
done();
}
}

};
p2p.emit('synced');
});
});
Expand Down Expand Up @@ -124,6 +123,17 @@ describe('P2P', function() {
});
});

describe('#startSync', function() {
it('will set disableSync to false and call _sync()', function() {
var p2p = new P2P();
p2p.disableSync = true;
p2p._sync = sinon.stub();
p2p.startSync();
p2p._sync.callCount.should.equal(1);
p2p.disableSync.should.equal(false);
});
});

describe('#sendMessage', function() {
it('should call pool.sendMessage', function() {
var p2p = new P2P();
Expand Down Expand Up @@ -586,6 +596,11 @@ describe('P2P', function() {
});

describe('#_sync', function() {
it('will immediatly return false if sync is disabled', function() {
var p2p = new P2P();
p2p.disableSync = true;
p2p._sync().should.equal(false);
});
it('should create and send a getblocks message using the hashes from the chain', function(done) {
var p2p = new P2P();
p2p.chain = {
Expand Down

0 comments on commit e56948a

Please sign in to comment.