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);
};
Chain.prototype.add = function add(initial, peer, callback, force) {
Chain.prototype.add = function add(block, peer, callback, force) {
var self = this;
var total = 0;
var ret = {};
assert(this.loaded);
var unlock = this._lock(add, [initial, peer, callback], force);
var unlock = this._lock(add, [block, peer, callback], force);
if (!unlock)
return;
(function next(block) {
(function next(block, initial) {
var hash = block.hash('hex');
var prevHash = block.prevBlock;
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) {
if (err)
return done(err);
@ -1037,25 +1051,11 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
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.
// This is only necessary for new
// blocks coming in, not the resolving
// orphans.
if (block === initial && !block.verify(ret)) {
if (initial && !block.verify(ret)) {
self.invalid[hash] = true;
self.emit('invalid', block, {
height: height,
@ -1083,7 +1083,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
self.purgePending();
self.emit('fork', block, {
height: -1,
height: block.getCoinbaseHeight(),
expected: orphan.hash('hex'),
received: hash,
checkpoint: false
@ -1093,7 +1093,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
}
self.emit('orphan', block, {
height: -1,
height: block.getCoinbaseHeight(),
hash: hash,
seen: true
}, peer);
@ -1121,7 +1121,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
self.orphan.map[prevHash] = block;
self.orphan.bmap[hash] = block;
self.emit('orphan', block, {
height: -1,
height: block.getCoinbaseHeight(),
hash: hash,
seen: false
}, peer);
@ -1250,7 +1250,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
else
self.emit('competitor', block, entry, peer);
if (block.hash('hex') !== initial.hash('hex'))
if (!initial)
self.emit('resolved', block, entry, peer);
// No orphan chain.
@ -1269,7 +1269,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
});
});
});
})(initial);
})(block, true);
function done(err) {
// Failsafe for large orphan chains. Do not