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 : 441721 : { # 33 : 441721 : SetNull(); # 34 : 441721 : } # 35 : : # 36 : 13998309 : SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); } # 37 : : # 38 : : void SetNull() # 39 : 860527 : { # 40 : 860527 : nVersion = 0; # 41 : 860527 : hashPrevBlock.SetNull(); # 42 : 860527 : hashMerkleRoot.SetNull(); # 43 : 860527 : nTime = 0; # 44 : 860527 : nBits = 0; # 45 : 860527 : nNonce = 0; # 46 : 860527 : } # 47 : : # 48 : : bool IsNull() const # 49 : 172614 : { # 50 : 172614 : return (nBits == 0); # 51 : 172614 : } # 52 : : # 53 : : uint256 GetHash() const; # 54 : : # 55 : : int64_t GetBlockTime() const # 56 : 408314 : { # 57 : 408314 : return (int64_t)nTime; # 58 : 408314 : } # 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 : 237668 : { # 73 : 237668 : SetNull(); # 74 : 237668 : } # 75 : : # 76 : : CBlock(const CBlockHeader &header) # 77 : 39666 : { # 78 : 39666 : SetNull(); # 79 : 39666 : *(static_cast<CBlockHeader*>(this)) = header; # 80 : 39666 : } # 81 : : # 82 : : SERIALIZE_METHODS(CBlock, obj) # 83 : 750698 : { # 84 : 750698 : READWRITEAS(CBlockHeader, obj); # 85 : 750698 : READWRITE(obj.vtx); # 86 : 750698 : } # 87 : : # 88 : : void SetNull() # 89 : 404280 : { # 90 : 404280 : CBlockHeader::SetNull(); # 91 : 404280 : vtx.clear(); # 92 : 404280 : fChecked = false; # 93 : 404280 : } # 94 : : # 95 : : CBlockHeader GetBlockHeader() const # 96 : 981 : { # 97 : 981 : CBlockHeader block; # 98 : 981 : block.nVersion = nVersion; # 99 : 981 : block.hashPrevBlock = hashPrevBlock; # 100 : 981 : block.hashMerkleRoot = hashMerkleRoot; # 101 : 981 : block.nTime = nTime; # 102 : 981 : block.nBits = nBits; # 103 : 981 : block.nNonce = nNonce; # 104 : 981 : return block; # 105 : 981 : } # 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 : 3200 : CBlockLocator() {} # 119 : : # 120 : 4070 : explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {} # 121 : : # 122 : : SERIALIZE_METHODS(CBlockLocator, obj) # 123 : 6841 : { # 124 : 6841 : int nVersion = s.GetVersion(); # 125 [ + - ][ + - ]: 6841 : if (!(s.GetType() & SER_GETHASH)) # [ + - ] # 126 : 6841 : READWRITE(nVersion); # 127 : 6841 : READWRITE(obj.vHave); # 128 : 6841 : } # 129 : : # 130 : : void SetNull() # 131 : 58 : { # 132 : 58 : vHave.clear(); # 133 : 58 : } # 134 : : # 135 : : bool IsNull() const # 136 : 3470 : { # 137 : 3470 : return vHave.empty(); # 138 : 3470 : } # 139 : : }; # 140 : : # 141 : : #endif // BITCOIN_PRIMITIVES_BLOCK_H