Bitcoin Core  26.99.0
P2P Digital Currency
block.h
Go to the documentation of this file.
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 
10 #include <serialize.h>
11 #include <uint256.h>
12 #include <util/time.h>
13 
22 {
23 public:
24  // header
25  int32_t nVersion;
28  uint32_t nTime;
29  uint32_t nBits;
30  uint32_t nNonce;
31 
33  {
34  SetNull();
35  }
36 
37  SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
38 
39  void SetNull()
40  {
41  nVersion = 0;
44  nTime = 0;
45  nBits = 0;
46  nNonce = 0;
47  }
48 
49  bool IsNull() const
50  {
51  return (nBits == 0);
52  }
53 
54  uint256 GetHash() const;
55 
56  NodeSeconds Time() const
57  {
58  return NodeSeconds{std::chrono::seconds{nTime}};
59  }
60 
61  int64_t GetBlockTime() const
62  {
63  return (int64_t)nTime;
64  }
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 
80  {
81  SetNull();
82  }
83 
84  CBlock(const CBlockHeader &header)
85  {
86  SetNull();
87  *(static_cast<CBlockHeader*>(this)) = header;
88  }
89 
91  {
92  READWRITE(AsBase<CBlockHeader>(obj), obj.vtx);
93  }
94 
95  void SetNull()
96  {
98  vtx.clear();
99  fChecked = false;
101  m_checked_merkle_root = false;
102  }
103 
105  {
106  CBlockHeader block;
107  block.nVersion = nVersion;
110  block.nTime = nTime;
111  block.nBits = nBits;
112  block.nNonce = nNonce;
113  return block;
114  }
115 
116  std::string ToString() const;
117 };
118 
124 {
132  static constexpr int DUMMY_VERSION = 70016;
133 
134  std::vector<uint256> vHave;
135 
137 
138  explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}
139 
141  {
142  int nVersion = DUMMY_VERSION;
143  READWRITE(nVersion);
144  READWRITE(obj.vHave);
145  }
146 
147  void SetNull()
148  {
149  vHave.clear();
150  }
151 
152  bool IsNull() const
153  {
154  return vHave.empty();
155  }
156 };
157 
158 #endif // BITCOIN_PRIMITIVES_BLOCK_H
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
NodeSeconds Time() const
Definition: block.h:56
uint32_t nNonce
Definition: block.h:30
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:37
uint32_t nBits
Definition: block.h:29
CBlockHeader()
Definition: block.h:32
uint32_t nTime
Definition: block.h:28
int64_t GetBlockTime() const
Definition: block.h:61
int32_t nVersion
Definition: block.h:25
uint256 hashPrevBlock
Definition: block.h:26
void SetNull()
Definition: block.h:39
uint256 hashMerkleRoot
Definition: block.h:27
uint256 GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:49
Definition: block.h:69
void SetNull()
Definition: block.h:95
std::string ToString() const
Definition: block.cpp:16
bool m_checked_merkle_root
Definition: block.h:77
std::vector< CTransactionRef > vtx
Definition: block.h:72
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:90
bool m_checked_witness_commitment
Definition: block.h:76
CBlockHeader GetBlockHeader() const
Definition: block.h:104
CBlock()
Definition: block.h:79
CBlock(const CBlockHeader &header)
Definition: block.h:84
bool fChecked
Definition: block.h:75
constexpr void SetNull()
Definition: uint256.h:49
256-bit opaque blob.
Definition: uint256.h:106
#define READWRITE(...)
Definition: serialize.h:156
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:124
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:140
std::vector< uint256 > vHave
Definition: block.h:134
bool IsNull() const
Definition: block.h:152
CBlockLocator(std::vector< uint256 > &&have)
Definition: block.h:138
static constexpr int DUMMY_VERSION
Historically CBlockLocator's version field has been written to network streams as the negotiated prot...
Definition: block.h:132
CBlockLocator()
Definition: block.h:136
void SetNull()
Definition: block.h:147
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:23