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