Bitcoin
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
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);
46  }
47 
48  void SetNull()
49  {
50  nVersion = 0;
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 
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
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:95
void SetNull()
Definition: block.h:100
ADD_SERIALIZE_METHODS
Definition: block.h:134
void SetNull()
Definition: uint256.h:39
Definition: block.h:126
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:137
uint256 hashMerkleRoot
Definition: block.h:26
void SetNull()
Definition: block.h:48
int64_t GetBlockTime() const
Definition: block.h:65
bool IsNull() const
Definition: block.h:149
uint32_t nTime
Definition: block.h:27
#define READWRITEAS(type, obj)
Definition: serialize.h:185
bool fChecked
Definition: block.h:79
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:132
CBlockLocator()
Definition: block.h:130
uint256 hashPrevBlock
Definition: block.h:25
ADD_SERIALIZE_METHODS
Definition: block.h:36
Definition: serialize.h:177
unsigned int uint32_t
Definition: stdint.h:21
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:39
uint32_t nBits
Definition: block.h:28
std::string ToString() const
Definition: block.cpp:17
CBlock()
Definition: block.h:81
CBlock(const CBlockHeader &header)
Definition: block.h:86
Definition: uint256.h:121
Definition: block.h:20
uint32_t nNonce
Definition: block.h:29
void SetNull()
Definition: block.h:144
ADD_SERIALIZE_METHODS
Definition: block.h:92
int32_t nVersion
Definition: block.h:24
signed long long int64_t
Definition: stdint.h:18
bool IsNull() const
Definition: block.h:58
CBlockHeader GetBlockHeader() const
Definition: block.h:107
CBlockHeader()
Definition: block.h:31
std::vector< CTransactionRef > vtx
Definition: block.h:76
signed int int32_t
Definition: stdint.h:17
#define READWRITE(...)
Definition: serialize.h:184
Definition: block.h:72
uint256 GetHash() const
Definition: block.cpp:12
std::vector< uint256 > vHave
Definition: block.h:128