/root/bitcoin/src/primitives/block.h
| Line | Count | Source | 
| 1 |  | // Copyright (c) 2009-2010 Satoshi Nakamoto | 
| 2 |  | // Copyright (c) 2009-2022 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 |  | #include <util/time.h> | 
| 13 |  |  | 
| 14 |  | /** Nodes collect new transactions into a block, hash them into a hash tree, | 
| 15 |  |  * and scan through nonce values to make the block's hash satisfy proof-of-work | 
| 16 |  |  * requirements.  When they solve the proof-of-work, they broadcast the block | 
| 17 |  |  * to everyone and the block is added to the block chain.  The first transaction | 
| 18 |  |  * in the block is a special one that creates a new coin owned by the creator | 
| 19 |  |  * of the block. | 
| 20 |  |  */ | 
| 21 |  | class CBlockHeader | 
| 22 |  | { | 
| 23 |  | public: | 
| 24 |  |     // header | 
| 25 |  |     int32_t nVersion; | 
| 26 |  |     uint256 hashPrevBlock; | 
| 27 |  |     uint256 hashMerkleRoot; | 
| 28 |  |     uint32_t nTime; | 
| 29 |  |     uint32_t nBits; | 
| 30 |  |     uint32_t nNonce; | 
| 31 |  |  | 
| 32 |  |     CBlockHeader() | 
| 33 | 868 |     { | 
| 34 | 868 |         SetNull(); | 
| 35 | 868 |     } | 
| 36 |  |  | 
| 37 | 6.94k |     SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }_ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR12SizeComputer20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_| Line | Count | Source |  | 37 | 5.20k |     SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); } | 
Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR10DataStream20TransactionSerParamsES_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI10DataStreamS_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI10DataStreamKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR10DataStream20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR12VectorWriter20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12SizeComputerKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12VectorWriterKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR10SpanReader20TransactionSerParamsES_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR14BufferedWriterI8AutoFileE20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12BufferedFileS_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI12ParamsStreamIR12BufferedFile20TransactionSerParamsES_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN12CBlockHeader16SerializationOpsI8AutoFileS_17ActionUnserializeEEvRT0_RT_T1__ZN12CBlockHeader16SerializationOpsI10HashWriterKS_15ActionSerializeEEvRT0_RT_T1_| Line | Count | Source |  | 37 | 1.73k |     SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); } | 
 | 
| 38 |  |  | 
| 39 |  |     void SetNull() | 
| 40 | 1.73k |     { | 
| 41 | 1.73k |         nVersion = 0; | 
| 42 | 1.73k |         hashPrevBlock.SetNull(); | 
| 43 | 1.73k |         hashMerkleRoot.SetNull(); | 
| 44 | 1.73k |         nTime = 0; | 
| 45 | 1.73k |         nBits = 0; | 
| 46 | 1.73k |         nNonce = 0; | 
| 47 | 1.73k |     } | 
| 48 |  |  | 
| 49 |  |     bool IsNull() const | 
| 50 | 0 |     { | 
| 51 | 0 |         return (nBits == 0); | 
| 52 | 0 |     } | 
| 53 |  |  | 
| 54 |  |     uint256 GetHash() const; | 
| 55 |  |  | 
| 56 |  |     NodeSeconds Time() const | 
| 57 | 868 |     { | 
| 58 | 868 |         return NodeSeconds{std::chrono::seconds{nTime}}; | 
| 59 | 868 |     } | 
| 60 |  |  | 
| 61 |  |     int64_t GetBlockTime() const | 
| 62 | 3.47k |     { | 
| 63 | 3.47k |         return (int64_t)nTime; | 
| 64 | 3.47k |     } | 
| 65 |  | }; | 
| 66 |  |  | 
| 67 |  |  | 
| 68 |  | class CBlock : public CBlockHeader | 
| 69 |  | { | 
| 70 |  | public: | 
| 71 |  |     // network and disk | 
| 72 |  |     std::vector<CTransactionRef> vtx; | 
| 73 |  |  | 
| 74 |  |     // Memory-only flags for caching expensive checks | 
| 75 |  |     mutable bool fChecked;                            // CheckBlock() | 
| 76 |  |     mutable bool m_checked_witness_commitment{false}; // CheckWitnessCommitment() | 
| 77 |  |     mutable bool m_checked_merkle_root{false};        // CheckMerkleRoot() | 
| 78 |  |  | 
| 79 |  |     CBlock() | 
| 80 | 868 |     { | 
| 81 | 868 |         SetNull(); | 
| 82 | 868 |     } | 
| 83 |  |  | 
| 84 |  |     CBlock(const CBlockHeader &header) | 
| 85 | 0 |     { | 
| 86 | 0 |         SetNull(); | 
| 87 | 0 |         *(static_cast<CBlockHeader*>(this)) = header; | 
| 88 | 0 |     } | 
| 89 |  |  | 
| 90 |  |     SERIALIZE_METHODS(CBlock, obj) | 
| 91 | 5.20k |     { | 
| 92 | 5.20k |         READWRITE(AsBase<CBlockHeader>(obj), obj.vtx); | 
| 93 | 5.20k |     } _ZN6CBlock16SerializationOpsI12ParamsStreamIR12SizeComputer20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_| Line | Count | Source |  | 91 | 5.20k |     { |  | 92 | 5.20k |         READWRITE(AsBase<CBlockHeader>(obj), obj.vtx); |  | 93 | 5.20k |     } | 
Unexecuted instantiation: _ZN6CBlock16SerializationOpsI12ParamsStreamIR10DataStream20TransactionSerParamsES_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN6CBlock16SerializationOpsI12ParamsStreamIR10DataStream20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN6CBlock16SerializationOpsI12ParamsStreamIR12VectorWriter20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN6CBlock16SerializationOpsI12ParamsStreamIR10SpanReader20TransactionSerParamsES_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN6CBlock16SerializationOpsI12ParamsStreamIR14BufferedWriterI8AutoFileE20TransactionSerParamsEKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN6CBlock16SerializationOpsI12ParamsStreamIR12BufferedFile20TransactionSerParamsES_17ActionUnserializeEEvRT0_RT_T1_ | 
| 94 |  |  | 
| 95 |  |     void SetNull() | 
| 96 | 868 |     { | 
| 97 | 868 |         CBlockHeader::SetNull(); | 
| 98 | 868 |         vtx.clear(); | 
| 99 | 868 |         fChecked = false; | 
| 100 | 868 |         m_checked_witness_commitment = false; | 
| 101 | 868 |         m_checked_merkle_root = false; | 
| 102 | 868 |     } | 
| 103 |  |  | 
| 104 |  |     CBlockHeader GetBlockHeader() const | 
| 105 | 0 |     { | 
| 106 | 0 |         CBlockHeader block; | 
| 107 | 0 |         block.nVersion       = nVersion; | 
| 108 | 0 |         block.hashPrevBlock  = hashPrevBlock; | 
| 109 | 0 |         block.hashMerkleRoot = hashMerkleRoot; | 
| 110 | 0 |         block.nTime          = nTime; | 
| 111 | 0 |         block.nBits          = nBits; | 
| 112 | 0 |         block.nNonce         = nNonce; | 
| 113 | 0 |         return block; | 
| 114 | 0 |     } | 
| 115 |  |  | 
| 116 |  |     std::string ToString() const; | 
| 117 |  | }; | 
| 118 |  |  | 
| 119 |  | /** Describes a place in the block chain to another node such that if the | 
| 120 |  |  * other node doesn't have the same branch, it can find a recent common trunk. | 
| 121 |  |  * The further back it is, the further before the fork it may be. | 
| 122 |  |  */ | 
| 123 |  | struct CBlockLocator | 
| 124 |  | { | 
| 125 |  |     /** Historically CBlockLocator's version field has been written to network | 
| 126 |  |      * streams as the negotiated protocol version and to disk streams as the | 
| 127 |  |      * client version, but the value has never been used. | 
| 128 |  |      * | 
| 129 |  |      * Hard-code to the highest protocol version ever written to a network stream. | 
| 130 |  |      * SerParams can be used if the field requires any meaning in the future, | 
| 131 |  |      **/ | 
| 132 |  |     static constexpr int DUMMY_VERSION = 70016; | 
| 133 |  |  | 
| 134 |  |     std::vector<uint256> vHave; | 
| 135 |  |  | 
| 136 | 0 |     CBlockLocator() = default; | 
| 137 |  |  | 
| 138 | 13 |     explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {} | 
| 139 |  |  | 
| 140 |  |     SERIALIZE_METHODS(CBlockLocator, obj) | 
| 141 | 0 |     { | 
| 142 | 0 |         int nVersion = DUMMY_VERSION; | 
| 143 | 0 |         READWRITE(nVersion); | 
| 144 | 0 |         READWRITE(obj.vHave); | 
| 145 | 0 |     } Unexecuted instantiation: _ZN13CBlockLocator16SerializationOpsI10DataStreamS_17ActionUnserializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN13CBlockLocator16SerializationOpsI10DataStreamKS_15ActionSerializeEEvRT0_RT_T1_Unexecuted instantiation: _ZN13CBlockLocator16SerializationOpsI12VectorWriterKS_15ActionSerializeEEvRT0_RT_T1_ | 
| 146 |  |  | 
| 147 |  |     void SetNull() | 
| 148 | 0 |     { | 
| 149 | 0 |         vHave.clear(); | 
| 150 | 0 |     } | 
| 151 |  |  | 
| 152 |  |     bool IsNull() const | 
| 153 | 0 |     { | 
| 154 | 0 |         return vHave.empty(); | 
| 155 | 0 |     } | 
| 156 |  | }; | 
| 157 |  |  | 
| 158 |  | #endif // BITCOIN_PRIMITIVES_BLOCK_H |