Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / primitives / block.h
blob59f46deb1c420d8aef28a85a0d144a06259d3a18
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=3;
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 std::vector<uint256> vMerkleTree;
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 vMerkleTree.clear();
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 // Build the in-memory 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 BuildMerkleTree(bool* mutated = NULL) const;
127 std::vector<uint256> GetMerkleBranch(int nIndex) const;
128 static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex);
129 std::string ToString() const;
133 /** Describes a place in the block chain to another node such that if the
134 * other node doesn't have the same branch, it can find a recent common trunk.
135 * The further back it is, the further before the fork it may be.
137 struct CBlockLocator
139 std::vector<uint256> vHave;
141 CBlockLocator() {}
143 CBlockLocator(const std::vector<uint256>& vHaveIn)
145 vHave = vHaveIn;
148 ADD_SERIALIZE_METHODS;
150 template <typename Stream, typename Operation>
151 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
152 if (!(nType & SER_GETHASH))
153 READWRITE(nVersion);
154 READWRITE(vHave);
157 void SetNull()
159 vHave.clear();
162 bool IsNull() const
164 return vHave.empty();
168 #endif // BITCOIN_PRIMITIVES_BLOCK_H