diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 14bae0d8..f327d9cc 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -600,231 +600,6 @@ Chain.prototype._verify = function _verify(block, prev, callback) { }); }; -Chain.prototype._verify2 = function _verify(block, prev, callback) { - var self = this; - var flags = constants.flags.MANDATORY_VERIFY_FLAGS; - var height, ts, i, tx, cb, coinbaseHeight; - var locktimeMedian, segwit, check; - - if (!block.verify()) - return callback(null, false); - - // Skip the genesis block - if (block.isGenesis()) - return callback(null, flags); - - // Ensure it's not an orphan - if (!prev) { - utils.debug('Block has no previous entry: %s', block.rhash); - return callback(null, false); - } - - height = prev.height + 1; - - prev.getMedianTime(function(err, medianTime) { - if (err) - return callback(err); - - // Ensure the timestamp is correct - if (block.ts <= medianTime) { - utils.debug('Block time is lower than median: %s', block.rhash); - return callback(null, false); - } - - // Ensure the miner's target is equal to what we expect - self.getTarget(prev, block, function(err, target) { - if (err) - return callback(err); - - if (block.bits !== target) { - utils.debug('Block is using wrong target: %s', block.rhash); - return callback(null, false); - } - - // For some reason bitcoind has p2sh in the - // mandatory flags by default, when in reality - // it wasn't activated until march 30th 2012. - // The first p2sh output and redeem script - // appeared on march 7th 2012, only it did - // not have a signature. See: - // https://blockchain.info/tx/6a26d2ecb67f27d1fa5524763b49029d7106e91e3cc05743073461a719776192 - // https://blockchain.info/tx/9c08a4d78931342b37fd5f72900fb9983087e6f46c4a097d8a1f52c74e28eaf6 - if (block.ts < constants.block.bip16time) - flags &= ~constants.flags.VERIFY_P2SH; - - check = []; - - // Only allow version 2 blocks (coinbase height) - // once the majority of blocks are using it. - if (block.version < 2) { - check.push([2, function(result) { - if (result) - utils.debug('Block is outdated (v2): %s', block.rhash); - }]); - } - - // Only allow version 3 blocks (sig validation) - // once the majority of blocks are using it. - if (block.version < 3) { - check.push([3, function(result) { - if (result) - utils.debug('Block is outdated (v3): %s', block.rhash); - }]); - } - - // Only allow version 4 blocks (checklocktimeverify) - // once the majority of blocks are using it. - if (block.version < 4) { - check.push([4, function(result) { - if (result) - utils.debug('Block is outdated (v4): %s', block.rhash); - }]); - } - - // Only allow version 5 blocks (segwit) - // once the majority of blocks are using it. - if (network.segwitHeight !== -1 && height >= network.segwitHeight) { - if (block.version < 5) { - check.push([5, function(result) { - if (result) - utils.debug('Block is outdated (v5): %s', block.rhash); - }]); - } - } - - // Only allow version 8 blocks (locktime median past) - // once the majority of blocks are using it. - // if (block.version < 8 && prev.isOutdated(8)) { - // utils.debug('Block is outdated (v8): %s', block.rhash); - // return false; - // } - - check = []; - utils.forEachSerial(check, function(item, next) { - var version = item[0]; - var cb = item[1]; - prev.isOutdated(version, function(err, result) { - if (err) - return next(err); - - cb(result); - - if (!result) - return callback(null, false); - - next(); - }); - }, function(err) { - if (err) - return callback(err); - - check = []; - - // Make sure the height contained in the coinbase is correct. - if (network.block.bip34height !== -1 && height >= network.block.bip34height) { - check.push([2, function(result) { - if (result) - coinbaseHeight = true; - }]); - } - - // Signature validation is now enforced (bip66) - if (block.version >= 3) { - check.push([3, function(result) { - if (result) - flags |= constants.flags.VERIFY_DERSIG; - }]); - } - - // CHECKLOCKTIMEVERIFY is now usable (bip65) - if (block.version >= 4) { - check.push([4, function(result) { - if (result) - flags |= constants.flags.VERIFY_CHECKLOCKTIMEVERIFY; - }]); - } - - // Segregrated witness is now usable (the-bip-that-really-needs-to-be-rewritten) - if (network.segwitHeight !== -1 && height >= network.segwitHeight) { - if (block.version >= 5) { - check.push([5, function(result) { - if (result) { - flags |= constants.flags.VERIFY_WITNESS; - segwit = true; - } - }]); - } - } - - segwit = true; - check = []; - // Use nLockTime median past (bip113) - // https://github.com/btcdrak/bips/blob/d4c9a236ecb947866c61aefb868b284498489c2b/bip-0113.mediawiki - // Support version bits: - // https://gist.github.com/sipa/bf69659f43e763540550 - // http://lists.linuxfoundation.org/pipermail/bitcoin-dev/2015-August/010396.html - // if (block.version >= 8 && prev.isUpgraded(8)) - // locktimeMedian = true; - - utils.forEachSerial(check, function(item, next) { - var version = item[0]; - var cb = item[1]; - prev.isUpgraded(version, function(err, result) { - if (err) - return next(err); - cb(result); - next(); - }); - }, function(err) { - if (err) - return callback(err); - - // Can't verify any further when merkleblock or headers. - if (block.type !== 'block') - return callback(null, flags); - - // Make sure the height contained in the coinbase is correct. - if (coinbaseHeight) { - if (block.getCoinbaseHeight() !== height) { - utils.debug('Block has bad coinbase height: %s', block.rhash); - return callback(null, false); - } - } - - if (block.version >= 5 && segwit) { - if (block.commitmentHash !== block.getCommitmentHash()) { - utils.debug('Block failed witnessroot test: %s', block.rhash); - return callback(null, false); - } - } else { - if (block.hasWitness()) { - utils.debug('Unexpected witness data found: %s', block.rhash); - return callback(null, false); - } - } - - // Get timestamp for tx.isFinal(). - ts = locktimeMedian ? medianTime : block.ts; - - // Check all transactions - for (i = 0; i < block.txs.length; i++) { - tx = block.txs[i]; - - // Transactions must be finalized with - // regards to nSequence and nLockTime. - if (!tx.isFinal(height, ts)) { - utils.debug('TX is not final: %s (%s)', block.rhash, i); - return callback(null, false); - } - } - - return callback(null, flags); - }); - }); - }); - }); -}; - Chain.prototype._checkDuplicates = function _checkDuplicates(block, prev, callback) { var self = this; var height = prev.height + 1; @@ -1148,7 +923,7 @@ Chain.prototype._reorganize = function _reorganize(entry, callback) { self.emit('fork', block, { height: fork.height, expected: self.tip.hash, - received: fork.hash, + received: entry.hash, checkpoint: false }); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 9aa77329..30c6ee6d 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -191,9 +191,6 @@ Pool.prototype._init = function _init() { self.setMisbehavior(peer, 100); return; } - - // Only destroy peer here. Wait for higher chain. - peer.destroy(); }); this.chain.on('invalid', function(block, data, peer) {