Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2018 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 : 6 : #ifndef BITCOIN_PRIMITIVES_BLOCK_H 7 : #define BITCOIN_PRIMITIVES_BLOCK_H 8 : 9 : #include <primitives/transaction.h> 10 : #include <serialize.h> 11 : #include <uint256.h> 12 : 13 : /** Nodes collect new transactions into a block, hash them into a hash tree, 14 : * and scan through nonce values to make the block's hash satisfy proof-of-work 15 : * requirements. When they solve the proof-of-work, they broadcast the block 16 : * to everyone and the block is added to the block chain. The first transaction 17 : * in the block is a special one that creates a new coin owned by the creator 18 : * of the block. 19 : */ 20 : class CBlockHeader 21 : { 22 : public: 23 : // header 24 : int32_t nVersion; 25 : uint256 hashPrevBlock; 26 : uint256 hashMerkleRoot; 27 : uint32_t nTime; 28 : uint32_t nBits; 29 : uint32_t nNonce; 30 : 31 1202547 : CBlockHeader() 32 422435 : { 33 780112 : SetNull(); 34 1202547 : } 35 : 36 7422585 : SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); } 37 : 38 1246470 : void SetNull() 39 : { 40 1246470 : nVersion = 0; 41 1246470 : hashPrevBlock.SetNull(); 42 1246470 : hashMerkleRoot.SetNull(); 43 1246470 : nTime = 0; 44 1246470 : nBits = 0; 45 1246470 : nNonce = 0; 46 1246470 : } 47 : 48 130149 : bool IsNull() const 49 : { 50 130149 : return (nBits == 0); 51 : } 52 : 53 : uint256 GetHash() const; 54 : 55 351030 : int64_t GetBlockTime() const 56 : { 57 351030 : return (int64_t)nTime; 58 : } 59 : }; 60 : 61 : 62 2532149 : class CBlock : public CBlockHeader 63 : { 64 : public: 65 : // network and disk 66 : std::vector<CTransactionRef> vtx; 67 : 68 : // memory only 69 : mutable bool fChecked; 70 : 71 350916 : CBlock() 72 350916 : { 73 175458 : SetNull(); 74 350915 : } 75 : 76 364438 : CBlock(const CBlockHeader &header) 77 364438 : { 78 182219 : SetNull(); 79 182219 : *(static_cast<CBlockHeader*>(this)) = header; 80 364438 : } 81 : 82 2138352 : SERIALIZE_METHODS(CBlock, obj) 83 : { 84 712784 : READWRITEAS(CBlockHeader, obj); 85 712784 : READWRITE(obj.vtx); 86 712784 : } 87 : 88 456251 : void SetNull() 89 : { 90 456251 : CBlockHeader::SetNull(); 91 456251 : vtx.clear(); 92 456251 : fChecked = false; 93 456251 : } 94 : 95 107 : CBlockHeader GetBlockHeader() const 96 : { 97 107 : CBlockHeader block; 98 107 : block.nVersion = nVersion; 99 107 : block.hashPrevBlock = hashPrevBlock; 100 107 : block.hashMerkleRoot = hashMerkleRoot; 101 107 : block.nTime = nTime; 102 107 : block.nBits = nBits; 103 107 : block.nNonce = nNonce; 104 107 : return block; 105 : } 106 : 107 : std::string ToString() const; 108 : }; 109 : 110 : /** Describes a place in the block chain to another node such that if the 111 : * other node doesn't have the same branch, it can find a recent common trunk. 112 : * The further back it is, the further before the fork it may be. 113 : */ 114 40986 : struct CBlockLocator 115 : { 116 : std::vector<uint256> vHave; 117 : 118 6584 : CBlockLocator() {} 119 : 120 7402 : explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {} 121 : 122 20538 : SERIALIZE_METHODS(CBlockLocator, obj) 123 : { 124 6846 : int nVersion = s.GetVersion(); 125 6846 : if (!(s.GetType() & SER_GETHASH)) 126 6846 : READWRITE(nVersion); 127 6846 : READWRITE(obj.vHave); 128 6846 : } 129 : 130 24 : void SetNull() 131 : { 132 24 : vHave.clear(); 133 24 : } 134 : 135 3493 : bool IsNull() const 136 : { 137 3493 : return vHave.empty(); 138 : } 139 : }; 140 : 141 : #endif // BITCOIN_PRIMITIVES_BLOCK_H