Get rid of fTrickle parameter.
[novacoin.git] / src / miner.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2013 The NovaCoin developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7 #include "txdb.h"
8 #include "miner.h"
9 #include "kernel.h"
10 #include "kernel_worker.h"
11
12 using namespace std;
13
14 //////////////////////////////////////////////////////////////////////////////
15 //
16 // BitcoinMiner
17 //
18
19 int64_t nReserveBalance = 0;
20 static unsigned int nMaxStakeSearchInterval = 60;
21 uint64_t nStakeInputsMapSize = 0;
22
23 int static FormatHashBlocks(void* pbuffer, unsigned int len)
24 {
25     unsigned char* pdata = (unsigned char*)pbuffer;
26     unsigned int blocks = 1 + ((len + 8) / 64);
27     unsigned char* pend = pdata + 64 * blocks;
28     memset(pdata + len, 0, 64 * blocks - len);
29     pdata[len] = 0x80;
30     unsigned int bits = len * 8;
31     pend[-1] = (bits >> 0) & 0xff;
32     pend[-2] = (bits >> 8) & 0xff;
33     pend[-3] = (bits >> 16) & 0xff;
34     pend[-4] = (bits >> 24) & 0xff;
35     return blocks;
36 }
37
38 static const unsigned int pSHA256InitState[8] =
39 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
40
41 void SHA256Transform(void* pstate, void* pinput, const void* pinit)
42 {
43     SHA256_CTX ctx;
44     unsigned char data[64];
45
46     SHA256_Init(&ctx);
47
48     for (int i = 0; i < 16; i++)
49         ((uint32_t*)data)[i] = ByteReverse(((uint32_t*)pinput)[i]);
50
51     for (int i = 0; i < 8; i++)
52         ctx.h[i] = ((uint32_t*)pinit)[i];
53
54     SHA256_Update(&ctx, data, sizeof(data));
55     for (int i = 0; i < 8; i++)
56         ((uint32_t*)pstate)[i] = ctx.h[i];
57 }
58
59 // Some explaining would be appreciated
60 class COrphan
61 {
62 public:
63     CTransaction* ptx;
64     set<uint256> setDependsOn;
65     double dPriority;
66     double dFeePerKb;
67
68     COrphan(CTransaction* ptxIn)
69     {
70         ptx = ptxIn;
71         dPriority = dFeePerKb = 0;
72     }
73
74     void print() const
75     {
76         printf("COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n",
77                ptx->GetHash().ToString().substr(0,10).c_str(), dPriority, dFeePerKb);
78         BOOST_FOREACH(uint256 hash, setDependsOn)
79             printf("   setDependsOn %s\n", hash.ToString().substr(0,10).c_str());
80     }
81 };
82
83
84 uint64_t nLastBlockTx = 0;
85 uint64_t nLastBlockSize = 0;
86 uint32_t nLastCoinStakeSearchInterval = 0;
87  
88 // We want to sort transactions by priority and fee, so:
89 typedef boost::tuple<double, double, CTransaction*> TxPriority;
90 class TxPriorityCompare
91 {
92     bool byFee;
93 public:
94     TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
95     bool operator()(const TxPriority& a, const TxPriority& b)
96     {
97         if (byFee)
98         {
99             if (a.get<1>() == b.get<1>())
100                 return a.get<0>() < b.get<0>();
101             return a.get<1>() < b.get<1>();
102         }
103         else
104         {
105             if (a.get<0>() == b.get<0>())
106                 return a.get<1>() < b.get<1>();
107             return a.get<0>() < b.get<0>();
108         }
109     }
110 };
111
112 // CreateNewBlock: create new block (without proof-of-work/with provided coinstake)
113 CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake)
114 {
115     bool fProofOfStake = txCoinStake != NULL;
116
117     // Create new block
118     auto_ptr<CBlock> pblock(new CBlock());
119     if (!pblock.get())
120         return NULL;
121
122     // Create coinbase tx
123     CTransaction txCoinBase;
124     txCoinBase.vin.resize(1);
125     txCoinBase.vin[0].prevout.SetNull();
126     txCoinBase.vout.resize(1);
127
128     if (!fProofOfStake)
129     {
130         CReserveKey reservekey(pwallet);
131         txCoinBase.vout[0].scriptPubKey.SetDestination(reservekey.GetReservedKey().GetID());
132
133         // Add our coinbase tx as first transaction
134         pblock->vtx.push_back(txCoinBase);
135     }
136     else
137     {
138         // Coinbase output must be empty for Proof-of-Stake block
139         txCoinBase.vout[0].SetEmpty();
140
141         // Syncronize timestamps
142         pblock->nTime = txCoinBase.nTime = txCoinStake->nTime;
143
144         // Add coinbase and coinstake transactions
145         pblock->vtx.push_back(txCoinBase);
146         pblock->vtx.push_back(*txCoinStake);
147     }
148
149     // Largest block you're willing to create:
150     unsigned int nBlockMaxSize = GetArgUInt("-blockmaxsize", MAX_BLOCK_SIZE_GEN/2);
151     // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
152     nBlockMaxSize = std::max(1000u, std::min(MAX_BLOCK_SIZE-1000u, nBlockMaxSize));
153
154     // How much of the block should be dedicated to high-priority transactions,
155     // included regardless of the fees they pay
156     unsigned int nBlockPrioritySize = GetArgUInt("-blockprioritysize", 27000);
157     nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
158
159     // Minimum block size you want to create; block will be filled with free transactions
160     // until there are no more or the block reaches this size:
161     unsigned int nBlockMinSize = GetArgUInt("-blockminsize", 0);
162     nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
163
164     // Fee-per-kilobyte amount considered the same as "free"
165     // Be careful setting this: if you set it to zero then
166     // a transaction spammer can cheaply fill blocks using
167     // 1-satoshi-fee transactions. It should be set above the real
168     // cost to you of processing a transaction.
169     int64_t nMinTxFee = MIN_TX_FEE;
170     if (mapArgs.count("-mintxfee"))
171         ParseMoney(mapArgs["-mintxfee"], nMinTxFee);
172
173     CBlockIndex* pindexPrev = pindexBest;
174
175     pblock->nBits = GetNextTargetRequired(pindexPrev, fProofOfStake);
176
177     // Collect memory pool transactions into the block
178     int64_t nFees = 0;
179     {
180         LOCK2(cs_main, mempool.cs);
181         CBlockIndex* pindexPrev = pindexBest;
182         CTxDB txdb("r");
183
184         // Priority order to process transactions
185         list<COrphan> vOrphan; // list memory doesn't move
186         map<uint256, vector<COrphan*> > mapDependers;
187
188         // This vector will be sorted into a priority queue:
189         vector<TxPriority> vecPriority;
190         vecPriority.reserve(mempool.mapTx.size());
191         for (map<uint256, CTransaction>::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi)
192         {
193             CTransaction& tx = (*mi).second;
194             if (tx.IsCoinBase() || tx.IsCoinStake() || !tx.IsFinal())
195                 continue;
196
197             COrphan* porphan = NULL;
198             double dPriority = 0;
199             int64_t nTotalIn = 0;
200             bool fMissingInputs = false;
201             BOOST_FOREACH(const CTxIn& txin, tx.vin)
202             {
203                 // Read prev transaction
204                 CTransaction txPrev;
205                 CTxIndex txindex;
206                 if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
207                 {
208                     // This should never happen; all transactions in the memory
209                     // pool should connect to either transactions in the chain
210                     // or other transactions in the memory pool.
211                     if (!mempool.mapTx.count(txin.prevout.hash))
212                     {
213                         printf("ERROR: mempool transaction missing input\n");
214                         if (fDebug) assert("mempool transaction missing input" == 0);
215                         fMissingInputs = true;
216                         if (porphan)
217                             vOrphan.pop_back();
218                         break;
219                     }
220
221                     // Has to wait for dependencies
222                     if (!porphan)
223                     {
224                         // Use list for automatic deletion
225                         vOrphan.push_back(COrphan(&tx));
226                         porphan = &vOrphan.back();
227                     }
228                     mapDependers[txin.prevout.hash].push_back(porphan);
229                     porphan->setDependsOn.insert(txin.prevout.hash);
230                     nTotalIn += mempool.mapTx[txin.prevout.hash].vout[txin.prevout.n].nValue;
231                     continue;
232                 }
233                 int64_t nValueIn = txPrev.vout[txin.prevout.n].nValue;
234                 nTotalIn += nValueIn;
235
236                 int nConf = txindex.GetDepthInMainChain();
237                 dPriority += (double)nValueIn * nConf;
238             }
239             if (fMissingInputs) continue;
240
241             // Priority is sum(valuein * age) / txsize
242             unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
243             dPriority /= nTxSize;
244
245             // This is a more accurate fee-per-kilobyte than is used by the client code, because the
246             // client code rounds up the size to the nearest 1K. That's good, because it gives an
247             // incentive to create smaller transactions.
248             double dFeePerKb =  double(nTotalIn-tx.GetValueOut()) / (double(nTxSize)/1000.0);
249
250             if (porphan)
251             {
252                 porphan->dPriority = dPriority;
253                 porphan->dFeePerKb = dFeePerKb;
254             }
255             else
256                 vecPriority.push_back(TxPriority(dPriority, dFeePerKb, &(*mi).second));
257         }
258
259         // Collect transactions into block
260         map<uint256, CTxIndex> mapTestPool;
261         uint64_t nBlockSize = 1000;
262         uint64_t nBlockTx = 0;
263         int nBlockSigOps = 100;
264         bool fSortedByFee = (nBlockPrioritySize <= 0);
265
266         TxPriorityCompare comparer(fSortedByFee);
267         std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
268
269         while (!vecPriority.empty())
270         {
271             // Take highest priority transaction off the priority queue:
272             double dPriority = vecPriority.front().get<0>();
273             double dFeePerKb = vecPriority.front().get<1>();
274             CTransaction& tx = *(vecPriority.front().get<2>());
275
276             std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
277             vecPriority.pop_back();
278
279             // Size limits
280             unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
281             if (nBlockSize + nTxSize >= nBlockMaxSize)
282                 continue;
283
284             // Legacy limits on sigOps:
285             unsigned int nTxSigOps = tx.GetLegacySigOpCount();
286             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
287                 continue;
288
289             // Timestamp limit
290             if (tx.nTime > GetAdjustedTime() || (fProofOfStake && tx.nTime > txCoinStake->nTime))
291                 continue;
292
293             // Skip free transactions if we're past the minimum block size:
294             if (fSortedByFee && (dFeePerKb < nMinTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
295                 continue;
296
297             // Prioritize by fee once past the priority size or we run out of high-priority
298             // transactions:
299             if (!fSortedByFee &&
300                 ((nBlockSize + nTxSize >= nBlockPrioritySize) || (dPriority < COIN * 144 / 250)))
301             {
302                 fSortedByFee = true;
303                 comparer = TxPriorityCompare(fSortedByFee);
304                 std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
305             }
306
307             // Connecting shouldn't fail due to dependency on other memory pool transactions
308             // because we're already processing them in order of dependency
309             map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
310             MapPrevTx mapInputs;
311             bool fInvalid;
312             if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid))
313                 continue;
314
315             // Transaction fee
316             int64_t nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();
317             int64_t nMinFee = tx.GetMinFee(nBlockSize, true, GMF_BLOCK, nTxSize);
318             if (nTxFees < nMinFee)
319                 continue;
320
321             // Sigops accumulation
322             nTxSigOps += tx.GetP2SHSigOpCount(mapInputs);
323             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
324                 continue;
325
326             if (!tx.ConnectInputs(txdb, mapInputs, mapTestPoolTmp, CDiskTxPos(1,1,1), pindexPrev, false, true, true, MANDATORY_SCRIPT_VERIFY_FLAGS))
327                 continue;
328             mapTestPoolTmp[tx.GetHash()] = CTxIndex(CDiskTxPos(1,1,1), tx.vout.size());
329             swap(mapTestPool, mapTestPoolTmp);
330
331             // Added
332             pblock->vtx.push_back(tx);
333             nBlockSize += nTxSize;
334             ++nBlockTx;
335             nBlockSigOps += nTxSigOps;
336             nFees += nTxFees;
337
338             if (fDebug && GetBoolArg("-printpriority"))
339             {
340                 printf("priority %.1f feeperkb %.1f txid %s\n",
341                        dPriority, dFeePerKb, tx.GetHash().ToString().c_str());
342             }
343
344             // Add transactions that depend on this one to the priority queue
345             uint256 hash = tx.GetHash();
346             if (mapDependers.count(hash))
347             {
348                 BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
349                 {
350                     if (!porphan->setDependsOn.empty())
351                     {
352                         porphan->setDependsOn.erase(hash);
353                         if (porphan->setDependsOn.empty())
354                         {
355                             vecPriority.push_back(TxPriority(porphan->dPriority, porphan->dFeePerKb, porphan->ptx));
356                             std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
357                         }
358                     }
359                 }
360             }
361         }
362
363         nLastBlockTx = nBlockTx;
364         nLastBlockSize = nBlockSize;
365
366         if (!fProofOfStake)
367         {
368             pblock->vtx[0].vout[0].nValue = GetProofOfWorkReward(pblock->nBits, nFees);
369
370             if (fDebug)
371                 printf("CreateNewBlock(): PoW reward %" PRIu64 "\n", pblock->vtx[0].vout[0].nValue);
372         }
373
374         if (fDebug && GetBoolArg("-printpriority"))
375             printf("CreateNewBlock(): total size %" PRIu64 "\n", nBlockSize);
376
377         // Fill in header
378         pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
379         if (!fProofOfStake)
380         {
381             pblock->nTime          = max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime());
382             pblock->nTime          = max(pblock->GetBlockTime(), PastDrift(pindexPrev->GetBlockTime()));
383             pblock->UpdateTime(pindexPrev);
384         }
385         pblock->nNonce         = 0;
386     }
387
388     return pblock.release();
389 }
390
391
392 void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
393 {
394     // Update nExtraNonce
395     static uint256 hashPrevBlock;
396     if (hashPrevBlock != pblock->hashPrevBlock)
397     {
398         nExtraNonce = 0;
399         hashPrevBlock = pblock->hashPrevBlock;
400     }
401     ++nExtraNonce;
402
403     unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
404     pblock->vtx[0].vin[0].scriptSig = (CScript() << nHeight << CBigNum(nExtraNonce)) + COINBASE_FLAGS;
405     assert(pblock->vtx[0].vin[0].scriptSig.size() <= 100);
406
407     pblock->hashMerkleRoot = pblock->BuildMerkleTree();
408 }
409
410
411 void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1)
412 {
413     //
414     // Pre-build hash buffers
415     //
416     struct
417     {
418         struct unnamed2
419         {
420             int nVersion;
421             uint256 hashPrevBlock;
422             uint256 hashMerkleRoot;
423             unsigned int nTime;
424             unsigned int nBits;
425             unsigned int nNonce;
426         }
427         block;
428         unsigned char pchPadding0[64];
429         uint256 hash1;
430         unsigned char pchPadding1[64];
431     }
432     tmp;
433     memset(&tmp, 0, sizeof(tmp));
434
435     tmp.block.nVersion       = pblock->nVersion;
436     tmp.block.hashPrevBlock  = pblock->hashPrevBlock;
437     tmp.block.hashMerkleRoot = pblock->hashMerkleRoot;
438     tmp.block.nTime          = pblock->nTime;
439     tmp.block.nBits          = pblock->nBits;
440     tmp.block.nNonce         = pblock->nNonce;
441
442     FormatHashBlocks(&tmp.block, sizeof(tmp.block));
443     FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
444
445     // Byte swap all the input buffer
446     for (unsigned int i = 0; i < sizeof(tmp)/4; i++)
447         ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
448
449     // Precalc the first half of the first hash, which stays constant
450     SHA256Transform(pmidstate, &tmp.block, pSHA256InitState);
451
452     memcpy(pdata, &tmp.block, 128);
453     memcpy(phash1, &tmp.hash1, 64);
454 }
455
456
457 bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
458 {
459     uint256 hashBlock = pblock->GetHash();
460     uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
461
462     if(!pblock->IsProofOfWork())
463         return error("CheckWork() : %s is not a proof-of-work block", hashBlock.GetHex().c_str());
464
465     if (hashBlock > hashTarget)
466         return error("CheckWork() : proof-of-work not meeting target");
467
468     //// debug print
469     printf("CheckWork() : new proof-of-work block found  \n  hash: %s  \ntarget: %s\n", hashBlock.GetHex().c_str(), hashTarget.GetHex().c_str());
470     pblock->print();
471     printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str());
472
473     // Found a solution
474     {
475         LOCK(cs_main);
476         if (pblock->hashPrevBlock != hashBestChain)
477             return error("CheckWork() : generated block is stale");
478
479         // Remove key from key pool
480         reservekey.KeepKey();
481
482         // Track how many getdata requests this block gets
483         {
484             LOCK(wallet.cs_wallet);
485             wallet.mapRequestCount[hashBlock] = 0;
486         }
487
488         // Process this block the same as if we had received it from another node
489         if (!ProcessBlock(NULL, pblock))
490             return error("CheckWork() : ProcessBlock, block not accepted");
491     }
492
493     return true;
494 }
495
496 bool CheckStake(CBlock* pblock, CWallet& wallet)
497 {
498     uint256 proofHash = 0, hashTarget = 0;
499     uint256 hashBlock = pblock->GetHash();
500
501     if(!pblock->IsProofOfStake())
502         return error("CheckStake() : %s is not a proof-of-stake block", hashBlock.GetHex().c_str());
503
504     // verify hash target and signature of coinstake tx
505     if (!CheckProofOfStake(pblock->vtx[1], pblock->nBits, proofHash, hashTarget))
506         return error("CheckStake() : proof-of-stake checking failed");
507
508     //// debug print
509     printf("CheckStake() : new proof-of-stake block found  \n  hash: %s \nproofhash: %s  \ntarget: %s\n", hashBlock.GetHex().c_str(), proofHash.GetHex().c_str(), hashTarget.GetHex().c_str());
510     pblock->print();
511     printf("out %s\n", FormatMoney(pblock->vtx[1].GetValueOut()).c_str());
512
513     // Found a solution
514     {
515         LOCK(cs_main);
516         if (pblock->hashPrevBlock != hashBestChain)
517             return error("CheckStake() : generated block is stale");
518
519         // Track how many getdata requests this block gets
520         {
521             LOCK(wallet.cs_wallet);
522             wallet.mapRequestCount[hashBlock] = 0;
523         }
524
525         // Process this block the same as if we had received it from another node
526         if (!ProcessBlock(NULL, pblock))
527             return error("CheckStake() : ProcessBlock, block not accepted");
528     }
529
530     return true;
531 }
532
533 // Precalculated SHA256 contexts and metadata
534 // (txid, vout.n) => (kernel, (tx.nTime, nAmount))
535 typedef std::map<std::pair<uint256, unsigned int>, std::pair<std::vector<unsigned char>, std::pair<uint32_t, uint64_t> > > MidstateMap;
536
537 // Fill the inputs map with precalculated contexts and metadata
538 bool FillMap(CWallet *pwallet, uint32_t nUpperTime, MidstateMap &inputsMap)
539 {
540     // Choose coins to use
541     int64_t nBalance = pwallet->GetBalance();
542
543     if (nBalance <= nReserveBalance)
544         return false;
545
546     uint32_t nTime = GetAdjustedTime();
547
548     CTxDB txdb("r");
549     {
550         LOCK2(cs_main, pwallet->cs_wallet);
551
552         CoinsSet setCoins;
553         int64_t nValueIn = 0;
554         if (!pwallet->SelectCoinsSimple(nBalance - nReserveBalance, MIN_TX_FEE, MAX_MONEY, nUpperTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
555             return error("FillMap() : SelectCoinsSimple failed");
556
557         if (setCoins.empty())
558             return false;
559
560         CBlock block;
561         CTxIndex txindex;
562
563         for(CoinsSet::const_iterator pcoin = setCoins.begin(); pcoin != setCoins.end(); pcoin++)
564         {
565             pair<uint256, uint32_t> key = make_pair(pcoin->first->GetHash(), pcoin->second);
566
567             // Skip existent inputs
568             if (inputsMap.find(key) != inputsMap.end())
569                 continue;
570
571             // Trying to parse scriptPubKey
572             txnouttype whichType;
573             vector<valtype> vSolutions;
574             if (!Solver(pcoin->first->vout[pcoin->second].scriptPubKey, whichType, vSolutions))
575                 continue;
576
577             // Only support pay to public key and pay to address
578             if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH)
579                 continue;
580
581             // Load transaction index item
582             if (!txdb.ReadTxIndex(pcoin->first->GetHash(), txindex))
583                 continue;
584
585             // Read block header
586             if (!block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
587                 continue;
588
589             // Only load coins meeting min age requirement
590             if (nStakeMinAge + block.nTime > nTime - nMaxStakeSearchInterval)
591                 continue;
592
593             // Get stake modifier
594             uint64_t nStakeModifier = 0;
595             if (!GetKernelStakeModifier(block.GetHash(), nStakeModifier))
596                 continue;
597
598             // Build static part of kernel
599             CDataStream ssKernel(SER_GETHASH, 0);
600             ssKernel << nStakeModifier;
601             ssKernel << block.nTime << (txindex.pos.nTxPos - txindex.pos.nBlockPos) << pcoin->first->nTime << pcoin->second;
602
603             // (txid, vout.n) => (kernel, (tx.nTime, nAmount))
604             inputsMap[key] = make_pair(std::vector<unsigned char>(ssKernel.begin(), ssKernel.end()), make_pair(pcoin->first->nTime, pcoin->first->vout[pcoin->second].nValue));
605         }
606
607         nStakeInputsMapSize = inputsMap.size();
608
609         if (fDebug)
610             printf("FillMap() : Map of %" PRIu64 " precalculated contexts has been created by stake miner\n", nStakeInputsMapSize);
611     }
612
613     return true;
614 }
615
616 // Scan inputs map in order to find a solution
617 bool ScanMap(const MidstateMap &inputsMap, uint32_t nBits, MidstateMap::key_type &LuckyInput, std::pair<uint256, uint32_t> &solution)
618 {
619     static uint32_t nLastCoinStakeSearchTime = GetAdjustedTime(); // startup timestamp
620     uint32_t nSearchTime = GetAdjustedTime();
621
622     if (inputsMap.size() > 0 && nSearchTime > nLastCoinStakeSearchTime)
623     {
624         // Scanning interval (begintime, endtime)
625         std::pair<uint32_t, uint32_t> interval;
626
627         interval.first = nSearchTime;
628         interval.second = nSearchTime - min(nSearchTime-nLastCoinStakeSearchTime, nMaxStakeSearchInterval);
629
630         // (txid, nout) => (kernel, (tx.nTime, nAmount))
631         for(MidstateMap::const_iterator input = inputsMap.begin(); input != inputsMap.end(); input++)
632         {
633             unsigned char *kernel = (unsigned char *) &input->second.first[0];
634
635             // scan(State, Bits, Time, Amount, ...)
636             if (ScanKernelBackward(kernel, nBits, input->second.second.first, input->second.second.second, interval, solution))
637             {
638                 // Solution found
639                 LuckyInput = input->first; // (txid, nout)
640
641                 return true;
642             }
643         }
644
645         // Inputs map iteration can be big enough to consume few seconds while scanning.
646         // We're using dynamical calculation of scanning interval in order to compensate this delay.
647         nLastCoinStakeSearchInterval = nSearchTime - nLastCoinStakeSearchTime;
648         nLastCoinStakeSearchTime = nSearchTime;
649     }
650
651     // No solutions were found
652     return false;
653 }
654
655 // Stake miner thread
656 void ThreadStakeMiner(void* parg)
657 {
658     SetThreadPriority(THREAD_PRIORITY_LOWEST);
659
660     // Make this thread recognisable as the mining thread
661     RenameThread("novacoin-miner");
662     CWallet* pwallet = (CWallet*)parg;
663
664     MidstateMap inputsMap;
665     if (!FillMap(pwallet, GetAdjustedTime(), inputsMap))
666         return;
667
668     bool fTrySync = true;
669
670     CBlockIndex* pindexPrev = pindexBest;
671     uint32_t nBits = GetNextTargetRequired(pindexPrev, true);
672
673     printf("ThreadStakeMinter started\n");
674
675     try
676     {
677         vnThreadsRunning[THREAD_MINTER]++;
678
679         MidstateMap::key_type LuckyInput;
680         std::pair<uint256, uint32_t> solution;
681
682         // Main miner loop
683         do
684         {
685             if (fShutdown)
686                 goto _endloop;
687
688             while (pwallet->IsLocked())
689             {
690                 Sleep(1000);
691                 if (fShutdown)
692                     goto _endloop; // Don't be afraid to use a goto if that's the best option.
693             }
694
695             while (vNodes.empty() || IsInitialBlockDownload())
696             {
697                 fTrySync = true;
698
699                 Sleep(1000);
700                 if (fShutdown)
701                     goto _endloop;
702             }
703
704             if (fTrySync)
705             {
706                 // Don't try mine blocks unless we're at the top of chain and have at least three p2p connections.
707                 fTrySync = false;
708                 if (vNodes.size() < 3 || nBestHeight < GetNumBlocksOfPeers())
709                 {
710                     Sleep(1000);
711                     continue;
712                 }
713             }
714
715             if (ScanMap(inputsMap, nBits, LuckyInput, solution))
716             {
717                 SetThreadPriority(THREAD_PRIORITY_NORMAL);
718
719                 // Remove lucky input from the map
720                 inputsMap.erase(inputsMap.find(LuckyInput));
721
722                 CKey key;
723                 CTransaction txCoinStake;
724
725                 // Create new coinstake transaction
726                 if (!pwallet->CreateCoinStake(LuckyInput.first, LuckyInput.second, solution.second, nBits, txCoinStake, key))
727                 {
728                     string strMessage = _("Warning: Unable to create coinstake transaction, see debug.log for the details. Mining thread has been stopped.");
729                     strMiscWarning = strMessage;
730                     printf("*** %s\n", strMessage.c_str());
731
732                     break;
733                 }
734
735                 // Now we have new coinstake, it's time to create the block ...
736                 CBlock* pblock;
737                 pblock = CreateNewBlock(pwallet, &txCoinStake);
738                 if (!pblock)
739                 {
740                     string strMessage = _("Warning: Unable to allocate memory for the new block object. Mining thread has been stopped.");
741                     strMiscWarning = strMessage;
742                     printf("*** %s\n", strMessage.c_str());
743
744                     break;
745                 }
746
747                 unsigned int nExtraNonce = 0;
748                 IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
749
750                 // ... and sign it
751                 if (!key.Sign(pblock->GetHash(), pblock->vchBlockSig))
752                 {
753                     string strMessage = _("Warning: Proof-of-Stake miner is unable to sign the block (locked wallet?). Mining thread has been stopped.");
754                     strMiscWarning = strMessage;
755                     printf("*** %s\n", strMessage.c_str());
756
757                     break;
758                 }
759
760                 CheckStake(pblock, *pwallet);
761                 SetThreadPriority(THREAD_PRIORITY_LOWEST);
762                 Sleep(500);
763             }
764
765             if (pindexPrev != pindexBest)
766             {
767                 // The best block has been changed, we need to refill the map
768                 if (FillMap(pwallet, GetAdjustedTime(), inputsMap))
769                 {
770                     pindexPrev = pindexBest;
771                     nBits = GetNextTargetRequired(pindexPrev, true);
772                 }
773                 else
774                 {
775                     // Clear existent data if FillMap failed
776                     inputsMap.clear();
777                 }
778             }
779
780             Sleep(500);
781
782             _endloop:
783                 (void)0; // do nothing
784         }
785         while(!fShutdown);
786
787         vnThreadsRunning[THREAD_MINTER]--;
788     }
789     catch (std::exception& e) {
790         vnThreadsRunning[THREAD_MINTER]--;
791         PrintException(&e, "ThreadStakeMinter()");
792     } catch (...) {
793         vnThreadsRunning[THREAD_MINTER]--;
794         PrintException(NULL, "ThreadStakeMinter()");
795     }
796     printf("ThreadStakeMinter exiting, %d threads remaining\n", vnThreadsRunning[THREAD_MINTER]);
797 }