You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chain.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_CHAIN_H
  6. #define BITCOIN_CHAIN_H
  7. #include "arith_uint256.h"
  8. #include "primitives/block.h"
  9. #include "pow.h"
  10. #include "tinyformat.h"
  11. #include "uint256.h"
  12. #include <vector>
  13. #include <boost/foreach.hpp>
  14. struct CDiskBlockPos
  15. {
  16. int nFile;
  17. unsigned int nPos;
  18. ADD_SERIALIZE_METHODS;
  19. template <typename Stream, typename Operation>
  20. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  21. READWRITE(VARINT(nFile));
  22. READWRITE(VARINT(nPos));
  23. }
  24. CDiskBlockPos() {
  25. SetNull();
  26. }
  27. CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
  28. nFile = nFileIn;
  29. nPos = nPosIn;
  30. }
  31. friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
  32. return (a.nFile == b.nFile && a.nPos == b.nPos);
  33. }
  34. friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
  35. return !(a == b);
  36. }
  37. void SetNull() { nFile = -1; nPos = 0; }
  38. bool IsNull() const { return (nFile == -1); }
  39. std::string ToString() const
  40. {
  41. return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
  42. }
  43. };
  44. enum BlockStatus {
  45. //! Unused.
  46. BLOCK_VALID_UNKNOWN = 0,
  47. //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
  48. BLOCK_VALID_HEADER = 1,
  49. //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
  50. //! are also at least TREE.
  51. BLOCK_VALID_TREE = 2,
  52. /**
  53. * Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid, no duplicate txids,
  54. * sigops, size, merkle root. Implies all parents are at least TREE but not necessarily TRANSACTIONS. When all
  55. * parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will be set.
  56. */
  57. BLOCK_VALID_TRANSACTIONS = 3,
  58. //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
  59. //! Implies all parents are also at least CHAIN.
  60. BLOCK_VALID_CHAIN = 4,
  61. //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
  62. BLOCK_VALID_SCRIPTS = 5,
  63. //! All validity bits.
  64. BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
  65. BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS,
  66. BLOCK_HAVE_DATA = 8, //! full block available in blk*.dat
  67. BLOCK_HAVE_UNDO = 16, //! undo data available in rev*.dat
  68. BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO,
  69. BLOCK_FAILED_VALID = 32, //! stage after last reached validness failed
  70. BLOCK_FAILED_CHILD = 64, //! descends from failed block
  71. BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
  72. };
  73. /** The block chain is a tree shaped structure starting with the
  74. * genesis block at the root, with each block potentially having multiple
  75. * candidates to be the next block. A blockindex may have multiple pprev pointing
  76. * to it, but at most one of them can be part of the currently active branch.
  77. */
  78. class CBlockIndex
  79. {
  80. public:
  81. //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
  82. const uint256* phashBlock;
  83. //! pointer to the index of the predecessor of this block
  84. CBlockIndex* pprev;
  85. //! pointer to the index of some further predecessor of this block
  86. CBlockIndex* pskip;
  87. //! height of the entry in the chain. The genesis block has height 0
  88. int nHeight;
  89. //! Which # file this block is stored in (blk?????.dat)
  90. int nFile;
  91. //! Byte offset within blk?????.dat where this block's data is stored
  92. unsigned int nDataPos;
  93. //! Byte offset within rev?????.dat where this block's undo data is stored
  94. unsigned int nUndoPos;
  95. //! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
  96. arith_uint256 nChainWork;
  97. //! Number of transactions in this block.
  98. //! Note: in a potential headers-first mode, this number cannot be relied upon
  99. unsigned int nTx;
  100. //! (memory only) Number of transactions in the chain up to and including this block.
  101. //! This value will be non-zero only if and only if transactions for this block and all its parents are available.
  102. //! Change to 64-bit type when necessary; won't happen before 2030
  103. unsigned int nChainTx;
  104. //! Verification status of this block. See enum BlockStatus
  105. unsigned int nStatus;
  106. //! block header
  107. int nVersion;
  108. uint256 hashMerkleRoot;
  109. unsigned int nTime;
  110. unsigned int nBits;
  111. unsigned int nNonce;
  112. //! (memory only) Sequential id assigned to distinguish order in which blocks are received.
  113. uint32_t nSequenceId;
  114. void SetNull()
  115. {
  116. phashBlock = NULL;
  117. pprev = NULL;
  118. pskip = NULL;
  119. nHeight = 0;
  120. nFile = 0;
  121. nDataPos = 0;
  122. nUndoPos = 0;
  123. nChainWork = arith_uint256();
  124. nTx = 0;
  125. nChainTx = 0;
  126. nStatus = 0;
  127. nSequenceId = 0;
  128. nVersion = 0;
  129. hashMerkleRoot = uint256();
  130. nTime = 0;
  131. nBits = 0;
  132. nNonce = 0;
  133. }
  134. CBlockIndex()
  135. {
  136. SetNull();
  137. }
  138. CBlockIndex(const CBlockHeader& block)
  139. {
  140. SetNull();
  141. nVersion = block.nVersion;
  142. hashMerkleRoot = block.hashMerkleRoot;
  143. nTime = block.nTime;
  144. nBits = block.nBits;
  145. nNonce = block.nNonce;
  146. }
  147. CDiskBlockPos GetBlockPos() const {
  148. CDiskBlockPos ret;
  149. if (nStatus & BLOCK_HAVE_DATA) {
  150. ret.nFile = nFile;
  151. ret.nPos = nDataPos;
  152. }
  153. return ret;
  154. }
  155. CDiskBlockPos GetUndoPos() const {
  156. CDiskBlockPos ret;
  157. if (nStatus & BLOCK_HAVE_UNDO) {
  158. ret.nFile = nFile;
  159. ret.nPos = nUndoPos;
  160. }
  161. return ret;
  162. }
  163. CBlockHeader GetBlockHeader() const
  164. {
  165. CBlockHeader block;
  166. block.nVersion = nVersion;
  167. if (pprev)
  168. block.hashPrevBlock = pprev->GetBlockHash();
  169. block.hashMerkleRoot = hashMerkleRoot;
  170. block.nTime = nTime;
  171. block.nBits = nBits;
  172. block.nNonce = nNonce;
  173. return block;
  174. }
  175. uint256 GetBlockHash() const
  176. {
  177. return *phashBlock;
  178. }
  179. int64_t GetBlockTime() const
  180. {
  181. return (int64_t)nTime;
  182. }
  183. enum { nMedianTimeSpan=11 };
  184. int64_t GetMedianTimePast() const
  185. {
  186. int64_t pmedian[nMedianTimeSpan];
  187. int64_t* pbegin = &pmedian[nMedianTimeSpan];
  188. int64_t* pend = &pmedian[nMedianTimeSpan];
  189. const CBlockIndex* pindex = this;
  190. for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
  191. *(--pbegin) = pindex->GetBlockTime();
  192. std::sort(pbegin, pend);
  193. return pbegin[(pend - pbegin)/2];
  194. }
  195. std::string ToString() const
  196. {
  197. return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
  198. pprev, nHeight,
  199. hashMerkleRoot.ToString(),
  200. GetBlockHash().ToString());
  201. }
  202. //! Check whether this block index entry is valid up to the passed validity level.
  203. bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const
  204. {
  205. assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
  206. if (nStatus & BLOCK_FAILED_MASK)
  207. return false;
  208. return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
  209. }
  210. //! Raise the validity level of this block index entry.
  211. //! Returns true if the validity was changed.
  212. bool RaiseValidity(enum BlockStatus nUpTo)
  213. {
  214. assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
  215. if (nStatus & BLOCK_FAILED_MASK)
  216. return false;
  217. if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
  218. nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
  219. return true;
  220. }
  221. return false;
  222. }
  223. //! Build the skiplist pointer for this entry.
  224. void BuildSkip();
  225. //! Efficiently find an ancestor of this block.
  226. CBlockIndex* GetAncestor(int height);
  227. const CBlockIndex* GetAncestor(int height) const;
  228. };
  229. /** Used to marshal pointers into hashes for db storage. */
  230. class CDiskBlockIndex : public CBlockIndex
  231. {
  232. public:
  233. uint256 hashPrev;
  234. CDiskBlockIndex() {
  235. hashPrev = uint256();
  236. }
  237. explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
  238. hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
  239. }
  240. ADD_SERIALIZE_METHODS;
  241. template <typename Stream, typename Operation>
  242. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  243. if (!(nType & SER_GETHASH))
  244. READWRITE(VARINT(nVersion));
  245. READWRITE(VARINT(nHeight));
  246. READWRITE(VARINT(nStatus));
  247. READWRITE(VARINT(nTx));
  248. if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
  249. READWRITE(VARINT(nFile));
  250. if (nStatus & BLOCK_HAVE_DATA)
  251. READWRITE(VARINT(nDataPos));
  252. if (nStatus & BLOCK_HAVE_UNDO)
  253. READWRITE(VARINT(nUndoPos));
  254. // block header
  255. READWRITE(this->nVersion);
  256. READWRITE(hashPrev);
  257. READWRITE(hashMerkleRoot);
  258. READWRITE(nTime);
  259. READWRITE(nBits);
  260. READWRITE(nNonce);
  261. }
  262. uint256 GetBlockHash() const
  263. {
  264. CBlockHeader block;
  265. block.nVersion = nVersion;
  266. block.hashPrevBlock = hashPrev;
  267. block.hashMerkleRoot = hashMerkleRoot;
  268. block.nTime = nTime;
  269. block.nBits = nBits;
  270. block.nNonce = nNonce;
  271. return block.GetHash();
  272. }
  273. std::string ToString() const
  274. {
  275. std::string str = "CDiskBlockIndex(";
  276. str += CBlockIndex::ToString();
  277. str += strprintf("\n hashBlock=%s, hashPrev=%s)",
  278. GetBlockHash().ToString(),
  279. hashPrev.ToString());
  280. return str;
  281. }
  282. };
  283. /** An in-memory indexed chain of blocks. */
  284. class CChain {
  285. private:
  286. std::vector<CBlockIndex*> vChain;
  287. public:
  288. /** Returns the index entry for the genesis block of this chain, or NULL if none. */
  289. CBlockIndex *Genesis() const {
  290. return vChain.size() > 0 ? vChain[0] : NULL;
  291. }
  292. /** Returns the index entry for the tip of this chain, or NULL if none. */
  293. CBlockIndex *Tip() const {
  294. return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
  295. }
  296. /** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */
  297. CBlockIndex *operator[](int nHeight) const {
  298. if (nHeight < 0 || nHeight >= (int)vChain.size())
  299. return NULL;
  300. return vChain[nHeight];
  301. }
  302. /** Compare two chains efficiently. */
  303. friend bool operator==(const CChain &a, const CChain &b) {
  304. return a.vChain.size() == b.vChain.size() &&
  305. a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
  306. }
  307. /** Efficiently check whether a block is present in this chain. */
  308. bool Contains(const CBlockIndex *pindex) const {
  309. return (*this)[pindex->nHeight] == pindex;
  310. }
  311. /** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */
  312. CBlockIndex *Next(const CBlockIndex *pindex) const {
  313. if (Contains(pindex))
  314. return (*this)[pindex->nHeight + 1];
  315. else
  316. return NULL;
  317. }
  318. /** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
  319. int Height() const {
  320. return vChain.size() - 1;
  321. }
  322. /** Set/initialize a chain with a given tip. */
  323. void SetTip(CBlockIndex *pindex);
  324. /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
  325. CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
  326. /** Find the last common block between this chain and a block index entry. */
  327. const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
  328. };
  329. #endif // BITCOIN_CHAIN_H