From 3f4e0f5040c0b4f371dda5999207f1593bf74f80 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 23 May 2016 01:04:55 -0700 Subject: [PATCH] refactor miner. --- lib/bcoin/miner.js | 15 ++++++++++----- lib/bcoin/script.js | 16 +++++++++++++--- lib/bcoin/workers.js | 6 +++--- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 7cc96105..430faf16 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -342,6 +342,7 @@ Miner.prototype.mineBlock = function mineBlock(version, callback) { * @param {Base58Address} options.address - Payout address. * @param {Boolean} options.witness - Allow witness * transactions, mine a witness block. + * @param {String} options.coinbaseFlags * @property {Block} block * @property {TX} coinbase * @property {BN} hashes - Number of hashes attempted. @@ -361,6 +362,10 @@ function MinerBlock(options) { this.extraNonce = new bn(0); this.iterations = 0; this.dsha256 = options.dsha256; + this.coinbaseFlags = options.coinbaseFlags; + this.witness = options.witness; + this.address = options.address; + this.witnessNonce = null; // Create a coinbase this.coinbase = new bcoin.mtx(); @@ -385,14 +390,14 @@ function MinerBlock(options) { bcoin.script.array(utils.nonce()), // Let the world know this little // miner succeeded. - new Buffer(options.coinbaseFlags, 'ascii') + new Buffer(this.coinbaseFlags, 'utf8') ]), witness: new bcoin.witness(), sequence: 0xffffffff }); this.coinbase.addOutput({ - address: options.address, + address: this.address, value: 0 }); @@ -410,10 +415,9 @@ function MinerBlock(options) { this.block.addTX(this.coinbase); - if (options.witness) { + if (this.witness) { // Set up the witness nonce and // commitment output for segwit. - this.witness = true; this.witnessNonce = utils.dsha256(new Buffer(this.tip.hash, 'hex')); this.coinbase.inputs[0].witness.items[0] = this.witnessNonce; this.coinbase.addOutput({ @@ -437,7 +441,8 @@ utils.inherits(MinerBlock, EventEmitter); MinerBlock.prototype.updateCommitment = function updateCommitment() { var hash = this.block.getCommitmentHash(); - this.coinbase.outputs[1].script = bcoin.script.createCommitment(hash); + var commit = bcoin.script.createCommitment(hash, this.coinbaseFlags); + this.coinbase.outputs[1].script = commit; }; /** diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 65f520ba..102c4c02 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -2246,17 +2246,27 @@ Script.createWitnessProgram = function createWitnessProgram(version, data) { /** * Create a witness block commitment. * @param {Buffer} hash + * @params {String|Buffer} flags * @returns {Script} */ -Script.createCommitment = function createCommitment(hash) { - var p = new BufferWriter(); +Script.createCommitment = function createCommitment(hash, flags) { + var p; + + if (!flags) + flags = 'mined by bcoin'; + + if (typeof flags === 'string') + flags = new Buffer(flags, 'utf8'); + + p = new BufferWriter(); p.writeU32BE(0xaa21a9ed); p.writeHash(hash); + return new Script([ opcodes.OP_RETURN, p.render(), - new Buffer('mined by bcoin', 'ascii') + flags ]); }; diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index a0be3cdc..28ba21ec 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -225,9 +225,9 @@ Workers.prototype.mine = function mine(attempt, callback) { tip: attempt.tip, version: attempt.block.version, target: attempt.block.bits, - address: attempt.options.address, - coinbaseFlags: attempt.options.coinbaseFlags, - witness: attempt.options.witness + address: attempt.address, + coinbaseFlags: attempt.coinbaseFlags, + witness: attempt.witness }; return this.execute('mine', [data], -1, callback); };