Switch blocks to a constant-space Merkle root/branch algorithm.
[bitcoinplatinum.git] / src / primitives / block.h
blob5c017d436f50ec5e7ac95d965fc2a3964b152d8b
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"
11 #include "uint256.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
18 * of the block.
20 class CBlockHeader
22 public:
23 // header
24 static const int32_t CURRENT_VERSION=4;
25 int32_t nVersion;
26 uint256 hashPrevBlock;
27 uint256 hashMerkleRoot;
28 uint32_t nTime;
29 uint32_t nBits;
30 uint32_t nNonce;
32 CBlockHeader()
34 SetNull();
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);
45 READWRITE(nTime);
46 READWRITE(nBits);
47 READWRITE(nNonce);
50 void SetNull()
52 nVersion = CBlockHeader::CURRENT_VERSION;
53 hashPrevBlock.SetNull();
54 hashMerkleRoot.SetNull();
55 nTime = 0;
56 nBits = 0;
57 nNonce = 0;
60 bool IsNull() const
62 return (nBits == 0);
65 uint256 GetHash() const;
67 int64_t GetBlockTime() const
69 return (int64_t)nTime;
74 class CBlock : public CBlockHeader
76 public:
77 // network and disk
78 std::vector<CTransaction> vtx;
80 // memory only
81 mutable bool fChecked;
83 CBlock()
85 SetNull();
88 CBlock(const CBlockHeader &header)
90 SetNull();
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);
99 READWRITE(vtx);
102 void SetNull()
104 CBlockHeader::SetNull();
105 vtx.clear();
106 fChecked = false;
109 CBlockHeader GetBlockHeader() const
111 CBlockHeader block;
112 block.nVersion = nVersion;
113 block.hashPrevBlock = hashPrevBlock;
114 block.hashMerkleRoot = hashMerkleRoot;
115 block.nTime = nTime;
116 block.nBits = nBits;
117 block.nNonce = nNonce;
118 return block;
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.
129 struct CBlockLocator
131 std::vector<uint256> vHave;
133 CBlockLocator() {}
135 CBlockLocator(const std::vector<uint256>& vHaveIn)
137 vHave = 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))
145 READWRITE(nVersion);
146 READWRITE(vHave);
149 void SetNull()
151 vHave.clear();
154 bool IsNull() const
156 return vHave.empty();
160 #endif // BITCOIN_PRIMITIVES_BLOCK_H