Bitcoin Core  0.19.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 
37 
38  template <typename Stream, typename Operation>
39  inline void SerializationOp(Stream& s, Operation ser_action) {
40  READWRITE(this->nVersion);
41  READWRITE(hashPrevBlock);
42  READWRITE(hashMerkleRoot);
43  READWRITE(nTime);
44  READWRITE(nBits);
45  READWRITE(nNonce);
46  }
47 
48  void SetNull()
49  {
50  nVersion = 0;
51  hashPrevBlock.SetNull();
52  hashMerkleRoot.SetNull();
53  nTime = 0;
54  nBits = 0;
55  nNonce = 0;
56  }
57 
58  bool IsNull() const
59  {
60  return (nBits == 0);
61  }
62 
63  uint256 GetHash() const;
64 
65  int64_t GetBlockTime() const
66  {
67  return (int64_t)nTime;
68  }
69 };
70 
71 
72 class CBlock : public CBlockHeader
73 {
74 public:
75  // network and disk
76  std::vector<CTransactionRef> vtx;
77 
78  // memory only
79  mutable bool fChecked;
80 
82  {
83  SetNull();
84  }
85 
86  CBlock(const CBlockHeader &header)
87  {
88  SetNull();
89  *(static_cast<CBlockHeader*>(this)) = header;
90  }
91 
93 
94  template <typename Stream, typename Operation>
95  inline void SerializationOp(Stream& s, Operation ser_action) {
96  READWRITEAS(CBlockHeader, *this);
97  READWRITE(vtx);
98  }
99 
100  void SetNull()
101  {
103  vtx.clear();
104  fChecked = false;
105  }
106 
108  {
109  CBlockHeader block;
110  block.nVersion = nVersion;
113  block.nTime = nTime;
114  block.nBits = nBits;
115  block.nNonce = nNonce;
116  return block;
117  }
118 
119  std::string ToString() const;
120 };
121 
127 {
128  std::vector<uint256> vHave;
129 
131 
132  explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
133 
135 
136  template <typename Stream, typename Operation>
137  inline void SerializationOp(Stream& s, Operation ser_action) {
138  int nVersion = s.GetVersion();
139  if (!(s.GetType() & SER_GETHASH))
140  READWRITE(nVersion);
141  READWRITE(vHave);
142  }
143 
144  void SetNull()
145  {
146  vHave.clear();
147  }
148 
149  bool IsNull() const
150  {
151  return vHave.empty();
152  }
153 };
154 
155 #endif // BITCOIN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:29
CBlockHeader()
Definition: block.h:31
CBlockLocator()
Definition: block.h:130
CBlockHeader GetBlockHeader() const
Definition: block.h:107
void SetNull()
Definition: uint256.h:38
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:126
Definition: block.h:72
CBlock(const CBlockHeader &header)
Definition: block.h:86
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:95
ADD_SERIALIZE_METHODS
Definition: block.h:92
bool IsNull() const
Definition: block.h:149
#define READWRITEAS(type, obj)
Definition: serialize.h:184
CBlock()
Definition: block.h:81
uint32_t nTime
Definition: block.h:27
void SetNull()
Definition: block.h:144
uint256 hashMerkleRoot
Definition: block.h:26
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:137
uint256 hashPrevBlock
Definition: block.h:25
ADD_SERIALIZE_METHODS
Definition: block.h:36
void SetNull()
Definition: block.h:48
std::vector< uint256 > vHave
Definition: block.h:128
int64_t GetBlockTime() const
Definition: block.h:65
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:132
uint256 GetHash() const
Definition: block.cpp:11
256-bit opaque blob.
Definition: uint256.h:120
std::vector< CTransactionRef > vtx
Definition: block.h:76
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:39
void SetNull()
Definition: block.h:100
bool IsNull() const
Definition: block.h:58
bool fChecked
Definition: block.h:79
#define READWRITE(...)
Definition: serialize.h:183
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
ADD_SERIALIZE_METHODS
Definition: block.h:134