我的挖矿函数无限循环,这正常吗?

问题描述 投票:0回答:1

我想为学校的项目实现一种加密货币,但我卡在了区块验证部分,即挖矿。我使用了我在网上找到的这段代码,并把它改编成了我的项目。

void Block::MineBlock(int nDifficulty)
{
std::string cstr(nDifficulty + 1, '\0');
for (int i = 0; i < nDifficulty; i++)
{
    cstr[i] = '0';
}
cstr[nDifficulty] = '\0';

std::string str(cstr);

do
{
    nNonce++;
    blockHash = generateHash();
} while (blockHash.substr(0, nDifficulty) != str);
this->checked = true;
}

我的nDifficulty设置为1. 它在无限循环,这正常吗? 这也是generateHash函数:

std::string Block::generateHash()
{
std::stringstream ss;
ss << index << timestamp << filename << nNonce << prevHash;
return sha256(ss.str());
}
c++ hash data-mining sha256 cryptocurrency
1个回答
0
投票

你的 str 长短不一 nDifficulty + 1.

所以,它永远不可能与一个长度等于 nDifficulty而循环条件永远不会是假的。

© www.soinside.com 2019 - 2024. All rights reserved.