chain.add: minor.

This commit is contained in:
Christopher Jeffrey 2016-03-30 05:14:29 -07:00
parent 9f88173881
commit ae2a5fb702

View File

@ -1001,23 +1001,37 @@ Chain.prototype.onFlush = function onFlush(callback) {
return this.locker.onFlush(callback); return this.locker.onFlush(callback);
}; };
Chain.prototype.add = function add(initial, peer, callback, force) { Chain.prototype.add = function add(block, peer, callback, force) {
var self = this; var self = this;
var total = 0; var total = 0;
var ret = {}; var ret = {};
assert(this.loaded); assert(this.loaded);
var unlock = this._lock(add, [initial, peer, callback], force); var unlock = this._lock(add, [block, peer, callback], force);
if (!unlock) if (!unlock)
return; return;
(function next(block) { (function next(block, initial) {
var hash = block.hash('hex'); var hash = block.hash('hex');
var prevHash = block.prevBlock; var prevHash = block.prevBlock;
var height, checkpoint, orphan; var height, checkpoint, orphan;
// We already have this block. // Do not revalidate known invalid blocks.
if (self.invalid[hash] || self.invalid[prevHash]) {
self.emit('invalid', block, {
height: block.getCoinbaseHeight(),
hash: hash,
seen: !!self.invalid[hash],
chain: !!self.invalid[prevHash]
}, peer);
self.invalid[hash] = true;
self.emit('verify-error',
block, 'duplicate', 'duplicate', 0, peer);
return done();
}
// Do we already have this block?
self.db.has(hash, function(err, existing) { self.db.has(hash, function(err, existing) {
if (err) if (err)
return done(err); return done(err);
@ -1037,25 +1051,11 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
height = !prev ? -1 : prev.height + 1; height = !prev ? -1 : prev.height + 1;
// Do not revalidate known invalid blocks.
if (self.invalid[hash] || self.invalid[prevHash]) {
self.emit('invalid', block, {
height: height,
hash: hash,
seen: !!self.invalid[hash],
chain: !!self.invalid[prevHash]
}, peer);
self.invalid[hash] = true;
self.emit('verify-error',
block, 'duplicate', 'duplicate', 0, peer);
return done();
}
// Validate the block we want to add. // Validate the block we want to add.
// This is only necessary for new // This is only necessary for new
// blocks coming in, not the resolving // blocks coming in, not the resolving
// orphans. // orphans.
if (block === initial && !block.verify(ret)) { if (initial && !block.verify(ret)) {
self.invalid[hash] = true; self.invalid[hash] = true;
self.emit('invalid', block, { self.emit('invalid', block, {
height: height, height: height,
@ -1083,7 +1083,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
self.purgePending(); self.purgePending();
self.emit('fork', block, { self.emit('fork', block, {
height: -1, height: block.getCoinbaseHeight(),
expected: orphan.hash('hex'), expected: orphan.hash('hex'),
received: hash, received: hash,
checkpoint: false checkpoint: false
@ -1093,7 +1093,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
} }
self.emit('orphan', block, { self.emit('orphan', block, {
height: -1, height: block.getCoinbaseHeight(),
hash: hash, hash: hash,
seen: true seen: true
}, peer); }, peer);
@ -1121,7 +1121,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
self.orphan.map[prevHash] = block; self.orphan.map[prevHash] = block;
self.orphan.bmap[hash] = block; self.orphan.bmap[hash] = block;
self.emit('orphan', block, { self.emit('orphan', block, {
height: -1, height: block.getCoinbaseHeight(),
hash: hash, hash: hash,
seen: false seen: false
}, peer); }, peer);
@ -1250,7 +1250,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
else else
self.emit('competitor', block, entry, peer); self.emit('competitor', block, entry, peer);
if (block.hash('hex') !== initial.hash('hex')) if (!initial)
self.emit('resolved', block, entry, peer); self.emit('resolved', block, entry, peer);
// No orphan chain. // No orphan chain.
@ -1269,7 +1269,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
}); });
}); });
}); });
})(initial); })(block, true);
function done(err) { function done(err) {
// Failsafe for large orphan chains. Do not // Failsafe for large orphan chains. Do not