1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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
24 static const int32_t CURRENT_VERSION
=4;
26 uint256 hashPrevBlock
;
27 uint256 hashMerkleRoot
;
37 ADD_SERIALIZE_METHODS
;
39 template <typename Stream
, typename Operation
>
40 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
41 READWRITE(this->nVersion
);
42 nVersion
= this->nVersion
;
43 READWRITE(hashPrevBlock
);
44 READWRITE(hashMerkleRoot
);
52 nVersion
= CBlockHeader::CURRENT_VERSION
;
53 hashPrevBlock
.SetNull();
54 hashMerkleRoot
.SetNull();
65 uint256
GetHash() const;
67 int64_t GetBlockTime() const
69 return (int64_t)nTime
;
74 class CBlock
: public CBlockHeader
78 std::vector
<CTransaction
> vtx
;
81 mutable bool fChecked
;
88 CBlock(const CBlockHeader
&header
)
91 *((CBlockHeader
*)this) = header
;
94 ADD_SERIALIZE_METHODS
;
96 template <typename Stream
, typename Operation
>
97 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
98 READWRITE(*(CBlockHeader
*)this);
104 CBlockHeader::SetNull();
109 CBlockHeader
GetBlockHeader() const
112 block
.nVersion
= nVersion
;
113 block
.hashPrevBlock
= hashPrevBlock
;
114 block
.hashMerkleRoot
= hashMerkleRoot
;
117 block
.nNonce
= nNonce
;
121 std::string
ToString() const;
125 /** Describes a place in the block chain to another node such that if the
126 * other node doesn't have the same branch, it can find a recent common trunk.
127 * The further back it is, the further before the fork it may be.
131 std::vector
<uint256
> vHave
;
135 CBlockLocator(const std::vector
<uint256
>& vHaveIn
)
140 ADD_SERIALIZE_METHODS
;
142 template <typename Stream
, typename Operation
>
143 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
144 if (!(nType
& SER_GETHASH
))
156 return vHave
.empty();
160 #endif // BITCOIN_PRIMITIVES_BLOCK_H