mining: change some miner apis.

This commit is contained in:
Christopher Jeffrey 2017-01-31 16:27:05 -08:00
parent 231d08b1c4
commit 389adee8f0
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 23 additions and 45 deletions

View File

@ -449,8 +449,7 @@ Miner.prototype.build = function build(attempt) {
}
}
attempt.updateCoinbase();
attempt.updateMerkle();
attempt.refresh();
assert(block.getWeight() <= attempt.weight);
};

View File

@ -172,11 +172,8 @@ MinerBlock.prototype._init = function _init() {
block.txs.push(cb);
// Update coinbase since our coinbase was added.
this.updateCoinbase();
// Create our merkle root.
this.updateMerkle();
// Update commitments.
this.refresh();
// Initialize weight.
this.weight = this.block.getWeight();
@ -188,6 +185,23 @@ MinerBlock.prototype._init = function _init() {
this.sigops = cb.getSigopsCost(null, this.flags);
};
/**
* Update coinbase, witness
* commitment, and merkle root.
*/
MinerBlock.prototype.refresh = function refresh() {
// Update coinbase.
this.updateCoinbase();
// Witness commitment.
if (this.witness)
this.updateCommitment();
// Create our merkle root.
this.updateMerkle();
};
/**
* Update the commitment output for segwit.
*/
@ -226,8 +240,6 @@ MinerBlock.prototype.updateCoinbase = function updateCoinbase() {
*/
MinerBlock.prototype.updateNonce = function updateNonce() {
this.block.ts = Math.max(this.network.now(), this.tip.ts + 1);
// Overflow the nonce and increment the extraNonce.
this.block.nonce = 0;
this.nonce1++;
@ -246,20 +258,10 @@ MinerBlock.prototype.updateNonce = function updateNonce() {
};
/**
* Rebuild the merkle tree and update merkle root as well as the
* timestamp (also calls {@link MinerBlock#updateCommitment}
* if segwit is enabled).
* Rebuild the merkle tree and update merkle root.
*/
MinerBlock.prototype.updateMerkle = function updateMerkle() {
// Always update commitment before updating merkle root.
// The updated commitment output will change the merkle root.
if (this.witness)
this.updateCommitment();
// Update timestamp.
this.block.ts = Math.max(this.network.now(), this.tip.ts + 1);
// Recalculate merkle root.
this.block.merkleRoot = this.block.createMerkleRoot('hex');
};
@ -315,11 +317,8 @@ MinerBlock.prototype.addTX = function addTX(tx, view) {
this.block.txs.push(tx);
this.items.push(item);
// Update coinbase value
this.updateCoinbase();
// Update merkle root for new coinbase and new tx
this.updateMerkle();
// Update commitments.
this.refresh();
return true;
};
@ -400,8 +399,6 @@ MinerBlock.prototype.mine = function mine() {
// Track how long we've been at it.
this.begin = util.now();
assert(this.block.ts > this.tip.ts);
for (;;) {
nonce = this.findNonce();
@ -427,8 +424,6 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() {
// Track how long we've been at it.
this.begin = util.now();
assert(this.block.ts > this.tip.ts);
for (;;) {
nonce = yield this.findNonceAsync();
@ -451,28 +446,12 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() {
*/
MinerBlock.prototype.iterate = function iterate() {
var block = this.block;
var tip = this.tip;
var now = this.network.now();
// Keep track of our iterations.
this.iterations++;
// Send progress report.
this.sendStatus();
// If we took more a second or more (likely),
// skip incrementing the extra nonce and just
// update the timestamp. This improves
// performance because we do not have to
// recalculate the merkle root.
if (now > block.ts && now > tip.ts) {
block.ts = now;
// Overflow the nonce
block.nonce = 0;
return;
}
// Overflow the nonce and increment the extraNonce.
this.updateNonce();
};