Bitcoin ABC  0.26.3
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;
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
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
BlockHash GetHash() const
Definition: block.cpp:11
uint32_t nNonce
Definition: block.h:30
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:34
uint32_t nBits
Definition: block.h:29
CBlockHeader()
Definition: block.h:32
uint32_t nTime
Definition: block.h:28
BlockHash hashPrevBlock
Definition: block.h:26
int64_t GetBlockTime() const
Definition: block.h:52
int32_t nVersion
Definition: block.h:25
void SetNull()
Definition: block.h:39
uint256 hashMerkleRoot
Definition: block.h:27
bool IsNull() const
Definition: block.h:48
Definition: block.h:55
void SetNull()
Definition: block.h:75
std::string ToString() const
Definition: block.cpp:15
std::vector< CTransactionRef > vtx
Definition: block.h:58
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:70
CBlockHeader GetBlockHeader() const
Definition: block.h:81
CBlock()
Definition: block.h:63
CBlock(const CBlockHeader &header)
Definition: block.h:65
bool fChecked
Definition: block.h:61
void SetNull()
Definition: uint256.h:39
256-bit opaque blob.
Definition: uint256.h:127
#define READWRITEAS(type, obj)
Definition: serialize.h:181
@ SER_GETHASH
Definition: serialize.h:168
#define READWRITE(...)
Definition: serialize.h:180
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:100
std::vector< BlockHash > vHave
Definition: block.h:101
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:108
bool IsNull() const
Definition: block.h:118
CBlockLocator()
Definition: block.h:103
void SetNull()
Definition: block.h:116
CBlockLocator(const std::vector< BlockHash > &vHaveIn)
Definition: block.h:105