Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show All 36 Lines | struct CUpdatedBlock { | ||||
uint256 hash; | uint256 hash; | ||||
int height; | int height; | ||||
}; | }; | ||||
static Mutex cs_blockchange; | static Mutex cs_blockchange; | ||||
static std::condition_variable cond_blockchange; | static std::condition_variable cond_blockchange; | ||||
static CUpdatedBlock latestblock; | static CUpdatedBlock latestblock; | ||||
static double GetDifficultyFromBits(uint32_t nBits) { | /* Calculate the difficulty for a given block index. | ||||
int nShift = (nBits >> 24) & 0xff; | */ | ||||
double dDiff = 0x0000ffff / double(nBits & 0x00ffffff); | double GetDifficulty(const CBlockIndex *blockindex) { | ||||
if (blockindex == nullptr) { | |||||
return 1.0; | |||||
} | |||||
int nShift = (blockindex->nBits >> 24) & 0xff; | |||||
double dDiff = | |||||
(double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff); | |||||
while (nShift < 29) { | while (nShift < 29) { | ||||
dDiff *= 256.0; | dDiff *= 256.0; | ||||
nShift++; | nShift++; | ||||
} | } | ||||
while (nShift > 29) { | while (nShift > 29) { | ||||
dDiff /= 256.0; | dDiff /= 256.0; | ||||
nShift--; | nShift--; | ||||
} | } | ||||
return dDiff; | return dDiff; | ||||
} | } | ||||
double GetDifficulty(const CBlockIndex *blockindex) { | |||||
// Floating point number that is a multiple of the minimum difficulty, | |||||
// minimum difficulty = 1.0. | |||||
if (blockindex == nullptr) { | |||||
return 1.0; | |||||
} | |||||
return GetDifficultyFromBits(blockindex->nBits); | |||||
} | |||||
UniValue blockheaderToJSON(const CBlockIndex *blockindex) { | UniValue blockheaderToJSON(const CBlockIndex *blockindex) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
UniValue result(UniValue::VOBJ); | UniValue result(UniValue::VOBJ); | ||||
result.pushKV("hash", blockindex->GetBlockHash().GetHex()); | result.pushKV("hash", blockindex->GetBlockHash().GetHex()); | ||||
int confirmations = -1; | int confirmations = -1; | ||||
// Only report confirmations if the block is on the main chain | // Only report confirmations if the block is on the main chain | ||||
if (chainActive.Contains(blockindex)) { | if (chainActive.Contains(blockindex)) { | ||||
confirmations = chainActive.Height() - blockindex->nHeight + 1; | confirmations = chainActive.Height() - blockindex->nHeight + 1; | ||||
▲ Show 20 Lines • Show All 1,854 Lines • Show Last 20 Lines |