moving towards removing memory leak
This commit is contained in:
parent
6cf224b0a7
commit
1986cb40ef
@ -140,10 +140,10 @@ BlockChain.prototype.hasData = function(hash) {
|
|||||||
|
|
||||||
BlockChain.prototype.prune = function() {
|
BlockChain.prototype.prune = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
_.each(this.prev, function(key, value) {
|
_.each(this.prev, function(key) {
|
||||||
if (!self.height[key]) {
|
if (!self.height[key]) {
|
||||||
delete this.prev[key];
|
delete self.prev[key];
|
||||||
delete this.work[key];
|
delete self.work[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -78,6 +78,7 @@ NetworkMonitor.prototype.requestBlocks = function(locator) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
NetworkMonitor.prototype.start = function() {
|
NetworkMonitor.prototype.start = function() {
|
||||||
|
console.log('starting network monitor');
|
||||||
this.peer.connect();
|
this.peer.connect();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
14
lib/node.js
14
lib/node.js
@ -79,15 +79,22 @@ BitcoreNode.prototype.initialize = function() {
|
|||||||
var prevHeight = 0;
|
var prevHeight = 0;
|
||||||
var statTimer = 5 * 1000;
|
var statTimer = 5 * 1000;
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
|
console.log(process.memoryUsage().heapTotal / 1024 / 1024, 'mb used');
|
||||||
if (!self.blockchain) {
|
if (!self.blockchain) {
|
||||||
// not ready yet
|
// not ready yet
|
||||||
|
console.log('No blockchain yet');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var tipHash = self.blockchain.tip;
|
var tipHash = self.blockchain.tip;
|
||||||
var block = self.blockCache[tipHash];
|
var block = self.blockCache[tipHash];
|
||||||
|
if (_.isUndefined(block)) {
|
||||||
|
console.log('No blocks yet');
|
||||||
|
return;
|
||||||
|
}
|
||||||
var delta = block.height - prevHeight;
|
var delta = block.height - prevHeight;
|
||||||
prevHeight = block.height;
|
prevHeight = block.height;
|
||||||
console.log(block.id, block.height, 'vel', delta * 1000 / statTimer, 'b/s');
|
console.log(block.id, block.height, 'vel', delta * 1000 / statTimer, 'b/s');
|
||||||
|
console.log('cache', Object.keys(self.blockCache).length);
|
||||||
}, statTimer);
|
}, statTimer);
|
||||||
|
|
||||||
this.bus.register(bitcore.Block, function(block) {
|
this.bus.register(bitcore.Block, function(block) {
|
||||||
@ -149,7 +156,9 @@ BitcoreNode.prototype.start = function() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
var genesis = bitcore.Block.fromBuffer(genesisBlocks[bitcore.Networks.defaultNetwork.name]);
|
var genesis = bitcore.Block.fromBuffer(genesisBlocks[bitcore.Networks.defaultNetwork.name]);
|
||||||
|
|
||||||
|
console.log('getting blockchain');
|
||||||
this.blockService.getBlockchain().then(function(blockchain) {
|
this.blockService.getBlockchain().then(function(blockchain) {
|
||||||
|
console.log('got blockchain', _.isUndefined(blockchain));
|
||||||
if (!blockchain) {
|
if (!blockchain) {
|
||||||
self.blockchain = new BlockChain();
|
self.blockchain = new BlockChain();
|
||||||
self.bus.process(genesis);
|
self.bus.process(genesis);
|
||||||
@ -159,9 +168,6 @@ BitcoreNode.prototype.start = function() {
|
|||||||
self.sync();
|
self.sync();
|
||||||
self.networkMonitor.start();
|
self.networkMonitor.start();
|
||||||
});
|
});
|
||||||
this.networkMonitor.on('stop', function() {
|
|
||||||
self.blockService.saveBlockchain(self.blockchain);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BitcoreNode.prototype.stop = function(reason) {
|
BitcoreNode.prototype.stop = function(reason) {
|
||||||
@ -170,7 +176,7 @@ BitcoreNode.prototype.stop = function(reason) {
|
|||||||
|
|
||||||
BitcoreNode.prototype.requestFromTip = function() {
|
BitcoreNode.prototype.requestFromTip = function() {
|
||||||
var locator = this.blockchain.getBlockLocator();
|
var locator = this.blockchain.getBlockLocator();
|
||||||
console.log('requesting blocks, locator size:', locator.length);
|
//console.log('requesting blocks, locator size:', locator.length);
|
||||||
this.networkMonitor.requestBlocks(locator);
|
this.networkMonitor.requestBlocks(locator);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -182,27 +182,26 @@ BlockService.prototype.getBlockByHeight = function(height) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BlockService.prototype._getLatestHash = function() {
|
||||||
|
var self = this;
|
||||||
|
return Promise.try(function() {
|
||||||
|
return self.database.getAsync(Index.tip);
|
||||||
|
}).catch(LevelUp.errors.NotFoundError, function() {
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* Fetch the block that is currently the tip of the blockchain
|
* Fetch the block that is currently the tip of the blockchain
|
||||||
*
|
*
|
||||||
* @return {Promise<Block>}
|
* @return {Promise<Block>}
|
||||||
*/
|
*/
|
||||||
BlockService.prototype.getLatest = function() {
|
BlockService.prototype.getLatest = function() {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
return this._getLatestHash().then(function(blockHash) {
|
||||||
return Promise.try(function() {
|
if (!blockHash) {
|
||||||
|
return null;
|
||||||
return self.database.getAsync(Index.tip);
|
}
|
||||||
|
|
||||||
}).then(function(blockHash) {
|
|
||||||
|
|
||||||
return self.getBlock(blockHash);
|
return self.getBlock(blockHash);
|
||||||
|
|
||||||
}).catch(LevelUp.errors.NotFoundError, function() {
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -444,42 +443,48 @@ BlockService.prototype.getBlockchain = function() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var blockchain = new BlockChain();
|
var blockchain = new BlockChain();
|
||||||
|
console.log(process.memoryUsage().heapTotal / 1024 / 1024, 'mb used');
|
||||||
|
|
||||||
|
var amount = 0;
|
||||||
|
var limit = 100;
|
||||||
var fetchBlock = function(blockHash) {
|
var fetchBlock = function(blockHash) {
|
||||||
return Promise.all([
|
//console.log(process.memoryUsage().heapTotal / 1024 / 1024);
|
||||||
self.database.getAsync(Index.getPreviousBlock(blockHash)).then(function(prevHash) {
|
if (blockHash === BlockChain.NULL) {
|
||||||
|
console.log(process.memoryUsage().heapTotal / 1024 / 1024, 'mb used');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
amount += 1;
|
||||||
|
var prevKey = Index.getPreviousBlock(blockHash);
|
||||||
|
var heightKey = Index.getBlockHeight(blockHash);
|
||||||
|
var workKey = Index.getBlockWork(blockHash);
|
||||||
|
|
||||||
|
return self.database.getAsync(prevKey)
|
||||||
|
.then(function(prevHash) {
|
||||||
blockchain.prev[blockHash] = prevHash;
|
blockchain.prev[blockHash] = prevHash;
|
||||||
blockchain.next[prevHash] = blockHash;
|
blockchain.next[prevHash] = blockHash;
|
||||||
}),
|
|
||||||
self.database.getAsync(Index.getBlockHeight(blockHash)).then(function(height) {
|
|
||||||
blockchain.height[blockHash] = +height;
|
|
||||||
blockchain.hashByHeight[height] = blockHash;
|
|
||||||
}),
|
|
||||||
self.database.getAsync(Index.getBlockWork(blockHash)).then(function(work) {
|
|
||||||
blockchain.work[blockHash] = work;
|
|
||||||
})
|
})
|
||||||
]).then(function() {
|
.then(function() {
|
||||||
return blockHash;
|
return self.database.getAsync(heightKey)
|
||||||
});
|
.then(function(height) {
|
||||||
|
blockchain.height[blockHash] = +height;
|
||||||
|
blockchain.hashByHeight[height] = blockHash;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(function() {
|
||||||
|
if (amount >= limit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return self.database.getAsync(workKey)
|
||||||
|
.then(function(work) {
|
||||||
|
blockchain.work[blockHash] = work;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(function() {
|
||||||
|
return fetchBlock(blockchain.prev[blockHash]);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var fetchUnlessGenesis = function(blockHash) {
|
return self._getLatestHash()
|
||||||
return fetchBlock(blockHash).then(function() {
|
|
||||||
if (blockchain.prev[blockHash] === BlockChain.NULL) {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
return fetchUnlessGenesis(blockchain.prev[blockHash]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return self.database.getAsync(Index.tip)
|
|
||||||
.catch(function(err) {
|
|
||||||
if (err.notFound) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
})
|
|
||||||
.then(function(tip) {
|
.then(function(tip) {
|
||||||
if (!tip) {
|
if (!tip) {
|
||||||
console.log('No tip found');
|
console.log('No tip found');
|
||||||
@ -487,9 +492,10 @@ BlockService.prototype.getBlockchain = function() {
|
|||||||
}
|
}
|
||||||
console.log('Tip is', tip);
|
console.log('Tip is', tip);
|
||||||
blockchain.tip = tip;
|
blockchain.tip = tip;
|
||||||
return fetchUnlessGenesis(tip).then(function() {
|
return fetchBlock(tip)
|
||||||
return blockchain;
|
.then(function() {
|
||||||
});
|
return blockchain;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user