Bitcoin ABC  0.22.12
P2P Digital Currency
block.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 
9 #include <primitives/blockhash.h>
10 #include <primitives/transaction.h>
11 #include <serialize.h>
12 #include <uint256.h>
13 
22 class CBlockHeader {
23 public:
24  // header
25  int32_t nVersion;
28  uint32_t nTime;
29  uint32_t nBits;
30  uint32_t nNonce;
31 
33 
35 
36  template <typename Stream, typename Operation>
37  inline void SerializationOp(Stream &s, Operation ser_action) {
38  READWRITE(this->nVersion);
39  READWRITE(hashPrevBlock);
40  READWRITE(hashMerkleRoot);
41  READWRITE(nTime);
42  READWRITE(nBits);
43  READWRITE(nNonce);
44  }
45 
46  void SetNull() {
47  nVersion = 0;
48  hashPrevBlock = BlockHash();
49  hashMerkleRoot.SetNull();
50  nTime = 0;
51  nBits = 0;
52  nNonce = 0;
53  }
54 
55  bool IsNull() const { return (nBits == 0); }
56 
57  BlockHash GetHash() const;
58 
59  int64_t GetBlockTime() const { return (int64_t)nTime; }
60 };
61 
62 class CBlock : public CBlockHeader {
63 public:
64  // network and disk
65  std::vector<CTransactionRef> vtx;
66 
67  // memory only
68  mutable bool fChecked;
69 
70  CBlock() { SetNull(); }
71 
72  CBlock(const CBlockHeader &header) {
73  SetNull();
74  *(static_cast<CBlockHeader *>(this)) = header;
75  }
76 
78 
79  template <typename Stream, typename Operation>
80  inline void SerializationOp(Stream &s, Operation ser_action) {
81  READWRITEAS(CBlockHeader, *this);
82  READWRITE(vtx);
83  }
84 
85  void SetNull() {
87  vtx.clear();
88  fChecked = false;
89  }
90 
92  CBlockHeader block;
93  block.nVersion = nVersion;
96  block.nTime = nTime;
97  block.nBits = nBits;
98  block.nNonce = nNonce;
99  return block;
100  }
101 
102  std::string ToString() const;
103 };
104 
111  std::vector<BlockHash> vHave;
112 
114 
115  explicit CBlockLocator(const std::vector<BlockHash> &vHaveIn)
116  : vHave(vHaveIn) {}
117 
119 
120  template <typename Stream, typename Operation>
121  inline void SerializationOp(Stream &s, Operation ser_action) {
122  int nVersion = s.GetVersion();
123  if (!(s.GetType() & SER_GETHASH)) {
124  READWRITE(nVersion);
125  }
126  READWRITE(vHave);
127  }
128 
129  void SetNull() { vHave.clear(); }
130 
131  bool IsNull() const { return vHave.empty(); }
132 };
133 
134 #endif // BITCOIN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:30
CBlockHeader()
Definition: block.h:32
CBlockLocator()
Definition: block.h:113
CBlockHeader GetBlockHeader() const
Definition: block.h:91
void SetNull()
Definition: uint256.h:35
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:110
Definition: block.h:62
CBlock(const CBlockHeader &header)
Definition: block.h:72
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:80
ADD_SERIALIZE_METHODS
Definition: block.h:77
std::vector< BlockHash > vHave
Definition: block.h:111
bool IsNull() const
Definition: block.h:131
#define READWRITEAS(type, obj)
Definition: serialize.h:192
CBlock()
Definition: block.h:70
uint32_t nTime
Definition: block.h:28
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:67
void SetNull()
Definition: block.h:129
uint256 hashMerkleRoot
Definition: block.h:27
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:121
ADD_SERIALIZE_METHODS
Definition: block.h:34
void SetNull()
Definition: block.h:46
int64_t GetBlockTime() const
Definition: block.h:59
256-bit opaque blob.
Definition: uint256.h:120
std::vector< CTransactionRef > vtx
Definition: block.h:65
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:37
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
void SetNull()
Definition: block.h:85
BlockHash GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:55
bool fChecked
Definition: block.h:68
CBlockLocator(const std::vector< BlockHash > &vHaveIn)
Definition: block.h:115
#define READWRITE(...)
Definition: serialize.h:191
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:22
BlockHash hashPrevBlock
Definition: block.h:26
uint32_t nBits
Definition: block.h:29
ADD_SERIALIZE_METHODS
Definition: block.h:118