From 9ddc23227cb38e21f95cf007077fcd0f43bddc11 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Feb 2016 13:06:12 -0800 Subject: [PATCH] fix LoadRequest. misc. --- lib/bcoin/blockdb.js | 12 ++++++++---- lib/bcoin/chain.js | 26 +++++++++++++++++++++++++- lib/bcoin/pool.js | 17 ++++++++++------- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 12982782..22d5e09e 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -382,8 +382,9 @@ BlockDB.prototype.removeBlock = function removeBlock(hash, callback) { // can ONLY remove the last block. assert(block._fileOffset >= 0); 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); + // XXX This seems to be truncating too much right now self.data.truncateAsync(block._fileOffset, function(err) { if (err) return callback(err); @@ -725,7 +726,8 @@ BlockDB.prototype._getTXByAddress = function _getTXByAddress(address, callback) if (data) { try { - tx = bcoin.tx.fromRaw(data); + tx = self.parser.parseTX(data); + tx = new bcoin.tx(tx); } catch (e) { return callback(e); } @@ -799,7 +801,8 @@ BlockDB.prototype.getTX = function getTX(hash, callback) { if (data) { try { - tx = bcoin.tx.fromRaw(data); + tx = self.parser.parseTX(data); + tx = new bcoin.tx(tx); } catch (e) { return callback(e); } @@ -852,7 +855,8 @@ BlockDB.prototype.getBlock = function getBlock(hash, callback) { if (data) { try { - block = bcoin.block.fromRaw(data); + block = self.parse.parseBlock(data); + block = new bcoin.block(block, 'block'); } catch (e) { return callback(e); } diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 35309311..e799e8d5 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -606,6 +606,7 @@ Chain.prototype._checkDuplicates = function _checkDuplicates(block, prev, callba Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callback) { var self = this; var height = prev.height + 1; + var scriptCheck = true; if (!this.blockdb || block.subtype !== 'block') return callback(null, true); @@ -617,7 +618,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac // skip the input verification. if (this.options.useCheckpoints) { if (height < network.checkpoints.lastHeight && !network.checkpoints[height]) - return callback(null, true); + scriptCheck = false; } this._fillBlock(block, function(err) { @@ -649,6 +650,9 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac return callback(null, false); } + if (!scriptCheck) + continue; + // Verify the scripts if (!tx.verify(j, true, flags)) { 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 // Start counting P2SH sigops once block // timestamps reach March 31st, 2012. @@ -1762,6 +1769,23 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) { 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) { return this.db.getHeight(hash); }; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 304dbbb3..08a2eccb 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -66,7 +66,7 @@ function Pool(options) { options.multiplePeers = true; } else { if (options.headers == null) - options.headers = false; + options.headers = true; if (options.multiplePeers == null) options.multiplePeers = false; } @@ -228,7 +228,7 @@ Pool.prototype._init = function _init() { } // 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) { @@ -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; assert(orphan); this.chain.onFlush(function() { @@ -590,7 +590,10 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) { // Resolve orphan chain. if (self.chain.hasOrphan(hash)) { 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; } @@ -1899,6 +1902,9 @@ function LoadRequest(pool, peer, type, hash, callback) { if (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); } @@ -1912,9 +1918,6 @@ LoadRequest.prototype.start = function start() { else this.pool.request.activeBlocks++; - assert(!this.pool.request.map[this.hash]); - this.pool.request.map[this.hash] = this; - return this; };