BIP9 Implementation
[bitcoinplatinum.git] / src / primitives / block.h
blob42276b2bc26bcbdb046a0d760a51c2afff2553bf
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 int32_t nVersion;
25 uint256 hashPrevBlock;
26 uint256 hashMerkleRoot;
27 uint32_t nTime;
28 uint32_t nBits;
29 uint32_t nNonce;
31 CBlockHeader()
33 SetNull();
36 ADD_SERIALIZE_METHODS;
38 template <typename Stream, typename Operation>
39 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
40 READWRITE(this->nVersion);
41 nVersion = this->nVersion;
42 READWRITE(hashPrevBlock);
43 READWRITE(hashMerkleRoot);
44 READWRITE(nTime);
45 READWRITE(nBits);
46 READWRITE(nNonce);
49 void SetNull()
51 nVersion = 0;
52 hashPrevBlock.SetNull();
53 hashMerkleRoot.SetNull();
54 nTime = 0;
55 nBits = 0;
56 nNonce = 0;
59 bool IsNull() const
61 return (nBits == 0);
64 uint256 GetHash() const;
66 int64_t GetBlockTime() const
68 return (int64_t)nTime;
73 class CBlock : public CBlockHeader
75 public:
76 // network and disk
77 std::vector<CTransaction> vtx;
79 // memory only
80 mutable bool fChecked;
82 CBlock()
84 SetNull();
87 CBlock(const CBlockHeader &header)
89 SetNull();
90 *((CBlockHeader*)this) = header;
93 ADD_SERIALIZE_METHODS;
95 template <typename Stream, typename Operation>
96 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
97 READWRITE(*(CBlockHeader*)this);
98 READWRITE(vtx);
101 void SetNull()
103 CBlockHeader::SetNull();
104 vtx.clear();
105 fChecked = false;
108 CBlockHeader GetBlockHeader() const
110 CBlockHeader block;
111 block.nVersion = nVersion;
112 block.hashPrevBlock = hashPrevBlock;
113 block.hashMerkleRoot = hashMerkleRoot;
114 block.nTime = nTime;
115 block.nBits = nBits;
116 block.nNonce = nNonce;
117 return block;
120 std::string ToString() const;
124 /** Describes a place in the block chain to another node such that if the
125 * other node doesn't have the same branch, it can find a recent common trunk.
126 * The further back it is, the further before the fork it may be.
128 struct CBlockLocator
130 std::vector<uint256> vHave;
132 CBlockLocator() {}
134 CBlockLocator(const std::vector<uint256>& vHaveIn)
136 vHave = vHaveIn;
139 ADD_SERIALIZE_METHODS;
141 template <typename Stream, typename Operation>
142 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
143 if (!(nType & SER_GETHASH))
144 READWRITE(nVersion);
145 READWRITE(vHave);
148 void SetNull()
150 vHave.clear();
153 bool IsNull() const
155 return vHave.empty();
159 #endif // BITCOIN_PRIMITIVES_BLOCK_H