From d364d5a2feccc53c0b2007466394865ae2f6de55 Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Tue, 17 Jun 2014 08:37:00 +0200 Subject: [PATCH] Add difficulty field to getblock / getblockbycount. Add the difficulty as new field in the JSON representation of a block. See https://github.com/namecoin/namecoin/issues/112. --- src/bitcoinrpc.cpp | 61 ++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 1c5418414..c37fbd39b 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -278,6 +278,39 @@ Value getblocknumber(const Array& params, bool fHelp) return nBestHeight; } +static double +GetDifficulty (unsigned int nBits) +{ + // Floating point number that is a multiple of the minimum difficulty, + // minimum difficulty = 1.0. + + int nShift = (nBits >> 24) & 0xff; + + double dDiff = + (double)0x0000ffff / (double)(nBits & 0x00ffffff); + + while (nShift < 29) + { + dDiff *= 256.0; + nShift++; + } + while (nShift > 29) + { + dDiff /= 256.0; + nShift--; + } + + return dDiff; +} + +static double +GetDifficulty () +{ + if (pindexBest == NULL) + return 1.0; + + return GetDifficulty (pindexBest->nBits); +} Value BlockToValue(CBlock &block) { @@ -288,6 +321,7 @@ Value BlockToValue(CBlock &block) obj.push_back(Pair("merkleroot", block.hashMerkleRoot.ToString().c_str())); obj.push_back(Pair("time", (uint64_t)block.nTime)); obj.push_back(Pair("bits", (uint64_t)block.nBits)); + obj.push_back(Pair("difficulty", GetDifficulty (block.nBits))); obj.push_back(Pair("nonce", (uint64_t)block.nNonce)); obj.push_back(Pair("n_tx", (int)block.vtx.size())); obj.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK))); @@ -492,33 +526,6 @@ Value getconnectioncount(const Array& params, bool fHelp) return (int)vNodes.size(); } - -double GetDifficulty() -{ - // Floating point number that is a multiple of the minimum difficulty, - // minimum difficulty = 1.0. - - if (pindexBest == NULL) - return 1.0; - int nShift = (pindexBest->nBits >> 24) & 0xff; - - double dDiff = - (double)0x0000ffff / (double)(pindexBest->nBits & 0x00ffffff); - - while (nShift < 29) - { - dDiff *= 256.0; - nShift++; - } - while (nShift > 29) - { - dDiff /= 256.0; - nShift--; - } - - return dDiff; -} - Value getdifficulty(const Array& params, bool fHelp) { if (fHelp || params.size() != 0)