Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2020 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 : : CBlockHeader() # 32 : 404850 : { # 33 : 404850 : SetNull(); # 34 : 404850 : } # 35 : : # 36 : 2988235 : SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); } # 37 : : # 38 : : void SetNull() # 39 : 791806 : { # 40 : 791806 : nVersion = 0; # 41 : 791806 : hashPrevBlock.SetNull(); # 42 : 791806 : hashMerkleRoot.SetNull(); # 43 : 791806 : nTime = 0; # 44 : 791806 : nBits = 0; # 45 : 791806 : nNonce = 0; # 46 : 791806 : } # 47 : : # 48 : : bool IsNull() const # 49 : 160772 : { # 50 : 160772 : return (nBits == 0); # 51 : 160772 : } # 52 : : # 53 : : uint256 GetHash() const; # 54 : : # 55 : : int64_t GetBlockTime() const # 56 : 503176 : { # 57 : 503176 : return (int64_t)nTime; # 58 : 503176 : } # 59 : : }; # 60 : : # 61 : : # 62 : : 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 : : CBlock() # 72 : 227031 : { # 73 : 227031 : SetNull(); # 74 : 227031 : } # 75 : : # 76 : : CBlock(const CBlockHeader &header) # 77 : 35531 : { # 78 : 35531 : SetNull(); # 79 : 35531 : *(static_cast<CBlockHeader*>(this)) = header; # 80 : 35531 : } # 81 : : # 82 : : SERIALIZE_METHODS(CBlock, obj) # 83 : 773788 : { # 84 : 773788 : READWRITEAS(CBlockHeader, obj); # 85 : 773788 : READWRITE(obj.vtx); # 86 : 773788 : } # 87 : : # 88 : : void SetNull() # 89 : 370224 : { # 90 : 370224 : CBlockHeader::SetNull(); # 91 : 370224 : vtx.clear(); # 92 : 370224 : fChecked = false; # 93 : 370224 : } # 94 : : # 95 : : CBlockHeader GetBlockHeader() const # 96 : 170 : { # 97 : 170 : CBlockHeader block; # 98 : 170 : block.nVersion = nVersion; # 99 : 170 : block.hashPrevBlock = hashPrevBlock; # 100 : 170 : block.hashMerkleRoot = hashMerkleRoot; # 101 : 170 : block.nTime = nTime; # 102 : 170 : block.nBits = nBits; # 103 : 170 : block.nNonce = nNonce; # 104 : 170 : return block; # 105 : 170 : } # 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 : : struct CBlockLocator # 115 : : { # 116 : : std::vector<uint256> vHave; # 117 : : # 118 : 2968 : CBlockLocator() {} # 119 : : # 120 : 3687 : explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {} # 121 : : # 122 : : SERIALIZE_METHODS(CBlockLocator, obj) # 123 : 6387 : { # 124 : 6387 : int nVersion = s.GetVersion(); # 125 [ + - ][ + - ]: 6387 : if (!(s.GetType() & SER_GETHASH)) # [ + - ] # 126 : 6387 : READWRITE(nVersion); # 127 : 6387 : READWRITE(obj.vHave); # 128 : 6387 : } # 129 : : # 130 : : void SetNull() # 131 : 48 : { # 132 : 48 : vHave.clear(); # 133 : 48 : } # 134 : : # 135 : : bool IsNull() const # 136 : 3201 : { # 137 : 3201 : return vHave.empty(); # 138 : 3201 : } # 139 : : }; # 140 : : # 141 : : #endif // BITCOIN_PRIMITIVES_BLOCK_H