fix LoadRequest. misc.

This commit is contained in:
Christopher Jeffrey 2016-02-22 13:06:12 -08:00
parent ab3fb2e550
commit 9ddc23227c
3 changed files with 43 additions and 12 deletions

View File

@ -382,8 +382,9 @@ BlockDB.prototype.removeBlock = function removeBlock(hash, callback) {
// can ONLY remove the last block. // can ONLY remove the last block.
assert(block._fileOffset >= 0); assert(block._fileOffset >= 0);
assert(block._fileOffset < self.data.size); assert(block._fileOffset < self.data.size);
// XXX This seems to be truncating too much right now self.emit('remove block', block);
return callback(null, block); return callback(null, block);
// XXX This seems to be truncating too much right now
self.data.truncateAsync(block._fileOffset, function(err) { self.data.truncateAsync(block._fileOffset, function(err) {
if (err) if (err)
return callback(err); return callback(err);
@ -725,7 +726,8 @@ BlockDB.prototype._getTXByAddress = function _getTXByAddress(address, callback)
if (data) { if (data) {
try { try {
tx = bcoin.tx.fromRaw(data); tx = self.parser.parseTX(data);
tx = new bcoin.tx(tx);
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
@ -799,7 +801,8 @@ BlockDB.prototype.getTX = function getTX(hash, callback) {
if (data) { if (data) {
try { try {
tx = bcoin.tx.fromRaw(data); tx = self.parser.parseTX(data);
tx = new bcoin.tx(tx);
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
@ -852,7 +855,8 @@ BlockDB.prototype.getBlock = function getBlock(hash, callback) {
if (data) { if (data) {
try { try {
block = bcoin.block.fromRaw(data); block = self.parse.parseBlock(data);
block = new bcoin.block(block, 'block');
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }

View File

@ -606,6 +606,7 @@ Chain.prototype._checkDuplicates = function _checkDuplicates(block, prev, callba
Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callback) { Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callback) {
var self = this; var self = this;
var height = prev.height + 1; var height = prev.height + 1;
var scriptCheck = true;
if (!this.blockdb || block.subtype !== 'block') if (!this.blockdb || block.subtype !== 'block')
return callback(null, true); return callback(null, true);
@ -617,7 +618,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
// skip the input verification. // skip the input verification.
if (this.options.useCheckpoints) { if (this.options.useCheckpoints) {
if (height < network.checkpoints.lastHeight && !network.checkpoints[height]) if (height < network.checkpoints.lastHeight && !network.checkpoints[height])
return callback(null, true); scriptCheck = false;
} }
this._fillBlock(block, function(err) { this._fillBlock(block, function(err) {
@ -649,6 +650,9 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
return callback(null, false); return callback(null, false);
} }
if (!scriptCheck)
continue;
// Verify the scripts // Verify the scripts
if (!tx.verify(j, true, flags)) { if (!tx.verify(j, true, flags)) {
utils.debug('Block has invalid inputs: %s (%s/%d)', utils.debug('Block has invalid inputs: %s (%s/%d)',
@ -666,6 +670,9 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
} }
} }
if (!scriptCheck)
continue;
// Check for block sigops limits // Check for block sigops limits
// Start counting P2SH sigops once block // Start counting P2SH sigops once block
// timestamps reach March 31st, 2012. // timestamps reach March 31st, 2012.
@ -1762,6 +1769,23 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
return root; return root;
}; };
Chain.prototype.getOrphanSoil = function getOrphanSoil(hash) {
var root;
if (utils.isBuffer(hash))
hash = utils.toHex(hash);
else if (hash.hash)
hash = hash.hash('hex');
while (this.orphan.bmap[hash]) {
root = this.orphan.bmap[hash];
hash = this.orphan.bmap[hash].prevBlock;
}
if (root)
return root.prevBlock;
};
Chain.prototype.getHeight = function getHeight(hash) { Chain.prototype.getHeight = function getHeight(hash) {
return this.db.getHeight(hash); return this.db.getHeight(hash);
}; };

View File

@ -66,7 +66,7 @@ function Pool(options) {
options.multiplePeers = true; options.multiplePeers = true;
} else { } else {
if (options.headers == null) if (options.headers == null)
options.headers = false; options.headers = true;
if (options.multiplePeers == null) if (options.multiplePeers == null)
options.multiplePeers = false; options.multiplePeers = false;
} }
@ -228,7 +228,7 @@ Pool.prototype._init = function _init() {
} }
// Resolve orphan chain // Resolve orphan chain
self.loadOrphan(self.peers.load, null, data.hash); self.resolveOrphan(self.peers.load, null, data.hash);
}); });
this.options.wallets.forEach(function(wallet) { this.options.wallets.forEach(function(wallet) {
@ -257,7 +257,7 @@ Pool.prototype.getBlocks = function getBlocks(peer, top, stop) {
}); });
}; };
Pool.prototype.loadOrphan = function loadOrphan(peer, top, orphan) { Pool.prototype.resolveOrphan = function resolveOrphan(peer, top, orphan) {
var self = this; var self = this;
assert(orphan); assert(orphan);
this.chain.onFlush(function() { this.chain.onFlush(function() {
@ -590,7 +590,10 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) {
// Resolve orphan chain. // Resolve orphan chain.
if (self.chain.hasOrphan(hash)) { if (self.chain.hasOrphan(hash)) {
utils.debug('Peer sent a hash that is already a known orphan.'); utils.debug('Peer sent a hash that is already a known orphan.');
self.loadOrphan(peer, null, hash); // var soil = self.chain.getOrphanSoil(hash);
// if (self.request.map[soil] || self.chain.hasPending(soil))
// break;
self.resolveOrphan(peer, null, hash);
continue; continue;
} }
@ -1899,6 +1902,9 @@ function LoadRequest(pool, peer, type, hash, callback) {
if (callback) if (callback)
this.callback.push(callback); this.callback.push(callback);
assert(!this.pool.request.map[this.hash]);
this.pool.request.map[this.hash] = this;
this._finish = this.finish.bind(this); this._finish = this.finish.bind(this);
} }
@ -1912,9 +1918,6 @@ LoadRequest.prototype.start = function start() {
else else
this.pool.request.activeBlocks++; this.pool.request.activeBlocks++;
assert(!this.pool.request.map[this.hash]);
this.pool.request.map[this.hash] = this;
return this; return this;
}; };