1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
6 #ifndef BITCOIN_PRIMITIVES_BLOCK_H
7 #define BITCOIN_PRIMITIVES_BLOCK_H
9 #include <primitives/transaction.h>
10 #include <serialize.h>
13 /** Nodes collect new transactions into a block, hash them into a hash tree,
14 * and scan through nonce values to make the block's hash satisfy proof-of-work
15 * requirements. When they solve the proof-of-work, they broadcast the block
16 * to everyone and the block is added to the block chain. The first transaction
17 * in the block is a special one that creates a new coin owned by the creator
25 uint256 hashPrevBlock
;
26 uint256 hashMerkleRoot
;
36 ADD_SERIALIZE_METHODS
;
38 template <typename Stream
, typename Operation
>
39 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
40 READWRITE(this->nVersion
);
41 READWRITE(hashPrevBlock
);
42 READWRITE(hashMerkleRoot
);
51 hashPrevBlock
.SetNull();
52 hashMerkleRoot
.SetNull();
63 uint256
GetHash() const;
65 int64_t GetBlockTime() const
67 return (int64_t)nTime
;
72 class CBlock
: public CBlockHeader
76 std::vector
<CTransactionRef
> vtx
;
79 mutable bool fChecked
;
86 CBlock(const CBlockHeader
&header
)
89 *((CBlockHeader
*)this) = header
;
92 ADD_SERIALIZE_METHODS
;
94 template <typename Stream
, typename Operation
>
95 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
96 READWRITE(*(CBlockHeader
*)this);
102 CBlockHeader::SetNull();
107 CBlockHeader
GetBlockHeader() const
110 block
.nVersion
= nVersion
;
111 block
.hashPrevBlock
= hashPrevBlock
;
112 block
.hashMerkleRoot
= hashMerkleRoot
;
115 block
.nNonce
= nNonce
;
119 std::string
ToString() const;
122 /** Describes a place in the block chain to another node such that if the
123 * other node doesn't have the same branch, it can find a recent common trunk.
124 * The further back it is, the further before the fork it may be.
128 std::vector
<uint256
> vHave
;
132 explicit CBlockLocator(const std::vector
<uint256
>& vHaveIn
) : vHave(vHaveIn
) {}
134 ADD_SERIALIZE_METHODS
;
136 template <typename Stream
, typename Operation
>
137 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
138 int nVersion
= s
.GetVersion();
139 if (!(s
.GetType() & SER_GETHASH
))
151 return vHave
.empty();
155 #endif // BITCOIN_PRIMITIVES_BLOCK_H