Calculate the difficulty for a given block index. */
| 57 | /* Calculate the difficulty for a given block index. |
| 58 | */ |
| 59 | double GetDifficulty(const CBlockIndex* blockindex) |
| 60 | { |
| 61 | if (blockindex == nullptr) |
| 62 | { |
| 63 | return 1.0; |
| 64 | } |
| 65 | |
| 66 | // Floating point number that is a multiple of the minimum difficulty, |
| 67 | // minimum difficulty = 1.0. |
| 68 | |
| 69 | uint32_t bits = blockindex->nBits; |
| 70 | uint32_t powLimit = 0; |
| 71 | |
| 72 | if (blockindex->nHeight >= Params().GetConsensus().BTGHeight) { |
| 73 | powLimit = UintToArith256(Params().GetConsensus().powLimit).GetCompact(); |
| 74 | } else { |
| 75 | powLimit = 0x1d00ffff; |
| 76 | } |
| 77 | |
| 78 | int nShift = (bits >> 24) & 0xff; |
| 79 | int nShiftAmount = (powLimit >> 24) & 0xff; |
| 80 | |
| 81 | double dDiff = (double)(powLimit & 0x00ffffff) / (double)(bits & 0x00ffffff); |
| 82 | |
| 83 | while (nShift < nShiftAmount) |
| 84 | { |
| 85 | dDiff *= 256.0; |
| 86 | nShift++; |
| 87 | } |
| 88 | while (nShift > nShiftAmount) |
| 89 | { |
| 90 | dDiff /= 256.0; |
| 91 | nShift--; |
| 92 | } |
| 93 | |
| 94 | return dDiff; |
| 95 | } |
| 96 | |
| 97 | UniValue blockheaderToJSON(const CBlockIndex* blockindex) |
| 98 | { |