Bitcoin Core  24.1.0
P2P Digital Currency
block.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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
75  mutable bool fChecked;
76 
78  {
79  SetNull();
80  }
81 
82  CBlock(const CBlockHeader &header)
83  {
84  SetNull();
85  *(static_cast<CBlockHeader*>(this)) = header;
86  }
87 
89  {
91  READWRITE(obj.vtx);
92  }
93 
94  void SetNull()
95  {
97  vtx.clear();
98  fChecked = false;
99  }
100 
102  {
103  CBlockHeader block;
104  block.nVersion = nVersion;
107  block.nTime = nTime;
108  block.nBits = nBits;
109  block.nNonce = nNonce;
110  return block;
111  }
112 
113  std::string ToString() const;
114 };
115 
121 {
122  std::vector<uint256> vHave;
123 
125 
126  explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}
127 
129  {
130  int nVersion = s.GetVersion();
131  if (!(s.GetType() & SER_GETHASH))
132  READWRITE(nVersion);
133  READWRITE(obj.vHave);
134  }
135 
136  void SetNull()
137  {
138  vHave.clear();
139  }
140 
141  bool IsNull() const
142  {
143  return vHave.empty();
144  }
145 };
146 
147 #endif // BITCOIN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:30
CBlockHeader()
Definition: block.h:32
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:37
CBlockLocator()
Definition: block.h:124
CBlockHeader GetBlockHeader() const
Definition: block.h:101
void SetNull()
Definition: uint256.h:42
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:120
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:128
Definition: block.h:68
CBlock(const CBlockHeader &header)
Definition: block.h:82
std::string ToString() const
Definition: block.cpp:16
bool IsNull() const
Definition: block.h:141
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:88
#define READWRITEAS(type, obj)
Definition: serialize.h:141
CBlock()
Definition: block.h:77
uint32_t nTime
Definition: block.h:28
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
void SetNull()
Definition: block.h:136
uint256 hashMerkleRoot
Definition: block.h:27
uint256 hashPrevBlock
Definition: block.h:26
void SetNull()
Definition: block.h:39
std::vector< uint256 > vHave
Definition: block.h:122
int64_t GetBlockTime() const
Definition: block.h:61
uint256 GetHash() const
Definition: block.cpp:11
256-bit opaque blob.
Definition: uint256.h:119
std::vector< CTransactionRef > vtx
Definition: block.h:72
void SetNull()
Definition: block.h:94
bool IsNull() const
Definition: block.h:49
NodeSeconds Time() const
Definition: block.h:56
bool fChecked
Definition: block.h:75
#define READWRITE(...)
Definition: serialize.h:140
CBlockLocator(std::vector< uint256 > &&have)
Definition: block.h:126
int32_t nVersion
Definition: block.h:25
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
uint32_t nBits
Definition: block.h:29