Avoid masking of difficulty adjustment errors by checkpoints
[bitcoinplatinum.git] / src / primitives / block.h
blobc90a1dfa6448631db06e522d3d0876c79a2e4977
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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) {
40 READWRITE(this->nVersion);
41 READWRITE(hashPrevBlock);
42 READWRITE(hashMerkleRoot);
43 READWRITE(nTime);
44 READWRITE(nBits);
45 READWRITE(nNonce);
48 void SetNull()
50 nVersion = 0;
51 hashPrevBlock.SetNull();
52 hashMerkleRoot.SetNull();
53 nTime = 0;
54 nBits = 0;
55 nNonce = 0;
58 bool IsNull() const
60 return (nBits == 0);
63 uint256 GetHash() const;
65 int64_t GetBlockTime() const
67 return (int64_t)nTime;
72 class CBlock : public CBlockHeader
74 public:
75 // network and disk
76 std::vector<CTransactionRef> vtx;
78 // memory only
79 mutable bool fChecked;
81 CBlock()
83 SetNull();
86 CBlock(const CBlockHeader &header)
88 SetNull();
89 *((CBlockHeader*)this) = header;
92 ADD_SERIALIZE_METHODS;
94 template <typename Stream, typename Operation>
95 inline void SerializationOp(Stream& s, Operation ser_action) {
96 READWRITE(*(CBlockHeader*)this);
97 READWRITE(vtx);
100 void SetNull()
102 CBlockHeader::SetNull();
103 vtx.clear();
104 fChecked = false;
107 CBlockHeader GetBlockHeader() const
109 CBlockHeader block;
110 block.nVersion = nVersion;
111 block.hashPrevBlock = hashPrevBlock;
112 block.hashMerkleRoot = hashMerkleRoot;
113 block.nTime = nTime;
114 block.nBits = nBits;
115 block.nNonce = nNonce;
116 return block;
119 std::string ToString() const;
122 /** Describes a place in the block chain to another node such that if the
123 * other node doesn't have the same branch, it can find a recent common trunk.
124 * The further back it is, the further before the fork it may be.
126 struct CBlockLocator
128 std::vector<uint256> vHave;
130 CBlockLocator() {}
132 CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
134 ADD_SERIALIZE_METHODS;
136 template <typename Stream, typename Operation>
137 inline void SerializationOp(Stream& s, Operation ser_action) {
138 int nVersion = s.GetVersion();
139 if (!(s.GetType() & SER_GETHASH))
140 READWRITE(nVersion);
141 READWRITE(vHave);
144 void SetNull()
146 vHave.clear();
149 bool IsNull() const
151 return vHave.empty();
155 #endif // BITCOIN_PRIMITIVES_BLOCK_H