WIP: started fixing make check. Fixed difficulty computation.

This commit is contained in:
Jianping Wu 2019-03-20 20:51:25 -07:00
parent d48d05a182
commit 937095dd4d
8 changed files with 29 additions and 17 deletions

View File

@ -134,6 +134,7 @@ public:
CBlockHeader() CBlockHeader()
{ {
SetNull(); SetNull();
cnHeader.SetNull();
} }
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;

View File

@ -134,8 +134,9 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGen
LOCK(cs_main); LOCK(cs_main);
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce); IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
} }
pblock->cnHeader.prev_id = pblock->GetHash();
while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) { while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) {
++pblock->nNonce; ++pblock->cnHeader.nonce;
--nMaxTries; --nMaxTries;
} }
if (nMaxTries == 0) { if (nMaxTries == 0) {
@ -437,7 +438,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
LOCK(cs_main); LOCK(cs_main);
std::string strMode = "template"; std::string strMode = "template";
#if 0 #if 1
// TODO: IMPORTANT!!!! uncomment the following!!! // TODO: IMPORTANT!!!! uncomment the following!!!
if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0) if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Kevacoin is not connected!"); throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Kevacoin is not connected!");
@ -595,7 +596,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
std::string hex_template_blob = HexStr(block_blob.begin(), block_blob.end()); std::string hex_template_blob = HexStr(block_blob.begin(), block_blob.end());
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
const uint64_t difficulty = ConvertNBitsToDiff(pblock->nBits); const uint64_t difficulty = ConvertNBitsToDiffU64(pblock->nBits);
result.push_back(Pair("blocktemplate_blob", hex_template_blob)); result.push_back(Pair("blocktemplate_blob", hex_template_blob));
result.push_back(Pair("difficulty", (double)difficulty)); result.push_back(Pair("difficulty", (double)difficulty));
result.push_back(Pair("height", (uint64_t)height)); result.push_back(Pair("height", (uint64_t)height));

View File

@ -58,27 +58,27 @@ BOOST_FIXTURE_TEST_SUITE(blockchain_difficulty_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target) BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
{ {
TestDifficulty(0x1f111111, 0.937486); TestDifficulty(0x1f111111, 3839.941635);
} }
BOOST_AUTO_TEST_CASE(get_difficulty_for_low_target) BOOST_AUTO_TEST_CASE(get_difficulty_for_low_target)
{ {
TestDifficulty(0x1ef88f6f, 16.478648); TestDifficulty(0x1ef88f6f, 67496.542470);
} }
BOOST_AUTO_TEST_CASE(get_difficulty_for_mid_target) BOOST_AUTO_TEST_CASE(get_difficulty_for_mid_target)
{ {
TestDifficulty(0x1df88f6f, 4218.533904); TestDifficulty(0x1df88f6f, 17279114.872370);
} }
BOOST_AUTO_TEST_CASE(get_difficulty_for_high_target) BOOST_AUTO_TEST_CASE(get_difficulty_for_high_target)
{ {
TestDifficulty(0x1cf88f6f, 1079944.679523); TestDifficulty(0x1cf88f6f, 4423453407.326761);
} }
BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target) BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)
{ {
TestDifficulty(0x12345678, 6200371373479302643982960427008.000000); TestDifficulty(0x12345678, 25396721145771223629754205909024768.000000);
} }
// Verify that difficulty is 1.0 for an empty chain. // Verify that difficulty is 1.0 for an empty chain.
@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(get_difficulty_for_null_block_index)
double difficulty = GetDifficulty(chain, nullptr); double difficulty = GetDifficulty(chain, nullptr);
delete chain.Tip(); delete chain.Tip();
double expected_difficulty = 4218.533904; double expected_difficulty = 17279114.872370;
RejectDifficultyMismatch(difficulty, expected_difficulty); RejectDifficultyMismatch(difficulty, expected_difficulty);
} }
@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(get_difficulty_for_block_index_overrides_tip)
delete chain.Tip(); delete chain.Tip();
delete override_block_index; delete override_block_index;
RejectDifficultyMismatch(difficulty, 6200371373479302643982960427008.000000); RejectDifficultyMismatch(difficulty, 25396721145771223629754205909024768.000000);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -47,7 +47,8 @@ static CBlock BuildBlockTestCase() {
bool mutated; bool mutated;
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated); block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
assert(!mutated); assert(!mutated);
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, Params().GetConsensus())) ++block.nNonce; block.cnHeader.prev_id = block.GetHash();
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, Params().GetConsensus())) ++block.cnHeader.nonce;
return block; return block;
} }
@ -292,7 +293,8 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
bool mutated; bool mutated;
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated); block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
assert(!mutated); assert(!mutated);
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, Params().GetConsensus())) ++block.nNonce; block.cnHeader.prev_id = block.GetHash();
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, Params().GetConsensus())) ++block.cnHeader.nonce;
// Test simple header round-trip with only coinbase // Test simple header round-trip with only coinbase
{ {

View File

@ -154,7 +154,8 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce); IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
} }
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce; block.cnHeader.prev_id = block.GetHash();
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, chainparams.GetConsensus())) ++block.cnHeader.nonce;
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block); std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
ProcessNewBlock(chainparams, shared_pblock, true, nullptr); ProcessNewBlock(chainparams, shared_pblock, true, nullptr);

View File

@ -71,8 +71,9 @@ std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock)
{ {
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
pblock->cnHeader.prev_id = pblock->GetHash();
while (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) { while (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) {
++(pblock->nNonce); ++(pblock->cnHeader.nonce);
} }
return pblock; return pblock;

View File

@ -962,7 +962,7 @@ int64_t GetStartupTime()
return nStartupTime; return nStartupTime;
} }
uint64_t ConvertNBitsToDiff(uint32_t nBits) double ConvertNBitsToDiff(uint32_t nBits)
{ {
int nShift = (nBits >> 24) & 0xff; int nShift = (nBits >> 24) & 0xff;
@ -981,5 +981,10 @@ uint64_t ConvertNBitsToDiff(uint32_t nBits)
nShift--; nShift--;
} }
return (uint64_t)round(dDiff); return dDiff;
}
uint64_t ConvertNBitsToDiffU64(uint32_t nBits)
{
return (uint64_t)round(ConvertNBitsToDiff(nBits));
} }

View File

@ -349,6 +349,7 @@ std::unique_ptr<T> MakeUnique(Args&&... args)
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
} }
uint64_t ConvertNBitsToDiff(uint32_t nBits); double ConvertNBitsToDiff(uint32_t nBits);
uint64_t ConvertNBitsToDiffU64(uint32_t nBits);
#endif // BITCOIN_UTIL_H #endif // BITCOIN_UTIL_H