BSHA3  0.17.99
P2P Blockchain, based on 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
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);
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 
65  uint256 GetHash3() const;
66 
67  int64_t GetBlockTime() const
68  {
69  return (int64_t)nTime;
70  }
71 };
72 
73 
74 class CBlock : public CBlockHeader
75 {
76 public:
77  // network and disk
78  std::vector<CTransactionRef> vtx;
79 
80  // memory only
81  mutable bool fChecked;
82 
84  {
85  SetNull();
86  }
87 
88  CBlock(const CBlockHeader &header)
89  {
90  SetNull();
91  *(static_cast<CBlockHeader*>(this)) = header;
92  }
93 
95 
96  template <typename Stream, typename Operation>
97  inline void SerializationOp(Stream& s, Operation ser_action) {
98  READWRITEAS(CBlockHeader, *this);
99  READWRITE(vtx);
100  }
101 
102  void SetNull()
103  {
105  vtx.clear();
106  fChecked = false;
107  }
108 
110  {
111  CBlockHeader block;
112  block.nVersion = nVersion;
115  block.nTime = nTime;
116  block.nBits = nBits;
117  block.nNonce = nNonce;
118  return block;
119  }
120 
121  std::string ToString() const;
122 };
123 
129 {
130  std::vector<uint256> vHave;
131 
133 
134  explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
135 
137 
138  template <typename Stream, typename Operation>
139  inline void SerializationOp(Stream& s, Operation ser_action) {
140  int nVersion = s.GetVersion();
141  if (!(s.GetType() & SER_GETHASH))
142  READWRITE(nVersion);
143  READWRITE(vHave);
144  }
145 
146  void SetNull()
147  {
148  vHave.clear();
149  }
150 
151  bool IsNull() const
152  {
153  return vHave.empty();
154  }
155 };
156 
157 #endif // BITCOIN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:29
CBlockHeader()
Definition: block.h:31
CBlockLocator()
Definition: block.h:132
CBlockHeader GetBlockHeader() const
Definition: block.h:109
void SetNull()
Definition: uint256.h:40
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:128
Definition: block.h:74
CBlock(const CBlockHeader &header)
Definition: block.h:88
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:97
std::string ToString() const
Definition: block.cpp:18
ADD_SERIALIZE_METHODS
Definition: block.h:94
bool IsNull() const
Definition: block.h:151
#define READWRITEAS(type, obj)
Definition: serialize.h:174
CBlock()
Definition: block.h:83
uint32_t nTime
Definition: block.h:27
void SetNull()
Definition: block.h:146
uint256 hashMerkleRoot
Definition: block.h:26
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:139
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:130
int64_t GetBlockTime() const
Definition: block.h:67
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:134
uint256 GetHash3() const
uint256 GetHash() const
Definition: block.cpp:13
256-bit opaque blob.
Definition: uint256.h:122
std::vector< CTransactionRef > vtx
Definition: block.h:78
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:39
void SetNull()
Definition: block.h:102
bool IsNull() const
Definition: block.h:58
bool fChecked
Definition: block.h:81
#define READWRITE(...)
Definition: serialize.h:173
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:136