X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fminer.cpp;h=0f04521bc5bba70e1ecd7f0d567e734b6ee2f133;hb=056f3b1ef51d160dad763ed43cacc151735999de;hp=ac25b97097172d1c645b4aedfd7233afb335611e;hpb=5d0ece51961598251142744ac8e85a5d725e020e;p=novacoin.git diff --git a/src/miner.cpp b/src/miner.cpp index ac25b97..0f04521 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -4,6 +4,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "txdb.h" #include "miner.h" #include "kernel.h" @@ -14,8 +15,7 @@ using namespace std; // BitcoinMiner // -string strMintMessage = "Info: Minting suspended due to locked wallet."; -string strMintWarning; +extern unsigned int nMinerSleep; int static FormatHashBlocks(void* pbuffer, unsigned int len) { @@ -78,9 +78,9 @@ public: }; -uint64 nLastBlockTx = 0; -uint64 nLastBlockSize = 0; -int64 nLastCoinStakeSearchInterval = 0; +uint64_t nLastBlockTx = 0; +uint64_t nLastBlockSize = 0; +uint32_t nLastCoinStakeSearchInterval = 0; // We want to sort transactions by priority and fee, so: typedef boost::tuple TxPriority; @@ -106,8 +106,7 @@ public: } }; -// CreateNewBlock: -// fProofOfStake: try (best effort) to make a proof-of-stake block +// CreateNewBlock: create new block (without proof-of-work/proof-of-stake) CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) { // Create new block @@ -124,7 +123,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) if (!fProofOfStake) { CReserveKey reservekey(pwallet); - txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG; + txNew.vout[0].scriptPubKey.SetDestination(reservekey.GetReservedKey().GetID()); } else txNew.vout[0].SetEmpty(); @@ -152,39 +151,16 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) // a transaction spammer can cheaply fill blocks using // 1-satoshi-fee transactions. It should be set above the real // cost to you of processing a transaction. - int64 nMinTxFee = MIN_TX_FEE; + int64_t nMinTxFee = MIN_TX_FEE; if (mapArgs.count("-mintxfee")) ParseMoney(mapArgs["-mintxfee"], nMinTxFee); - // ppcoin: if coinstake available add coinstake tx - static int64 nLastCoinStakeSearchTime = GetAdjustedTime(); // only initialized at startup CBlockIndex* pindexPrev = pindexBest; - if (fProofOfStake) // attempt to find a coinstake - { - pblock->nBits = GetNextTargetRequired(pindexPrev, true); - CTransaction txCoinStake; - int64 nSearchTime = txCoinStake.nTime; // search to current time - if (nSearchTime > nLastCoinStakeSearchTime) - { - if (pwallet->CreateCoinStake(*pwallet, pblock->nBits, nSearchTime-nLastCoinStakeSearchTime, txCoinStake)) - { - if (txCoinStake.nTime >= max(pindexPrev->GetMedianTimePast()+1, pindexPrev->GetBlockTime() - nMaxClockDrift)) - { // make sure coinstake would meet timestamp protocol - // as it would be the same as the block timestamp - pblock->vtx[0].nTime = txCoinStake.nTime; - pblock->vtx.push_back(txCoinStake); - } - } - nLastCoinStakeSearchInterval = nSearchTime - nLastCoinStakeSearchTime; - nLastCoinStakeSearchTime = nSearchTime; - } - } - - pblock->nBits = GetNextTargetRequired(pindexPrev, pblock->IsProofOfStake()); + pblock->nBits = GetNextTargetRequired(pindexPrev, fProofOfStake); // Collect memory pool transactions into the block - int64 nFees = 0; + int64_t nFees = 0; { LOCK2(cs_main, mempool.cs); CBlockIndex* pindexPrev = pindexBest; @@ -205,7 +181,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) COrphan* porphan = NULL; double dPriority = 0; - int64 nTotalIn = 0; + int64_t nTotalIn = 0; bool fMissingInputs = false; BOOST_FOREACH(const CTxIn& txin, tx.vin) { @@ -239,7 +215,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) nTotalIn += mempool.mapTx[txin.prevout.hash].vout[txin.prevout.n].nValue; continue; } - int64 nValueIn = txPrev.vout[txin.prevout.n].nValue; + int64_t nValueIn = txPrev.vout[txin.prevout.n].nValue; nTotalIn += nValueIn; int nConf = txindex.GetDepthInMainChain(); @@ -267,8 +243,8 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) // Collect transactions into block map mapTestPool; - uint64 nBlockSize = 1000; - uint64 nBlockTx = 0; + uint64_t nBlockSize = 1000; + uint64_t nBlockTx = 0; int nBlockSigOps = 100; bool fSortedByFee = (nBlockPrioritySize <= 0); @@ -296,11 +272,11 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) continue; // Timestamp limit - if (tx.nTime > GetAdjustedTime() || (pblock->IsProofOfStake() && tx.nTime > pblock->vtx[1].nTime)) + if (tx.nTime > GetAdjustedTime() || (fProofOfStake && tx.nTime > pblock->vtx[0].nTime)) continue; - // ppcoin: simplify transaction fee - allow free = false - int64 nMinFee = tx.GetMinFee(nBlockSize, false, GMF_BLOCK); + // Simplify transaction fee - allow free = false + int64_t nMinFee = tx.GetMinFee(nBlockSize, true, GMF_BLOCK, nTxSize); // Skip free transactions if we're past the minimum block size: if (fSortedByFee && (dFeePerKb < nMinTxFee) && (nBlockSize + nTxSize >= nBlockMinSize)) @@ -324,7 +300,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid)) continue; - int64 nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut(); + int64_t nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut(); if (nTxFees < nMinFee) continue; @@ -332,7 +308,7 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS) continue; - if (!tx.ConnectInputs(txdb, mapInputs, mapTestPoolTmp, CDiskTxPos(1,1,1), pindexPrev, false, true)) + if (!tx.ConnectInputs(txdb, mapInputs, mapTestPoolTmp, CDiskTxPos(1,1,1), pindexPrev, false, true, true, MANDATORY_SCRIPT_VERIFY_FLAGS)) continue; mapTestPoolTmp[tx.GetHash()] = CTxIndex(CDiskTxPos(1,1,1), tx.vout.size()); swap(mapTestPool, mapTestPoolTmp); @@ -372,19 +348,22 @@ CBlock* CreateNewBlock(CWallet* pwallet, bool fProofOfStake) nLastBlockTx = nBlockTx; nLastBlockSize = nBlockSize; - if (fDebug && GetBoolArg("-printpriority")) - printf("CreateNewBlock(): total size %"PRI64u"\n", nBlockSize); + if (!fProofOfStake) + { + pblock->vtx[0].vout[0].nValue = GetProofOfWorkReward(pblock->nBits, nFees); - if (pblock->IsProofOfWork()) - pblock->vtx[0].vout[0].nValue = GetProofOfWorkReward(pblock->nBits); + if (fDebug) + printf("CreateNewBlock(): reward %" PRIu64 "\n", pblock->vtx[0].vout[0].nValue); + } + + if (fDebug && GetBoolArg("-printpriority")) + printf("CreateNewBlock(): total size %" PRIu64 "\n", nBlockSize); // Fill in header pblock->hashPrevBlock = pindexPrev->GetBlockHash(); - if (pblock->IsProofOfStake()) - pblock->nTime = pblock->vtx[1].nTime; //same as coinstake timestamp pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime()); - pblock->nTime = max(pblock->GetBlockTime(), pindexPrev->GetBlockTime() - nMaxClockDrift); - if (pblock->IsProofOfWork()) + pblock->nTime = max(pblock->GetBlockTime(), PastDrift(pindexPrev->GetBlockTime())); + if (!fProofOfStake) pblock->UpdateTime(pindexPrev); pblock->nNonce = 0; } @@ -403,6 +382,7 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& hashPrevBlock = pblock->hashPrevBlock; } ++nExtraNonce; + unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2 pblock->vtx[0].vin[0].scriptSig = (CScript() << nHeight << CBigNum(nExtraNonce)) + COINBASE_FLAGS; assert(pblock->vtx[0].vin[0].scriptSig.size() <= 100); @@ -547,19 +527,20 @@ void StakeMiner(CWallet *pwallet) { if (fShutdown) return; - while (vNodes.empty() || IsInitialBlockDownload()) + + while (pwallet->IsLocked()) { Sleep(1000); if (fShutdown) return; } - while (pwallet->IsLocked()) + while (vNodes.empty() || IsInitialBlockDownload()) { - strMintWarning = strMintMessage; Sleep(1000); + if (fShutdown) + return; } - strMintWarning = ""; // // Create new block @@ -571,22 +552,17 @@ void StakeMiner(CWallet *pwallet) return; IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce); - if(pblock->IsProofOfStake()) + // Trying to sign a block + if (pblock->SignBlock(*pwallet)) { - // Trying to sign a block - if (!pblock->SignBlock(*pwallet)) - { - strMintWarning = strMintMessage; - continue; - } - - strMintWarning = ""; SetThreadPriority(THREAD_PRIORITY_NORMAL); CheckStake(pblock.get(), *pwallet); SetThreadPriority(THREAD_PRIORITY_LOWEST); + Sleep(500); } + else + Sleep(nMinerSleep); - Sleep(500); continue; } }