mining: change some miner apis.
This commit is contained in:
parent
231d08b1c4
commit
389adee8f0
@ -449,8 +449,7 @@ Miner.prototype.build = function build(attempt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
attempt.updateCoinbase();
|
attempt.refresh();
|
||||||
attempt.updateMerkle();
|
|
||||||
|
|
||||||
assert(block.getWeight() <= attempt.weight);
|
assert(block.getWeight() <= attempt.weight);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -172,11 +172,8 @@ MinerBlock.prototype._init = function _init() {
|
|||||||
|
|
||||||
block.txs.push(cb);
|
block.txs.push(cb);
|
||||||
|
|
||||||
// Update coinbase since our coinbase was added.
|
// Update commitments.
|
||||||
this.updateCoinbase();
|
this.refresh();
|
||||||
|
|
||||||
// Create our merkle root.
|
|
||||||
this.updateMerkle();
|
|
||||||
|
|
||||||
// Initialize weight.
|
// Initialize weight.
|
||||||
this.weight = this.block.getWeight();
|
this.weight = this.block.getWeight();
|
||||||
@ -188,6 +185,23 @@ MinerBlock.prototype._init = function _init() {
|
|||||||
this.sigops = cb.getSigopsCost(null, this.flags);
|
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.
|
* Update the commitment output for segwit.
|
||||||
*/
|
*/
|
||||||
@ -226,8 +240,6 @@ MinerBlock.prototype.updateCoinbase = function updateCoinbase() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
MinerBlock.prototype.updateNonce = function updateNonce() {
|
MinerBlock.prototype.updateNonce = function updateNonce() {
|
||||||
this.block.ts = Math.max(this.network.now(), this.tip.ts + 1);
|
|
||||||
|
|
||||||
// Overflow the nonce and increment the extraNonce.
|
// Overflow the nonce and increment the extraNonce.
|
||||||
this.block.nonce = 0;
|
this.block.nonce = 0;
|
||||||
this.nonce1++;
|
this.nonce1++;
|
||||||
@ -246,20 +258,10 @@ MinerBlock.prototype.updateNonce = function updateNonce() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rebuild the merkle tree and update merkle root as well as the
|
* Rebuild the merkle tree and update merkle root.
|
||||||
* timestamp (also calls {@link MinerBlock#updateCommitment}
|
|
||||||
* if segwit is enabled).
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MinerBlock.prototype.updateMerkle = function updateMerkle() {
|
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.
|
// Recalculate merkle root.
|
||||||
this.block.merkleRoot = this.block.createMerkleRoot('hex');
|
this.block.merkleRoot = this.block.createMerkleRoot('hex');
|
||||||
};
|
};
|
||||||
@ -315,11 +317,8 @@ MinerBlock.prototype.addTX = function addTX(tx, view) {
|
|||||||
this.block.txs.push(tx);
|
this.block.txs.push(tx);
|
||||||
this.items.push(item);
|
this.items.push(item);
|
||||||
|
|
||||||
// Update coinbase value
|
// Update commitments.
|
||||||
this.updateCoinbase();
|
this.refresh();
|
||||||
|
|
||||||
// Update merkle root for new coinbase and new tx
|
|
||||||
this.updateMerkle();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@ -400,8 +399,6 @@ MinerBlock.prototype.mine = function mine() {
|
|||||||
// Track how long we've been at it.
|
// Track how long we've been at it.
|
||||||
this.begin = util.now();
|
this.begin = util.now();
|
||||||
|
|
||||||
assert(this.block.ts > this.tip.ts);
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
nonce = this.findNonce();
|
nonce = this.findNonce();
|
||||||
|
|
||||||
@ -427,8 +424,6 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() {
|
|||||||
// Track how long we've been at it.
|
// Track how long we've been at it.
|
||||||
this.begin = util.now();
|
this.begin = util.now();
|
||||||
|
|
||||||
assert(this.block.ts > this.tip.ts);
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
nonce = yield this.findNonceAsync();
|
nonce = yield this.findNonceAsync();
|
||||||
|
|
||||||
@ -451,28 +446,12 @@ MinerBlock.prototype.mineAsync = co(function* mineAsync() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
MinerBlock.prototype.iterate = function iterate() {
|
MinerBlock.prototype.iterate = function iterate() {
|
||||||
var block = this.block;
|
|
||||||
var tip = this.tip;
|
|
||||||
var now = this.network.now();
|
|
||||||
|
|
||||||
// Keep track of our iterations.
|
// Keep track of our iterations.
|
||||||
this.iterations++;
|
this.iterations++;
|
||||||
|
|
||||||
// Send progress report.
|
// Send progress report.
|
||||||
this.sendStatus();
|
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.
|
// Overflow the nonce and increment the extraNonce.
|
||||||
this.updateNonce();
|
this.updateNonce();
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user