Bitcoin ABC  0.23.2
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  READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot,
36  obj.nTime, obj.nBits, obj.nNonce);
37  }
38 
39  void SetNull() {
40  nVersion = 0;
41  hashPrevBlock = BlockHash();
42  hashMerkleRoot.SetNull();
43  nTime = 0;
44  nBits = 0;
45  nNonce = 0;
46  }
47 
48  bool IsNull() const { return (nBits == 0); }
49 
50  BlockHash GetHash() const;
51 
52  int64_t GetBlockTime() const { return (int64_t)nTime; }
53 };
54 
55 class CBlock : public CBlockHeader {
56 public:
57  // network and disk
58  std::vector<CTransactionRef> vtx;
59 
60  // memory only
61  mutable bool fChecked;
62 
63  CBlock() { SetNull(); }
64 
65  CBlock(const CBlockHeader &header) {
66  SetNull();
67  *(static_cast<CBlockHeader *>(this)) = header;
68  }
69 
72  READWRITE(obj.vtx);
73  }
74 
75  void SetNull() {
77  vtx.clear();
78  fChecked = false;
79  }
80 
82  CBlockHeader block;
83  block.nVersion = nVersion;
86  block.nTime = nTime;
87  block.nBits = nBits;
88  block.nNonce = nNonce;
89  return block;
90  }
91 
92  std::string ToString() const;
93 };
94 
101  std::vector<BlockHash> vHave;
102 
104 
105  explicit CBlockLocator(const std::vector<BlockHash> &vHaveIn)
106  : vHave(vHaveIn) {}
107 
109  int nVersion = s.GetVersion();
110  if (!(s.GetType() & SER_GETHASH)) {
111  READWRITE(nVersion);
112  }
113  READWRITE(obj.vHave);
114  }
115 
116  void SetNull() { vHave.clear(); }
117 
118  bool IsNull() const { return vHave.empty(); }
119 };
120 
121 #endif // BITCOIN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:30
CBlockHeader()
Definition: block.h:32
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:34
CBlockLocator()
Definition: block.h:103
CBlockHeader GetBlockHeader() const
Definition: block.h:81
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:100
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:108
Definition: block.h:55
CBlock(const CBlockHeader &header)
Definition: block.h:65
std::vector< BlockHash > vHave
Definition: block.h:101
bool IsNull() const
Definition: block.h:118
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:70
#define READWRITEAS(type, obj)
Definition: serialize.h:180
CBlock()
Definition: block.h:63
uint32_t nTime
Definition: block.h:28
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:69
void SetNull()
Definition: block.h:116
uint256 hashMerkleRoot
Definition: block.h:27
void SetNull()
Definition: block.h:39
int64_t GetBlockTime() const
Definition: block.h:52
256-bit opaque blob.
Definition: uint256.h:123
std::vector< CTransactionRef > vtx
Definition: block.h:58
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
void SetNull()
Definition: block.h:75
BlockHash GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:48
bool fChecked
Definition: block.h:61
CBlockLocator(const std::vector< BlockHash > &vHaveIn)
Definition: block.h:105
#define READWRITE(...)
Definition: serialize.h:179
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