rpc: move getDifficulty.

This commit is contained in:
Christopher Jeffrey 2017-03-16 14:13:03 -07:00
parent 35d9727bc3
commit 1f10bf8253
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

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