improve spv. fix preloading.

This commit is contained in:
Christopher Jeffrey 2016-04-17 03:19:10 -07:00
parent 45115995f6
commit c8ba1aaa51
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 11 additions and 22 deletions

View File

@ -11,7 +11,7 @@ var node = bcoin.spvnode({
});
node.on('error', function(err) {
bcoin.debug(err.message);
bcoin.debug(err.stack + '');
});
node.open(function(err) {

View File

@ -336,18 +336,14 @@ Chain.prototype._preload = function _preload(callback) {
});
stream.on('error', function(err) {
var start = Math.max(0, height - 2);
self.reset(start, function(e) {
if (e)
return callback(e);
return callback(err);
});
stream.destroy();
return callback(err);
});
stream.on('data', function(data) {
var blocks = [];
var need = 80 - buf.size;
var lastEntry, block, entry, start;
var lastEntry, block, data, entry;
while (data.length >= need) {
buf.data.push(data.slice(0, need));
@ -368,11 +364,12 @@ Chain.prototype._preload = function _preload(callback) {
return;
for (i = 0; i < blocks.length; i++) {
block = blocks[i];
data = blocks[i];
try {
data = parseHeader(data);
} catch (e) {
stream.destroy();
return callback(e);
}
@ -386,13 +383,8 @@ Chain.prototype._preload = function _preload(callback) {
// Do some paranoid checks.
if (lastEntry && data.prevBlock !== lastEntry.hash) {
start = Math.max(0, height - 2);
stream.destroy();
return self.reset(start, function(err) {
if (err)
return callback(err);
return callback(new Error('Corrupt headers.'));
});
return callback(new Error('Corrupt headers.'));
}
// Create headers object for validation.
@ -401,13 +393,8 @@ Chain.prototype._preload = function _preload(callback) {
// Verify the block headers. We don't want to
// trust an external centralized source completely.
if (!block.verifyHeaders()) {
start = Math.max(0, height - 2);
stream.destroy();
return self.reset(start, function(err) {
if (err)
return callback(err);
return callback(new Error('Bad headers.'));
});
return callback(new Error('Bad headers.'));
}
// Create a chain entry.
@ -490,6 +477,9 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
if (!block.verify(ret))
return done(new VerifyError(block, 'invalid', ret.reason, ret.score));
if (this.options.spv || block.type !== 'block')
return done();
// Skip the genesis block
if (block.isGenesis())
return done(null, flags);

View File

@ -834,7 +834,6 @@ Parser.parseBlockHeaders = function parseBlockHeaders(p) {
ts: p.readU32(),
bits: p.readU32(),
nonce: p.readU32(),
totalTX: p.readVarint(),
_size: p.end()
}
};