rpc: fix cpuminer.

This commit is contained in:
Christopher Jeffrey 2017-03-12 10:47:50 -07:00
parent f7853aa639
commit 5ce806a723
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 35 additions and 30 deletions

View File

@ -1423,7 +1423,7 @@ RPC.prototype.getGenerate = co(function* getGenerate(args, help) {
RPC.prototype.setGenerate = co(function* setGenerate(args, help) { RPC.prototype.setGenerate = co(function* setGenerate(args, help) {
var valid = new Validator([args]); var valid = new Validator([args]);
var mine = valid.bool(0, false); var mine = valid.bool(0, false);
var limit = valid.u32(0, 0); var limit = valid.u32(1, 0);
if (help || args.length < 1 || args.length > 2) if (help || args.length < 1 || args.length > 2)
throw new RPCError('setgenerate mine ( proclimit )'); throw new RPCError('setgenerate mine ( proclimit )');
@ -1432,11 +1432,11 @@ RPC.prototype.setGenerate = co(function* setGenerate(args, help) {
this.procLimit = limit; this.procLimit = limit;
if (mine) { if (mine) {
this.miner.start().catch(util.nop); this.miner.cpu.start();
return true; return true;
} }
yield this.miner.stop(); yield this.miner.cpu.stop();
return false; return false;
}); });

View File

@ -39,7 +39,6 @@ function CPUMiner(miner) {
this.running = false; this.running = false;
this.stopping = false; this.stopping = false;
this.job = null; this.job = null;
this.since = 0;
this.stopJob = null; this.stopJob = null;
this._init(); this._init();
@ -96,10 +95,20 @@ CPUMiner.prototype._close = co(function* close() {
/** /**
* Start mining. * Start mining.
* @method * @method
*/
CPUMiner.prototype.start = function start() {
this._start().catch(util.nop);
};
/**
* Start mining.
* @method
* @private
* @returns {Promise} * @returns {Promise}
*/ */
CPUMiner.prototype.start = co(function* start() { CPUMiner.prototype._start = co(function* start() {
var block, entry, job; var block, entry, job;
assert(!this.running, 'Miner is already running.'); assert(!this.running, 'Miner is already running.');
@ -155,7 +164,7 @@ CPUMiner.prototype.start = co(function* start() {
if (!entry) { if (!entry) {
this.logger.warning('Mined a bad-prevblk (race condition?)'); this.logger.warning('Mined a bad-prevblk (race condition?)');
break; continue;
} }
if (this.stopping) if (this.stopping)
@ -206,6 +215,11 @@ CPUMiner.prototype.stop = co(function* stop() {
this.stopping = true; this.stopping = true;
if (this.job) {
this.job.destroy();
this.job = null;
}
yield this.wait(); yield this.wait();
this.running = false; this.running = false;
@ -265,9 +279,9 @@ CPUMiner.prototype.notifyEntry = function notifyEntry() {
if (!this.job) if (!this.job)
return; return;
if (++this.since > 20) { if (util.now() - this.job.start > 10) {
this.since = 0;
this.job.destroy(); this.job.destroy();
this.job = null;
} }
}; };
@ -342,8 +356,7 @@ CPUMiner.prototype.findNonceAsync = co(function* findNonceAsync(job) {
CPUMiner.prototype.mine = function mine(job) { CPUMiner.prototype.mine = function mine(job) {
var nonce; var nonce;
// Track how long we've been at it. job.start = util.now();
job.begin = util.now();
for (;;) { for (;;) {
nonce = this.findNonce(job); nonce = this.findNonce(job);
@ -351,7 +364,9 @@ CPUMiner.prototype.mine = function mine(job) {
if (nonce !== -1) if (nonce !== -1)
break; break;
this.iterate(job); job.updateNonce();
this.sendStatus(job, 0);
} }
return job.commit(nonce); return job.commit(nonce);
@ -367,8 +382,7 @@ CPUMiner.prototype.mine = function mine(job) {
CPUMiner.prototype.mineAsync = co(function* mineAsync(job) { CPUMiner.prototype.mineAsync = co(function* mineAsync(job) {
var nonce; var nonce;
// Track how long we've been at it. job.start = util.now();
job.begin = util.now();
for (;;) { for (;;) {
nonce = yield this.findNonceAsync(job); nonce = yield this.findNonceAsync(job);
@ -379,23 +393,14 @@ CPUMiner.prototype.mineAsync = co(function* mineAsync(job) {
if (job.destroyed) if (job.destroyed)
return; return;
this.iterate(job); job.updateNonce();
this.sendStatus(job, 0);
} }
return job.commit(nonce); return job.commit(nonce);
}); });
/**
* Increment extraNonce and send status.
* @param {CPUJob} job
*/
CPUMiner.prototype.iterate = function iterate(job) {
job.iterations++;
job.updateNonce();
this.sendStatus(job, 0);
};
/** /**
* Send a progress report (emits `status`). * Send a progress report (emits `status`).
* @param {CPUJob} job * @param {CPUJob} job
@ -409,7 +414,7 @@ CPUMiner.prototype.sendStatus = function sendStatus(job, nonce) {
var hashrate = job.getRate(nonce); var hashrate = job.getRate(nonce);
this.logger.info( this.logger.info(
'Status: hashrate=%dkhs hashes=%d target=%d height=%d best=%s', 'Status: hashrate=%dkhs hashes=%d target=%d height=%d tip=%s',
Math.floor(hashrate / 1000), Math.floor(hashrate / 1000),
hashes, hashes,
attempt.bits, attempt.bits,
@ -432,8 +437,7 @@ function CPUJob(miner, attempt) {
this.attempt = attempt; this.attempt = attempt;
this.destroyed = false; this.destroyed = false;
this.committed = false; this.committed = false;
this.iterations = 0; this.start = util.now();
this.begin = 0;
this.nonce1 = 0; this.nonce1 = 0;
this.nonce2 = 0; this.nonce2 = 0;
this.refresh(); this.refresh();
@ -528,7 +532,7 @@ CPUJob.prototype.destroy = function destroy() {
*/ */
CPUJob.prototype.getHashes = function getHashes(nonce) { CPUJob.prototype.getHashes = function getHashes(nonce) {
return this.iterations * 0xffffffff + nonce; return this.nonce1 * 0x100000000 + this.nonce2 + nonce;
}; };
/** /**
@ -537,7 +541,8 @@ CPUJob.prototype.getHashes = function getHashes(nonce) {
*/ */
CPUJob.prototype.getRate = function getRate(nonce) { CPUJob.prototype.getRate = function getRate(nonce) {
return Math.floor(nonce / (util.now() - this.begin)); var hashes = this.getHashes(nonce);
return Math.floor(hashes / (util.now() - this.start));
}; };
/** /**