Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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 : 92795 : CBlockHeader()
33 : 92795 : {
34 : 92795 : SetNull();
35 : 92795 : }
36 : :
37 [ # # # # ]: 771708 : SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
38 : :
39 : 129504 : void SetNull()
40 : : {
41 : 129504 : nVersion = 0;
42 : 129504 : hashPrevBlock.SetNull();
43 : 129504 : hashMerkleRoot.SetNull();
44 : 129504 : nTime = 0;
45 : 129504 : nBits = 0;
46 : 129504 : nNonce = 0;
47 : 129504 : }
48 : :
49 : 457 : bool IsNull() const
50 : : {
51 [ + + ][ + - : 457 : return (nBits == 0);
+ - + - +
- ]
52 : : }
53 : :
54 : : uint256 GetHash() const;
55 : :
56 : 15668 : NodeSeconds Time() const
57 : : {
58 : 15668 : return NodeSeconds{std::chrono::seconds{nTime}};
59 : : }
60 : :
61 : 54468 : int64_t GetBlockTime() const
62 : : {
63 [ + + ]: 54468 : return (int64_t)nTime;
64 : : }
65 : : };
66 : :
67 : :
68 [ + + ]: 34223 : 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 : 31696 : CBlock()
80 : 31696 : {
81 : 31696 : SetNull();
82 : 31696 : }
83 : :
84 : 8 : CBlock(const CBlockHeader &header)
85 : 8 : {
86 : 8 : SetNull();
87 : 8 : *(static_cast<CBlockHeader*>(this)) = header;
88 : 8 : }
89 : :
90 [ # # # # ]: 107466 : SERIALIZE_METHODS(CBlock, obj)
91 : : {
92 : 92201 : READWRITE(AsBase<CBlockHeader>(obj), obj.vtx);
93 : 92199 : }
94 : :
95 : 36699 : void SetNull()
96 : : {
97 : 36699 : CBlockHeader::SetNull();
98 : 36699 : vtx.clear();
99 : 36699 : fChecked = false;
100 : 36699 : m_checked_witness_commitment = false;
101 : 36699 : m_checked_merkle_root = false;
102 : 36699 : }
103 : :
104 : : std::string ToString() const;
105 : : };
106 : :
107 : : /** Describes a place in the block chain to another node such that if the
108 : : * other node doesn't have the same branch, it can find a recent common trunk.
109 : : * The further back it is, the further before the fork it may be.
110 : : */
111 [ + - # # : 991 : struct CBlockLocator
# # # # ]
[ + - + - ]
[ + - - -
- - - - ]
112 : : {
113 : : /** Historically CBlockLocator's version field has been written to network
114 : : * streams as the negotiated protocol version and to disk streams as the
115 : : * client version, but the value has never been used.
116 : : *
117 : : * Hard-code to the highest protocol version ever written to a network stream.
118 : : * SerParams can be used if the field requires any meaning in the future,
119 : : **/
120 : : static constexpr int DUMMY_VERSION = 70016;
121 : :
122 : : std::vector<uint256> vHave;
123 : :
124 : : CBlockLocator() = default;
125 : :
126 : 214 : explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}
127 : :
128 : 148 : SERIALIZE_METHODS(CBlockLocator, obj)
129 : : {
130 : 74 : int nVersion = DUMMY_VERSION;
131 : 74 : READWRITE(nVersion);
132 : 74 : READWRITE(obj.vHave);
133 : 74 : }
134 : :
135 : 5 : void SetNull()
136 : : {
137 [ - + ]: 6 : vHave.clear();
138 : : }
139 : :
140 : 595 : bool IsNull() const
141 : : {
142 [ + + + + ]: 595 : return vHave.empty();
[ # # # # ]
143 : : }
144 : : };
145 : :
146 : : #endif // BITCOIN_PRIMITIVES_BLOCK_H
|