随着比特币网络算力指数级的增长,挖矿的难度也随之增大,以保证每十分钟生成一个区块。
难度目标:
hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
nBits每2016个区块根据出块速度调整一次,其计算函数如下:
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
{
const unsigned int nTargetTimespan = 14 * 24 * 60 * 60; // two weeks 单位秒
const unsigned int nTargetSpacing = 10 * 60; //理想状态 每10分钟生成一个区块
const unsigned int nInterval = nTargetTimespan / nTargetSpacing; //每2016个区块调整一次难度
// Genesis block
if (pindexLast == NULL) //创世区块采用系统定义的最小难度值
return bnProofOfWorkLimit.GetCompact();
// Only change once per interval
if ((pindexLast->nHeight+1) % nInterval != 0)
return pindexLast->nBits;
// Go back by what we want to be 14 days worth of blocks
const CBlockIndex* pindexFirst = pindexLast; //pindexFirst 指向2016个区块之前的区块
for (int i = 0; pindexFirst && i < nInterval-1; i++)
pindexFirst = pindexFirst->pprev;
assert(pindexFirst);
// Limit adjustment step
unsigned int nActualTimespan = pindexLast->nTime - pindexFirst->nTime; // nActualTimespan 生成最近2016个区块所用的时间
printf(" nActualTimespan = %d before bounds\n", nActualTimespan);
if (nActualTimespan < nTargetTimespan/4) //限定nActualTimespan 最小最大值
nActualTimespan = nTargetTimespan/4;
if (nActualTimespan > nTargetTimespan*4)
nActualTimespan = nTargetTimespan*4;
// Retarget
CBigNum bnNew;
bnNew.SetCompact(pindexLast->nBits); //新的难度值 = 旧难度值 * (nActualTimespan/nTargetTimespan)
bnNew *= nActualTimespan; // bnNew 数值与难度成反比
bnNew /= nTargetTimespan;
if (bnNew > bnProofOfWorkLimit) //bnProofOfWorkLimit 最小难度值
bnNew = bnProofOfWorkLimit;
/// debug print
printf("\n\n\nGetNextWorkRequired RETARGET *****\n");
printf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
return bnNew.GetCompact(); //返回新的难度对应的 nBits 值
}
矿工节点每次构造新区块时都会调用上面的函数算出当前的难度值,然后在对新区快头进行hash运算 以得出符合条件的nNonce值
nBits:存放在区块头 占4字节,第一个字节为幂,后三个字节为底数
target = CBigNum.setCompact(nBits)
nBits = target.getCompact()
难度值target 是一个CBigNum对象
- CBigNum
CBigNum是openssl库中定义的BIGNUM的包装类。公钥密码学需要能够处理非常大的整数。标准的数据类型无法满足要求。BIGNUM可以存放任意长度的整型。
CBigNum类的结构并不复杂。它是由一堆不同类型构造BIGNUM的构造器组成,包括char,short,int,long,int64,int256,它们unsigned版本和vector<unsigned char>等。它同样重构操作符,例如加、减、乘、除、位操作等。所有的实际工作代理给了BIGNUM类行。大部分CBigNum的代码仅仅是为BIGNUM的函数准备输入数据。
CBigNum& SetCompact(unsigned int nCompact)
{
unsigned int nSize = nCompact >> 24;
std::vector<unsigned char> vch(4 + nSize);
vch[3] = nSize;
if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
BN_mpi2bn(&vch[0], vch.size(), this); //调用openssl库函数
return *this;
}
unsigned int GetCompact() const
{
unsigned int nSize = BN_bn2mpi(this, NULL);
std::vector<unsigned char> vch(nSize);
nSize -= 4;
BN_bn2mpi(this, &vch[0]);
unsigned int nCompact = nSize << 24;
if (nSize >= 1) nCompact |= (vch[4] << 16);
if (nSize >= 2) nCompact |= (vch[5] << 8);
if (nSize >= 3) nCompact |= (vch[6] << 0);
return nCompact;
}
网友评论