From 68ffbfc104460b46d71a972f6cacdced46128820 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Thu, 21 Mar 2019 23:10:45 -0700 Subject: [PATCH] Changed block hash calculation, used cryptonote header instead. --- src/chainparams.cpp | 8 ++++---- src/primitives/block.cpp | 15 ++++----------- src/primitives/block.h | 4 ++-- src/rpc/blockchain.cpp | 4 ++-- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 4473fcfe4..67957f4a2 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -36,7 +36,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi genesis.hashMerkleRoot = BlockMerkleRoot(genesis); genesis.cnHeader.major_version = 10; // Cryptonight variant 4 - genesis.cnHeader.prev_id = genesis.GetHash(); + genesis.cnHeader.prev_id = genesis.GetOriginalBlockHash(); genesis.cnHeader.nonce = nNonce; return genesis; } @@ -120,7 +120,7 @@ public: //TODO: target: 0x1e0fffff, update timestampe and nonce. genesis = CreateGenesisBlock(1553145843, 6146, 0x1f0ffff0, 1, genesisBlockReward); consensus.hashGenesisBlock = genesis.GetHash(); - assert(consensus.hashGenesisBlock == uint256S("0x8bc96b56465c9aa5f6511c4d2fca2f6bf6ae67b9c9553272908ae511d59e9b77")); + assert(consensus.hashGenesisBlock == uint256S("0xbad6e50683aa6032b9ea9bbbc67a6b4621612221176b963fc9ce9ae10a77effd")); assert(genesis.hashMerkleRoot == uint256S("0xb21d4680875c0e472b7dbf3dbab2aaeb2dbbb5fa8b154f978b5ea0706d1fd5b9")); // Note that of those with the service bits flag, most only support a subset of possible options @@ -226,7 +226,7 @@ public: printf("new testnet genesis hash: %s\n", genesis.GetHash().ToString().c_str()); } #endif - assert(consensus.hashGenesisBlock == uint256S("cb3c982e8c3d3f7d03b745ba9c511d0a586045b4170c977a72883d3277bdd5e6")); + assert(consensus.hashGenesisBlock == uint256S("f1b4d0460060c2cdfe4be2e71c15f23338b262b72dfa73c389cc144896a453ec")); assert(genesis.hashMerkleRoot == uint256S("3cf6c3b6da3f4058853ee70369ee43d473aca91ae8fc8f44a645beb21c392d80")); vFixedSeeds.clear(); @@ -315,7 +315,7 @@ public: genesis = CreateGenesisBlock(1553147907, 6, 0x207fffff, 1, 50 * COIN); consensus.hashGenesisBlock = genesis.GetHash(); - assert(consensus.hashGenesisBlock == uint256S("0x193385e60d04db90e30332508d97401ec3568ee2fe765b279fd9b3e4b0b78ca5")); + assert(consensus.hashGenesisBlock == uint256S("0x16c9c15caf4524e61004fcd4feb2217818b8884d4ad9b8769f5ea6c62c69097e")); assert(genesis.hashMerkleRoot == uint256S("0x13ec98c3307b8e6b67b91c605c7347916a99f9dfde7b5d88365aaef322192314")); vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds. diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp index d90d7f2bf..0f273a820 100644 --- a/src/primitives/block.cpp +++ b/src/primitives/block.cpp @@ -15,23 +15,16 @@ extern "C" void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int prehashed, uint64_t height); extern "C" void cn_fast_hash(const void *data, size_t length, char *hash); -uint256 CBlockHeader::GetHash() const +uint256 CBlockHeader::GetOriginalBlockHash() const { CHashWriter hashWriter(SER_GETHASH, PROTOCOL_VERSION); hashWriter.write(BEGIN(nVersion), 80); return hashWriter.GetHash(); } -uint256 CBlockHeader::GetCryptonoteFastHash() const +uint256 CBlockHeader::GetHash() const { uint256 thash; - // prev_id of CN header is used to store the kevacoin block hash. - // The value of prev_id and block hash must be the same to prove - // that PoW has been properly done. - if (GetHash() != cnHeader.prev_id) { - memset(thash.begin(), 0xff, thash.size()); - return thash; - } cryptonote::blobdata blob = cryptonote::t_serializable_object_to_blob(cnHeader); cn_fast_hash(blob.data(), blob.size(), BEGIN(thash)); return thash; @@ -43,7 +36,7 @@ uint256 CBlockHeader::GetPoWHash() const // prev_id of CN header is used to store the kevacoin block hash. // The value of prev_id and block hash must be the same to prove // that PoW has been properly done. - if (GetHash() != cnHeader.prev_id) { + if (GetOriginalBlockHash() != cnHeader.prev_id) { memset(thash.begin(), 0xff, thash.size()); return thash; } @@ -56,7 +49,7 @@ std::string CBlock::ToString() const { std::stringstream s; s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n", - GetHash().ToString(), + GetOriginalBlockHash().ToString(), nVersion, hashPrevBlock.ToString(), hashMerkleRoot.ToString(), diff --git a/src/primitives/block.h b/src/primitives/block.h index e35418f6b..566b3e44e 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -168,9 +168,9 @@ public: return (nBits == 0); } - uint256 GetHash() const; + uint256 GetOriginalBlockHash() const; - uint256 GetCryptonoteFastHash() const; + uint256 GetHash() const; uint256 GetPoWHash() const; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a44d80ca5..8f417e3ec 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -763,7 +763,7 @@ UniValue getlastblockheader(const JSONRPCRequest& request) blockHeader.push_back(Pair("height", (uint64_t)pblockindex->nHeight)); const uint64_t depth = chainActive.Height() - pblockindex->nHeight + 1; // Same as confirmations. blockHeader.push_back(Pair("depth", (uint64_t)depth)); - blockHeader.push_back(Pair("hash", block.GetCryptonoteFastHash().GetHex())); + blockHeader.push_back(Pair("hash", block.GetHash().GetHex())); blockHeader.push_back(Pair("difficulty", GetDifficulty(pblockindex))); // TODO: implement cumulative_difficulty @@ -846,7 +846,7 @@ UniValue getblockheaderbyheight(const JSONRPCRequest& request) blockHeader.push_back(Pair("height", (uint64_t)pblockindex->nHeight)); const uint64_t depth = chainActive.Height() - nHeight + 1; // Same as confirmations. blockHeader.push_back(Pair("depth", (uint64_t)depth)); - blockHeader.push_back(Pair("hash", block.GetCryptonoteFastHash().GetHex())); + blockHeader.push_back(Pair("hash", block.GetHash().GetHex())); blockHeader.push_back(Pair("difficulty", GetDifficulty(pblockindex))); // TODO: implement cumulative_difficulty