Bitcoin Core 22.99.0
P2P Digital Currency
block.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 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
10#include <serialize.h>
11#include <uint256.h>
12
21{
22public:
23 // header
24 int32_t nVersion;
27 uint32_t nTime;
28 uint32_t nBits;
29 uint32_t nNonce;
30
32 {
33 SetNull();
34 }
35
36 SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
37
38 void SetNull()
39 {
40 nVersion = 0;
43 nTime = 0;
44 nBits = 0;
45 nNonce = 0;
46 }
47
48 bool IsNull() const
49 {
50 return (nBits == 0);
51 }
52
53 uint256 GetHash() const;
54
55 int64_t GetBlockTime() const
56 {
57 return (int64_t)nTime;
58 }
59};
60
61
62class CBlock : public CBlockHeader
63{
64public:
65 // network and disk
66 std::vector<CTransactionRef> vtx;
67
68 // memory only
69 mutable bool fChecked;
70
72 {
73 SetNull();
74 }
75
76 CBlock(const CBlockHeader &header)
77 {
78 SetNull();
79 *(static_cast<CBlockHeader*>(this)) = header;
80 }
81
83 {
85 READWRITE(obj.vtx);
86 }
87
88 void SetNull()
89 {
91 vtx.clear();
92 fChecked = false;
93 }
94
96 {
97 CBlockHeader block;
98 block.nVersion = nVersion;
101 block.nTime = nTime;
102 block.nBits = nBits;
103 block.nNonce = nNonce;
104 return block;
105 }
106
107 std::string ToString() const;
108};
109
115{
116 std::vector<uint256> vHave;
117
119
120 explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
121
123 {
124 int nVersion = s.GetVersion();
125 if (!(s.GetType() & SER_GETHASH))
126 READWRITE(nVersion);
127 READWRITE(obj.vHave);
128 }
129
130 void SetNull()
131 {
132 vHave.clear();
133 }
134
135 bool IsNull() const
136 {
137 return vHave.empty();
138 }
139};
140
141#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:21
uint32_t nNonce
Definition: block.h:29
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:36
uint32_t nBits
Definition: block.h:28
CBlockHeader()
Definition: block.h:31
uint32_t nTime
Definition: block.h:27
int64_t GetBlockTime() const
Definition: block.h:55
int32_t nVersion
Definition: block.h:24
uint256 hashPrevBlock
Definition: block.h:25
void SetNull()
Definition: block.h:38
uint256 hashMerkleRoot
Definition: block.h:26
uint256 GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:48
Definition: block.h:63
void SetNull()
Definition: block.h:88
std::string ToString() const
Definition: block.cpp:16
std::vector< CTransactionRef > vtx
Definition: block.h:66
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:82
CBlockHeader GetBlockHeader() const
Definition: block.h:95
CBlock()
Definition: block.h:71
CBlock(const CBlockHeader &header)
Definition: block.h:76
bool fChecked
Definition: block.h:69
void SetNull()
Definition: uint256.h:39
256-bit opaque blob.
Definition: uint256.h:124
@ SER_GETHASH
Definition: serialize.h:140
#define READWRITEAS(type, obj)
Definition: serialize.h:148
#define READWRITE(...)
Definition: serialize.h:147
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:115
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:120
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:122
std::vector< uint256 > vHave
Definition: block.h:116
bool IsNull() const
Definition: block.h:135
CBlockLocator()
Definition: block.h:118
void SetNull()
Definition: block.h:130