diff --git a/src/primitives/block.h b/src/primitives/block.h index e84ea8109..9ae37ebd0 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -134,6 +134,7 @@ public: CBlockHeader() { SetNull(); + cnHeader.SetNull(); } ADD_SERIALIZE_METHODS; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 2bc8e7cf5..1d641a295 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -134,8 +134,9 @@ UniValue generateBlocks(std::shared_ptr coinbaseScript, int nGen LOCK(cs_main); IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce); } + pblock->cnHeader.prev_id = pblock->GetHash(); while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) { - ++pblock->nNonce; + ++pblock->cnHeader.nonce; --nMaxTries; } if (nMaxTries == 0) { @@ -437,7 +438,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request) LOCK(cs_main); std::string strMode = "template"; -#if 0 +#if 1 // TODO: IMPORTANT!!!! uncomment the following!!! if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0) 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()); 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("difficulty", (double)difficulty)); result.push_back(Pair("height", (uint64_t)height)); diff --git a/src/test/blockchain_tests.cpp b/src/test/blockchain_tests.cpp index c30bae2c5..48f475eb6 100644 --- a/src/test/blockchain_tests.cpp +++ b/src/test/blockchain_tests.cpp @@ -58,27 +58,27 @@ BOOST_FIXTURE_TEST_SUITE(blockchain_difficulty_tests, BasicTestingSetup) 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) { - TestDifficulty(0x1ef88f6f, 16.478648); + TestDifficulty(0x1ef88f6f, 67496.542470); } 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) { - TestDifficulty(0x1cf88f6f, 1079944.679523); + TestDifficulty(0x1cf88f6f, 4423453407.326761); } 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. @@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(get_difficulty_for_null_block_index) double difficulty = GetDifficulty(chain, nullptr); delete chain.Tip(); - double expected_difficulty = 4218.533904; + double expected_difficulty = 17279114.872370; RejectDifficultyMismatch(difficulty, expected_difficulty); } @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(get_difficulty_for_block_index_overrides_tip) delete chain.Tip(); delete override_block_index; - RejectDifficultyMismatch(difficulty, 6200371373479302643982960427008.000000); + RejectDifficultyMismatch(difficulty, 25396721145771223629754205909024768.000000); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp index 0ec60fd36..d1f0b206c 100644 --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -47,7 +47,8 @@ static CBlock BuildBlockTestCase() { bool mutated; block.hashMerkleRoot = BlockMerkleRoot(block, &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; } @@ -292,7 +293,8 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest) bool mutated; block.hashMerkleRoot = BlockMerkleRoot(block, &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 { diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 9d1e84678..879707f86 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -154,7 +154,8 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector& 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 shared_pblock = std::make_shared(block); ProcessNewBlock(chainparams, shared_pblock, true, nullptr); diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp index b19ed6c14..c430d9065 100644 --- a/src/test/validation_block_tests.cpp +++ b/src/test/validation_block_tests.cpp @@ -71,8 +71,9 @@ std::shared_ptr FinalizeBlock(std::shared_ptr pblock) { pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); + pblock->cnHeader.prev_id = pblock->GetHash(); while (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) { - ++(pblock->nNonce); + ++(pblock->cnHeader.nonce); } return pblock; diff --git a/src/util.cpp b/src/util.cpp index 568d39cdc..6e5b4aa4d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -962,7 +962,7 @@ int64_t GetStartupTime() return nStartupTime; } -uint64_t ConvertNBitsToDiff(uint32_t nBits) +double ConvertNBitsToDiff(uint32_t nBits) { int nShift = (nBits >> 24) & 0xff; @@ -981,5 +981,10 @@ uint64_t ConvertNBitsToDiff(uint32_t nBits) nShift--; } - return (uint64_t)round(dDiff); + return dDiff; +} + +uint64_t ConvertNBitsToDiffU64(uint32_t nBits) +{ + return (uint64_t)round(ConvertNBitsToDiff(nBits)); } \ No newline at end of file diff --git a/src/util.h b/src/util.h index f9036f702..8e8793c62 100644 --- a/src/util.h +++ b/src/util.h @@ -349,6 +349,7 @@ std::unique_ptr MakeUnique(Args&&... args) return std::unique_ptr(new T(std::forward(args)...)); } -uint64_t ConvertNBitsToDiff(uint32_t nBits); +double ConvertNBitsToDiff(uint32_t nBits); +uint64_t ConvertNBitsToDiffU64(uint32_t nBits); #endif // BITCOIN_UTIL_H