From 74e7fc7d432c7f6d586d54ff276d6babad0fe505 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Mon, 12 Aug 2019 02:26:40 +0200 Subject: [PATCH] [Miner] Don't create new keys when generating PoS blocks key creation for coinbase output is a waste for PoS blocks and can be removed --- src/miner.cpp | 11 +++++++---- src/miner.h | 2 +- src/rpc/mining.cpp | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 10a1f988f23e..89502c2667e3 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -576,7 +576,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& double dHashesPerSec = 0.0; int64_t nHPSTimerStart = 0; -CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet, bool fProofOfStake) +CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet) { CPubKey pubkey; if (!reservekey.GetReservedKey(pubkey)) @@ -586,7 +586,8 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet, static int nLastPOWBlock = Params().LAST_POW_BLOCK(); // If we're building a late PoW block, don't continue - if ((nHeightNext > nLastPOWBlock) && !fProofOfStake) { + // PoS blocks are built directly with CreateNewBlock + if ((nHeightNext > nLastPOWBlock)) { LogPrintf("%s: Aborting PoW block creation during PoS phase\n", __func__); // sleep 1/2 a block time so we don't go into a tight loop. MilliSleep((Params().TargetSpacing() * 1000) >> 1); @@ -594,7 +595,7 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet, } CScript scriptPubKey = CScript() << ToByteVector(pubkey) << OP_CHECKSIG; - return CreateNewBlock(scriptPubKey, pwallet, fProofOfStake); + return CreateNewBlock(scriptPubKey, pwallet, false); } bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) @@ -709,7 +710,9 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake) if (!pindexPrev) continue; - std::unique_ptr pblocktemplate(CreateNewBlockWithKey(reservekey, pwallet, fProofOfStake)); + std::unique_ptr pblocktemplate( + fProofOfStake ? CreateNewBlock(CScript(), pwallet, fProofOfStake) : CreateNewBlockWithKey(reservekey, pwallet) + ); if (!pblocktemplate.get()) continue; diff --git a/src/miner.h b/src/miner.h index 4a76ceb027d4..ab4668979fa3 100644 --- a/src/miner.h +++ b/src/miner.h @@ -29,7 +29,7 @@ void UpdateTime(CBlockHeader* block, const CBlockIndex* pindexPrev); /** Run the miner threads */ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads); /** Generate a new block, without valid proof-of-work */ - CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet, bool fProofOfStake); + CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, CWallet* pwallet); void BitcoinMiner(CWallet* pwallet, bool fProofOfStake); void ThreadStakeMinter(); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index ad32b3ed3c7a..2c66aafac0d2 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -154,7 +154,9 @@ UniValue generate(const UniValue& params, bool fHelp) while (nHeight < nHeightEnd) { - std::unique_ptr pblocktemplate(CreateNewBlockWithKey(reservekey, pwalletMain, fPoS)); + std::unique_ptr pblocktemplate( + fPoS ? CreateNewBlock(CScript(), pwalletMain, fPoS) : CreateNewBlockWithKey(reservekey, pwalletMain) + ); if (!pblocktemplate.get()) throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); CBlock *pblock = &pblocktemplate->block;