Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Chain.prototype._updateTip = function(block, callback) {

self.tip = block;
self.tipWeight = weight;
log.debug('Saving metadata');
self.saveMetadata();
log.debug('Chain added block to main chain');
self.emit('addblock', block);
Expand Down
15 changes: 15 additions & 0 deletions lib/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var _ = bitcore.deps._;
var BufferReader = bitcore.encoding.BufferReader;

function P2P(options) {
var self = this;

if(!options) {
options = {};
}
Expand All @@ -35,6 +37,11 @@ function P2P(options) {
this.lastBlockReceived = 0;
this.numBlocksDownloading = 0;
this.synced = false;

this.on('synced', function() {
self.chain.lastSavedMetadataThreshold = 0;
self.chain.saveMetadata();
});
}

util.inherits(P2P, EventEmitter);
Expand All @@ -49,6 +56,8 @@ P2P.prototype.initialize = function() {
var self = this;
this.mempool = this.db.mempool;

this.chain.lastSavedMetadataThreshold = 30000; // While syncing, only save metadata every 30 seconds

this.chain.on('addblock', this._onChainAddBlock.bind(this));
this.mempool.on('transaction', this._onMempoolTransaction.bind(this));
this.pool.on('peerready', this._onPeerReady.bind(this));
Expand Down Expand Up @@ -205,6 +214,12 @@ P2P.prototype._hashToBuffer = function(hash) {
P2P.prototype._sync = function(forceSync) {
var self = this;

if((self.chain.tip.timestamp.getTime() > (Date.now() - self.staleTipAge)) && !self.synced) {
// Just emit synced immediately if the block is reasonably new
self.synced = true;
self.emit('synced');
}

// If forceSync, sync
// If chain does not have anything in its queue and
// chain tip is older than STALE_TIP_AGE and
Expand Down
30 changes: 24 additions & 6 deletions test/p2p.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ describe('P2P', function() {
should.exist(p2p.pool);
p2p.ready.should.equal(false);
});

it('should save metadata when synced', function(done) {
var p2p = new P2P();
p2p.chain = {
saveMetadata: function() {
p2p.chain.lastSavedMetadataThreshold.should.equal(0);
done();
}
}

p2p.emit('synced');
});
});

describe('#initialize', function() {
Expand Down Expand Up @@ -489,7 +501,8 @@ describe('P2P', function() {
sendMessage: sinon.spy()
};
p2p.chain = {
blockQueue: []
blockQueue: [],
saveMetadata: sinon.spy()
}

p2p._onChainAddBlock('block');
Expand Down Expand Up @@ -549,7 +562,8 @@ describe('P2P', function() {
timestamp: new Date('2015-04-07')
},
blockQueue: [],
getHashes: sinon.stub().callsArgWith(1, null, ['hash1', 'hash2'])
getHashes: sinon.stub().callsArgWith(1, null, ['hash1', 'hash2']),
saveMetadata: sinon.spy()
};
p2p._buildGetBlocksMessage = sinon.spy();
var peer = {
Expand Down Expand Up @@ -591,7 +605,8 @@ describe('P2P', function() {
timestamp: new Date()
},
blockQueue: [],
getHashes: sinon.spy()
getHashes: sinon.spy(),
saveMetadata: sinon.spy()
};
p2p._sync();
p2p.chain.getHashes.called.should.equal(false);
Expand All @@ -604,7 +619,8 @@ describe('P2P', function() {
timestamp: new Date()
},
blockQueue: [],
getHashes: sinon.spy()
getHashes: sinon.spy(),
saveMetadata: sinon.spy()
};
p2p._sync();
p2p.chain.getHashes.called.should.equal(false);
Expand All @@ -617,7 +633,8 @@ describe('P2P', function() {
timestamp: new Date()
},
blockQueue: [],
getHashes: sinon.stub().callsArgWith(1, null, ['hash1', 'hash2'])
getHashes: sinon.stub().callsArgWith(1, null, ['hash1', 'hash2']),
saveMetadata: sinon.spy()
};
p2p._buildGetBlocksMessage = sinon.spy();
var peer = {
Expand All @@ -640,7 +657,8 @@ describe('P2P', function() {
timestamp: new Date('2015-04-07')
},
blockQueue: [],
getHashes: sinon.stub().callsArgWith(1, null, ['hash1', 'hash2'])
getHashes: sinon.stub().callsArgWith(1, null, ['hash1', 'hash2']),
saveMetadata: sinon.spy()
};
p2p._buildGetBlocksMessage = sinon.spy();
p2p._getRandomPeer = sinon.stub().returns(null);
Expand Down