From 5ce806a7239f68a4d12cefcb865c13169e33ad65 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 12 Mar 2017 10:47:50 -0700 Subject: [PATCH] rpc: fix cpuminer. --- lib/http/rpc.js | 6 ++--- lib/mining/cpuminer.js | 59 +++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 6b7a4675..da9829c7 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1423,7 +1423,7 @@ RPC.prototype.getGenerate = co(function* getGenerate(args, help) { RPC.prototype.setGenerate = co(function* setGenerate(args, help) { var valid = new Validator([args]); 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) throw new RPCError('setgenerate mine ( proclimit )'); @@ -1432,11 +1432,11 @@ RPC.prototype.setGenerate = co(function* setGenerate(args, help) { this.procLimit = limit; if (mine) { - this.miner.start().catch(util.nop); + this.miner.cpu.start(); return true; } - yield this.miner.stop(); + yield this.miner.cpu.stop(); return false; }); diff --git a/lib/mining/cpuminer.js b/lib/mining/cpuminer.js index 2b2d19d9..fe47b123 100644 --- a/lib/mining/cpuminer.js +++ b/lib/mining/cpuminer.js @@ -39,7 +39,6 @@ function CPUMiner(miner) { this.running = false; this.stopping = false; this.job = null; - this.since = 0; this.stopJob = null; this._init(); @@ -96,10 +95,20 @@ CPUMiner.prototype._close = co(function* close() { /** * Start mining. * @method + */ + +CPUMiner.prototype.start = function start() { + this._start().catch(util.nop); +}; + +/** + * Start mining. + * @method + * @private * @returns {Promise} */ -CPUMiner.prototype.start = co(function* start() { +CPUMiner.prototype._start = co(function* start() { var block, entry, job; assert(!this.running, 'Miner is already running.'); @@ -155,7 +164,7 @@ CPUMiner.prototype.start = co(function* start() { if (!entry) { this.logger.warning('Mined a bad-prevblk (race condition?)'); - break; + continue; } if (this.stopping) @@ -206,6 +215,11 @@ CPUMiner.prototype.stop = co(function* stop() { this.stopping = true; + if (this.job) { + this.job.destroy(); + this.job = null; + } + yield this.wait(); this.running = false; @@ -265,9 +279,9 @@ CPUMiner.prototype.notifyEntry = function notifyEntry() { if (!this.job) return; - if (++this.since > 20) { - this.since = 0; + if (util.now() - this.job.start > 10) { this.job.destroy(); + this.job = null; } }; @@ -342,8 +356,7 @@ CPUMiner.prototype.findNonceAsync = co(function* findNonceAsync(job) { CPUMiner.prototype.mine = function mine(job) { var nonce; - // Track how long we've been at it. - job.begin = util.now(); + job.start = util.now(); for (;;) { nonce = this.findNonce(job); @@ -351,7 +364,9 @@ CPUMiner.prototype.mine = function mine(job) { if (nonce !== -1) break; - this.iterate(job); + job.updateNonce(); + + this.sendStatus(job, 0); } return job.commit(nonce); @@ -367,8 +382,7 @@ CPUMiner.prototype.mine = function mine(job) { CPUMiner.prototype.mineAsync = co(function* mineAsync(job) { var nonce; - // Track how long we've been at it. - job.begin = util.now(); + job.start = util.now(); for (;;) { nonce = yield this.findNonceAsync(job); @@ -379,23 +393,14 @@ CPUMiner.prototype.mineAsync = co(function* mineAsync(job) { if (job.destroyed) return; - this.iterate(job); + job.updateNonce(); + + this.sendStatus(job, 0); } 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`). * @param {CPUJob} job @@ -409,7 +414,7 @@ CPUMiner.prototype.sendStatus = function sendStatus(job, nonce) { var hashrate = job.getRate(nonce); 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), hashes, attempt.bits, @@ -432,8 +437,7 @@ function CPUJob(miner, attempt) { this.attempt = attempt; this.destroyed = false; this.committed = false; - this.iterations = 0; - this.begin = 0; + this.start = util.now(); this.nonce1 = 0; this.nonce2 = 0; this.refresh(); @@ -528,7 +532,7 @@ CPUJob.prototype.destroy = function destroy() { */ 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) { - return Math.floor(nonce / (util.now() - this.begin)); + var hashes = this.getHashes(nonce); + return Math.floor(hashes / (util.now() - this.start)); }; /**