Bitcoin Core  0.20.99
P2P Digital Currency
block.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
21 {
22 public:
23  // header
24  int32_t nVersion;
27  uint32_t nTime;
28  uint32_t nBits;
29  uint32_t nNonce;
30 
32  {
33  SetNull();
34  }
35 
36  SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
37 
38  void SetNull()
39  {
40  nVersion = 0;
41  hashPrevBlock.SetNull();
42  hashMerkleRoot.SetNull();
43  nTime = 0;
44  nBits = 0;
45  nNonce = 0;
46  }
47 
48  bool IsNull() const
49  {
50  return (nBits == 0);
51  }
52 
53  uint256 GetHash() const;
54 
55  int64_t GetBlockTime() const
56  {
57  return (int64_t)nTime;
58  }
59 };
60 
61 
62 class CBlock : public CBlockHeader
63 {
64 public:
65  // network and disk
66  std::vector<CTransactionRef> vtx;
67 
68  // memory only
69  mutable bool fChecked;
70 
72  {
73  SetNull();
74  }
75 
76  CBlock(const CBlockHeader &header)
77  {
78  SetNull();
79  *(static_cast<CBlockHeader*>(this)) = header;
80  }
81 
83  {
85  READWRITE(obj.vtx);
86  }
87 
88  void SetNull()
89  {
91  vtx.clear();
92  fChecked = false;
93  }
94 
96  {
97  CBlockHeader block;
98  block.nVersion = nVersion;
101  block.nTime = nTime;
102  block.nBits = nBits;
103  block.nNonce = nNonce;
104  return block;
105  }
106 
107  std::string ToString() const;
108 };
109 
115 {
116  std::vector<uint256> vHave;
117 
119 
120  explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
121 
123  {
124  int nVersion = s.GetVersion();
125  if (!(s.GetType() & SER_GETHASH))
126  READWRITE(nVersion);
127  READWRITE(obj.vHave);
128  }
129 
130  void SetNull()
131  {
132  vHave.clear();
133  }
134 
135  bool IsNull() const
136  {
137  return vHave.empty();
138  }
139 };
140 
141 #endif // BITCOIN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:29
CBlockHeader()
Definition: block.h:31
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:36
CBlockLocator()
Definition: block.h:118
CBlockHeader GetBlockHeader() const
Definition: block.h:95
void SetNull()
Definition: uint256.h:39
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:114
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:122
Definition: block.h:62
CBlock(const CBlockHeader &header)
Definition: block.h:76
bool IsNull() const
Definition: block.h:135
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:82
#define READWRITEAS(type, obj)
Definition: serialize.h:176
CBlock()
Definition: block.h:71
uint32_t nTime
Definition: block.h:27
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:71
void SetNull()
Definition: block.h:130
uint256 hashMerkleRoot
Definition: block.h:26
uint256 hashPrevBlock
Definition: block.h:25
void SetNull()
Definition: block.h:38
std::vector< uint256 > vHave
Definition: block.h:116
int64_t GetBlockTime() const
Definition: block.h:55
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:120
uint256 GetHash() const
Definition: block.cpp:11
256-bit opaque blob.
Definition: uint256.h:124
std::vector< CTransactionRef > vtx
Definition: block.h:66
void SetNull()
Definition: block.h:88
bool IsNull() const
Definition: block.h:48
bool fChecked
Definition: block.h:69
#define READWRITE(...)
Definition: serialize.h:175
int32_t nVersion
Definition: block.h:24
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
uint32_t nBits
Definition: block.h:28