Anoncoin  0.9.4
P2P Digital Currency
main.h
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-2015 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 #ifndef ANONCOIN_MAIN_H
8 #define ANONCOIN_MAIN_H
9 
10 #if defined(HAVE_CONFIG_H)
11 #include "config/anoncoin-config.h"
12 #endif
13 
14 #include "bignum.h"
15 #include "clientversion.h"
16 #include "chainparams.h"
17 #include "coins.h"
18 #include "core.h"
19 #include "net.h"
20 #include "script.h"
21 #include "sync.h"
22 #include "txmempool.h"
23 #include "uint256.h"
24 
25 #include <algorithm>
26 #include <exception>
27 #include <map>
28 #include <set>
29 #include <stdint.h>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 class CBlockIndex;
35 class CBloomFilter;
36 class CInv;
37 
39 static const unsigned int MAX_BLOCK_SIZE = 1000000;
41 static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 750000;
42 static const unsigned int DEFAULT_BLOCK_MIN_SIZE = 0;
44 static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 17000;
46 static const unsigned int MAX_STANDARD_TX_SIZE = 100000;
48 static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
50 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
52 static const unsigned int DEFAULT_MAX_ORPHAN_BLOCKS = 750;
54 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
56 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
58 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
60 static const int COINBASE_MATURITY = 100;
62 static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC
64 static const int MAX_SCRIPTCHECK_THREADS = 16;
66 static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
68 static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 128;
70 // For I2p, this could be considerably longer, up'n the value by double
71 // static const unsigned int BLOCK_DOWNLOAD_TIMEOUT = 60;
72 static const unsigned int BLOCK_DOWNLOAD_TIMEOUT = 120;
73 
75 static const int64_t DUST_SOFT_LIMIT = 100000; // 0.001 LTC
76 
77 #ifdef USE_UPNP
78 static const int fHaveUPnP = true;
79 #else
80 static const int fHaveUPnP = false;
81 #endif
82 
84 static const unsigned char REJECT_MALFORMED = 0x01;
85 static const unsigned char REJECT_INVALID = 0x10;
86 static const unsigned char REJECT_OBSOLETE = 0x11;
87 static const unsigned char REJECT_DUPLICATE = 0x12;
88 static const unsigned char REJECT_NONSTANDARD = 0x40;
89 static const unsigned char REJECT_DUST = 0x41;
90 static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
91 static const unsigned char REJECT_CHECKPOINT = 0x43;
92 
93 
94 extern CScript COINBASE_FLAGS;
96 extern CTxMemPool mempool;
97 extern std::map<uint256, CBlockIndex*> mapBlockIndex;
98 extern uint64_t nLastBlockTx;
99 extern uint64_t nLastBlockSize;
100 extern const std::string strMessageMagic;
101 extern int64_t nTimeBestReceived;
102 extern bool fImporting;
103 extern bool fReindex;
104 extern bool fBenchmark;
105 extern int nScriptCheckThreads;
106 extern bool fTxIndex;
107 extern unsigned int nCoinCacheSize;
108 
109 // Minimum disk space required - used in CheckDiskSpace()
110 static const uint64_t nMinDiskSpace = 52428800;
111 
112 
113 class CCoinsDB;
114 class CBlockTreeDB;
115 struct CDiskBlockPos;
116 class CTxUndo;
117 class CScriptCheck;
118 class CValidationState;
119 class CWalletInterface;
120 struct CNodeStateStats;
121 
122 struct CBlockTemplate;
123 
125 void RegisterWallet(CWalletInterface* pwalletIn);
127 void UnregisterWallet(CWalletInterface* pwalletIn);
129 void UnregisterAllWallets();
131 void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock = NULL);
132 
134 void RegisterNodeSignals(CNodeSignals& nodeSignals);
136 void UnregisterNodeSignals(CNodeSignals& nodeSignals);
137 
138 void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd);
139 
141 bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp = NULL);
143 bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
145 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
147 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
149 bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp = NULL);
151 bool InitBlockIndex();
153 bool LoadBlockIndex();
155 void UnloadBlockIndex();
157 bool VerifyDB(int nCheckLevel, int nCheckDepth);
159 void PrintBlockTree();
161 bool ProcessMessages(CNode* pfrom);
163 bool SendMessages(CNode* pto, bool fSendTrickle);
165 void ThreadScriptCheck();
167 bool CheckProofOfWork(uint256 hash, unsigned int nBits);
169 unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime);
173 std::string GetWarnings(std::string strFor);
175 bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false);
178 int64_t GetBlockValue(int nHeight, int64_t nFees);
179 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock);
180 
181 void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev);
182 
186 bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
188 bool AbortNode(const std::string &msg);
190 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
192 void Misbehaving(NodeId nodeid, int howmuch);
193 
194 
196 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
197  bool* pfMissingInputs, bool fRejectInsaneFee=false);
198 
199 
200 
201 
202 
203 
204 
205 
210  std::vector<int> vHeightInFlight;
211 };
212 
214 {
215  int nFile;
216  unsigned int nPos;
217 
219  READWRITE(VARINT(nFile));
220  READWRITE(VARINT(nPos));
221  )
222 
223  CDiskBlockPos() {
224  SetNull();
225  }
226 
227  CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
228  nFile = nFileIn;
229  nPos = nPosIn;
230  }
231 
232  friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
233  return (a.nFile == b.nFile && a.nPos == b.nPos);
234  }
235 
236  friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
237  return !(a == b);
238  }
239 
240  void SetNull() { nFile = -1; nPos = 0; }
241  bool IsNull() const { return (nFile == -1); }
242 };
243 
244 struct CDiskTxPos : public CDiskBlockPos
245 {
246  unsigned int nTxOffset; // after header
247 
249  READWRITE(*(CDiskBlockPos*)this);
250  READWRITE(VARINT(nTxOffset));
251  )
252 
253  CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
254  }
255 
257  SetNull();
258  }
259 
260  void SetNull() {
262  nTxOffset = 0;
263  }
264 };
265 
266 
267 
269 {
272 };
273 
274 int64_t GetMinFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree, enum GetMinFee_mode mode);
275 
276 //
277 // Check transaction inputs, and make sure any
278 // pay-to-script-hash transactions are evaluating IsStandard scripts
279 //
280 // Why bother? To avoid denial-of-service attacks; an attacker
281 // can submit a standard HASH... OP_EQUAL transaction,
282 // which will get accepted into blocks. The redemption
283 // script can be anything; an attacker could use a very
284 // expensive-to-check-upon-redemption script like:
285 // DUP CHECKSIG DROP ... repeated 100 times... OP_1
286 //
287 
292 bool AreInputsStandard(const CTransaction& tx, CCoinsViewCache& mapInputs);
293 
298 unsigned int GetLegacySigOpCount(const CTransaction& tx);
299 
306 unsigned int GetP2SHSigOpCount(const CTransaction& tx, CCoinsViewCache& mapInputs);
307 
308 
309 inline bool AllowFree(double dPriority)
310 {
311  // Large (in bytes) low-priority (new, small-coin) transactions
312  // need a fee.
313  return dPriority > COIN * 576 / 250;
314 }
315 
316 // Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
317 // This does not modify the UTXO set. If pvChecks is not NULL, script checks are pushed onto it
318 // instead of being performed inline.
319 bool CheckInputs(const CTransaction& tx, CValidationState &state, CCoinsViewCache &view, bool fScriptChecks = true,
320  unsigned int flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC,
321  std::vector<CScriptCheck> *pvChecks = NULL);
322 
323 // Apply the effects of this transaction on the UTXO set represented by view
324 void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash);
325 
326 // Context-independent validity checks
327 bool CheckTransaction(const CTransaction& tx, CValidationState& state);
328 
332 bool IsStandardTx(const CTransaction& tx, std::string& reason);
333 
334 bool IsFinalTx(const CTransaction &tx, int nBlockHeight = 0, int64_t nBlockTime = 0);
335 
338 {
339 public:
340  std::vector<CTxUndo> vtxundo; // for all but the coinbase
341 
343  READWRITE(vtxundo);
344  )
345 
346  bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
347  {
348  // Open history file to append
349  CAutoFile fileout = CAutoFile(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
350  if (!fileout)
351  return error("CBlockUndo::WriteToDisk : OpenUndoFile failed");
352 
353  // Write index header
354  unsigned int nSize = fileout.GetSerializeSize(*this);
355  fileout << FLATDATA(Params().MessageStart()) << nSize;
356 
357  // Write undo data
358  long fileOutPos = ftell(fileout);
359  if (fileOutPos < 0)
360  return error("CBlockUndo::WriteToDisk : ftell failed");
361  pos.nPos = (unsigned int)fileOutPos;
362  fileout << *this;
363 
364  // calculate & write checksum
365  CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
366  hasher << hashBlock;
367  hasher << *this;
368  fileout << hasher.GetHash();
369 
370  // Flush stdio buffers and commit to disk before returning
371  fflush(fileout);
372  if (!IsInitialBlockDownload())
373  FileCommit(fileout);
374 
375  return true;
376  }
377 
378  bool ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock)
379  {
380  // Open history file to read
381  CAutoFile filein = CAutoFile(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
382  if (!filein)
383  return error("CBlockUndo::ReadFromDisk : OpenBlockFile failed");
384 
385  // Read block
386  uint256 hashChecksum;
387  try {
388  filein >> *this;
389  filein >> hashChecksum;
390  }
391  catch (std::exception &e) {
392  return error("%s : Deserialize or I/O error - %s", __func__, e.what());
393  }
394 
395  // Verify checksum
396  CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
397  hasher << hashBlock;
398  hasher << *this;
399  if (hashChecksum != hasher.GetHash())
400  return error("CBlockUndo::ReadFromDisk : Checksum mismatch");
401 
402  return true;
403  }
404 };
405 
406 
410 {
411 private:
414  unsigned int nIn;
415  unsigned int nFlags;
417 
418 public:
420  CScriptCheck(const CCoins& txFromIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, int nHashTypeIn) :
421  scriptPubKey(txFromIn.vout[txToIn.vin[nInIn].prevout.n].scriptPubKey),
422  ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), nHashType(nHashTypeIn) { }
423 
424  bool operator()() const;
425 
426  void swap(CScriptCheck &check) {
427  scriptPubKey.swap(check.scriptPubKey);
428  std::swap(ptxTo, check.ptxTo);
429  std::swap(nIn, check.nIn);
430  std::swap(nFlags, check.nFlags);
431  std::swap(nHashType, check.nHashType);
432  }
433 };
434 
436 class CMerkleTx : public CTransaction
437 {
438 private:
439  int GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const;
440 
441 public:
443  std::vector<uint256> vMerkleBranch;
444  int nIndex;
445 
446  // memory only
447  mutable bool fMerkleVerified;
448 
449 
451  {
452  Init();
453  }
454 
455  CMerkleTx(const CTransaction& txIn) : CTransaction(txIn)
456  {
457  Init();
458  }
459 
460  void Init()
461  {
462  hashBlock = 0;
463  nIndex = -1;
464  fMerkleVerified = false;
465  }
466 
467 
469  (
470  nSerSize += SerReadWrite(s, *(CTransaction*)this, nType, nVersion, ser_action);
471  nVersion = this->nVersion;
472  READWRITE(hashBlock);
473  READWRITE(vMerkleBranch);
474  READWRITE(nIndex);
475  )
476 
477 
478  int SetMerkleBranch(const CBlock* pblock=NULL);
479 
480  // Return depth of transaction in blockchain:
481  // -1 : not in blockchain, and not in memory pool (conflicted transaction)
482  // 0 : in memory pool, waiting to be included in a block
483  // >=1 : this many blocks deep in the main chain
484  int GetDepthInMainChain(CBlockIndex* &pindexRet) const;
485  int GetDepthInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); }
486  bool IsInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChainINTERNAL(pindexRet) > 0; }
487  int GetBlocksToMaturity() const;
488  bool AcceptToMemoryPool(bool fLimitFree=true);
489 };
490 
491 
492 
493 
494 
530 {
531 protected:
532  // the total number of transactions in the block
533  unsigned int nTransactions;
534 
535  // node-is-parent-of-matched-txid bits
536  std::vector<bool> vBits;
537 
538  // txids and internal hashes
539  std::vector<uint256> vHash;
540 
541  // flag set when encountering invalid data
542  bool fBad;
543 
544  // helper function to efficiently calculate the number of nodes at given height in the merkle tree
545  unsigned int CalcTreeWidth(int height) {
546  return (nTransactions+(1 << height)-1) >> height;
547  }
548 
549  // calculate the hash of a node in the merkle tree (at leaf level: the txid's themself)
550  uint256 CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid);
551 
552  // recursive function that traverses tree nodes, storing the data as bits and hashes
553  void TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
554 
555  // recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBuild.
556  // it returns the hash of the respective node.
557  uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch);
558 
559 public:
560 
561  // serialization implementation
563  READWRITE(nTransactions);
564  READWRITE(vHash);
565  std::vector<unsigned char> vBytes;
566  if (fRead) {
567  READWRITE(vBytes);
568  CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree*>(this));
569  us.vBits.resize(vBytes.size() * 8);
570  for (unsigned int p = 0; p < us.vBits.size(); p++)
571  us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0;
572  us.fBad = false;
573  } else {
574  vBytes.resize((vBits.size()+7)/8);
575  for (unsigned int p = 0; p < vBits.size(); p++)
576  vBytes[p / 8] |= vBits[p] << (p % 8);
577  READWRITE(vBytes);
578  }
579  )
580 
581  // Construct a partial merkle tree from a list of transaction id's, and a mask that selects a subset of them
582  CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
583 
585 
586  // extract the matching txid's represented by this partial merkle tree.
587  // returns the merkle root, or 0 in case of failure
588  uint256 ExtractMatches(std::vector<uint256> &vMatch);
589 };
590 
591 
592 
594 bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos);
595 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos);
596 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex);
597 
598 
605 bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& coins, bool* pfClean = NULL);
606 
607 // Apply the effects of this block (with given index) on the UTXO set represented by coins
608 bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& coins, bool fJustCheck = false);
609 
610 // Add this block to the block index, and if necessary, switch the active block chain to this
611 bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos& pos);
612 
613 // Context-independent validity checks
614 bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
615 
616 // Store block on disk
617 // if dbp is provided, the file is known to already reside on disk
618 bool AcceptBlock(CBlock& block, CValidationState& state, CDiskBlockPos* dbp = NULL);
619 
620 
621 
623 {
624 public:
625  unsigned int nBlocks; // number of blocks stored in file
626  unsigned int nSize; // number of used bytes of block file
627  unsigned int nUndoSize; // number of used bytes in the undo file
628  unsigned int nHeightFirst; // lowest height of block in file
629  unsigned int nHeightLast; // highest height of block in file
630  uint64_t nTimeFirst; // earliest time of block in file
631  uint64_t nTimeLast; // latest time of block in file
632 
634  READWRITE(VARINT(nBlocks));
635  READWRITE(VARINT(nSize));
636  READWRITE(VARINT(nUndoSize));
637  READWRITE(VARINT(nHeightFirst));
638  READWRITE(VARINT(nHeightLast));
639  READWRITE(VARINT(nTimeFirst));
640  READWRITE(VARINT(nTimeLast));
641  )
642 
643  void SetNull() {
644  nBlocks = 0;
645  nSize = 0;
646  nUndoSize = 0;
647  nHeightFirst = 0;
648  nHeightLast = 0;
649  nTimeFirst = 0;
650  nTimeLast = 0;
651  }
652 
654  SetNull();
655  }
656 
657  std::string ToString() const {
658  return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst).c_str(), DateTimeStrFormat("%Y-%m-%d", nTimeLast).c_str());
659  }
660 
661  // update statistics (does not update nSize)
662  void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
663  if (nBlocks==0 || nHeightFirst > nHeightIn)
664  nHeightFirst = nHeightIn;
665  if (nBlocks==0 || nTimeFirst > nTimeIn)
666  nTimeFirst = nTimeIn;
667  nBlocks++;
668  if (nHeightIn > nHeightLast)
669  nHeightLast = nHeightIn;
670  if (nTimeIn > nTimeLast)
671  nTimeLast = nTimeIn;
672  }
673 };
674 
677  BLOCK_VALID_HEADER = 1, // parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
678  BLOCK_VALID_TREE = 2, // parent found, difficulty matches, timestamp >= median previous, checkpoint
679  BLOCK_VALID_TRANSACTIONS = 3, // only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids, sigops, size, merkle root
680  BLOCK_VALID_CHAIN = 4, // outputs do not overspend inputs, no double spends, coinbase output ok, immature coinbase spends, BIP30
681  BLOCK_VALID_SCRIPTS = 5, // scripts/signatures ok
683 
684  BLOCK_HAVE_DATA = 8, // full block available in blk*.dat
685  BLOCK_HAVE_UNDO = 16, // undo data available in rev*.dat
687 
688  BLOCK_FAILED_VALID = 32, // stage after last reached validness failed
689  BLOCK_FAILED_CHILD = 64, // descends from failed block
691 };
692 
699 {
700 public:
701  // pointer to the hash of the block, if any. memory is owned by this CBlockIndex
703 
704  // pointer to the index of the predecessor of this block
706 
707  // height of the entry in the chain. The genesis block has height 0
708  int nHeight;
709 
710  // Which # file this block is stored in (blk?????.dat)
711  int nFile;
712 
713  // Byte offset within blk?????.dat where this block's data is stored
714  unsigned int nDataPos;
715 
716  // Byte offset within rev?????.dat where this block's undo data is stored
717  unsigned int nUndoPos;
718 
719  // (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
721 
722  // Number of transactions in this block.
723  // Note: in a potential headers-first mode, this number cannot be relied upon
724  unsigned int nTx;
725 
726  // (memory only) Number of transactions in the chain up to and including this block
727  unsigned int nChainTx; // change to 64-bit type when necessary; won't happen before 2030
728 
729  // Verification status of this block. See enum BlockStatus
730  unsigned int nStatus;
731 
732  // block header
733  int nVersion;
735  unsigned int nTime;
736  unsigned int nBits;
737  unsigned int nNonce;
738 
739  // (memory only) Sequencial id assigned to distinguish order in which blocks are received.
740  uint32_t nSequenceId;
741 
743  {
744  phashBlock = NULL;
745  pprev = NULL;
746  nHeight = 0;
747  nFile = 0;
748  nDataPos = 0;
749  nUndoPos = 0;
750  nChainWork = 0;
751  nTx = 0;
752  nChainTx = 0;
753  nStatus = 0;
754  nSequenceId = 0;
755 
756  nVersion = 0;
757  hashMerkleRoot = 0;
758  nTime = 0;
759  nBits = 0;
760  nNonce = 0;
761  }
762 
764  {
765  phashBlock = NULL;
766  pprev = NULL;
767  nHeight = 0;
768  nFile = 0;
769  nDataPos = 0;
770  nUndoPos = 0;
771  nChainWork = 0;
772  nTx = 0;
773  nChainTx = 0;
774  nStatus = 0;
775  nSequenceId = 0;
776 
777  nVersion = block.nVersion;
778  hashMerkleRoot = block.hashMerkleRoot;
779  nTime = block.nTime;
780  nBits = block.nBits;
781  nNonce = block.nNonce;
782  }
783 
785  CDiskBlockPos ret;
786  if (nStatus & BLOCK_HAVE_DATA) {
787  ret.nFile = nFile;
788  ret.nPos = nDataPos;
789  }
790  return ret;
791  }
792 
794  CDiskBlockPos ret;
795  if (nStatus & BLOCK_HAVE_UNDO) {
796  ret.nFile = nFile;
797  ret.nPos = nUndoPos;
798  }
799  return ret;
800  }
801 
803  {
804  CBlockHeader block;
805  block.nVersion = nVersion;
806  if (pprev)
807  block.hashPrevBlock = pprev->GetBlockHash();
809  block.nTime = nTime;
810  block.nBits = nBits;
811  block.nNonce = nNonce;
812  return block;
813  }
814 
816  {
817  return *phashBlock;
818  }
819 
820  int64_t GetBlockTime() const
821  {
822  return (int64_t)nTime;
823  }
824 
826  {
827  CBigNum bnTarget;
828  bnTarget.SetCompact(nBits);
829  if (bnTarget <= 0)
830  return 0;
831  return (CBigNum(1)<<256) / (bnTarget+1);
832  }
833 
834  bool CheckIndex() const
835  {
838  return true; // return CheckProofOfWork(GetBlockHash(), nBits);
839  }
840 
841  enum { nMedianTimeSpan=11 };
842 
843  int64_t GetMedianTimePast() const
844  {
845  int64_t pmedian[nMedianTimeSpan];
846  int64_t* pbegin = &pmedian[nMedianTimeSpan];
847  int64_t* pend = &pmedian[nMedianTimeSpan];
848 
849  const CBlockIndex* pindex = this;
850  for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
851  *(--pbegin) = pindex->GetBlockTime();
852 
853  std::sort(pbegin, pend);
854  return pbegin[(pend - pbegin)/2];
855  }
856 
857  int64_t GetMedianTime() const;
858 
863  static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart,
864  unsigned int nRequired, unsigned int nToCheck);
865 
866  std::string ToString() const
867  {
868  return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
869  pprev, nHeight,
870  hashMerkleRoot.ToString().c_str(),
871  GetBlockHash().ToString().c_str());
872  }
873 
874  void print() const
875  {
876  LogPrintf("%s\n", ToString().c_str());
877  }
878 };
879 
880 
881 
884 {
885 public:
887 
889  hashPrev = 0;
890  }
891 
892  explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex) {
893  hashPrev = (pprev ? pprev->GetBlockHash() : 0);
894  }
895 
897  (
898  if (!(nType & SER_GETHASH))
900 
903  READWRITE(VARINT(nTx));
905  READWRITE(VARINT(nFile));
906  if (nStatus & BLOCK_HAVE_DATA)
908  if (nStatus & BLOCK_HAVE_UNDO)
910 
911  // block header
912  READWRITE(this->nVersion);
913  READWRITE(hashPrev);
915  READWRITE(nTime);
916  READWRITE(nBits);
917  READWRITE(nNonce);
918  )
919 
920  uint256 GetBlockHash() const
921  {
922  CBlockHeader block;
923  block.nVersion = nVersion;
924  block.hashPrevBlock = hashPrev;
926  block.nTime = nTime;
927  block.nBits = nBits;
928  block.nNonce = nNonce;
929  return block.GetHash();
930  }
931 
932 
933  std::string ToString() const
934  {
935  std::string str = "CDiskBlockIndex(";
936  str += CBlockIndex::ToString();
937  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
938  GetBlockHash().ToString().c_str(),
939  hashPrev.ToString().c_str());
940  return str;
941  }
942 
943  void print() const
944  {
945  LogPrintf("%s\n", ToString().c_str());
946  }
947 };
948 
951 private:
952  enum mode_state {
953  MODE_VALID, // everything ok
954  MODE_INVALID, // network rule violation (DoS value may be set)
955  MODE_ERROR, // run-time error
956  } mode;
957  int nDoS;
958  std::string strRejectReason;
959  unsigned char chRejectCode;
961 public:
962  CValidationState() : mode(MODE_VALID), nDoS(0), corruptionPossible(false) {}
963  bool DoS(int level, bool ret = false,
964  unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="",
965  bool corruptionIn=false) {
966  chRejectCode = chRejectCodeIn;
967  strRejectReason = strRejectReasonIn;
968  corruptionPossible = corruptionIn;
969  if (mode == MODE_ERROR)
970  return ret;
971  nDoS += level;
972  mode = MODE_INVALID;
973  return ret;
974  }
975  bool Invalid(bool ret = false,
976  unsigned char _chRejectCode=0, std::string _strRejectReason="") {
977  return DoS(0, ret, _chRejectCode, _strRejectReason);
978  }
979  bool Error(std::string strRejectReasonIn="") {
980  if (mode == MODE_VALID)
981  strRejectReason = strRejectReasonIn;
982  mode = MODE_ERROR;
983  return false;
984  }
985  bool Abort(const std::string &msg) {
986  AbortNode(msg);
987  return Error(msg);
988  }
989  bool IsValid() const {
990  return mode == MODE_VALID;
991  }
992  bool IsInvalid() const {
993  return mode == MODE_INVALID;
994  }
995  bool IsError() const {
996  return mode == MODE_ERROR;
997  }
998  bool IsInvalid(int &nDoSOut) const {
999  if (IsInvalid()) {
1000  nDoSOut = nDoS;
1001  return true;
1002  }
1003  return false;
1004  }
1005  bool CorruptionPossible() const {
1006  return corruptionPossible;
1007  }
1008  unsigned char GetRejectCode() const { return chRejectCode; }
1009  std::string GetRejectReason() const { return strRejectReason; }
1010 };
1011 
1013 class CChain {
1014 private:
1015  std::vector<CBlockIndex*> vChain;
1016 
1017 public:
1020  return vChain.size() > 0 ? vChain[0] : NULL;
1021  }
1022 
1024  CBlockIndex *Tip() const {
1025  return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
1026  }
1027 
1029  CBlockIndex *operator[](int nHeight) const {
1030  if (nHeight < 0 || nHeight >= (int)vChain.size())
1031  return NULL;
1032  return vChain[nHeight];
1033  }
1034 
1036  friend bool operator==(const CChain &a, const CChain &b) {
1037  return a.vChain.size() == b.vChain.size() &&
1038  a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
1039  }
1040 
1042  bool Contains(const CBlockIndex *pindex) const {
1043  return (*this)[pindex->nHeight] == pindex;
1044  }
1045 
1047  CBlockIndex *Next(const CBlockIndex *pindex) const {
1048  if (Contains(pindex))
1049  return (*this)[pindex->nHeight + 1];
1050  else
1051  return NULL;
1052  }
1053 
1055  int Height() const {
1056  return vChain.size() - 1;
1057  }
1058 
1060  CBlockIndex *SetTip(CBlockIndex *pindex);
1061 
1063  CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
1064 
1066  CBlockIndex *FindFork(const CBlockLocator &locator) const;
1067 };
1068 
1070 extern CChain chainActive;
1071 
1073 extern CChain chainMostWork;
1074 
1076 extern CCoinsViewCache *pcoinsTip;
1077 
1079 extern CBlockTreeDB *pblocktree;
1080 
1082 {
1084  std::vector<int64_t> vTxFees;
1085  std::vector<int64_t> vTxSigOps;
1086 };
1087 
1088 
1089 
1090 
1091 
1092 
1097 {
1098 public:
1099  // Public only for unit testing
1102 
1103 public:
1104  // Public only for unit testing and relay testing
1105  // (not relayed)
1106  std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
1107 
1108  // Create from a CBlock, filtering transactions according to filter
1109  // Note that this will call IsRelevantAndUpdate on the filter for each transaction,
1110  // thus the filter will likely be modified.
1111  CMerkleBlock(const CBlock& block, CBloomFilter& filter);
1112 
1114  (
1115  READWRITE(header);
1116  READWRITE(txn);
1117  )
1118 };
1119 
1120 
1122 protected:
1123  virtual void SyncTransaction(const uint256 &hash, const CTransaction &tx, const CBlock *pblock) =0;
1124  virtual void EraseFromWallet(const uint256 &hash) =0;
1125  virtual void SetBestChain(const CBlockLocator &locator) =0;
1126  virtual void UpdatedTransaction(const uint256 &hash) =0;
1127  virtual void Inventory(const uint256 &hash) =0;
1128  virtual void ResendWalletTransactions() =0;
1131  friend void ::UnregisterAllWallets();
1132 };
1133 
1134 #endif
uint64_t nLastBlockSize
Definition: miner.cpp:87
std::map< uint256, CBlockIndex * > mapBlockIndex
Definition: main.cpp:42
#define VARINT(obj)
Definition: serialize.h:319
bool AcceptBlock(CBlock &block, CValidationState &state, CDiskBlockPos *dbp=NULL)
Definition: main.cpp:2295
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: main2.cpp:243
CDiskBlockPos GetBlockPos() const
Definition: main.h:784
unsigned int nTxOffsetIn
Definition: main.h:253
int nVersion
Definition: core.h:185
CBlockHeader header
Definition: main.h:1100
bool CheckBlock(const CBlock &block, CValidationState &state, bool fCheckPOW=true, bool fCheckMerkleRoot=true)
Definition: main.cpp:2229
CBigNum GetBlockWork() const
Definition: main.h:825
void TraverseAndBuild(int height, unsigned int pos, const std::vector< uint256 > &vTxid, const std::vector< bool > &vMatch)
Definition: main.cpp:2584
void UnregisterWallet(CWalletInterface *pwalletIn)
Unregister a wallet from core.
Definition: main.cpp:165
std::vector< CBlockIndex * > vChain
Definition: main.h:1015
CDiskBlockPos(int nFileIn, unsigned int nPosIn)
Definition: main.h:227
bool AddToBlockIndex(CBlock &block, CValidationState &state, const CDiskBlockPos &pos)
Definition: main.cpp:2076
unsigned int nTxOffset
Definition: main.h:246
int nVersion
Definition: core.h:345
bool IsInitialBlockDownload()
Check whether we are doing an initial block download (synchronizing from disk or network) ...
Definition: main.cpp:1246
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
Definition: serialize.h:806
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:43
IMPLEMENT_SERIALIZE(READWRITE(VARINT(nFile));READWRITE(VARINT(nPos));) CDiskBlockPos()
Definition: main.h:218
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: core.h:457
CScript COINBASE_FLAGS
Definition: main.cpp:75
CBlockIndex * pprev
Definition: main.h:705
#define READWRITE(obj)
Definition: serialize.h:101
int64_t GetBlockValue(int nHeight, int64_t nFees)
Definition: main2.cpp:14
int nMisbehavior
Definition: main.h:207
bool LoadExternalBlockFile(FILE *fileIn, CDiskBlockPos *dbp=NULL)
Import blocks from an external file.
Definition: main.cpp:3007
bool IsError() const
Definition: main.h:995
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
Definition: main.h:662
void FileCommit(FILE *fileout)
Definition: util.cpp:1097
void swap(CScriptCheck &check)
Definition: main.h:426
unsigned int nTransactions
Definition: main.h:533
Definition: core.h:394
CScriptCheck()
Definition: main.h:419
#define strprintf
Definition: tinyformat.h:1011
CScriptCheck(const CCoins &txFromIn, const CTransaction &txToIn, unsigned int nInIn, unsigned int nFlagsIn, int nHashTypeIn)
Definition: main.h:420
An in-memory indexed chain of blocks.
Definition: main.h:1013
GetMinFee_mode
Definition: main.h:268
CBlockIndex * operator[](int nHeight) const
Returns the index entry at a particular height in this chain, or NULL if no such height exists...
Definition: main.h:1029
unsigned int nFlags
Definition: main.h:415
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
Calculate the minimum amount of work a received block needs, without knowing its direct parent...
Definition: main2.cpp:35
int nIndex
Definition: main.h:444
inv message data
Definition: protocol.h:128
CBlockIndex()
Definition: main.h:742
int GetDepthInMainChainINTERNAL(CBlockIndex *&pindexRet) const
Definition: main.cpp:1023
enum CValidationState::mode_state mode
uint256 hashBlock
Definition: main.h:442
bool CorruptionPossible() const
Definition: main.h:1005
void PrintBlockTree()
Print the loaded block tree.
Definition: main.cpp:2938
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:43
std::string ToString() const
Definition: main.h:866
unsigned char chRejectCode
Definition: main.h:959
std::vector< int64_t > vTxFees
Definition: main.h:1084
CScript scriptPubKey
Definition: main.h:412
bool IsStandardTx(const CTransaction &tx, std::string &reason)
Check for standard transaction types.
IMPLEMENT_SERIALIZE(READWRITE(*(CDiskBlockPos *) this);READWRITE(VARINT(nTxOffset));) CDiskTxPos(const CDiskBlockPos &blockIn
unsigned int nSize
Definition: main.h:626
friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b)
Definition: main.h:232
const uint256 &hashBlock CAutoFile fileout
Definition: main.h:349
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:63
bool fMerkleVerified
Definition: main.h:447
Data structure that represents a partial merkle tree.
Definition: main.h:529
int nSyncHeight
Definition: main.h:208
int nFile
Definition: main.h:711
CTxMemPool mempool
Definition: main.cpp:40
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:69
bool fReindex
Definition: main.cpp:48
const std::string strMessageMagic
Definition: main.cpp:77
unsigned int GetSerializeSize(const T &obj)
Definition: serialize.h:1236
unsigned int nCoinCacheSize
Definition: main.cpp:51
bool AllowFree(double dPriority)
Definition: main.h:309
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: util.cpp:1411
bool corruptionPossible
Definition: main.h:960
bool ConnectBlock(CBlock &block, CValidationState &state, CBlockIndex *pindex, CCoinsViewCache &coins, bool fJustCheck=false)
Definition: main.cpp:1669
uint256 nChainWork
Definition: main.h:720
CBlockIndex(CBlockHeader &block)
Definition: main.h:763
CDiskBlockIndex(CBlockIndex *pindex)
Definition: main.h:892
unsigned int nHeightLast
Definition: main.h:629
#define FLATDATA(obj)
Definition: serialize.h:318
void Init()
Definition: main.h:460
unsigned int nChainTx
Definition: main.h:727
bool VerifySignature(const CCoins &txFrom, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Verify a signature.
Definition: main.cpp:1449
unsigned int nUndoSize
Definition: main.h:627
std::string GetWarnings(std::string strFor)
Format a string that describes several potential problems detected by the core.
int64_t GetMinFee(const CTransaction &tx, unsigned int nBytes, bool fAllowFree, enum GetMinFee_mode mode)
Definition: main.cpp:847
bool CheckIndex() const
Definition: main.h:834
bool InitBlockIndex()
Initialize a new block tree database + block data on disk.
Definition: main.cpp:2903
bool Invalid(bool ret=false, unsigned char _chRejectCode=0, std::string _strRejectReason="")
Definition: main.h:975
int GetDepthInMainChain() const
Definition: main.h:485
void UpdateCoins(const CTransaction &tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, const uint256 &txhash)
Definition: main.cpp:1423
friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b)
Definition: main.h:236
uint64_t nLastBlockTx
Definition: miner.cpp:86
CBlockLocator GetLocator(const CBlockIndex *pindex=NULL) const
Return a CBlockLocator that refers to a block in this chain (by default the tip). ...
Definition: main.cpp:395
int nHashType
Definition: main.h:416
bool operator()() const
Definition: main.cpp:1442
int nCommonHeight
Definition: main.h:209
int GetBlocksToMaturity() const
Definition: main.cpp:1059
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
std::vector< uint256 > vHash
Definition: main.h:539
int Height() const
Return the maximal height in the chain.
Definition: main.h:1055
bool IsInMainChain() const
Definition: main.h:486
Used to relay blocks as header + vector to filtered nodes.
Definition: main.h:1096
unsigned int nTime
Definition: main.h:735
unsigned int CalcTreeWidth(int height)
Definition: main.h:545
#define LogPrintf(...)
Definition: util.h:118
uint64_t nTimeFirst
Definition: main.h:630
FILE * OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly=false)
Open a block file (blk?????.dat)
Definition: main.cpp:2729
CBlockIndex * InsertBlockIndex(uint256 hash)
Create a new block index entry for a given block hash.
Definition: main.cpp:2737
unsigned int nStatus
Definition: main.h:730
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or NULL if the given index is not found or is the tip...
Definition: main.h:1047
void SyncWithWallets(const uint256 &hash, const CTransaction &tx, const CBlock *pblock=NULL)
Push an updated transaction to all registered wallets.
Definition: main.cpp:183
Access to the block database (blocks/index/)
Definition: txdb.h:47
CBlockHeader GetBlockHeader() const
Definition: main.h:802
uint256 hashMerkleRoot
Definition: core.h:347
void print() const
Definition: main.h:874
unsigned int nDataPos
Definition: main.h:714
void RegisterWallet(CWalletInterface *pwalletIn)
Register a wallet to receive updates from core.
Definition: main.cpp:156
CBlockTreeDB * pblocktree
Global variable that points to the active block tree (protected by cs_main)
Definition: main.cpp:437
IMPLEMENT_SERIALIZE(READWRITE(VARINT(nBlocks));READWRITE(VARINT(nSize));READWRITE(VARINT(nUndoSize));READWRITE(VARINT(nHeightFirst));READWRITE(VARINT(nHeightLast));READWRITE(VARINT(nTimeFirst));READWRITE(VARINT(nTimeLast));) void SetNull()
Definition: main.h:633
bool ProcessBlock(CValidationState &state, CNode *pfrom, CBlock *pblock, CDiskBlockPos *dbp=NULL)
Process an incoming block.
Definition: main.cpp:2431
bool VerifyDB(int nCheckLevel, int nCheckDepth)
Verify consistency of the block and coin databases.
Definition: main.cpp:2812
CBlockFileInfo()
Definition: main.h:653
uint256 hashPrevBlock
Definition: core.h:346
bool DoS(int level, bool ret=false, unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="", bool corruptionIn=false)
Definition: main.h:963
int64_t GetMedianTime() const
Definition: main.cpp:2406
C++ wrapper for BIGNUM (OpenSSL bignum)
Definition: bignum.h:57
bool CheckInputs(const CTransaction &tx, CValidationState &state, CCoinsViewCache &view, bool fScriptChecks=true, unsigned int flags=SCRIPT_VERIFY_P2SH|SCRIPT_VERIFY_STRICTENC, std::vector< CScriptCheck > *pvChecks=NULL)
Definition: main.cpp:1454
bool IsFinalTx(const CTransaction &tx, int nBlockHeight=0, int64_t nBlockTime=0)
Definition: main.cpp:617
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Count ECDSA signature operations the old-fashioned (pre-0.6) way.
Definition: main.cpp:700
std::vector< uint256 > vMerkleBranch
Definition: main.h:443
unsigned int nBits
Definition: main.h:736
CDiskBlockIndex()
Definition: main.h:888
bool AcceptToMemoryPool(bool fLimitFree=true)
Definition: main.cpp:1067
uint256 hashMerkleRoot
Definition: main.h:734
int nScriptCheckThreads
Definition: main.cpp:46
friend bool operator==(const CChain &a, const CChain &b)
Compare two chains efficiently.
Definition: main.h:1036
bool Error(std::string strRejectReasonIn="")
Definition: main.h:979
unsigned int nNonce
Definition: core.h:350
unsigned int nHeightFirst
Definition: main.h:628
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector< uint256 > &vMatch)
Definition: main.cpp:2602
Used to marshal pointers into hashes for db storage.
Definition: main.h:883
bool IsValid() const
Definition: main.h:989
bool CheckDiskSpace(uint64_t nAdditionalBytes=0)
Check whether enough disk space is available for an incoming block.
Definition: main.cpp:2695
void print() const
Definition: main.h:943
CPartialMerkleTree txn
Definition: main.h:1101
bool IsNull() const
Definition: main.h:241
bool Abort(const std::string &msg)
Definition: main.h:985
std::string ToString() const
Definition: main.h:933
void ThreadScriptCheck()
Run an instance of the script checking thread.
Definition: main.cpp:1664
unsigned int nPos
Definition: main.h:216
CChain chainMostWork
The currently best known chain of headers (some of which may be invalid).
Definition: main.cpp:44
void UnloadBlockIndex()
Unload database information.
Definition: main.cpp:2886
void UpdateTime(CBlockHeader &block, const CBlockIndex *pindexPrev)
Definition: main.cpp:1404
bool IsInvalid(int &nDoSOut) const
Definition: main.h:998
bool WriteBlockToDisk(CBlock &block, CDiskBlockPos &pos)
Functions for disk access for blocks.
Definition: main.cpp:1145
Closure representing one script verification Note that this stores references to the spending transac...
Definition: main.h:409
bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow=false)
Retrieve a transaction (from memory pool, or from disk, if possible)
Definition: main.cpp:1075
bool IsInvalid() const
Definition: main.h:992
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos)
Definition: main.cpp:1171
int nVersion
Definition: main.h:733
std::vector< bool > vBits
Definition: main.h:536
std::string GetRejectReason() const
Definition: main.h:1009
bool LoadBlockIndex()
Load the block tree and coins database from disk.
Definition: main.cpp:2894
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
Definition: main.cpp:436
unsigned char GetRejectCode() const
Definition: main.h:1008
CValidationState()
Definition: main.h:962
unsigned int nUndoPos
Definition: main.h:717
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or NULL if none.
Definition: main.h:1019
CMerkleTx(const CTransaction &txIn)
Definition: main.h:455
Capture information about block/transaction validation.
Definition: main.h:950
256-bit unsigned integer
Definition: uint256.h:532
IMPLEMENT_SERIALIZE(READWRITE(vtxundo);) bool WriteToDisk(CDiskBlockPos &pos
uint256 hashPrev
Definition: main.h:886
const CTransaction * ptxTo
Definition: main.h:413
uint256 ExtractMatches(std::vector< uint256 > &vMatch)
Definition: main.cpp:2648
unsigned int nTime
Definition: core.h:348
bool AcceptToMemoryPool(CTxMemPool &pool, CValidationState &state, const CTransaction &tx, bool fLimitFree, bool *pfMissingInputs, bool fRejectInsaneFee=false)
(try to) add transaction to memory pool
Definition: main.cpp:878
uint256 GetHash() const
Definition: core.cpp:216
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
Definition: main.cpp:344
void UnregisterAllWallets()
Unregister all wallets from core.
Definition: main.cpp:174
void RegisterNodeSignals(CNodeSignals &nodeSignals)
Register with a network node to receive its signals.
Definition: main.cpp:359
void PushGetBlocks(CNode *pnode, CBlockIndex *pindexBegin, uint256 hashEnd)
Definition: main.cpp:2419
IMPLEMENT_SERIALIZE(nSerSize+=SerReadWrite(s,*(CTransaction *) this, nType, nVersion, ser_action);nVersion=this->nVersion;READWRITE(hashBlock);READWRITE(vMerkleBranch);READWRITE(nIndex);) int SetMerkleBranch(const CBlock *pblock
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Definition: main.h:1106
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.
Undo information for a CBlock.
Definition: main.h:337
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
Undo information for a CTransaction.
Definition: core.h:321
std::string ToString() const
Definition: uint256.h:341
std::vector< int64_t > vTxSigOps
Definition: main.h:1085
CBlockIndex * FindFork(const CBlockLocator &locator) const
Find the last common block between this chain and a locator.
Definition: main.cpp:422
bool AreInputsStandard(const CTransaction &tx, CCoinsViewCache &mapInputs)
Check for standard transaction types.
Definition: main.cpp:646
int NodeId
Definition: net.h:75
void SetNull()
Definition: main.h:240
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: main.h:1042
bool DisconnectBlock(CBlock &block, CValidationState &state, CBlockIndex *pindex, CCoinsViewCache &coins, bool *pfClean=NULL)
Functions for validating blocks and updating the block tree.
Definition: main.cpp:1543
bool ActivateBestChain(CValidationState &state)
Find the best known block, and make it the tip of the block chain.
Definition: main.cpp:2028
CBlockIndex * SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: main.cpp:382
CMerkleBlock(const CBlock &block, CBloomFilter &filter)
Definition: main.cpp:2534
unsigned int nBlocks
Definition: main.h:625
bool fImporting
Definition: main.cpp:47
unsigned int GetP2SHSigOpCount(const CTransaction &tx, CCoinsViewCache &mapInputs)
Count ECDSA signature operations in pay-to-script-hash inputs.
Definition: main.cpp:714
std::string ToString() const
Definition: main.h:657
bool ProcessMessages(CNode *pfrom)
Process protocol messages received from a given node.
Definition: main.cpp:4042
BlockStatus
Definition: main.h:675
CBlock block
Definition: main.h:1083
unsigned int nIn
Definition: main.h:414
uint64_t nTimeLast
Definition: main.h:631
uint32_t nSequenceId
Definition: main.h:740
int64_t nTimeBestReceived
Definition: main.cpp:45
bool CheckTransaction(const CTransaction &tx, CValidationState &state)
Definition: main.cpp:783
CMerkleTx()
Definition: main.h:450
CDiskBlockPos GetUndoPos() const
Definition: main.h:793
FILE * OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly=false)
Open an undo file (rev?????.dat)
Definition: main.cpp:2733
std::string strRejectReason
Definition: main.h:958
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:179
int nHeight
Definition: main.h:708
Information about a peer.
Definition: net.h:223
bool SendMessages(CNode *pto, bool fSendTrickle)
Send queued protocol messages to be sent to a give node.
Definition: main.cpp:4163
std::vector< int > vHeightInFlight
Definition: main.h:210
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:309
unsigned int nBits
Definition: core.h:349
int64_t GetBlockTime() const
Definition: main.h:820
void SetNull()
Definition: main.h:260
CCriticalSection cs_main
Definition: main.cpp:38
std::vector< CTxUndo > vtxundo
Definition: main.h:340
int nFile
Definition: main.h:215
CDiskTxPos()
Definition: main.h:256
int64_t GetMedianTimePast() const
Definition: main.h:843
void Misbehaving(NodeId nodeid, int howmuch)
Increase a node's misbehavior score.
Definition: main.cpp:1346
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock)
Definition: main2.cpp:232
unsigned int nNonce
Definition: main.h:737
unsigned int nTx
Definition: main.h:724
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: core.h:340
Definition: main.h:271
RAII wrapper for FILE*.
Definition: serialize.h:1154
bool AbortNode(const std::string &msg)
Abort with a message.
Definition: main.cpp:2687
bool fBenchmark
Definition: main.cpp:49
IMPLEMENT_SERIALIZE(if(!(nType &SER_GETHASH)) READWRITE(VARINT(nVersion));READWRITE(VARINT(nHeight));READWRITE(VARINT(nStatus));READWRITE(VARINT(nTx));if(nStatus &(BLOCK_HAVE_DATA|BLOCK_HAVE_UNDO)) READWRITE(VARINT(nFile));if(nStatus &BLOCK_HAVE_DATA) READWRITE(VARINT(nDataPos));if(nStatus &BLOCK_HAVE_UNDO) READWRITE(VARINT(nUndoPos));READWRITE(this->nVersion);READWRITE(hashPrev);READWRITE(hashMerkleRoot);READWRITE(nTime);READWRITE(nBits);READWRITE(nNonce);) uint256 GetBlockHash() const
Definition: main.h:897
bool fTxIndex
Definition: main.cpp:50
uint256 GetBlockHash() const
Definition: main.h:815
A transaction with a merkle branch linking it to the block chain.
Definition: main.h:436
void UnregisterNodeSignals(CNodeSignals &nodeSignals)
Unregister a network node.
Definition: main.cpp:368
uint256 CalcHash(int height, unsigned int pos, const std::vector< uint256 > &vTxid)
Definition: main.cpp:2567
static bool IsSuperMajority(int minVersion, const CBlockIndex *pstart, unsigned int nRequired, unsigned int nToCheck)
Returns true if there are nRequired or more blocks of minVersion or above in the last nToCheck blocks...
Definition: main.cpp:2394
const uint256 * phashBlock
Definition: main.h:702