Bitcoin ABC 0.31.5
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
11#include <serialize.h>
12#include <uint256.h>
13#include <util/time.h>
14
24public:
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
60class CBlock : public CBlockHeader {
61public:
62 // network and disk
63 std::vector<CTransactionRef> vtx;
64
65 // Memory-only flags for caching expensive checks
66 // CheckBlock()
67 mutable bool fChecked;
68 // CheckMerkleRoot()
69 mutable bool m_checked_merkle_root{false};
70
71 CBlock() { SetNull(); }
72
73 CBlock(const CBlockHeader &header) {
74 SetNull();
75 *(static_cast<CBlockHeader *>(this)) = header;
76 }
77
80 READWRITE(obj.vtx);
81 }
82
83 void SetNull() {
85 vtx.clear();
86 fChecked = false;
88 }
89
91 CBlockHeader block;
92 block.nVersion = nVersion;
95 block.nTime = nTime;
96 block.nBits = nBits;
97 block.nNonce = nNonce;
98 return block;
99 }
100
101 std::string ToString() const;
102};
103
110 std::vector<BlockHash> vHave;
111
113
114 explicit CBlockLocator(std::vector<BlockHash> &&vHaveIn)
115 : vHave(std::move(vHaveIn)) {}
116
118 int nVersion = s.GetVersion();
119 if (!(s.GetType() & SER_GETHASH)) {
120 READWRITE(nVersion);
121 }
122 READWRITE(obj.vHave);
123 }
124
125 void SetNull() { vHave.clear(); }
126
127 bool IsNull() const { return vHave.empty(); }
128};
129
130#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:83
std::string ToString() const
Definition: block.cpp:15
bool m_checked_merkle_root
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:63
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:78
CBlockHeader GetBlockHeader() const
Definition: block.h:90
CBlock()
Definition: block.h:71
CBlock(const CBlockHeader &header)
Definition: block.h:73
bool fChecked
Definition: block.h:67
void SetNull()
Definition: uint256.h:41
256-bit opaque blob.
Definition: uint256.h:129
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
#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:109
std::vector< BlockHash > vHave
Definition: block.h:110
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:117
bool IsNull() const
Definition: block.h:127
CBlockLocator(std::vector< BlockHash > &&vHaveIn)
Definition: block.h:114
CBlockLocator()
Definition: block.h:112
void SetNull()
Definition: block.h:125
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25