Branch data 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 : : CBlockHeader() # 32 : 289129 : { # 33 : 289129 : SetNull(); # 34 : 289129 : } # 35 : : # 36 : 1779477 : SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); } # 37 : : # 38 : : void SetNull() # 39 : 580156 : { # 40 : 580156 : nVersion = 0; # 41 : 580156 : hashPrevBlock.SetNull(); # 42 : 580156 : hashMerkleRoot.SetNull(); # 43 : 580156 : nTime = 0; # 44 : 580156 : nBits = 0; # 45 : 580156 : nNonce = 0; # 46 : 580156 : } # 47 : : # 48 : : bool IsNull() const # 49 : 115364 : { # 50 : 115364 : return (nBits == 0); # 51 : 115364 : } # 52 : : # 53 : : uint256 GetHash() const; # 54 : : # 55 : : int64_t GetBlockTime() const # 56 : 345096 : { # 57 : 345096 : return (int64_t)nTime; # 58 : 345096 : } # 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 : 169262 : { # 73 : 169262 : SetNull(); # 74 : 169262 : } # 75 : : # 76 : : CBlock(const CBlockHeader &header) # 77 : 19698 : { # 78 : 19698 : SetNull(); # 79 : 19698 : *(static_cast<CBlockHeader*>(this)) = header; # 80 : 19698 : } # 81 : : # 82 : : SERIALIZE_METHODS(CBlock, obj) # 83 : 542630 : { # 84 : 542630 : READWRITE(AsBase<CBlockHeader>(obj), obj.vtx); # 85 : 542630 : } # 86 : : # 87 : : void SetNull() # 88 : 284345 : { # 89 : 284345 : CBlockHeader::SetNull(); # 90 : 284345 : vtx.clear(); # 91 : 284345 : fChecked = false; # 92 : 284345 : } # 93 : : # 94 : : CBlockHeader GetBlockHeader() const # 95 : 142 : { # 96 : 142 : CBlockHeader block; # 97 : 142 : block.nVersion = nVersion; # 98 : 142 : block.hashPrevBlock = hashPrevBlock; # 99 : 142 : block.hashMerkleRoot = hashMerkleRoot; # 100 : 142 : block.nTime = nTime; # 101 : 142 : block.nBits = nBits; # 102 : 142 : block.nNonce = nNonce; # 103 : 142 : return block; # 104 : 142 : } # 105 : : # 106 : : std::string ToString() const; # 107 : : }; # 108 : : # 109 : : /** Describes a place in the block chain to another node such that if the # 110 : : * other node doesn't have the same branch, it can find a recent common trunk. # 111 : : * The further back it is, the further before the fork it may be. # 112 : : */ # 113 : : struct CBlockLocator # 114 : : { # 115 : : std::vector<uint256> vHave; # 116 : : # 117 : 2565 : CBlockLocator() {} # 118 : : # 119 : 3114 : explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {} # 120 : : # 121 : : SERIALIZE_METHODS(CBlockLocator, obj) # 122 : 5392 : { # 123 : 5392 : int nVersion = s.GetVersion(); # 124 : 5392 : if (!(s.GetType() & SER_GETHASH)) # 125 : 5392 : READWRITE(nVersion); # 126 : 5392 : READWRITE(obj.vHave); # 127 : 5392 : } # 128 : : # 129 : : void SetNull() # 130 : 24 : { # 131 : 24 : vHave.clear(); # 132 : 24 : } # 133 : : # 134 : : bool IsNull() const # 135 : 2801 : { # 136 : 2801 : return vHave.empty(); # 137 : 2801 : } # 138 : : }; # 139 : : # 140 : : #endif // BITCOIN_PRIMITIVES_BLOCK_H