比特币源码学习笔记

比特币源码阅读(工作量证明-检测)

2018-08-06  本文已影响0人  坠叶飘香
收到新区块后,需要检测区块的工作量证明是否真的完成了,检测代码如下:

代码文件:src/pow.cpp

/**
*
**/
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    //negative:负数
    //为负数;为0;溢出;大于最大限制
    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) //难度值是否合理
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget) //区块hash大于难度值
        return false;

    return true;
}
上一篇下一篇

猜你喜欢

热点阅读