From: CryptoManiac Date: Sat, 27 Nov 2021 11:39:14 +0000 (+0300) Subject: Use shared_ptr X-Git-Tag: nvc-v0.5.9~132 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=f54055f01b2a21b9e634e7d614a2982d49cf4f7e Use shared_ptr --- diff --git a/src/miner.cpp b/src/miner.cpp index aae9589..2712207 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -102,12 +102,12 @@ public: }; // CreateNewBlock: create new block (without proof-of-work/with provided coinstake) -CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake) +std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake) { bool fProofOfStake = txCoinStake != NULL; // Create new block - auto_ptr pblock(new CBlock()); + shared_ptr pblock(new CBlock()); if (!pblock.get()) return NULL; @@ -377,11 +377,11 @@ CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake) pblock->nNonce = 0; } - return pblock.release(); + return pblock; } -void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) +void IncrementExtraNonce(shared_ptr& pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) { // Update nExtraNonce static uint256 hashPrevBlock; @@ -400,7 +400,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& } -void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1) +void FormatHashBuffers(const shared_ptr& pblock, char* pmidstate, char* pdata, char* phash1) { // // Pre-build hash buffers @@ -446,7 +446,7 @@ void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash } -bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) +bool CheckWork(const std::shared_ptr& pblock, CWallet& wallet, CReserveKey& reservekey) { uint256 hashBlock = pblock->GetHash(); uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); @@ -478,14 +478,14 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) } // Process this block the same as if we had received it from another node - if (!ProcessBlock(NULL, pblock)) + if (!ProcessBlock(NULL, pblock.get())) return error("CheckWork() : ProcessBlock, block not accepted"); } return true; } -bool CheckStake(CBlock* pblock, CWallet& wallet) +bool CheckStake(const std::shared_ptr& pblock, CWallet& wallet) { uint256 proofHash = 0, hashTarget = 0; uint256 hashBlock = pblock->GetHash(); @@ -515,7 +515,7 @@ bool CheckStake(CBlock* pblock, CWallet& wallet) } // Process this block the same as if we had received it from another node - if (!ProcessBlock(NULL, pblock)) + if (!ProcessBlock(NULL, pblock.get())) return error("CheckStake() : ProcessBlock, block not accepted"); } @@ -725,8 +725,7 @@ void ThreadStakeMiner(void* parg) } // Now we have new coinstake, it's time to create the block ... - CBlock* pblock; - pblock = CreateNewBlock(pwallet, &txCoinStake); + std::shared_ptr pblock = CreateNewBlock(pwallet, &txCoinStake); if (!pblock) { string strMessage = _("Warning: Unable to allocate memory for the new block object. Mining thread has been stopped."); diff --git a/src/miner.h b/src/miner.h index d19dc21..40167af 100644 --- a/src/miner.h +++ b/src/miner.h @@ -8,21 +8,22 @@ #include "main.h" #include "wallet.h" +#include /* Generate a new block, without valid proof-of-work/with provided proof-of-stake */ -CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txAdd=NULL); +std::shared_ptr CreateNewBlock(CWallet* pwallet, CTransaction *txAdd=NULL); /** Modify the extranonce in a block */ -void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce); +void IncrementExtraNonce(std::shared_ptr& pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce); /** Do mining precalculation */ -void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1); +void FormatHashBuffers(const std::shared_ptr& pblock, char* pmidstate, char* pdata, char* phash1); /** Check mined proof-of-work block */ -bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey); +bool CheckWork(const std::shared_ptr& pblock, CWallet& wallet, CReserveKey& reservekey); /** Check mined proof-of-stake block */ -bool CheckStake(CBlock* pblock, CWallet& wallet); +bool CheckStake(const std::shared_ptr& pblock, CWallet& wallet); /** Base sha256 mining transform */ void SHA256Transform(void* pstate, void* pinput, const void* pinit); diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 62d8837..8f3358f 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -243,9 +243,9 @@ Value getworkex(const Array& params, bool fHelp) if (IsInitialBlockDownload()) throw JSONRPCError(-10, "NovaCoin is downloading blocks..."); - typedef map > mapNewBlock_t; + typedef map, CScript> > mapNewBlock_t; static mapNewBlock_t mapNewBlock; - static vector vNewBlock; + static vector> vNewBlock; static CReserveKey reservekey(pwalletMain); if (params.size() == 0) @@ -254,7 +254,7 @@ Value getworkex(const Array& params, bool fHelp) static unsigned int nTransactionsUpdatedLast; static CBlockIndex* pindexPrev; static int64_t nStart; - static CBlock* pblock; + static shared_ptr pblock; if (pindexPrev != pindexBest || (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)) { @@ -262,8 +262,6 @@ Value getworkex(const Array& params, bool fHelp) { // Deallocate old blocks since they're obsolete now mapNewBlock.clear(); - BOOST_FOREACH(CBlock* pblock, vNewBlock) - delete pblock; vNewBlock.clear(); } nTransactionsUpdatedLast = nTransactionsUpdated; @@ -339,7 +337,7 @@ Value getworkex(const Array& params, bool fHelp) // Get saved block if (!mapNewBlock.count(pdata->hashMerkleRoot)) return false; - CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first; + std::shared_ptr pblock = mapNewBlock[pdata->hashMerkleRoot].first; pblock->nTime = pdata->nTime; pblock->nNonce = pdata->nNonce; @@ -374,9 +372,9 @@ Value getwork(const Array& params, bool fHelp) if (IsInitialBlockDownload()) throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "NovaCoin is downloading blocks..."); - typedef map > mapNewBlock_t; + typedef map, CScript> > mapNewBlock_t; static mapNewBlock_t mapNewBlock; // FIXME: thread safety - static vector vNewBlock; + static vector> vNewBlock; static CReserveKey reservekey(pwalletMain); if (params.size() == 0) @@ -385,7 +383,7 @@ Value getwork(const Array& params, bool fHelp) static unsigned int nTransactionsUpdatedLast; static CBlockIndex* pindexPrev; static int64_t nStart; - static CBlock* pblock; + static shared_ptr pblock; if (pindexPrev != pindexBest || (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)) { @@ -393,8 +391,6 @@ Value getwork(const Array& params, bool fHelp) { // Deallocate old blocks since they're obsolete now mapNewBlock.clear(); - BOOST_FOREACH(CBlock* pblock, vNewBlock) - delete pblock; vNewBlock.clear(); } @@ -457,7 +453,7 @@ Value getwork(const Array& params, bool fHelp) // Get saved block if (!mapNewBlock.count(pdata->hashMerkleRoot)) return false; - CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first; + std::shared_ptr pblock = mapNewBlock[pdata->hashMerkleRoot].first; pblock->nTime = pdata->nTime; pblock->nNonce = pdata->nNonce; @@ -521,7 +517,7 @@ Value getblocktemplate(const Array& params, bool fHelp) static unsigned int nTransactionsUpdatedLast; static CBlockIndex* pindexPrev; static int64_t nStart; - static CBlock* pblock; + static std::shared_ptr pblock; if (pindexPrev != pindexBest || (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 5)) { @@ -536,8 +532,7 @@ Value getblocktemplate(const Array& params, bool fHelp) // Create new block if(pblock) { - delete pblock; - pblock = NULL; + pblock.reset(); } pblock = CreateNewBlock(pwalletMain); if (!pblock)