improve spv. fix preloading.
This commit is contained in:
parent
45115995f6
commit
c8ba1aaa51
@ -11,7 +11,7 @@ var node = bcoin.spvnode({
|
|||||||
});
|
});
|
||||||
|
|
||||||
node.on('error', function(err) {
|
node.on('error', function(err) {
|
||||||
bcoin.debug(err.message);
|
bcoin.debug(err.stack + '');
|
||||||
});
|
});
|
||||||
|
|
||||||
node.open(function(err) {
|
node.open(function(err) {
|
||||||
|
|||||||
@ -336,18 +336,14 @@ Chain.prototype._preload = function _preload(callback) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
stream.on('error', function(err) {
|
stream.on('error', function(err) {
|
||||||
var start = Math.max(0, height - 2);
|
stream.destroy();
|
||||||
self.reset(start, function(e) {
|
return callback(err);
|
||||||
if (e)
|
|
||||||
return callback(e);
|
|
||||||
return callback(err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on('data', function(data) {
|
stream.on('data', function(data) {
|
||||||
var blocks = [];
|
var blocks = [];
|
||||||
var need = 80 - buf.size;
|
var need = 80 - buf.size;
|
||||||
var lastEntry, block, entry, start;
|
var lastEntry, block, data, entry;
|
||||||
|
|
||||||
while (data.length >= need) {
|
while (data.length >= need) {
|
||||||
buf.data.push(data.slice(0, need));
|
buf.data.push(data.slice(0, need));
|
||||||
@ -368,11 +364,12 @@ Chain.prototype._preload = function _preload(callback) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
for (i = 0; i < blocks.length; i++) {
|
for (i = 0; i < blocks.length; i++) {
|
||||||
block = blocks[i];
|
data = blocks[i];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
data = parseHeader(data);
|
data = parseHeader(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
stream.destroy();
|
||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,13 +383,8 @@ Chain.prototype._preload = function _preload(callback) {
|
|||||||
|
|
||||||
// Do some paranoid checks.
|
// Do some paranoid checks.
|
||||||
if (lastEntry && data.prevBlock !== lastEntry.hash) {
|
if (lastEntry && data.prevBlock !== lastEntry.hash) {
|
||||||
start = Math.max(0, height - 2);
|
|
||||||
stream.destroy();
|
stream.destroy();
|
||||||
return self.reset(start, function(err) {
|
return callback(new Error('Corrupt headers.'));
|
||||||
if (err)
|
|
||||||
return callback(err);
|
|
||||||
return callback(new Error('Corrupt headers.'));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create headers object for validation.
|
// Create headers object for validation.
|
||||||
@ -401,13 +393,8 @@ Chain.prototype._preload = function _preload(callback) {
|
|||||||
// Verify the block headers. We don't want to
|
// Verify the block headers. We don't want to
|
||||||
// trust an external centralized source completely.
|
// trust an external centralized source completely.
|
||||||
if (!block.verifyHeaders()) {
|
if (!block.verifyHeaders()) {
|
||||||
start = Math.max(0, height - 2);
|
|
||||||
stream.destroy();
|
stream.destroy();
|
||||||
return self.reset(start, function(err) {
|
return callback(new Error('Bad headers.'));
|
||||||
if (err)
|
|
||||||
return callback(err);
|
|
||||||
return callback(new Error('Bad headers.'));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a chain entry.
|
// Create a chain entry.
|
||||||
@ -490,6 +477,9 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
|
|||||||
if (!block.verify(ret))
|
if (!block.verify(ret))
|
||||||
return done(new VerifyError(block, 'invalid', ret.reason, ret.score));
|
return done(new VerifyError(block, 'invalid', ret.reason, ret.score));
|
||||||
|
|
||||||
|
if (this.options.spv || block.type !== 'block')
|
||||||
|
return done();
|
||||||
|
|
||||||
// Skip the genesis block
|
// Skip the genesis block
|
||||||
if (block.isGenesis())
|
if (block.isGenesis())
|
||||||
return done(null, flags);
|
return done(null, flags);
|
||||||
|
|||||||
@ -834,7 +834,6 @@ Parser.parseBlockHeaders = function parseBlockHeaders(p) {
|
|||||||
ts: p.readU32(),
|
ts: p.readU32(),
|
||||||
bits: p.readU32(),
|
bits: p.readU32(),
|
||||||
nonce: p.readU32(),
|
nonce: p.readU32(),
|
||||||
totalTX: p.readVarint(),
|
|
||||||
_size: p.end()
|
_size: p.end()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user