pool wallets. chain errors. checkpoint failure.

This commit is contained in:
Christopher Jeffrey 2015-12-18 12:10:39 -08:00
parent d109eaba6c
commit 82983af52b
3 changed files with 29 additions and 21 deletions

View File

@ -142,7 +142,7 @@ Chain.prototype._probeIndex = function _probeIndex(hash, ts) {
Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
if (this._probeIndex(hash, ts))
return;
return new Error('Already added.');
var pos = utils.binaryInsert(this.index.ts, ts, compareTs, true);
@ -150,16 +150,18 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
if (this.index.hashes[pos] === hash ||
this.index.hashes[pos - 1] === hash ||
this.index.hashes[pos + 1] === hash) {
return;
return new Error('Duplicate height.');
}
// var checkpoint = network.checkpoints[height];
// if (checkpoint) {
// this.emit('checkpoint', height, hash, checkpoint);
// if (hash !== checkpoint) {
// this.resetLastCheckpoint(height);
// }
// }
var checkpoint = network.checkpoints[height];
if (checkpoint) {
this.emit('checkpoint', height, hash, checkpoint);
if (hash !== checkpoint) {
// this.resetLastCheckpoint(height);
this.emit('fork', height, hash, checkpoint);
return new Error('Forked chain at checkpoint.');
}
}
this.index.ts.splice(pos, 0, ts);
this.index.hashes.splice(pos, 0, hash);
@ -187,6 +189,7 @@ Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) {
};
Chain.prototype.resetHeight = function resetHeight(height) {
var self = this;
var index = this.index.heights.indexOf(height);
if (index < 0)
@ -201,6 +204,9 @@ Chain.prototype.resetHeight = function resetHeight(height) {
this.index.hashes = this.index.hashes.slice(0, index + 1);
this.index.heights = this.index.heights.slice(0, index + 1);
this.index.bloom.reset();
this.index.hashes.forEach(function(hash) {
self.index.bloom.add(hash, 'hex');
});
this.index.lastTs = this.index.ts[this.index.ts.length - 1];
};
@ -298,8 +304,10 @@ Chain.prototype.add = function add(block) {
}
// Validated known block at this point - add it to index
if (prevProbe)
if (prevProbe) {
this._addIndex(hash, block.ts, prevProbe.height + 1);
block.height = prevProbe.height + 1;
}
// At least one block was added
res = true;

View File

@ -33,11 +33,11 @@ function Peer(pool, createSocket, options) {
if (this.options.backoff) {
var self = this;
setTimeout(function() {
self.socket = createSocket();
self.socket = createSocket(self);
self.emit('socket');
}, this.options.backoff);
} else {
this.socket = createSocket();
this.socket = createSocket(this);
}
this._broadcast = {

View File

@ -16,6 +16,10 @@ function Pool(options) {
EventEmitter.call(this);
this.options = options || {};
if (this.options.network)
network.set(this.options.network);
this.options.fullNode = !!this.options.fullNode;
this.options.relay == null
? (this.options.fullNode ? false : true)
@ -91,6 +95,7 @@ function Pool(options) {
};
// Added and watched wallets
this.options.wallets = this.options.wallets || [];
this.wallets = [];
this.createSocket = options.createConnection || options.createSocket;
@ -126,6 +131,10 @@ Pool.prototype._init = function _init() {
self._scheduleRequests();
self._loadRange(preload);
});
this.options.wallets.forEach(function(w) {
self.addWallet(w);
});
};
Pool.prototype._addLoader = function _addLoader() {
@ -355,15 +364,6 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
if (self.chain.index.hashes.length === len)
return;
var height = self.chain.index.heights.length[self.chain.index.heights.length - 1];
var checkpoint = network.checkpoints[height];
if (checkpoint) {
self.emit('checkpoint', block, height, checkpoint, peer);
// if (checkpoint !== hash)
// self.chain.resetLastCheckpoint(height);
}
self.emit('chain-progress', self.chain.fillPercent(), peer);
self.emit('block', block, peer);
});