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