update getheaders sync.

This commit is contained in:
Christopher Jeffrey 2016-02-24 06:54:27 -08:00
parent 62caf5fd4a
commit 0af709388c
2 changed files with 8 additions and 16 deletions

View File

@ -415,17 +415,14 @@ Peer.prototype._onPacket = function onPacket(packet) {
return this._handleReject(payload);
if (cmd === 'block') {
payload.network = true;
payload.relayedBy = this.host || '0.0.0.0';
payload = bcoin.block(payload);
} else if (cmd === 'merkleblock') {
payload.network = true;
payload.relayedBy = this.host || '0.0.0.0';
payload = bcoin.merkleblock(payload);
this.lastBlock = payload;
return;
} else if (cmd === 'tx') {
payload.network = true;
payload.relayedBy = this.host || '0.0.0.0';
payload = bcoin.tx(payload, this.lastBlock);
if (this.lastBlock) {
@ -602,11 +599,7 @@ Peer.prototype._handleInv = function handleInv(items) {
Peer.prototype._handleHeaders = function handleHeaders(headers) {
headers = headers.map(function(header) {
header._hash = utils.dsha256(header._raw);
header.hash = utils.toHex(header._hash);
header.prevBlock = utils.toHex(header.prevBlock);
header.merkleRoot = utils.toHex(header.merkleRoot);
return header;
return new bcoin.headers(header);
});
this.emit('headers', headers);

View File

@ -539,19 +539,18 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
this.chain.onFlush(function() {
for (i = 0; i < headers.length; i++) {
header = headers[i];
hash = header.hash('hex');
if (last && header.prevBlock !== last.hash)
if (last && header.prevBlock !== last)
break;
if (!utils.testTarget(header.bits, header._hash)) {
utils.debug('Header failed POW test.');
if (!header.verify())
break;
}
if (!self.chain.has(header.hash))
self.getData(peer, self.block.type, header.hash);
if (!self.chain.has(hash))
self.getData(peer, self.block.type, hash);
last = header;
last = hash;
}
// Restart the getheaders process
@ -562,7 +561,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
// simply tries to find the latest block in
// the peer's chain.
if (last && headers.length === 2000)
self.getHeaders(peer, last.hash, null);
self.getHeaders(peer, last, null);
});
// Reset interval to avoid calling getheaders unnecessarily