Line data Source code
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.
5 :
6 : #ifndef BITCOIN_PRIMITIVES_BLOCK_H
7 : #define BITCOIN_PRIMITIVES_BLOCK_H
8 :
9 : #include "primitives/transaction.h"
10 : #include "serialize.h"
11 : #include "uint256.h"
12 :
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
18 : * of the block.
19 : */
20 : class CBlockHeader
21 : {
22 : public:
23 : // header
24 : static const int32_t CURRENT_VERSION=3;
25 : int32_t nVersion;
26 : uint256 hashPrevBlock;
27 : uint256 hashMerkleRoot;
28 : uint32_t nTime;
29 : uint32_t nBits;
30 : uint32_t nNonce;
31 :
32 434943 : CBlockHeader()
33 869886 : {
34 434943 : SetNull();
35 434943 : }
36 :
37 768635 : ADD_SERIALIZE_METHODS;
38 :
39 : template <typename Stream, typename Operation>
40 737105 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
41 768635 : READWRITE(this->nVersion);
42 768635 : nVersion = this->nVersion;
43 768635 : READWRITE(hashPrevBlock);
44 768635 : READWRITE(hashMerkleRoot);
45 768635 : READWRITE(nTime);
46 768635 : READWRITE(nBits);
47 768635 : READWRITE(nNonce);
48 737105 : }
49 :
50 690337 : void SetNull()
51 : {
52 690337 : nVersion = CBlockHeader::CURRENT_VERSION;
53 690337 : hashPrevBlock.SetNull();
54 690337 : hashMerkleRoot.SetNull();
55 690337 : nTime = 0;
56 690337 : nBits = 0;
57 690337 : nNonce = 0;
58 690337 : }
59 :
60 : bool IsNull() const
61 : {
62 : return (nBits == 0);
63 : }
64 :
65 : uint256 GetHash() const;
66 :
67 0 : int64_t GetBlockTime() const
68 : {
69 56392 : return (int64_t)nTime;
70 : }
71 : };
72 :
73 :
74 1134111 : class CBlock : public CBlockHeader
75 : {
76 : public:
77 : // network and disk
78 : std::vector<CTransaction> vtx;
79 :
80 : // memory only
81 : mutable bool fChecked;
82 :
83 42918 : CBlock()
84 42918 : {
85 42918 : SetNull();
86 42918 : }
87 :
88 188099 : CBlock(const CBlockHeader &header)
89 188099 : {
90 188099 : SetNull();
91 188099 : *((CBlockHeader*)this) = header;
92 188099 : }
93 :
94 257335 : ADD_SERIALIZE_METHODS;
95 :
96 : template <typename Stream, typename Operation>
97 31530 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
98 257335 : READWRITE(*(CBlockHeader*)this);
99 257335 : READWRITE(vtx);
100 31530 : }
101 :
102 255394 : void SetNull()
103 : {
104 255394 : CBlockHeader::SetNull();
105 255394 : vtx.clear();
106 255394 : fChecked = false;
107 255394 : }
108 :
109 : CBlockHeader GetBlockHeader() const
110 : {
111 18 : CBlockHeader block;
112 18 : block.nVersion = nVersion;
113 18 : block.hashPrevBlock = hashPrevBlock;
114 18 : block.hashMerkleRoot = hashMerkleRoot;
115 18 : block.nTime = nTime;
116 18 : block.nBits = nBits;
117 18 : block.nNonce = nNonce;
118 : return block;
119 : }
120 :
121 : // Build the 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
124 : // merkle root).
125 : uint256 ComputeMerkleRoot(bool* mutated = NULL) const;
126 :
127 : std::string ToString() const;
128 : };
129 :
130 :
131 : /** Describes a place in the block chain to another node such that if the
132 : * other node doesn't have the same branch, it can find a recent common trunk.
133 : * The further back it is, the further before the fork it may be.
134 : */
135 19546 : struct CBlockLocator
136 : {
137 : std::vector<uint256> vHave;
138 :
139 4834 : CBlockLocator() {}
140 :
141 4939 : CBlockLocator(const std::vector<uint256>& vHaveIn)
142 4939 : {
143 4939 : vHave = vHaveIn;
144 4939 : }
145 :
146 9766 : ADD_SERIALIZE_METHODS;
147 :
148 : template <typename Stream, typename Operation>
149 9766 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
150 9766 : if (!(nType & SER_GETHASH))
151 9766 : READWRITE(nVersion);
152 9766 : READWRITE(vHave);
153 9766 : }
154 :
155 : void SetNull()
156 : {
157 : vHave.clear();
158 : }
159 :
160 : bool IsNull() const
161 : {
162 4542 : return vHave.empty();
163 : }
164 : };
165 :
166 : #endif // BITCOIN_PRIMITIVES_BLOCK_H
|