diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 24163ec9..5725fd39 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -1170,6 +1170,9 @@ Chain.prototype._setBestChain = function _setBestChain(entry, prev, block, callb if (err) return callback(err); + // Do "contextual" verification on our block + // now that we're certain its previous + // block is in the chain. self._verifyContext(block, prev, function(err) { if (err) { // Couldn't verify block. @@ -1217,9 +1220,6 @@ Chain.prototype._setBestChain = function _setBestChain(entry, prev, block, callb } // Everything is in order. - // Do "contextual" verification on our block - // now that we're certain its previous - // block is in the chain. if (entry.prevBlock === this.tip.hash) return done(); @@ -2272,7 +2272,7 @@ Chain.prototype.getState = function getState(prev, id, callback) { })(null, prev); function walkForward(state) { - var entry; + var entry, count, i; if (compute.length === 0) return callback(null, state); @@ -2301,8 +2301,8 @@ Chain.prototype.getState = function getState(prev, id, callback) { if (medianTime >= timeTimeout) return walkForward(constants.thresholdStates.FAILED); - var count = 0; - var i = 0; + count = 0; + i = 0; (function next(err, entry) { if (err) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 0dc6a3cd..f4c885ae 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -205,7 +205,7 @@ Peer.prototype._init = function init() { this.challenge = utils.nonce(); this._ping.timer = setInterval(function() { - self._write(self.framer.ping({ + self.write(self.framer.ping({ nonce: self.challenge })); }, this._ping.interval); @@ -221,16 +221,16 @@ Peer.prototype._init = function init() { self.emit('ack'); self.ts = utils.now(); - self._write(self.framer.getAddr()); + self.write(self.framer.getAddr()); if (self.options.headers) { if (self.version && self.version.version > 70012) - self._write(self.framer.sendHeaders()); + self.write(self.framer.sendHeaders()); } if (self.options.witness) { if (self.version && self.version.version >= 70012) - self._write(self.framer.haveWitness()); + self.write(self.framer.haveWitness()); } if (self.chain.isFull()) @@ -238,7 +238,7 @@ Peer.prototype._init = function init() { }); // Send hello - this._write(this.framer.version({ + this.write(this.framer.version({ height: this.chain.height, relay: this.options.relay })); @@ -352,7 +352,7 @@ Peer.prototype.broadcast = function broadcast(items) { // Retransmit interval: setInterval(function() { - self._write(entry.inv); + self.write(entry.inv); }, this._broadcast.interval), inv: this.framer.inv([{ @@ -377,7 +377,7 @@ Peer.prototype.broadcast = function broadcast(items) { }); }, this); - this._write(this.framer.inv(payload)); + this.write(this.framer.inv(payload)); return result; }; @@ -391,7 +391,7 @@ Peer.prototype.updateWatch = function updateWatch() { return; if (this.ack) { - this._write(this.framer.filterLoad({ + this.write(this.framer.filterLoad({ filter: this.bloom.toBuffer(), n: this.bloom.n, tweak: this.bloom.tweak, @@ -433,15 +433,15 @@ Peer.prototype.destroy = function destroy() { /** * Write data to the peer's socket. - * @private * @param {Buffer} chunk + * @returns {Boolean} */ -Peer.prototype._write = function write(chunk) { +Peer.prototype.write = function write(chunk) { if (this.destroyed) - return; + return false; - this.socket.write(chunk); + return this.socket.write(chunk); }; /** @@ -547,7 +547,7 @@ Peer.prototype._res = function _res(cmd, payload) { */ Peer.prototype.getData = function getData(items) { - this._write(this.framer.getData(items)); + this.write(this.framer.getData(items)); }; Peer.prototype._onPacket = function onPacket(packet) { @@ -737,7 +737,7 @@ Peer.prototype._getUTXOs = function getUTXOs(utxos, callback) { return callback(null, payload.coins); }); - this._write(this.framer.getUTXOs({ + this.write(this.framer.getUTXOs({ mempool: true, prevout: utxos.map(function(item) { return { hash: item[0], index: item[1] }; @@ -822,7 +822,7 @@ Peer.prototype._handleGetUTXOs = function _handleGetUTXOs(payload) { if (err) self.emit('error', err); - self._write(self.framer.UTXOs({ + self.write(self.framer.UTXOs({ height: self.chain.height, tip: self.chain.tip.hash, hits: hits, @@ -882,7 +882,7 @@ Peer.prototype._handleGetHeaders = function _handleGetHeaders(payload) { if (err) return self.emit('error', err); - self._write(self.framer.headers(headers)); + self.write(self.framer.headers(headers)); } if (!payload.locator) @@ -915,7 +915,7 @@ Peer.prototype._handleGetBlocks = function _handleGetBlocks(payload) { function done(err) { if (err) return self.emit('error', err); - self._write(self.framer.inv(blocks)); + self.write(self.framer.inv(blocks)); } this.chain.findLocator(payload.locator, function(err, tip) { @@ -999,7 +999,7 @@ Peer.prototype._handleVersion = function handleVersion(payload) { this.relay = false; // ACK - this._write(this.framer.verack()); + this.write(this.framer.verack()); this.version = payload; this.emit('version', payload); }; @@ -1024,7 +1024,7 @@ Peer.prototype._handleMempool = function _handleMempool() { bcoin.debug('Sending mempool snapshot to %s.', self.host); - self._write(self.framer.inv(items)); + self.write(self.framer.inv(items)); }); }; @@ -1065,9 +1065,9 @@ Peer.prototype._handleGetData = function handleGetData(items) { isWitness ? 'witness' : 'normal'); if (isWitness) - this._write(this.framer.packet(entry.packetType, entry.witnessValue)); + this.write(this.framer.packet(entry.packetType, entry.witnessValue)); else - this._write(this.framer.packet(entry.packetType, entry.value)); + this.write(this.framer.packet(entry.packetType, entry.value)); entry.e.emit('request'); } @@ -1100,7 +1100,7 @@ Peer.prototype._handleGetData = function handleGetData(items) { else data = tx.renderNormal(); - self._write(self.framer.packet('tx', data)); + self.write(self.framer.packet('tx', data)); next(); }); @@ -1129,10 +1129,10 @@ Peer.prototype._handleGetData = function handleGetData(items) { else data = block.renderNormal(); - self._write(self.framer.packet('block', data)); + self.write(self.framer.packet('block', data)); if (hash === self.hashContinue) { - self._write(self.framer.inv([{ + self.write(self.framer.inv([{ type: constants.inv.BLOCK, hash: self.chain.tip.hash }])); @@ -1163,7 +1163,7 @@ Peer.prototype._handleGetData = function handleGetData(items) { block = block.toMerkle(self.filter); - self._write(self.framer.merkleBlock(block)); + self.write(self.framer.merkleBlock(block)); for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; @@ -1173,11 +1173,11 @@ Peer.prototype._handleGetData = function handleGetData(items) { else tx = tx.renderNormal(); - self._write(self.framer.packet('tx', tx)); + self.write(self.framer.packet('tx', tx)); } if (hash === self.hashContinue) { - self._write(self.framer.inv([{ + self.write(self.framer.inv([{ type: constants.inv.BLOCK, hash: self.chain.tip.hash }])); @@ -1201,7 +1201,7 @@ Peer.prototype._handleGetData = function handleGetData(items) { notfound.length); if (notfound.length > 0) - self._write(self.framer.notFound(notfound)); + self.write(self.framer.notFound(notfound)); }); }; @@ -1242,7 +1242,7 @@ Peer.prototype._handleAddr = function handleAddr(addrs) { }; Peer.prototype._handlePing = function handlePing(data) { - this._write(this.framer.pong({ + this.write(this.framer.pong({ nonce: data.nonce })); this.emit('ping', data); @@ -1292,7 +1292,7 @@ Peer.prototype._handleGetAddr = function handleGetAddr() { }); } - return this._write(this.framer.addr(peers)); + return this.write(this.framer.addr(peers)); }; Peer.prototype._handleInv = function handleInv(items) { @@ -1374,7 +1374,7 @@ Peer.prototype.getHeaders = function getHeaders(locator, stop) { locator && locator.length ? utils.revHex(locator[0]) : 0, stop ? utils.revHex(stop) : 0); - this._write(this.framer.getHeaders({ locator: locator, stop: stop })); + this.write(this.framer.getHeaders({ locator: locator, stop: stop })); }; /** @@ -1393,7 +1393,7 @@ Peer.prototype.getBlocks = function getBlocks(locator, stop) { locator && locator.length ? utils.revHex(locator[0]) : 0, stop ? utils.revHex(stop) : 0); - this._write(this.framer.getBlocks({ locator: locator, stop: stop })); + this.write(this.framer.getBlocks({ locator: locator, stop: stop })); }; /** @@ -1405,7 +1405,7 @@ Peer.prototype.getMempool = function getMempool() { 'Requesting inv packet from %s with mempool', this.host); - this._write(this.framer.mempool()); + this.write(this.framer.mempool()); }; /** @@ -1418,7 +1418,7 @@ Peer.prototype.reject = function reject(details) { 'Sending reject packet to %s', this.host); - this._write(this.framer.reject(details)); + this.write(this.framer.reject(details)); }; /** diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index 8e982724..13f0fe43 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -587,8 +587,6 @@ regtest.genesisBlock = + '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f' + 'ac00000000'; -regtest.pow = {}; - regtest.pow = { limit: new bn( '7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',