diff --git a/lib/http/rpc.js b/lib/http/rpc.js index b0668eaa..cac8ee1d 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -169,7 +169,7 @@ RPC.prototype.getInfo = co(function* getInfo(args, help) { timeoffset: this.network.time.offset, connections: this.pool.peers.size(), proxy: '', - difficulty: this.difficulty(), + difficulty: toDifficulty(this.chain.tip.bits), testnet: this.network !== Network.main, keypoololdest: 0, keypoolsize: 0, @@ -453,7 +453,7 @@ RPC.prototype.getBlockchainInfo = co(function* getBlockchainInfo(args, help) { blocks: this.chain.height, headers: this.chain.height, bestblockhash: this.chain.tip.rhash(), - difficulty: this.difficulty(), + difficulty: toDifficulty(this.chain.tip.bits), mediantime: yield this.chain.tip.getMedianTime(), verificationprogress: this.chain.getProgress(), chainwork: this.chain.tip.chainwork.toString('hex', 64), @@ -589,7 +589,7 @@ RPC.prototype.getDifficulty = co(function* getDifficulty(args, help) { if (help || args.length !== 0) throw new RPCError(errs.MISC_ERROR, 'getdifficulty'); - return this.difficulty(); + return toDifficulty(this.chain.tip.bits); }); RPC.prototype.getMempoolInfo = co(function* getMempoolInfo(args, help) { @@ -1353,6 +1353,7 @@ RPC.prototype.getMiningInfo = co(function* getMiningInfo(args, help) { var size = 0; var weight = 0; var txs = 0; + var diff = 0; var i, item; if (help || args.length !== 0) @@ -1361,6 +1362,7 @@ RPC.prototype.getMiningInfo = co(function* getMiningInfo(args, help) { if (attempt) { weight = attempt.weight; txs = attempt.items.length + 1; + diff = attempt.getDifficulty(); size = 1000; for (i = 0; i < attempt.items.length; i++) { item = attempt.items[i]; @@ -1373,7 +1375,7 @@ RPC.prototype.getMiningInfo = co(function* getMiningInfo(args, help) { currentblocksize: size, currentblockweight: weight, currentblocktx: txs, - difficulty: this.difficulty(), + difficulty: diff, errors: '', genproclimit: this.procLimit, networkhashps: yield this.getHashRate(120), @@ -2365,28 +2367,6 @@ RPC.prototype.getBIP9Softforks = co(function* getBIP9Softforks() { return forks; }); -RPC.prototype.difficulty = function difficulty(entry) { - var shift, diff; - - if (!entry) - entry = this.chain.tip; - - shift = (entry.bits >>> 24) & 0xff; - diff = 0x0000ffff / (entry.bits & 0x00ffffff); - - while (shift < 29) { - diff *= 256.0; - shift++; - } - - while (shift > 29) { - diff /= 256.0; - shift--; - } - - return diff; -}; - RPC.prototype.getHashRate = co(function* getHashRate(lookup, height) { var tip = this.chain.tip; var i, minTime, maxTime, workDiff, timeDiff, ps, entry; @@ -2573,7 +2553,7 @@ RPC.prototype.headerToJSON = co(function* headerToJSON(entry) { time: entry.ts, mediantime: medianTime, bits: entry.bits, - difficulty: this.difficulty(entry), + difficulty: toDifficulty(entry.bits), chainwork: entry.chainwork.toString('hex', 64), previousblockhash: entry.prevBlock !== encoding.NULL_HASH ? util.revHex(entry.prevBlock) @@ -2614,7 +2594,7 @@ RPC.prototype.blockToJSON = co(function* blockToJSON(entry, block, details) { time: entry.ts, mediantime: mtp, bits: entry.bits, - difficulty: this.difficulty(entry), + difficulty: toDifficulty(entry.bits), chainwork: entry.chainwork.toString('hex', 64), previousblockhash: entry.prevBlock !== encoding.NULL_HASH ? util.revHex(entry.prevBlock) @@ -2701,6 +2681,23 @@ function parseSecret(raw, network) { } } +function toDifficulty(bits) { + var shift = (bits >>> 24) & 0xff; + var diff = 0x0000ffff / (bits & 0x00ffffff); + + while (shift < 29) { + diff *= 256.0; + shift++; + } + + while (shift > 29) { + diff /= 256.0; + shift--; + } + + return diff; +} + /* * Expose */