preload: script to update
This commit is contained in:
parent
a6be7bf5f7
commit
2a4553f6c5
@ -29,6 +29,7 @@ function Chain(options) {
|
|||||||
};
|
};
|
||||||
this.index = {
|
this.index = {
|
||||||
bloom: null,
|
bloom: null,
|
||||||
|
initialSize: 0,
|
||||||
hashes: [],
|
hashes: [],
|
||||||
ts: []
|
ts: []
|
||||||
};
|
};
|
||||||
@ -226,11 +227,11 @@ Chain.prototype.toJSON = function toJSON() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var first = {
|
var first = {
|
||||||
hashes: [],
|
hashes: this.index.hashes.slice(0, this.index.initialSize - 1000),
|
||||||
ts: []
|
ts: this.index.ts.slice(0, this.index.initialSize - 1000)
|
||||||
};
|
};
|
||||||
var len = (this.index.hashes.length - 1000) - this.index.hashes.length % 50;
|
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.hashes.push(this.index.hashes[i]);
|
||||||
first.ts.push(this.index.ts[i]);
|
first.ts.push(this.index.ts[i]);
|
||||||
}
|
}
|
||||||
@ -246,6 +247,7 @@ Chain.prototype.toJSON = function toJSON() {
|
|||||||
Chain.prototype.fromJSON = function fromJSON(json) {
|
Chain.prototype.fromJSON = function fromJSON(json) {
|
||||||
assert.equal(json.v, 1);
|
assert.equal(json.v, 1);
|
||||||
assert.equal(json.type, 'chain');
|
assert.equal(json.type, 'chain');
|
||||||
|
this.index.initialSize = json.hashes.length;
|
||||||
this.index.hashes = json.hashes.slice();
|
this.index.hashes = json.hashes.slice();
|
||||||
this.index.ts = json.ts.slice();
|
this.index.ts = json.ts.slice();
|
||||||
if (this.index.bloom)
|
if (this.index.bloom)
|
||||||
|
|||||||
@ -161,6 +161,9 @@ Peer.prototype.destroy = function destroy() {
|
|||||||
|
|
||||||
clearInterval(this._ping.timer);
|
clearInterval(this._ping.timer);
|
||||||
this._ping.timer = null;
|
this._ping.timer = null;
|
||||||
|
|
||||||
|
for (var i = 0; i < this._request.queue.length; i++)
|
||||||
|
clearTimeout(this._request.queue[i].timer);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Private APIs
|
// Private APIs
|
||||||
|
|||||||
@ -13,6 +13,7 @@ function Pool(options) {
|
|||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
this.options = options || {};
|
this.options = options || {};
|
||||||
|
this.destroyed = false;
|
||||||
this.size = options.size || 32;
|
this.size = options.size || 32;
|
||||||
this.parallel = options.parallel || 2000;
|
this.parallel = options.parallel || 2000;
|
||||||
this.redundancy = 2;
|
this.redundancy = 2;
|
||||||
@ -87,6 +88,8 @@ Pool.prototype._init = function _init() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype._addLoader = function _addLoader() {
|
Pool.prototype._addLoader = function _addLoader() {
|
||||||
|
if (this.destroyed)
|
||||||
|
return;
|
||||||
if (this.peers.load !== null)
|
if (this.peers.load !== null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -104,6 +107,8 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
self._removePeer(peer);
|
self._removePeer(peer);
|
||||||
|
if (self.destroyed)
|
||||||
|
return;
|
||||||
self._addLoader();
|
self._addLoader();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -122,6 +127,7 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
if (self.block.lastHash === null && self.chain.isFull()) {
|
if (self.block.lastHash === null && self.chain.isFull()) {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
self._removePeer(peer);
|
self._removePeer(peer);
|
||||||
|
self.emit('full');
|
||||||
}
|
}
|
||||||
|
|
||||||
peer.destroy();
|
peer.destroy();
|
||||||
@ -190,11 +196,14 @@ Pool.prototype._load = function _load() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype._addPeer = function _addPeer() {
|
Pool.prototype._addPeer = function _addPeer() {
|
||||||
|
if (this.destroyed)
|
||||||
|
return;
|
||||||
if (this.peers.block.length + this.peers.pending.length >= this.size)
|
if (this.peers.block.length + this.peers.pending.length >= this.size)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var socket = this.createConnection();
|
var socket = this.createConnection();
|
||||||
var peer = bcoin.peer(this, socket, this.options.peer);
|
var peer = bcoin.peer(this, socket, this.options.peer);
|
||||||
|
|
||||||
this.peers.pending.push(peer);
|
this.peers.pending.push(peer);
|
||||||
|
|
||||||
peer._retry = 0;
|
peer._retry = 0;
|
||||||
@ -207,10 +216,15 @@ Pool.prototype._addPeer = function _addPeer() {
|
|||||||
|
|
||||||
peer.once('close', function() {
|
peer.once('close', function() {
|
||||||
self._removePeer(peer);
|
self._removePeer(peer);
|
||||||
|
if (self.destroyed)
|
||||||
|
return;
|
||||||
self._addPeer();
|
self._addPeer();
|
||||||
});
|
});
|
||||||
|
|
||||||
peer.once('ack', function() {
|
peer.once('ack', function() {
|
||||||
|
if (self.destroyed)
|
||||||
|
return;
|
||||||
|
|
||||||
var i = self.peers.pending.indexOf(peer);
|
var i = self.peers.pending.indexOf(peer);
|
||||||
if (i !== -1) {
|
if (i !== -1) {
|
||||||
self.peers.pending.splice(i, 1);
|
self.peers.pending.splice(i, 1);
|
||||||
@ -229,6 +243,7 @@ Pool.prototype._addPeer = function _addPeer() {
|
|||||||
peer.on('merkleblock', function(block) {
|
peer.on('merkleblock', function(block) {
|
||||||
self.chain.add(block);
|
self.chain.add(block);
|
||||||
self._response(block);
|
self._response(block);
|
||||||
|
self.emit('block', block);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Just FYI
|
// Just FYI
|
||||||
@ -532,6 +547,25 @@ Pool.prototype.sendTX = function sendTX(tx) {
|
|||||||
return e;
|
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() {
|
Pool.prototype.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
v: 1,
|
v: 1,
|
||||||
@ -561,7 +595,10 @@ function LoadRequest(pool, type, hash, cb) {
|
|||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
this.onclose = function onclose() {
|
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
51
scripts/update.js
Normal 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();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user