refactor miner.

This commit is contained in:
Christopher Jeffrey 2016-05-23 01:04:55 -07:00
parent 8c8b62a57f
commit 3f4e0f5040
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 26 additions and 11 deletions

View File

@ -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;
};
/**

View File

@ -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
]);
};

View File

@ -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);
};