do verification on forked blocks.
This commit is contained in:
parent
70553c884c
commit
ad95ffbfcd
@ -980,7 +980,6 @@ Chain.prototype._onFlush = function _onFlush(callback) {
|
|||||||
|
|
||||||
Chain.prototype.add = function add(initial, peer, callback, force) {
|
Chain.prototype.add = function add(initial, peer, callback, force) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var host = peer ? peer.host : 'unknown';
|
|
||||||
var total = 0;
|
var total = 0;
|
||||||
|
|
||||||
assert(!this.loading);
|
assert(!this.loading);
|
||||||
@ -994,17 +993,13 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
|
|||||||
var prevHash = block.prevBlock;
|
var prevHash = block.prevBlock;
|
||||||
var prevHeight, entry, checkpoint, prev, orphan;
|
var prevHeight, entry, checkpoint, prev, orphan;
|
||||||
|
|
||||||
// We already have this block. Do regular
|
// We already have this block.
|
||||||
// orphan resolution (won't do anything).
|
|
||||||
// NOTE: Wrap this in a nextTick to avoid
|
|
||||||
// a stack overflow if there are a lot of
|
|
||||||
// existing blocks.
|
|
||||||
if (self.db.has(hash)) {
|
if (self.db.has(hash)) {
|
||||||
self.emit('exists', block, {
|
self.emit('exists', block, {
|
||||||
height: entry.height,
|
height: entry.height,
|
||||||
hash: entry.hash
|
hash: entry.hash
|
||||||
}, peer);
|
}, peer);
|
||||||
return utils.nextTick(handleOrphans);
|
return done();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not revalidate known invalid blocks.
|
// Do not revalidate known invalid blocks.
|
||||||
@ -1012,9 +1007,10 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
|
|||||||
self.emit('invalid', block, {
|
self.emit('invalid', block, {
|
||||||
height: -1,
|
height: -1,
|
||||||
hash: hash,
|
hash: hash,
|
||||||
seen: true,
|
seen: !!self.invalid[hash],
|
||||||
chain: self.invalid[prevHash]
|
chain: !!self.invalid[prevHash]
|
||||||
}, peer);
|
}, peer);
|
||||||
|
self.invalid[hash] = true;
|
||||||
return done();
|
return done();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1133,6 +1129,24 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lookup previous entry.
|
||||||
|
// We can do this synchronously:
|
||||||
|
// it is already cached.
|
||||||
|
prev = self.db.getSync(prevHeight);
|
||||||
|
assert(prev);
|
||||||
|
|
||||||
|
// Do "contextual" verification on our block
|
||||||
|
// now that we're certain its previous
|
||||||
|
// block is in the chain.
|
||||||
|
self._verifyContext(block, prev, function(err, verified) {
|
||||||
|
if (err)
|
||||||
|
return done(err);
|
||||||
|
|
||||||
|
// Real fork resolution would just be this.
|
||||||
|
// if (entry.chainwork.cmp(self.tip.chainwork) > 0)
|
||||||
|
// return self.setBestChain(entry);
|
||||||
|
// return done();
|
||||||
|
|
||||||
// See if the entry already exists.
|
// See if the entry already exists.
|
||||||
if (self.db.has(entry.height)) {
|
if (self.db.has(entry.height)) {
|
||||||
return self.db.getAsync(entry.height, function(err, existing) {
|
return self.db.getAsync(entry.height, function(err, existing) {
|
||||||
@ -1155,12 +1169,6 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
|
|||||||
if (existing.chainwork.cmp(entry.chainwork) > 0)
|
if (existing.chainwork.cmp(entry.chainwork) > 0)
|
||||||
return done();
|
return done();
|
||||||
|
|
||||||
// NOTE: We should do contextual verification
|
|
||||||
// here if we were a fullnode that actually
|
|
||||||
// stored multiple chains, but since we backoff,
|
|
||||||
// we can ignore that until the shorter chain
|
|
||||||
// stops being propogated.
|
|
||||||
|
|
||||||
// The block has equal chainwork (an
|
// The block has equal chainwork (an
|
||||||
// alternate tip). Reset the chain, find
|
// alternate tip). Reset the chain, find
|
||||||
// a new peer, and wait to see who wins.
|
// a new peer, and wait to see who wins.
|
||||||
@ -1184,19 +1192,6 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
|
|||||||
// Add entry if we do not have it.
|
// Add entry if we do not have it.
|
||||||
assert(self.db.getHeight(entry.hash) === -1);
|
assert(self.db.getHeight(entry.hash) === -1);
|
||||||
|
|
||||||
// Lookup previous entry.
|
|
||||||
// We can do this synchronously:
|
|
||||||
// it is already cached.
|
|
||||||
prev = self.db.getSync(prevHeight);
|
|
||||||
assert(prev);
|
|
||||||
|
|
||||||
// Do "contextual" verification on our block
|
|
||||||
// now that we're certain its previous
|
|
||||||
// block is in the chain.
|
|
||||||
self._verifyContext(block, prev, function(err, verified) {
|
|
||||||
if (err)
|
|
||||||
return done(err);
|
|
||||||
|
|
||||||
if (!verified) {
|
if (!verified) {
|
||||||
self.invalid[entry.hash] = true;
|
self.invalid[entry.hash] = true;
|
||||||
self.emit('invalid', block, {
|
self.emit('invalid', block, {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user