Browse Source

WIP: started fixing make check. Fixed difficulty computation.

cn_mining
Jianping Wu 6 years ago
parent
commit
937095dd4d
  1. 1
      src/primitives/block.h
  2. 7
      src/rpc/mining.cpp
  3. 14
      src/test/blockchain_tests.cpp
  4. 6
      src/test/blockencodings_tests.cpp
  5. 3
      src/test/test_bitcoin.cpp
  6. 3
      src/test/validation_block_tests.cpp
  7. 9
      src/util.cpp
  8. 3
      src/util.h

1
src/primitives/block.h

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

7
src/rpc/mining.cpp

@ -134,8 +134,9 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGen @@ -134,8 +134,9 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> 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) @@ -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) @@ -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));

14
src/test/blockchain_tests.cpp

@ -58,27 +58,27 @@ BOOST_FIXTURE_TEST_SUITE(blockchain_difficulty_tests, BasicTestingSetup) @@ -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) @@ -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) @@ -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()

6
src/test/blockencodings_tests.cpp

@ -47,7 +47,8 @@ static CBlock BuildBlockTestCase() { @@ -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) @@ -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
{

3
src/test/test_bitcoin.cpp

@ -154,7 +154,8 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& @@ -154,7 +154,8 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
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);
ProcessNewBlock(chainparams, shared_pblock, true, nullptr);

3
src/test/validation_block_tests.cpp

@ -71,8 +71,9 @@ std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock) @@ -71,8 +71,9 @@ std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> 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;

9
src/util.cpp

@ -962,7 +962,7 @@ int64_t GetStartupTime() @@ -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) @@ -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));
}

3
src/util.h

@ -349,6 +349,7 @@ std::unique_ptr<T> MakeUnique(Args&&... args) @@ -349,6 +349,7 @@ std::unique_ptr<T> MakeUnique(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

Loading…
Cancel
Save