]> Git Repo - VerusCoin.git/blame - src/primitives/block.h
Auto merge of #1260 - str4d:1175-byte-array-manipulation, r=ebfull
[VerusCoin.git] / src / primitives / block.h
CommitLineData
effc2770 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2013 The Bitcoin Core developers
78253fcb 3// Distributed under the MIT software license, see the accompanying
effc2770 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
51ed9ec9 5
d2270111
LD
6#ifndef BITCOIN_PRIMITIVES_BLOCK_H
7#define BITCOIN_PRIMITIVES_BLOCK_H
effc2770 8
d2270111 9#include "primitives/transaction.h"
51ed9ec9
BD
10#include "serialize.h"
11#include "uint256.h"
12
aabdf9e8
EL
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 */
20class CBlockHeader
21{
22public:
23 // header
5e82e1c8 24 static const int32_t CURRENT_VERSION=4;
9f3d4767 25 int32_t nVersion;
aabdf9e8
EL
26 uint256 hashPrevBlock;
27 uint256 hashMerkleRoot;
a8d384ae 28 uint256 hashReserved;
9f3d4767
KD
29 uint32_t nTime;
30 uint32_t nBits;
fdda3c50 31 uint256 nNonce;
5be6abbf 32 std::vector<unsigned char> nSolution;
aabdf9e8
EL
33
34 CBlockHeader()
35 {
36 SetNull();
37 }
38
3f6540ad 39 ADD_SERIALIZE_METHODS;
3d796f89 40
84881f8c 41 template <typename Stream, typename Operation>
31e9a838 42 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
aabdf9e8
EL
43 READWRITE(this->nVersion);
44 nVersion = this->nVersion;
45 READWRITE(hashPrevBlock);
46 READWRITE(hashMerkleRoot);
a8d384ae 47 READWRITE(hashReserved);
aabdf9e8
EL
48 READWRITE(nTime);
49 READWRITE(nBits);
50 READWRITE(nNonce);
fdda3c50 51 READWRITE(nSolution);
3d796f89 52 }
aabdf9e8
EL
53
54 void SetNull()
55 {
56 nVersion = CBlockHeader::CURRENT_VERSION;
4f152496
WL
57 hashPrevBlock.SetNull();
58 hashMerkleRoot.SetNull();
a8d384ae 59 hashReserved.SetNull();
aabdf9e8
EL
60 nTime = 0;
61 nBits = 0;
fdda3c50
JG
62 nNonce = uint256();
63 nSolution.clear();
aabdf9e8
EL
64 }
65
66 bool IsNull() const
67 {
68 return (nBits == 0);
69 }
70
f121db58 71 uint256 GetHash() const;
aabdf9e8 72
51ed9ec9 73 int64_t GetBlockTime() const
aabdf9e8 74 {
51ed9ec9 75 return (int64_t)nTime;
aabdf9e8
EL
76 }
77};
78
33944573
EL
79
80class CBlock : public CBlockHeader
81{
82public:
83 // network and disk
84 std::vector<CTransaction> vtx;
85
86 // memory only
87 mutable std::vector<uint256> vMerkleTree;
88
89 CBlock()
90 {
91 SetNull();
92 }
93
94 CBlock(const CBlockHeader &header)
95 {
96 SetNull();
97 *((CBlockHeader*)this) = header;
98 }
99
3f6540ad 100 ADD_SERIALIZE_METHODS;
3d796f89 101
84881f8c 102 template <typename Stream, typename Operation>
31e9a838 103 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
33944573
EL
104 READWRITE(*(CBlockHeader*)this);
105 READWRITE(vtx);
3d796f89 106 }
33944573
EL
107
108 void SetNull()
109 {
110 CBlockHeader::SetNull();
111 vtx.clear();
112 vMerkleTree.clear();
113 }
114
115 CBlockHeader GetBlockHeader() const
116 {
117 CBlockHeader block;
118 block.nVersion = nVersion;
119 block.hashPrevBlock = hashPrevBlock;
120 block.hashMerkleRoot = hashMerkleRoot;
a8d384ae 121 block.hashReserved = hashReserved;
33944573
EL
122 block.nTime = nTime;
123 block.nBits = nBits;
124 block.nNonce = nNonce;
fdda3c50 125 block.nSolution = nSolution;
33944573
EL
126 return block;
127 }
128
584a3589
PW
129 // Build the in-memory merkle tree for this block and return the merkle root.
130 // If non-NULL, *mutated is set to whether mutation was detected in the merkle
131 // tree (a duplication of transactions in the block leading to an identical
132 // merkle root).
133 uint256 BuildMerkleTree(bool* mutated = NULL) const;
33944573 134
f121db58
PW
135 std::vector<uint256> GetMerkleBranch(int nIndex) const;
136 static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
81212588 137 std::string ToString() const;
33944573
EL
138};
139
f9b15a4f 140
fdda3c50
JG
141/**
142 * Custom serializer for CBlockHeader that omits the nonce and solution, for use
143 * as input to Equihash.
144 */
145class CEquihashInput : private CBlockHeader
146{
147public:
148 CEquihashInput(const CBlockHeader &header)
149 {
150 CBlockHeader::SetNull();
151 *((CBlockHeader*)this) = header;
152 }
153
154 ADD_SERIALIZE_METHODS;
155
156 template <typename Stream, typename Operation>
157 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
158 READWRITE(this->nVersion);
159 nVersion = this->nVersion;
160 READWRITE(hashPrevBlock);
161 READWRITE(hashMerkleRoot);
a8d384ae 162 READWRITE(hashReserved);
fdda3c50
JG
163 READWRITE(nTime);
164 READWRITE(nBits);
165 }
166};
167
168
f9b15a4f
PW
169/** Describes a place in the block chain to another node such that if the
170 * other node doesn't have the same branch, it can find a recent common trunk.
171 * The further back it is, the further before the fork it may be.
172 */
173struct CBlockLocator
174{
175 std::vector<uint256> vHave;
176
177 CBlockLocator() {}
178
179 CBlockLocator(const std::vector<uint256>& vHaveIn)
180 {
181 vHave = vHaveIn;
182 }
183
3f6540ad 184 ADD_SERIALIZE_METHODS;
3d796f89 185
84881f8c 186 template <typename Stream, typename Operation>
31e9a838 187 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
f9b15a4f
PW
188 if (!(nType & SER_GETHASH))
189 READWRITE(nVersion);
190 READWRITE(vHave);
3d796f89 191 }
f9b15a4f
PW
192
193 void SetNull()
194 {
195 vHave.clear();
196 }
197
bdb6a71d 198 bool IsNull() const
f9b15a4f
PW
199 {
200 return vHave.empty();
201 }
202};
203
d2270111 204#endif // BITCOIN_PRIMITIVES_BLOCK_H
This page took 0.16717 seconds and 4 git commands to generate.