Anoncoin  0.9.4
P2P Digital Currency
miner.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2013-2014 The Anoncoin Core 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 // Many builder specific things set in the config file, ENABLE_WALLET is a good example. Don't forget to include it this way in your source files.
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include "miner.h"
13 
14 #include "core.h"
15 #include "main.h"
16 #include "net.h"
17 #ifdef ENABLE_WALLET
18 #include "wallet.h"
19 #endif
20 //
22 // AnoncoinMiner
23 //
24 
25 int static FormatHashBlocks(void* pbuffer, unsigned int len)
26 {
27  unsigned char* pdata = (unsigned char*)pbuffer;
28  unsigned int blocks = 1 + ((len + 8) / 64);
29  unsigned char* pend = pdata + 64 * blocks;
30  memset(pdata + len, 0, 64 * blocks - len);
31  pdata[len] = 0x80;
32  unsigned int bits = len * 8;
33  pend[-1] = (bits >> 0) & 0xff;
34  pend[-2] = (bits >> 8) & 0xff;
35  pend[-3] = (bits >> 16) & 0xff;
36  pend[-4] = (bits >> 24) & 0xff;
37  return blocks;
38 }
39 
40 static const unsigned int pSHA256InitState[8] =
41 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
42 
43 void SHA256Transform(void* pstate, void* pinput, const void* pinit)
44 {
45  SHA256_CTX ctx;
46  unsigned char data[64];
47 
48  SHA256_Init(&ctx);
49 
50  for (int i = 0; i < 16; i++)
51  ((uint32_t*)data)[i] = ByteReverse(((uint32_t*)pinput)[i]);
52 
53  for (int i = 0; i < 8; i++)
54  ctx.h[i] = ((uint32_t*)pinit)[i];
55 
56  SHA256_Update(&ctx, data, sizeof(data));
57  for (int i = 0; i < 8; i++)
58  ((uint32_t*)pstate)[i] = ctx.h[i];
59 }
60 
61 // Some explaining would be appreciated
62 class COrphan
63 {
64 public:
65  const CTransaction* ptx;
66  set<uint256> setDependsOn;
67  double dPriority;
68  double dFeePerKb;
69 
70  COrphan(const CTransaction* ptxIn)
71  {
72  ptx = ptxIn;
73  dPriority = dFeePerKb = 0;
74  }
75 
76  void print() const
77  {
78  LogPrintf("COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n",
79  ptx->GetHash().ToString(), dPriority, dFeePerKb);
80  BOOST_FOREACH(uint256 hash, setDependsOn)
81  LogPrintf(" setDependsOn %s\n", hash.ToString());
82  }
83 };
84 
85 
86 uint64_t nLastBlockTx = 0;
87 uint64_t nLastBlockSize = 0;
88 
89 // We want to sort transactions by priority and fee, so:
90 typedef boost::tuple<double, double, const CTransaction*> TxPriority;
92 {
93  bool byFee;
94 public:
95  TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
96  bool operator()(const TxPriority& a, const TxPriority& b)
97  {
98  if (byFee)
99  {
100  if (a.get<1>() == b.get<1>())
101  return a.get<0>() < b.get<0>();
102  return a.get<1>() < b.get<1>();
103  }
104  else
105  {
106  if (a.get<0>() == b.get<0>())
107  return a.get<1>() < b.get<1>();
108  return a.get<0>() < b.get<0>();
109  }
110  }
111 };
112 
113 CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
114 {
115  // Create new block
116  auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
117  if(!pblocktemplate.get())
118  return NULL;
119  CBlock *pblock = &pblocktemplate->block; // pointer for convenience
120 
121  // Create coinbase tx
122  CTransaction txNew;
123  txNew.vin.resize(1);
124  txNew.vin[0].prevout.SetNull();
125  txNew.vout.resize(1);
126  txNew.vout[0].scriptPubKey = scriptPubKeyIn;
127 
128  // Add our coinbase tx as first transaction
129  pblock->vtx.push_back(txNew);
130  pblocktemplate->vTxFees.push_back(-1); // updated at end
131  pblocktemplate->vTxSigOps.push_back(-1); // updated at end
132 
133  // Largest block you're willing to create:
134  unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
135  // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
136  nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
137 
138  // How much of the block should be dedicated to high-priority transactions,
139  // included regardless of the fees they pay
140  unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
141  nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
142 
143  // Minimum block size you want to create; block will be filled with free transactions
144  // until there are no more or the block reaches this size:
145  unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
146  nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
147 
148  // Collect memory pool transactions into the block
149  int64_t nFees = 0;
150  {
152  CBlockIndex* pindexPrev = chainActive.Tip();
153  CCoinsViewCache view(*pcoinsTip, true);
154 
155  // Priority order to process transactions
156  list<COrphan> vOrphan; // list memory doesn't move
157  map<uint256, vector<COrphan*> > mapDependers;
158  bool fPrintPriority = GetBoolArg("-printpriority", false);
159 
160  // This vector will be sorted into a priority queue:
161  vector<TxPriority> vecPriority;
162  vecPriority.reserve(mempool.mapTx.size());
163  for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin();
164  mi != mempool.mapTx.end(); ++mi)
165  {
166  const CTransaction& tx = mi->second.GetTx();
167  if (tx.IsCoinBase() || !IsFinalTx(tx, pindexPrev->nHeight + 1))
168  continue;
169 
170  COrphan* porphan = NULL;
171  double dPriority = 0;
172  int64_t nTotalIn = 0;
173  bool fMissingInputs = false;
174  BOOST_FOREACH(const CTxIn& txin, tx.vin)
175  {
176  // Read prev transaction
177  if (!view.HaveCoins(txin.prevout.hash))
178  {
179  // This should never happen; all transactions in the memory
180  // pool should connect to either transactions in the chain
181  // or other transactions in the memory pool.
182  if (!mempool.mapTx.count(txin.prevout.hash))
183  {
184  LogPrintf("ERROR: mempool transaction missing input\n");
185  if (fDebug) assert("mempool transaction missing input" == 0);
186  fMissingInputs = true;
187  if (porphan)
188  vOrphan.pop_back();
189  break;
190  }
191 
192  // Has to wait for dependencies
193  if (!porphan)
194  {
195  // Use list for automatic deletion
196  vOrphan.push_back(COrphan(&tx));
197  porphan = &vOrphan.back();
198  }
199  mapDependers[txin.prevout.hash].push_back(porphan);
200  porphan->setDependsOn.insert(txin.prevout.hash);
201  nTotalIn += mempool.mapTx[txin.prevout.hash].GetTx().vout[txin.prevout.n].nValue;
202  continue;
203  }
204  const CCoins &coins = view.GetCoins(txin.prevout.hash);
205 
206  int64_t nValueIn = coins.vout[txin.prevout.n].nValue;
207  nTotalIn += nValueIn;
208 
209  int nConf = pindexPrev->nHeight - coins.nHeight + 1;
210 
211  dPriority += (double)nValueIn * nConf;
212  }
213  if (fMissingInputs) continue;
214 
215  // Priority is sum(valuein * age) / modified_txsize
216  unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
217  dPriority = tx.ComputePriority(dPriority, nTxSize);
218 
219  // This is a more accurate fee-per-kilobyte than is used by the client code, because the
220  // client code rounds up the size to the nearest 1K. That's good, because it gives an
221  // incentive to create smaller transactions.
222  double dFeePerKb = double(nTotalIn-tx.GetValueOut()) / (double(nTxSize)/1000.0);
223 
224  if (porphan)
225  {
226  porphan->dPriority = dPriority;
227  porphan->dFeePerKb = dFeePerKb;
228  }
229  else
230  vecPriority.push_back(TxPriority(dPriority, dFeePerKb, &mi->second.GetTx()));
231  }
232 
233  // Collect transactions into block
234  uint64_t nBlockSize = 1000;
235  uint64_t nBlockTx = 0;
236  int nBlockSigOps = 100;
237  bool fSortedByFee = (nBlockPrioritySize <= 0);
238 
239  TxPriorityCompare comparer(fSortedByFee);
240  std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
241 
242  while (!vecPriority.empty())
243  {
244  // Take highest priority transaction off the priority queue:
245  double dPriority = vecPriority.front().get<0>();
246  double dFeePerKb = vecPriority.front().get<1>();
247  const CTransaction& tx = *(vecPriority.front().get<2>());
248 
249  std::pop_heap(vecPriority.begin(), vecPriority.end(), comparer);
250  vecPriority.pop_back();
251 
252  // Size limits
253  unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
254  if (nBlockSize + nTxSize >= nBlockMaxSize)
255  continue;
256 
257  // Legacy limits on sigOps:
258  unsigned int nTxSigOps = GetLegacySigOpCount(tx);
259  if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
260  continue;
261 
262  // Skip free transactions if we're past the minimum block size:
263  if (fSortedByFee && (dFeePerKb < CTransaction::nMinRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
264  continue;
265 
266  // Prioritize by fee once past the priority size or we run out of high-priority
267  // transactions:
268  if (!fSortedByFee &&
269  ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
270  {
271  fSortedByFee = true;
272  comparer = TxPriorityCompare(fSortedByFee);
273  std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
274  }
275 
276  if (!view.HaveInputs(tx))
277  continue;
278 
279  int64_t nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
280 
281  nTxSigOps += GetP2SHSigOpCount(tx, view);
282  if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
283  continue;
284 
285  CValidationState state;
286  if (!CheckInputs(tx, state, view, true, SCRIPT_VERIFY_P2SH))
287  continue;
288 
289  CTxUndo txundo;
290  uint256 hash = tx.GetHash();
291  UpdateCoins(tx, state, view, txundo, pindexPrev->nHeight+1, hash);
292 
293  // Added
294  pblock->vtx.push_back(tx);
295  pblocktemplate->vTxFees.push_back(nTxFees);
296  pblocktemplate->vTxSigOps.push_back(nTxSigOps);
297  nBlockSize += nTxSize;
298  ++nBlockTx;
299  nBlockSigOps += nTxSigOps;
300  nFees += nTxFees;
301 
302  if (fPrintPriority)
303  {
304  LogPrintf("priority %.1f feeperkb %.1f txid %s\n",
305  dPriority, dFeePerKb, tx.GetHash().ToString());
306  }
307 
308  // Add transactions that depend on this one to the priority queue
309  if (mapDependers.count(hash))
310  {
311  BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
312  {
313  if (!porphan->setDependsOn.empty())
314  {
315  porphan->setDependsOn.erase(hash);
316  if (porphan->setDependsOn.empty())
317  {
318  vecPriority.push_back(TxPriority(porphan->dPriority, porphan->dFeePerKb, porphan->ptx));
319  std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
320  }
321  }
322  }
323  }
324  }
325 
326  nLastBlockTx = nBlockTx;
327  nLastBlockSize = nBlockSize;
328  LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
329 
330  pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
331  pblocktemplate->vTxFees[0] = -nFees;
332 
333  // Fill in header
334  pblock->hashPrevBlock = pindexPrev->GetBlockHash();
335  UpdateTime(*pblock, pindexPrev);
336  pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
337  pblock->nNonce = 0;
338  pblock->vtx[0].vin[0].scriptSig = CScript() << OP_0 << OP_0;
339  pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
340 
341  CBlockIndex indexDummy(*pblock);
342  indexDummy.pprev = pindexPrev;
343  indexDummy.nHeight = pindexPrev->nHeight + 1;
344  CCoinsViewCache viewNew(*pcoinsTip, true);
345  CValidationState state;
346  if (!ConnectBlock(*pblock, state, &indexDummy, viewNew, true))
347  throw std::runtime_error("CreateNewBlock() : ConnectBlock failed");
348  }
349 
350  return pblocktemplate.release();
351 }
352 
353 void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
354 {
355  // Update nExtraNonce
356  static uint256 hashPrevBlock;
357  if (hashPrevBlock != pblock->hashPrevBlock)
358  {
359  nExtraNonce = 0;
360  hashPrevBlock = pblock->hashPrevBlock;
361  }
362  ++nExtraNonce;
363  unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
364  pblock->vtx[0].vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
365  assert(pblock->vtx[0].vin[0].scriptSig.size() <= 100);
366 
367  pblock->hashMerkleRoot = pblock->BuildMerkleTree();
368 }
369 
370 
371 void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1)
372 {
373  //
374  // Pre-build hash buffers
375  //
376  struct
377  {
378  struct unnamed2
379  {
380  int nVersion;
381  uint256 hashPrevBlock;
382  uint256 hashMerkleRoot;
383  unsigned int nTime;
384  unsigned int nBits;
385  unsigned int nNonce;
386  }
387  block;
388  unsigned char pchPadding0[64];
389  uint256 hash1;
390  unsigned char pchPadding1[64];
391  }
392  tmp;
393  memset(&tmp, 0, sizeof(tmp));
394 
395  tmp.block.nVersion = pblock->nVersion;
396  tmp.block.hashPrevBlock = pblock->hashPrevBlock;
397  tmp.block.hashMerkleRoot = pblock->hashMerkleRoot;
398  tmp.block.nTime = pblock->nTime;
399  tmp.block.nBits = pblock->nBits;
400  tmp.block.nNonce = pblock->nNonce;
401 
402  FormatHashBlocks(&tmp.block, sizeof(tmp.block));
403  FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
404 
405  // Byte swap all the input buffer
406  for (unsigned int i = 0; i < sizeof(tmp)/4; i++)
407  ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
408 
409  // Precalc the first half of the first hash, which stays constant
410  SHA256Transform(pmidstate, &tmp.block, pSHA256InitState);
411 
412  memcpy(pdata, &tmp.block, 128);
413  memcpy(phash1, &tmp.hash1, 64);
414 }
415 
416 #ifdef ENABLE_WALLET
417 //
419 // Internal miner
420 //
421 double dHashesPerSec = 0.0;
422 int64_t nHPSTimerStart = 0;
423 
425 {
426  CPubKey pubkey;
427  if (!reservekey.GetReservedKey(pubkey))
428  return NULL;
429 
430  CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG;
431  return CreateNewBlock(scriptPubKey);
432 }
433 
434 bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
435 {
436  uint256 hash = pblock->GetPoWHash();
437  uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
438 
439  if (hash > hashTarget)
440  return false;
441 
443  LogPrintf("AnoncoinMiner:\n");
444  LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex());
445  pblock->print();
446  LogPrintf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue));
447 
448  // Found a solution
449  {
450  LOCK(cs_main);
451  if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
452  return error("AnoncoinMiner : generated block is stale");
453 
454  // Remove key from key pool
455  reservekey.KeepKey();
456 
457  // Track how many getdata requests this block gets
458  {
459  LOCK(wallet.cs_wallet);
460  wallet.mapRequestCount[pblock->GetHash()] = 0;
461  }
462 
463  // Process this block the same as if we had received it from another node
464  CValidationState state;
465  if (!ProcessBlock(state, NULL, pblock))
466  return error("AnoncoinMiner : ProcessBlock, block not accepted");
467  }
468 
469  return true;
470 }
471 
472 void static AnoncoinMiner(CWallet *pwallet)
473 {
474  LogPrintf("AnoncoinMiner started\n");
476  RenameThread("anoncoin-miner");
477 
478  // Each thread has its own key and counter
479  CReserveKey reservekey(pwallet);
480  unsigned int nExtraNonce = 0;
481 
482  try { while (true) {
483  if (Params().NetworkID() != CChainParams::REGTEST) {
484  // Busy-wait for the network to come online so we don't waste time mining
485  // on an obsolete chain. In regtest mode we expect to fly solo.
486  while (vNodes.empty())
487  MilliSleep(1000);
488  }
489 
490  //
491  // Create new block
492  //
493  unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
494  CBlockIndex* pindexPrev = chainActive.Tip();
495 
496  auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
497  if (!pblocktemplate.get())
498  return;
499  CBlock *pblock = &pblocktemplate->block;
500  IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
501 
502  LogPrintf("Running AnoncoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
503  ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
504 
505  //
506  // Pre-build hash buffers
507  //
508  char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
509  char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
510  char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf);
511 
512  FormatHashBuffers(pblock, pmidstate, pdata, phash1);
513 
514  unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4);
515  unsigned int& nBlockBits = *(unsigned int*)(pdata + 64 + 8);
516 
517  //
518  // Search
519  //
520  int64_t nStart = GetTime();
521  uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
522  while (true)
523  {
524  unsigned int nHashesDone = 0;
525  uint256 thash;
526  char scratchpad[SCRYPT_SCRATCHPAD_SIZE];
527  while(true)
528  {
529  scrypt_1024_1_1_256_sp(BEGIN(pblock->nVersion), BEGIN(thash), scratchpad);
530  if (thash <= hashTarget)
531  {
532  // Found a solution
534  CheckWork(pblock, *pwallet, reservekey);
536 
537  // In regression test mode, stop mining after a block is found. This
538  // allows developers to controllably generate a block on demand.
539  if (Params().NetworkID() == CChainParams::REGTEST)
540  throw boost::thread_interrupted();
541 
542  break;
543  }
544  pblock->nNonce += 1;
545  nHashesDone += 1;
546  if ((pblock->nNonce & 0xFF) == 0)
547  break;
548  }
549 
550  // Meter hashes/sec
551  static int64_t nHashCounter;
552  if (nHPSTimerStart == 0)
553  {
555  nHashCounter = 0;
556  }
557  else
558  nHashCounter += nHashesDone;
559  if (GetTimeMillis() - nHPSTimerStart > 4000)
560  {
561  static CCriticalSection cs;
562  {
563  LOCK(cs);
564  if (GetTimeMillis() - nHPSTimerStart > 4000)
565  {
566  dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
568  nHashCounter = 0;
569  static int64_t nLogTime;
570  if (GetTime() - nLogTime > 30 * 60)
571  {
572  nLogTime = GetTime();
573  LogPrintf("hashmeter %6.0f khash/s\n", dHashesPerSec/1000.0);
574  }
575  }
576  }
577  }
578 
579  // Check for stop or if block needs to be rebuilt
580  boost::this_thread::interruption_point();
581  if (vNodes.empty() && Params().NetworkID() != CChainParams::REGTEST)
582  break;
583  if (pblock->nNonce >= 0xffff0000)
584  break;
585  if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
586  break;
587  if (pindexPrev != chainActive.Tip())
588  break;
589 
590  // Update nTime every few seconds
591  UpdateTime(*pblock, pindexPrev);
592  nBlockTime = ByteReverse(pblock->nTime);
593  if (TestNet())
594  {
595  // Changing pblock->nTime can change work required on testnet:
596  nBlockBits = ByteReverse(pblock->nBits);
597  hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
598  }
599  }
600  } }
601  catch (boost::thread_interrupted)
602  {
603  LogPrintf("AnoncoinMiner terminated\n");
604  throw;
605  }
606 }
607 
608 void GenerateAnoncoins(bool fGenerate, CWallet* pwallet, int nThreads)
609 {
610  static boost::thread_group* minerThreads = NULL;
611 
612  if (nThreads < 0) {
613  if (Params().NetworkID() == CChainParams::REGTEST)
614  nThreads = 1;
615  else
616  nThreads = boost::thread::hardware_concurrency();
617  }
618 
619  if (minerThreads != NULL)
620  {
621  minerThreads->interrupt_all();
622  delete minerThreads;
623  minerThreads = NULL;
624  }
625 
626  if (nThreads == 0 || !fGenerate)
627  return;
628 
629  minerThreads = new boost::thread_group();
630  for (int i = 0; i < nThreads; i++)
631  minerThreads->create_thread(boost::bind(&AnoncoinMiner, pwallet));
632 }
633 
634 #endif
635 
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Definition: main.cpp:617
int64_t GetValueOut() const
Definition: core.cpp:110
CBlockTemplate * CreateNewBlockWithKey(CReserveKey &reservekey)
void IncrementExtraNonce(CBlock *pblock, CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:353
#define THREAD_PRIORITY_LOWEST
Definition: util.h:483
int nVersion
Definition: core.h:345
double dHashesPerSec
#define THREAD_PRIORITY_NORMAL
Definition: util.h:485
void KeepKey()
Definition: wallet.cpp:2014
CBlockIndex * pprev
Definition: main.h:705
int64_t GetBlockValue(int nHeight, int64_t nFees)
Definition: main2.cpp:14
uint64_t nLastBlockSize
Definition: miner.cpp:87
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
Definition: core.cpp:122
std::vector< CTxOut > vout
Definition: coins.h:76
uint256 getuint256() const
Definition: bignum.h:231
Definition: core.h:394
bool fDebug
Definition: util.cpp:91
CCriticalSection cs_wallet
Main wallet lock.
Definition: wallet.h:133
void MilliSleep(int64_t n)
Definition: util.h:80
bool TestNet()
Definition: chainparams.h:127
int nHeight
Definition: coins.h:79
CCriticalSection cs_main
Definition: main.cpp:38
uint256 GetHash() const
Definition: core.cpp:76
CBlockTemplate * CreateNewBlock(const CScript &scriptPubKeyIn)
Generate a new block, without valid proof-of-work.
Definition: miner.cpp:113
double dPriority
Definition: miner.cpp:67
#define scrypt_1024_1_1_256_sp(input, output, scratchpad)
Definition: scrypt.h:36
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:69
unsigned int n
Definition: core.h:28
bool AllowFree(double dPriority)
Definition: main.h:309
string FormatMoney(int64_t n, bool fPlus)
Definition: util.cpp:308
void RenameThread(const char *name)
Definition: util.cpp:1368
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:43
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:75
vector< CNode * > vNodes
Definition: net.cpp:74
bool CheckWork(CBlock *pblock, CWallet &wallet, CReserveKey &reservekey)
Check mined block.
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:60
uint256 BuildMerkleTree() const
Definition: core.cpp:228
void UpdateTime(CBlockHeader &block, const CBlockIndex *pindexPrev)
Definition: main.cpp:1404
uint256 GetPoWHash() const
Definition: core.cpp:221
CBigNum & SetCompact(unsigned int nCompact)
Definition: bignum.h:295
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
Definition: main.h:1024
#define LOCK2(cs1, cs2)
Definition: sync.h:158
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:520
#define LogPrintf(...)
Definition: util.h:118
CScript COINBASE_FLAGS
Definition: main.cpp:75
uint256 hashMerkleRoot
Definition: core.h:347
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:114
bool HaveCoins(const uint256 &txid)
Definition: coins.cpp:110
An input of a transaction.
Definition: core.h:72
#define LOCK(cs)
Definition: sync.h:157
bool HaveInputs(const CTransaction &tx)
Definition: coins.cpp:162
std::vector< CTxOut > vout
Definition: core.h:187
void FormatHashBuffers(CBlock *pblock, char *pmidstate, char *pdata, char *phash1)
Do mining precalculation.
Definition: miner.cpp:371
uint64_t nLastBlockTx
Definition: miner.cpp:86
An encapsulated public key.
Definition: key.h:43
std::vector< CTxIn > vin
Definition: core.h:186
set< uint256 > setDependsOn
Definition: miner.cpp:66
uint256 hashPrevBlock
Definition: core.h:346
boost::tuple< double, double, const CTransaction * > TxPriority
Definition: miner.cpp:90
C++ wrapper for BIGNUM (OpenSSL bignum)
Definition: bignum.h:57
std::map< uint256, CTxMemPoolEntry > mapTx
Definition: txmempool.h:63
unsigned int nNonce
Definition: core.h:350
void UpdateCoins(const CTransaction &tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash)
Definition: main.cpp:1423
bool GetReservedKey(CPubKey &pubkey)
Definition: wallet.cpp:1993
int64_t GetTimeMillis()
Definition: util.h:304
bool ConnectBlock(CBlock &block, CValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck)
Definition: main.cpp:1669
std::map< uint256, int > mapRequestCount
Definition: wallet.h:172
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
Definition: main.cpp:436
std::string GetHex() const
Definition: uint256.h:298
int64_t GetTime()
Definition: util.cpp:1220
void print() const
Definition: core.cpp:278
const CTransaction * ptx
Definition: miner.cpp:65
CCriticalSection cs
Definition: txmempool.h:62
CTxMemPool mempool
Definition: main.cpp:40
void SetThreadPriority(int nPriority)
Definition: util.h:488
unsigned int GetP2SHSigOpCount(const CTransaction &tx, CCoinsViewCache &inputs)
Count ECDSA signature operations in pay-to-script-hash inputs.
Definition: main.cpp:714
#define BEGIN(a)
Definition: util.h:42
Capture information about block/transaction validation.
Definition: main.h:950
256-bit unsigned integer
Definition: uint256.h:532
unsigned int nTime
Definition: core.h:348
uint256 GetHash() const
Definition: core.cpp:216
bool ProcessBlock(CValidationState &state, CNode *pfrom, CBlock *pblock, CDiskBlockPos *dbp)
Process an incoming block.
Definition: main.cpp:2431
A key allocated from the key pool.
Definition: wallet.h:415
uint32_t ByteReverse(uint32_t value)
Definition: util.h:502
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
Definition: main.cpp:700
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:698
const CChainParams & Params()
Return the currently selected parameters.
bool CheckInputs(const CTransaction &tx, CValidationState &state, CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, std::vector< CScriptCheck > *pvChecks)
Definition: main.cpp:1454
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
Undo information for a CTransaction.
Definition: core.h:321
void * memcpy(void *a, const void *b, size_t c)
int64_t nHPSTimerStart
std::string ToString() const
Definition: uint256.h:341
void print() const
Definition: miner.cpp:76
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:101
void GenerateAnoncoins(bool fGenerate, CWallet *pwallet, int nThreads)
Run the miner threads.
COrphan(const CTransaction *ptxIn)
Definition: miner.cpp:70
int64_t GetValueIn(const CTransaction &tx)
Amount of anoncoins coming in to a transaction Note that lightweight clients may not know anything be...
Definition: coins.cpp:150
static int64_t nMinRelayTxFee
Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) ...
Definition: core.h:183
bool IsCoinBase() const
Definition: core.h:228
bool operator()(const TxPriority &a, const TxPriority &b)
Definition: miner.cpp:96
std::vector< CTransaction > vtx
Definition: core.h:398
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:506
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:179
int nHeight
Definition: main.h:708
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:309
unsigned int nBits
Definition: core.h:349
void SHA256Transform(void *pstate, void *pinput, const void *pinit)
Base sha256 mining transform.
Definition: miner.cpp:43
COutPoint prevout
Definition: core.h:75
Definition: script.h:238
TxPriorityCompare(bool _byFee)
Definition: miner.cpp:95
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock)
Definition: main2.cpp:232
virtual Network NetworkID() const =0
double dFeePerKb
Definition: miner.cpp:68
uint256 GetBlockHash() const
Definition: main.h:815
uint256 hash
Definition: core.h:27