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 size_t HEADER_SIZE=4+32+32+32+4+4+32; // excluding Equihash solution
25 static const int32_t CURRENT_VERSION=4;
26 static uint256 (CBlockHeader::*hashFunction)() const;
28 static void SetHashAlgo();
31 uint256 hashPrevBlock;
32 uint256 hashMerkleRoot;
37 std::vector<unsigned char> nSolution;
44 ADD_SERIALIZE_METHODS;
46 template <typename Stream, typename Operation>
47 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
48 READWRITE(this->nVersion);
49 nVersion = this->nVersion;
50 READWRITE(hashPrevBlock);
51 READWRITE(hashMerkleRoot);
52 READWRITE(hashReserved);
61 nVersion = CBlockHeader::CURRENT_VERSION;
62 hashPrevBlock.SetNull();
63 hashMerkleRoot.SetNull();
64 hashReserved.SetNull();
76 uint256 GetHash() const
78 return (this->*hashFunction)();
81 uint256 GetSHA256DHash() const;
82 static void SetSHA256DHash();
84 uint256 GetVerusHash() const;
85 static void SetVerusHash();
87 int64_t GetBlockTime() const
89 return (int64_t)nTime;
94 class CBlock : public CBlockHeader
98 std::vector<CTransaction> vtx;
101 mutable std::vector<uint256> vMerkleTree;
108 CBlock(const CBlockHeader &header)
111 *((CBlockHeader*)this) = header;
114 ADD_SERIALIZE_METHODS;
116 template <typename Stream, typename Operation>
117 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
118 READWRITE(*(CBlockHeader*)this);
124 CBlockHeader::SetNull();
129 CBlockHeader GetBlockHeader() const
132 block.nVersion = nVersion;
133 block.hashPrevBlock = hashPrevBlock;
134 block.hashMerkleRoot = hashMerkleRoot;
135 block.hashReserved = hashReserved;
138 block.nNonce = nNonce;
139 block.nSolution = nSolution;
143 // Build the in-memory merkle tree for this block and return the merkle root.
144 // If non-NULL, *mutated is set to whether mutation was detected in the merkle
145 // tree (a duplication of transactions in the block leading to an identical
147 uint256 BuildMerkleTree(bool* mutated = NULL) const;
149 std::vector<uint256> GetMerkleBranch(int nIndex) const;
150 static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
151 std::string ToString() const;
156 * Custom serializer for CBlockHeader that omits the nonce and solution, for use
157 * as input to Equihash.
159 class CEquihashInput : private CBlockHeader
162 CEquihashInput(const CBlockHeader &header)
164 CBlockHeader::SetNull();
165 *((CBlockHeader*)this) = header;
168 ADD_SERIALIZE_METHODS;
170 template <typename Stream, typename Operation>
171 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
172 READWRITE(this->nVersion);
173 nVersion = this->nVersion;
174 READWRITE(hashPrevBlock);
175 READWRITE(hashMerkleRoot);
176 READWRITE(hashReserved);
183 /** Describes a place in the block chain to another node such that if the
184 * other node doesn't have the same branch, it can find a recent common trunk.
185 * The further back it is, the further before the fork it may be.
189 std::vector<uint256> vHave;
193 CBlockLocator(const std::vector<uint256>& vHaveIn)
198 ADD_SERIALIZE_METHODS;
200 template <typename Stream, typename Operation>
201 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
202 if (!(nType & SER_GETHASH))
214 return vHave.empty();
217 friend bool operator==(const CBlockLocator& a, const CBlockLocator& b) {
218 return (a.vHave == b.vHave);
222 #endif // BITCOIN_PRIMITIVES_BLOCK_H