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
=3;
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 std::vector
<uint256
> vMerkleTree
;
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 // Build the in-memory merkle tree for this block and return the merkle root.
122 // If non-NULL, *mutated is set to whether mutation was detected in the merkle
123 // tree (a duplication of transactions in the block leading to an identical
125 uint256
BuildMerkleTree(bool* mutated
= NULL
) const;
127 std::vector
<uint256
> GetMerkleBranch(int nIndex
) const;
128 static uint256
CheckMerkleBranch(uint256 hash
, const std::vector
<uint256
>& vMerkleBranch
, int nIndex
);
129 std::string
ToString() const;
133 /** Describes a place in the block chain to another node such that if the
134 * other node doesn't have the same branch, it can find a recent common trunk.
135 * The further back it is, the further before the fork it may be.
139 std::vector
<uint256
> vHave
;
143 CBlockLocator(const std::vector
<uint256
>& vHaveIn
)
148 ADD_SERIALIZE_METHODS
;
150 template <typename Stream
, typename Operation
>
151 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
152 if (!(nType
& SER_GETHASH
))
164 return vHave
.empty();
168 #endif // BITCOIN_PRIMITIVES_BLOCK_H