1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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
, int nType
, int nVersion
) {
40 READWRITE(this->nVersion
);
41 nVersion
= this->nVersion
;
42 READWRITE(hashPrevBlock
);
43 READWRITE(hashMerkleRoot
);
52 hashPrevBlock
.SetNull();
53 hashMerkleRoot
.SetNull();
64 uint256
GetHash() const;
66 int64_t GetBlockTime() const
68 return (int64_t)nTime
;
73 class CBlock
: public CBlockHeader
77 std::vector
<CTransaction
> vtx
;
80 mutable bool fChecked
;
87 CBlock(const CBlockHeader
&header
)
90 *((CBlockHeader
*)this) = header
;
93 ADD_SERIALIZE_METHODS
;
95 template <typename Stream
, typename Operation
>
96 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
97 READWRITE(*(CBlockHeader
*)this);
103 CBlockHeader::SetNull();
108 CBlockHeader
GetBlockHeader() const
111 block
.nVersion
= nVersion
;
112 block
.hashPrevBlock
= hashPrevBlock
;
113 block
.hashMerkleRoot
= hashMerkleRoot
;
116 block
.nNonce
= nNonce
;
120 std::string
ToString() const;
124 /** Describes a place in the block chain to another node such that if the
125 * other node doesn't have the same branch, it can find a recent common trunk.
126 * The further back it is, the further before the fork it may be.
130 std::vector
<uint256
> vHave
;
134 CBlockLocator(const std::vector
<uint256
>& vHaveIn
)
139 ADD_SERIALIZE_METHODS
;
141 template <typename Stream
, typename Operation
>
142 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
143 if (!(nType
& SER_GETHASH
))
155 return vHave
.empty();
159 #endif // BITCOIN_PRIMITIVES_BLOCK_H