From ddc43217caf3f64113d754acf435f714601eb1b2 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Thu, 21 Mar 2019 16:20:45 -0700 Subject: [PATCH] Genesis block also used cn header. --- src/chainparams.cpp | 37 +++++++++++++++++++++++++++++++------ src/primitives/block.cpp | 12 ------------ src/primitives/block.h | 3 +-- src/rpc/mining.cpp | 10 +++++----- src/validation.cpp | 2 +- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index a2f336ee5..4473fcfe4 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -29,11 +29,15 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi CBlock genesis; genesis.nTime = nTime; genesis.nBits = nBits; - genesis.nNonce = nNonce; + genesis.nNonce = 0; // Used as height. genesis.nVersion = nVersion; genesis.vtx.push_back(MakeTransactionRef(std::move(txNew))); genesis.hashPrevBlock.SetNull(); genesis.hashMerkleRoot = BlockMerkleRoot(genesis); + + genesis.cnHeader.major_version = 10; // Cryptonight variant 4 + genesis.cnHeader.prev_id = genesis.GetHash(); + genesis.cnHeader.nonce = nNonce; return genesis; } @@ -114,9 +118,9 @@ public: const uint32_t genesisBlockReward = 0.00001 * COIN; // A small reward for the core developers :-) //TODO: target: 0x1e0fffff, update timestampe and nonce. - genesis = CreateGenesisBlock(1553145843, 1706, 0x1f0ffff0, 1, genesisBlockReward); + genesis = CreateGenesisBlock(1553145843, 6146, 0x1f0ffff0, 1, genesisBlockReward); consensus.hashGenesisBlock = genesis.GetHash(); - assert(consensus.hashGenesisBlock == uint256S("0x413c6fb7d5a8484d027ce6654fdbe0567cab5684da807cd2bafb7808228710ad")); + assert(consensus.hashGenesisBlock == uint256S("0x8bc96b56465c9aa5f6511c4d2fca2f6bf6ae67b9c9553272908ae511d59e9b77")); assert(genesis.hashMerkleRoot == uint256S("0xb21d4680875c0e472b7dbf3dbab2aaeb2dbbb5fa8b154f978b5ea0706d1fd5b9")); // Note that of those with the service bits flag, most only support a subset of possible options @@ -203,16 +207,35 @@ public: nDefaultPort = 19335; nPruneAfterHeight = 1000; - genesis = CreateGenesisBlock(1553100443, 404, 0x1f0ffff0, 1, 500 * COIN); + genesis = CreateGenesisBlock(1553100443, 5758, 0x1f0ffff0, 1, 500 * COIN); consensus.hashGenesisBlock = genesis.GetHash(); - assert(consensus.hashGenesisBlock == uint256S("b2c61c05d5a1d34c6664784500715fc2959f828c32cddefc835ec037b138d0bc")); +#if 0 + arith_uint256 hashTarget = arith_uint256().SetCompact(genesis.nBits); + uint256 hashGenesisBlock = uint256S("0x01"); + if (genesis.GetHash() != hashGenesisBlock) { + printf("recalculating params for testnet.\n"); + printf("old testnet genesis nonce: %d\n", genesis.cnHeader.nonce); + printf("old testnet genesis hash: %s\n", hashGenesisBlock.ToString().c_str()); + // deliberately empty for loop finds nonce value. + printf("hashTarget is: %s\n", hashTarget.ToString().c_str()); + for(genesis.cnHeader.nonce = 0; hashTarget < UintToArith256(genesis.GetPoWHash()); genesis.cnHeader.nonce++) { + printf("nNonce: %d\n\n", genesis.cnHeader.nonce); + } + printf("new testnet genesis merkle root: %s\n", genesis.hashMerkleRoot.ToString().c_str()); + printf("new testnet genesis nonce: %d\n", genesis.cnHeader.nonce); + printf("new testnet genesis hash: %s\n", genesis.GetHash().ToString().c_str()); + } +#endif + assert(consensus.hashGenesisBlock == uint256S("cb3c982e8c3d3f7d03b745ba9c511d0a586045b4170c977a72883d3277bdd5e6")); assert(genesis.hashMerkleRoot == uint256S("3cf6c3b6da3f4058853ee70369ee43d473aca91ae8fc8f44a645beb21c392d80")); vFixedSeeds.clear(); vSeeds.clear(); // nodes with support for servicebits filtering should be at the top +#if 0 vSeeds.emplace_back("testnet-seed.kevacoin.org"); vSeeds.emplace_back("testnet-seed.honourchat.com"); +#endif base58Prefixes[PUBKEY_ADDRESS] = std::vector(1,55); // P base58Prefixes[SCRIPT_ADDRESS] = std::vector(1,5); @@ -223,7 +246,9 @@ public: base58Prefixes[KEVA_NAMESPACE] = std::vector(1,53); // N bech32_hrp = "tkva"; +#if 0 vFixedSeeds = std::vector(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test)); +#endif fDefaultConsistencyChecks = false; fRequireStandard = false; @@ -288,7 +313,7 @@ public: nDefaultPort = 19444; nPruneAfterHeight = 1000; - genesis = CreateGenesisBlock(1553147907, 0, 0x207fffff, 1, 50 * COIN); + genesis = CreateGenesisBlock(1553147907, 6, 0x207fffff, 1, 50 * COIN); consensus.hashGenesisBlock = genesis.GetHash(); assert(consensus.hashGenesisBlock == uint256S("0x193385e60d04db90e30332508d97401ec3568ee2fe765b279fd9b3e4b0b78ca5")); assert(genesis.hashMerkleRoot == uint256S("0x13ec98c3307b8e6b67b91c605c7347916a99f9dfde7b5d88365aaef322192314")); diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp index de6aeb311..d90d7f2bf 100644 --- a/src/primitives/block.cpp +++ b/src/primitives/block.cpp @@ -25,12 +25,6 @@ uint256 CBlockHeader::GetHash() const uint256 CBlockHeader::GetCryptonoteFastHash() const { uint256 thash; - if (hashPrevBlock.IsNull()) { - // Genesis block has no CN fast hash. - memset(thash.begin(), 0xff, thash.size()); - return 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. @@ -46,12 +40,6 @@ uint256 CBlockHeader::GetCryptonoteFastHash() const uint256 CBlockHeader::GetPoWHash() const { uint256 thash; - if (hashPrevBlock.IsNull()) { - // Genesis block - cn_slow_hash(BEGIN(nVersion), 80, BEGIN(thash), 4, 0, 0); - return 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. diff --git a/src/primitives/block.h b/src/primitives/block.h index 97e9a97e0..e35418f6b 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -147,8 +147,7 @@ public: READWRITE(nTime); READWRITE(nBits); READWRITE(nNonce); - // Genesis block does not have cnHeader. - if (!hashPrevBlock.IsNull() && !legacyMode) { + if (!legacyMode) { READWRITE(cnHeader); } } diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 1d641a295..d134c7bdd 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -416,29 +416,29 @@ UniValue getblocktemplate(const JSONRPCRequest& request) // reserve_size if (request.params[0].getType() != UniValue::VNUM) { - throw JSONRPCError(RPC_INVALID_PARAMS, "reserve_size must be an integer"); + throw CN_JSONRPCError(CORE_RPC_ERROR_CODE_WRONG_PARAM, "reserve_size must be an integer"); } reserve_size = request.params[0].get_int(); if (reserve_size <= 0 || reserve_size > MAX_RESERVE_SIZE) { - throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid reserve_size"); + throw CN_JSONRPCError(CORE_RPC_ERROR_CODE_WRONG_PARAM, "Invalid reserve_size"); } // wallet_address if (request.params[1].getType() != UniValue::VSTR) { - throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid wallet address, string expected"); + throw CN_JSONRPCError(CORE_RPC_ERROR_CODE_WRONG_PARAM, "Invalid wallet address, string expected"); } wallet_address = request.params[1].get_str(); walletDest = DecodeDestination(wallet_address); if (walletDest.which() == 0) { - throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid wallet address"); + throw CN_JSONRPCError(CORE_RPC_ERROR_CODE_WRONG_PARAM, "Invalid wallet address"); } LOCK(cs_main); std::string strMode = "template"; -#if 1 +#if 0 // TODO: IMPORTANT!!!! uncomment the following!!! if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0) throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Kevacoin is not connected!"); diff --git a/src/validation.cpp b/src/validation.cpp index 3e8aa78ae..f89c09160 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1841,7 +1841,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl // Check the height of the block is the same as its height on the blockchain. // Note: nNonce now holds the value of height. It is no longer used as nonce. - if (!block.hashPrevBlock.IsNull() && (int32_t)block.nNonce != pindex->nHeight) + if ((int32_t)block.nNonce != pindex->nHeight) return error("%s: block height mismatch", __func__); // Check it again in case a previous version let a bad block in