X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fminer.cpp;h=2712207343584a43c6fdad95f42c7f9b4fd123fe;hb=ef17ac0211ddd486127e1f94756fbb3fd704a9b4;hp=41a4303a67241e614baf07664d56a3246b942813;hpb=309244e6fd8edbd6e34e472d5287c1d597af967d;p=novacoin.git diff --git a/src/miner.cpp b/src/miner.cpp index 41a4303..2712207 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -7,6 +7,7 @@ #include "txdb.h" #include "miner.h" #include "kernel.h" +#include "kernel_worker.h" using namespace std; @@ -69,14 +70,6 @@ public: ptx = ptxIn; dPriority = dFeePerKb = 0; } - - void print() const - { - printf("COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n", - ptx->GetHash().ToString().substr(0,10).c_str(), dPriority, dFeePerKb); - BOOST_FOREACH(uint256 hash, setDependsOn) - printf(" setDependsOn %s\n", hash.ToString().substr(0,10).c_str()); - } }; @@ -109,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; @@ -146,18 +139,18 @@ CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake) } // Largest block you're willing to create: - unsigned int nBlockMaxSize = GetArg("-blockmaxsize", MAX_BLOCK_SIZE_GEN/2); + unsigned int nBlockMaxSize = GetArgUInt("-blockmaxsize", MAX_BLOCK_SIZE_GEN/2); // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity: - nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize)); + nBlockMaxSize = std::max(1000u, std::min(MAX_BLOCK_SIZE-1000u, nBlockMaxSize)); // How much of the block should be dedicated to high-priority transactions, // included regardless of the fees they pay - unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", 27000); + unsigned int nBlockPrioritySize = GetArgUInt("-blockprioritysize", 27000); nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize); // Minimum block size you want to create; block will be filled with free transactions // until there are no more or the block reaches this size: - unsigned int nBlockMinSize = GetArg("-blockminsize", 0); + unsigned int nBlockMinSize = GetArgUInt("-blockminsize", 0); nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize); // Fee-per-kilobyte amount considered the same as "free" @@ -384,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; @@ -407,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 @@ -453,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(); @@ -485,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(); @@ -522,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"); } @@ -530,10 +523,10 @@ bool CheckStake(CBlock* pblock, CWallet& wallet) } // Precalculated SHA256 contexts and metadata -// (txid, vout.n) => (SHA256_CTX, (tx.nTime, nAmount)) -typedef std::map, std::pair > > MidstateMap; +// (txid, vout.n) => (kernel, (tx.nTime, nAmount)) +typedef std::map, std::pair, std::pair > > MidstateMap; -// Fill the inputs map with precalculated states and metadata +// Fill the inputs map with precalculated contexts and metadata bool FillMap(CWallet *pwallet, uint32_t nUpperTime, MidstateMap &inputsMap) { // Choose coins to use @@ -589,22 +582,24 @@ bool FillMap(CWallet *pwallet, uint32_t nUpperTime, MidstateMap &inputsMap) if (nStakeMinAge + block.nTime > nTime - nMaxStakeSearchInterval) continue; + // Get stake modifier uint64_t nStakeModifier = 0; if (!GetKernelStakeModifier(block.GetHash(), nStakeModifier)) continue; - SHA256_CTX ctx; - // Calculate midstate using (modifier, block time, tx offset, tx time, output number) - GetKernelMidstate(nStakeModifier, block.nTime, txindex.pos.nTxPos - txindex.pos.nBlockPos, pcoin->first->nTime, pcoin->second, ctx); + // Build static part of kernel + CDataStream ssKernel(SER_GETHASH, 0); + ssKernel << nStakeModifier; + ssKernel << block.nTime << (txindex.pos.nTxPos - txindex.pos.nBlockPos) << pcoin->first->nTime << pcoin->second; - // (txid, vout.n) => (SHA256_CTX, (tx.nTime, nAmount)) - inputsMap[key] = make_pair(ctx, make_pair(pcoin->first->nTime, pcoin->first->vout[pcoin->second].nValue)); + // (txid, vout.n) => (kernel, (tx.nTime, nAmount)) + inputsMap[key] = make_pair(std::vector(ssKernel.begin(), ssKernel.end()), make_pair(pcoin->first->nTime, pcoin->first->vout[pcoin->second].nValue)); } nStakeInputsMapSize = inputsMap.size(); if (fDebug) - printf("Stake miner: map of %" PRIu64 " precalculated contexts has been created\n", nStakeInputsMapSize); + printf("FillMap() : Map of %" PRIu64 " precalculated contexts has been created by stake miner\n", nStakeInputsMapSize); } return true; @@ -624,13 +619,13 @@ bool ScanMap(const MidstateMap &inputsMap, uint32_t nBits, MidstateMap::key_type interval.first = nSearchTime; interval.second = nSearchTime - min(nSearchTime-nLastCoinStakeSearchTime, nMaxStakeSearchInterval); - // (txid, nout) => (SHA256_CTX, (tx.nTime, nAmount)) + // (txid, nout) => (kernel, (tx.nTime, nAmount)) for(MidstateMap::const_iterator input = inputsMap.begin(); input != inputsMap.end(); input++) { - SHA256_CTX ctx = input->second.first; + unsigned char *kernel = (unsigned char *) &input->second.first[0]; // scan(State, Bits, Time, Amount, ...) - if (ScanMidstateBackward(ctx, nBits, input->second.second.first, input->second.second.second, interval, solution)) + if (ScanKernelBackward(kernel, nBits, input->second.second.first, input->second.second.second, interval, solution)) { // Solution found LuckyInput = input->first; // (txid, nout) @@ -730,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.");