Bitcoin ABC  0.29.4
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 #include <util/time.h>
14 
23 class CBlockHeader {
24 public:
25  // header
26  int32_t nVersion;
29  uint32_t nTime;
30  uint32_t nBits;
31  uint32_t nNonce;
32 
34 
36  READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot,
37  obj.nTime, obj.nBits, obj.nNonce);
38  }
39 
40  void SetNull() {
41  nVersion = 0;
44  nTime = 0;
45  nBits = 0;
46  nNonce = 0;
47  }
48 
49  bool IsNull() const { return (nBits == 0); }
50 
51  BlockHash GetHash() const;
52 
53  NodeSeconds Time() const {
54  return NodeSeconds{std::chrono::seconds{nTime}};
55  }
56 
57  int64_t GetBlockTime() const { return (int64_t)nTime; }
58 };
59 
60 class CBlock : public CBlockHeader {
61 public:
62  // network and disk
63  std::vector<CTransactionRef> vtx;
64 
65  // memory only
66  mutable bool fChecked;
67 
68  CBlock() { SetNull(); }
69 
70  CBlock(const CBlockHeader &header) {
71  SetNull();
72  *(static_cast<CBlockHeader *>(this)) = header;
73  }
74 
77  READWRITE(obj.vtx);
78  }
79 
80  void SetNull() {
82  vtx.clear();
83  fChecked = false;
84  }
85 
87  CBlockHeader block;
88  block.nVersion = nVersion;
91  block.nTime = nTime;
92  block.nBits = nBits;
93  block.nNonce = nNonce;
94  return block;
95  }
96 
97  std::string ToString() const;
98 };
99 
106  std::vector<BlockHash> vHave;
107 
109 
110  explicit CBlockLocator(const std::vector<BlockHash> &vHaveIn)
111  : vHave(vHaveIn) {}
112 
114  int nVersion = s.GetVersion();
115  if (!(s.GetType() & SER_GETHASH)) {
116  READWRITE(nVersion);
117  }
118  READWRITE(obj.vHave);
119  }
120 
121  void SetNull() { vHave.clear(); }
122 
123  bool IsNull() const { return vHave.empty(); }
124 };
125 
126 #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:23
BlockHash GetHash() const
Definition: block.cpp:11
NodeSeconds Time() const
Definition: block.h:53
uint32_t nNonce
Definition: block.h:31
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:35
uint32_t nBits
Definition: block.h:30
CBlockHeader()
Definition: block.h:33
uint32_t nTime
Definition: block.h:29
BlockHash hashPrevBlock
Definition: block.h:27
int64_t GetBlockTime() const
Definition: block.h:57
int32_t nVersion
Definition: block.h:26
void SetNull()
Definition: block.h:40
uint256 hashMerkleRoot
Definition: block.h:28
bool IsNull() const
Definition: block.h:49
Definition: block.h:60
void SetNull()
Definition: block.h:80
std::string ToString() const
Definition: block.cpp:15
std::vector< CTransactionRef > vtx
Definition: block.h:63
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:75
CBlockHeader GetBlockHeader() const
Definition: block.h:86
CBlock()
Definition: block.h:68
CBlock(const CBlockHeader &header)
Definition: block.h:70
bool fChecked
Definition: block.h:66
void SetNull()
Definition: uint256.h:41
256-bit opaque blob.
Definition: uint256.h:129
#define READWRITEAS(type, obj)
Definition: serialize.h:167
@ SER_GETHASH
Definition: serialize.h:154
#define READWRITE(...)
Definition: serialize.h:166
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:105
std::vector< BlockHash > vHave
Definition: block.h:106
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:113
bool IsNull() const
Definition: block.h:123
CBlockLocator()
Definition: block.h:108
void SetNull()
Definition: block.h:121
CBlockLocator(const std::vector< BlockHash > &vHaveIn)
Definition: block.h:110
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25