refactor miner.
This commit is contained in:
parent
8c8b62a57f
commit
3f4e0f5040
@ -342,6 +342,7 @@ Miner.prototype.mineBlock = function mineBlock(version, callback) {
|
|||||||
* @param {Base58Address} options.address - Payout address.
|
* @param {Base58Address} options.address - Payout address.
|
||||||
* @param {Boolean} options.witness - Allow witness
|
* @param {Boolean} options.witness - Allow witness
|
||||||
* transactions, mine a witness block.
|
* transactions, mine a witness block.
|
||||||
|
* @param {String} options.coinbaseFlags
|
||||||
* @property {Block} block
|
* @property {Block} block
|
||||||
* @property {TX} coinbase
|
* @property {TX} coinbase
|
||||||
* @property {BN} hashes - Number of hashes attempted.
|
* @property {BN} hashes - Number of hashes attempted.
|
||||||
@ -361,6 +362,10 @@ function MinerBlock(options) {
|
|||||||
this.extraNonce = new bn(0);
|
this.extraNonce = new bn(0);
|
||||||
this.iterations = 0;
|
this.iterations = 0;
|
||||||
this.dsha256 = options.dsha256;
|
this.dsha256 = options.dsha256;
|
||||||
|
this.coinbaseFlags = options.coinbaseFlags;
|
||||||
|
this.witness = options.witness;
|
||||||
|
this.address = options.address;
|
||||||
|
this.witnessNonce = null;
|
||||||
|
|
||||||
// Create a coinbase
|
// Create a coinbase
|
||||||
this.coinbase = new bcoin.mtx();
|
this.coinbase = new bcoin.mtx();
|
||||||
@ -385,14 +390,14 @@ function MinerBlock(options) {
|
|||||||
bcoin.script.array(utils.nonce()),
|
bcoin.script.array(utils.nonce()),
|
||||||
// Let the world know this little
|
// Let the world know this little
|
||||||
// miner succeeded.
|
// miner succeeded.
|
||||||
new Buffer(options.coinbaseFlags, 'ascii')
|
new Buffer(this.coinbaseFlags, 'utf8')
|
||||||
]),
|
]),
|
||||||
witness: new bcoin.witness(),
|
witness: new bcoin.witness(),
|
||||||
sequence: 0xffffffff
|
sequence: 0xffffffff
|
||||||
});
|
});
|
||||||
|
|
||||||
this.coinbase.addOutput({
|
this.coinbase.addOutput({
|
||||||
address: options.address,
|
address: this.address,
|
||||||
value: 0
|
value: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -410,10 +415,9 @@ function MinerBlock(options) {
|
|||||||
|
|
||||||
this.block.addTX(this.coinbase);
|
this.block.addTX(this.coinbase);
|
||||||
|
|
||||||
if (options.witness) {
|
if (this.witness) {
|
||||||
// Set up the witness nonce and
|
// Set up the witness nonce and
|
||||||
// commitment output for segwit.
|
// commitment output for segwit.
|
||||||
this.witness = true;
|
|
||||||
this.witnessNonce = utils.dsha256(new Buffer(this.tip.hash, 'hex'));
|
this.witnessNonce = utils.dsha256(new Buffer(this.tip.hash, 'hex'));
|
||||||
this.coinbase.inputs[0].witness.items[0] = this.witnessNonce;
|
this.coinbase.inputs[0].witness.items[0] = this.witnessNonce;
|
||||||
this.coinbase.addOutput({
|
this.coinbase.addOutput({
|
||||||
@ -437,7 +441,8 @@ utils.inherits(MinerBlock, EventEmitter);
|
|||||||
|
|
||||||
MinerBlock.prototype.updateCommitment = function updateCommitment() {
|
MinerBlock.prototype.updateCommitment = function updateCommitment() {
|
||||||
var hash = this.block.getCommitmentHash();
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -2246,17 +2246,27 @@ Script.createWitnessProgram = function createWitnessProgram(version, data) {
|
|||||||
/**
|
/**
|
||||||
* Create a witness block commitment.
|
* Create a witness block commitment.
|
||||||
* @param {Buffer} hash
|
* @param {Buffer} hash
|
||||||
|
* @params {String|Buffer} flags
|
||||||
* @returns {Script}
|
* @returns {Script}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.createCommitment = function createCommitment(hash) {
|
Script.createCommitment = function createCommitment(hash, flags) {
|
||||||
var p = new BufferWriter();
|
var p;
|
||||||
|
|
||||||
|
if (!flags)
|
||||||
|
flags = 'mined by bcoin';
|
||||||
|
|
||||||
|
if (typeof flags === 'string')
|
||||||
|
flags = new Buffer(flags, 'utf8');
|
||||||
|
|
||||||
|
p = new BufferWriter();
|
||||||
p.writeU32BE(0xaa21a9ed);
|
p.writeU32BE(0xaa21a9ed);
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
|
||||||
return new Script([
|
return new Script([
|
||||||
opcodes.OP_RETURN,
|
opcodes.OP_RETURN,
|
||||||
p.render(),
|
p.render(),
|
||||||
new Buffer('mined by bcoin', 'ascii')
|
flags
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -225,9 +225,9 @@ Workers.prototype.mine = function mine(attempt, callback) {
|
|||||||
tip: attempt.tip,
|
tip: attempt.tip,
|
||||||
version: attempt.block.version,
|
version: attempt.block.version,
|
||||||
target: attempt.block.bits,
|
target: attempt.block.bits,
|
||||||
address: attempt.options.address,
|
address: attempt.address,
|
||||||
coinbaseFlags: attempt.options.coinbaseFlags,
|
coinbaseFlags: attempt.coinbaseFlags,
|
||||||
witness: attempt.options.witness
|
witness: attempt.witness
|
||||||
};
|
};
|
||||||
return this.execute('mine', [data], -1, callback);
|
return this.execute('mine', [data], -1, callback);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user