preload: script to update

This commit is contained in:
Fedor Indutny 2014-05-06 22:08:38 +04:00
parent a6be7bf5f7
commit 2a4553f6c5
5 changed files with 1452 additions and 1349 deletions

View File

@ -29,6 +29,7 @@ function Chain(options) {
};
this.index = {
bloom: null,
initialSize: 0,
hashes: [],
ts: []
};
@ -226,11 +227,11 @@ Chain.prototype.toJSON = function toJSON() {
};
var first = {
hashes: [],
ts: []
hashes: this.index.hashes.slice(0, this.index.initialSize - 1000),
ts: this.index.ts.slice(0, this.index.initialSize - 1000)
};
var len = (this.index.hashes.length - 1000) - this.index.hashes.length % 50;
for (var i = 0; i < len; i += 50) {
for (var i = this.index.initialSize - 1000; i < len; i += 50) {
first.hashes.push(this.index.hashes[i]);
first.ts.push(this.index.ts[i]);
}
@ -246,6 +247,7 @@ Chain.prototype.toJSON = function toJSON() {
Chain.prototype.fromJSON = function fromJSON(json) {
assert.equal(json.v, 1);
assert.equal(json.type, 'chain');
this.index.initialSize = json.hashes.length;
this.index.hashes = json.hashes.slice();
this.index.ts = json.ts.slice();
if (this.index.bloom)

View File

@ -161,6 +161,9 @@ Peer.prototype.destroy = function destroy() {
clearInterval(this._ping.timer);
this._ping.timer = null;
for (var i = 0; i < this._request.queue.length; i++)
clearTimeout(this._request.queue[i].timer);
};
// Private APIs

View File

@ -13,6 +13,7 @@ function Pool(options) {
EventEmitter.call(this);
this.options = options || {};
this.destroyed = false;
this.size = options.size || 32;
this.parallel = options.parallel || 2000;
this.redundancy = 2;
@ -87,6 +88,8 @@ Pool.prototype._init = function _init() {
};
Pool.prototype._addLoader = function _addLoader() {
if (this.destroyed)
return;
if (this.peers.load !== null)
return;
@ -104,6 +107,8 @@ Pool.prototype._addLoader = function _addLoader() {
clearTimeout(timer);
clearInterval(interval);
self._removePeer(peer);
if (self.destroyed)
return;
self._addLoader();
};
@ -122,6 +127,7 @@ Pool.prototype._addLoader = function _addLoader() {
if (self.block.lastHash === null && self.chain.isFull()) {
clearTimeout(timer);
self._removePeer(peer);
self.emit('full');
}
peer.destroy();
@ -190,11 +196,14 @@ Pool.prototype._load = function _load() {
};
Pool.prototype._addPeer = function _addPeer() {
if (this.destroyed)
return;
if (this.peers.block.length + this.peers.pending.length >= this.size)
return;
var socket = this.createConnection();
var peer = bcoin.peer(this, socket, this.options.peer);
this.peers.pending.push(peer);
peer._retry = 0;
@ -207,10 +216,15 @@ Pool.prototype._addPeer = function _addPeer() {
peer.once('close', function() {
self._removePeer(peer);
if (self.destroyed)
return;
self._addPeer();
});
peer.once('ack', function() {
if (self.destroyed)
return;
var i = self.peers.pending.indexOf(peer);
if (i !== -1) {
self.peers.pending.splice(i, 1);
@ -229,6 +243,7 @@ Pool.prototype._addPeer = function _addPeer() {
peer.on('merkleblock', function(block) {
self.chain.add(block);
self._response(block);
self.emit('block', block);
});
// Just FYI
@ -532,6 +547,25 @@ Pool.prototype.sendTX = function sendTX(tx) {
return e;
};
Pool.prototype.destroy = function destroy() {
if (this.destroyed)
return;
this.destroyed = true;
if (this.peers.load)
this.peers.load.destroy();
this.peers.pending.slice().forEach(function(peer) {
peer.destroy();
});
this.peers.block.slice().forEach(function(peer) {
peer.destroy();
});
this.request.queue.slice().forEach(function(item) {
item.finish(null);
});
};
Pool.prototype.toJSON = function toJSON() {
return {
v: 1,
@ -561,7 +595,10 @@ function LoadRequest(pool, type, hash, cb) {
var self = this;
this.onclose = function onclose() {
self.retry();
if (self.pool.destroyed)
self.clear();
else
self.retry();
};
}

File diff suppressed because it is too large Load Diff

51
scripts/update.js Normal file
View File

@ -0,0 +1,51 @@
// Update preloader
var assert = require('assert');
var dns = require('dns');
var net = require('net');
var path = require('path');
var bcoin = require('../');
var addrs = [
'seed.bitcoin.sipa.be',
'dnsseed.bluematt.me',
'dnsseed.bitcoin.dashjr.org',
'seed.bitcoinstats.com',
'seed.bitnodes.io',
'bitseed.xf2.org'
];
var pool = bcoin.pool({
count: 16,
createConnection: function() {
return net.connect(8333, addrs[(Math.random() * addrs.length) | 0]);
}
});
console.log('Updating bcoin preloaded chain...');
var last = 0;
pool.on('block', function(block) {
if (block.ts <= last)
return;
console.log('Got: ' + block.hash('hex') + ' ' + new Date(block.ts * 1000));
});
pool.once('full', finish);
process.once('SIGINT', finish);
var once = false;
function finish() {
if (once)
return;
once = true;
console.log('Done...');
var chain = '// Autogenerated, use scripts/update.js to update\n' +
'module.exports = ' +
JSON.stringify(pool.chain.toJSON(), null, 2) + '\n';
var file =
path.resolve(__dirname, '..', 'lib', 'bcoin', 'protocol', 'preload.js');
require('fs').writeFileSync(file, chain);
pool.destroy();
}