From 62bc8b077da24f22af93d6d0b9571ecc608a7582 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 10 May 2016 12:10:34 -0700 Subject: [PATCH] miner, chainblock, workers. --- lib/bcoin/chainblock.js | 19 ++++++++++++------- lib/bcoin/miner.js | 2 +- lib/bcoin/workers.js | 11 ++++++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/chainblock.js b/lib/bcoin/chainblock.js index 57346b17..10f6b859 100644 --- a/lib/bcoin/chainblock.js +++ b/lib/bcoin/chainblock.js @@ -385,8 +385,8 @@ ChainBlock.prototype.__defineGetter__('rhash', function() { * @returns {Buffer} */ -ChainBlock.prototype.toRaw = function toRaw() { - var p = new BufferWriter(); +ChainBlock.prototype.toRaw = function toRaw(writer) { + var p = new BufferWriter(writer); p.write32(this.version); p.writeHash(this.prevBlock); @@ -397,7 +397,10 @@ ChainBlock.prototype.toRaw = function toRaw() { p.writeU32(this.height); p.writeBytes(this.chainwork.toBuffer('le', 32)); - return p.render(); + if (!writer) + p = p.render(); + + return p; }; /** @@ -408,8 +411,10 @@ ChainBlock.prototype.toRaw = function toRaw() { */ ChainBlock.fromRaw = function fromRaw(chain, buf) { - var p = new BufferReader(buf); - var hash = utils.dsha256(buf.slice(0, 80)); + var p = new BufferReader(buf, true); + var hash = utils.dsha256(p.readBytes(80)); + + p.seek(-80); return new ChainBlock(chain, { hash: hash.toString('hex'), @@ -440,7 +445,7 @@ ChainBlock.prototype.toJSON = function toJSON() { bits: this.bits, nonce: this.nonce, height: this.height, - chainwork: this.chainwork.toString('hex') + chainwork: this.chainwork.toString(10) }; }; @@ -455,7 +460,7 @@ ChainBlock.fromJSON = function fromJSON(chain, json) { json.hash = utils.revHex(json.hash); json.prevBlock = utils.revHex(json.prevBlock); json.merkleRoot = utils.revHex(json.merkleRoot); - json.chainwork = new bn(json.chainwork, 'hex'); + json.chainwork = new bn(json.chainwork, 10); return new ChainBlock(chain, json); }; diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 4a705e1a..77402b75 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -555,7 +555,7 @@ MinerBlock.prototype.findNonce = function findNonce() { // Overflow the nonce and increment the extraNonce. block.nonce = 0; - block.extraNonce.iaddn(1); + this.extraNonce.iaddn(1); // We incremented the extraNonce, need to update coinbase. this.updateCoinbase(); diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index 2bae6978..d890ee46 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -195,7 +195,7 @@ Workers.prototype.verify = function verify(tx, index, force, flags, callback) { Workers.prototype.mine = function mine(attempt, callback) { var data = { - tip: attempt.tip.toRaw(), + tip: attempt.tip, version: attempt.block.version, target: attempt.block.bits, address: attempt.options.address, @@ -608,7 +608,7 @@ jobs.verify = function verify(tx, index, force, flags) { jobs.mine = function mine(data) { var attempt = new bcoin.miner.minerblock({ - tip: bcoin.chainblock.fromRaw(null, data.tip), + tip: data.tip, version: data.version, target: data.target, address: data.address, @@ -697,8 +697,11 @@ Framer.item = function _item(item, p) { } else if (item instanceof bcoin.coin) { p.writeU8(42); bcoin.protocol.framer.coin(item, true, p); - } else if (bn.isBN(item)) { + } else if (item instanceof bcoin.chainblock) { p.writeU8(43); + item.toRaw(p); + } else if (bn.isBN(item)) { + p.writeU8(50); p.writeVarBytes(item.toBuffer()); } else if (Buffer.isBuffer(item)) { p.writeU8(4); @@ -854,6 +857,8 @@ Parser.parseItem = function parseItem(p) { case 42: return bcoin.coin.fromExtended(p); case 43: + return bcoin.chainblock.fromRaw(null, p); + case 50: return new bn(p.readVarBytes()); default: assert(false, 'Bad type.');