Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2013 The Bitcoin developers 3 : // Copyright (c) 2015-2021 The PIVX Core developers 4 : // Distributed under the MIT/X11 software license, see the accompanying 5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 : 7 : #ifndef PIVX_PRIMITIVES_BLOCK_H 8 : #define PIVX_PRIMITIVES_BLOCK_H 9 : 10 : #include "primitives/transaction.h" 11 : #include "keystore.h" 12 : #include "serialize.h" 13 : #include "uint256.h" 14 : 15 : /** Nodes collect new transactions into a block, hash them into a hash tree, 16 : * and scan through nonce values to make the block's hash satisfy proof-of-work 17 : * requirements. When they solve the proof-of-work, they broadcast the block 18 : * to everyone and the block is added to the block chain. The first transaction 19 : * in the block is a special one that creates a new coin owned by the creator 20 : * of the block. 21 : */ 22 : class CBlockHeader 23 : { 24 : public: 25 : // header 26 : static const int32_t CURRENT_VERSION=11; // since v5.2.99 27 : int32_t nVersion; 28 : uint256 hashPrevBlock; 29 : uint256 hashMerkleRoot; 30 : uint32_t nTime; 31 : uint32_t nBits; 32 : uint32_t nNonce; 33 : uint256 nAccumulatorCheckpoint; // only for version 4, 5 and 6. 34 : uint256 hashFinalSaplingRoot; // only for version 8+ 35 : 36 106842 : CBlockHeader() 37 427368 : { 38 106842 : SetNull(); 39 106842 : } 40 : 41 918837 : SERIALIZE_METHODS(CBlockHeader, obj) { 42 787424 : READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); 43 : 44 : //zerocoin active, header changes to include accumulator checksum 45 787424 : if(obj.nVersion > 3 && obj.nVersion < 7) 46 63652 : READWRITE(obj.nAccumulatorCheckpoint); 47 : 48 : // Sapling active 49 787424 : if (obj.nVersion >= 8) 50 207622 : READWRITE(obj.hashFinalSaplingRoot); 51 787424 : } 52 : 53 227924 : void SetNull() 54 : { 55 227924 : nVersion = CBlockHeader::CURRENT_VERSION; 56 227924 : hashPrevBlock.SetNull(); 57 227924 : hashMerkleRoot.SetNull(); 58 227924 : nTime = 0; 59 227924 : nBits = 0; 60 227924 : nNonce = 0; 61 227924 : nAccumulatorCheckpoint.SetNull(); 62 227924 : hashFinalSaplingRoot.SetNull(); 63 227924 : } 64 : 65 : bool IsNull() const 66 : { 67 : return (nBits == 0); 68 : } 69 : 70 : uint256 GetHash() const; 71 : 72 235053 : int64_t GetBlockTime() const 73 : { 74 235053 : return (int64_t)nTime; 75 : } 76 : }; 77 : 78 : 79 12 : class CBlock : public CBlockHeader 80 : { 81 : public: 82 : // network and disk 83 : std::vector<CTransactionRef> vtx; 84 : 85 : // ppcoin: block signature - signed by one of the coin base txout[N]'s owner 86 : std::vector<unsigned char> vchBlockSig; 87 : 88 : // memory only 89 : mutable bool fChecked{false}; 90 : 91 75986 : CBlock() 92 75986 : { 93 75986 : SetNull(); 94 75986 : } 95 : 96 : CBlock(const CBlockHeader &header) 97 : { 98 : SetNull(); 99 : *(static_cast<CBlockHeader*>(this)) = header; 100 : } 101 : 102 364479 : SERIALIZE_METHODS(CBlock, obj) 103 : { 104 234351 : READWRITEAS(CBlockHeader, obj); 105 234351 : READWRITE(obj.vtx); 106 234351 : if(obj.vtx.size() > 1 && obj.vtx[1]->IsCoinStake()) 107 22010 : READWRITE(obj.vchBlockSig); 108 234351 : } 109 : 110 121082 : void SetNull() 111 : { 112 121082 : CBlockHeader::SetNull(); 113 121082 : vtx.clear(); 114 121082 : fChecked = false; 115 121082 : vchBlockSig.clear(); 116 121082 : } 117 : 118 13 : CBlockHeader GetBlockHeader() const 119 : { 120 13 : CBlockHeader block; 121 13 : block.nVersion = nVersion; 122 13 : block.hashPrevBlock = hashPrevBlock; 123 13 : block.hashMerkleRoot = hashMerkleRoot; 124 13 : block.nTime = nTime; 125 13 : block.nBits = nBits; 126 13 : block.nNonce = nNonce; 127 13 : if(nVersion > 3 && nVersion < 7) 128 0 : block.nAccumulatorCheckpoint = nAccumulatorCheckpoint; 129 13 : if (nVersion >= 8) 130 0 : block.hashFinalSaplingRoot = hashFinalSaplingRoot; 131 13 : return block; 132 : } 133 : 134 284382 : bool IsProofOfStake() const 135 : { 136 284382 : return (vtx.size() > 1 && vtx[1]->IsCoinStake()); 137 : } 138 : 139 71190 : bool IsProofOfWork() const 140 : { 141 71190 : return !IsProofOfStake(); 142 : } 143 : 144 : std::string ToString() const; 145 : void print() const; 146 : }; 147 : 148 : 149 : /** Describes a place in the block chain to another node such that if the 150 : * other node doesn't have the same branch, it can find a recent common trunk. 151 : * The further back it is, the further before the fork it may be. 152 : */ 153 3437 : struct CBlockLocator 154 : { 155 : std::vector<uint256> vHave; 156 : 157 1075 : CBlockLocator() {} 158 : 159 961 : explicit CBlockLocator(const std::vector<uint256>& vHaveIn) 160 961 : { 161 961 : vHave = vHaveIn; 162 961 : } 163 : 164 4450 : SERIALIZE_METHODS(CBlockLocator, obj) 165 : { 166 2225 : int nVersion = s.GetVersion(); 167 2225 : if (!(s.GetType() & SER_GETHASH)) 168 2225 : READWRITE(nVersion); 169 2225 : READWRITE(obj.vHave); 170 2225 : } 171 : 172 : void SetNull() 173 : { 174 : vHave.clear(); 175 : } 176 : 177 0 : bool IsNull() const 178 : { 179 0 : return vHave.empty(); 180 : } 181 : }; 182 : 183 : #endif // PIVX_PRIMITIVES_BLOCK_H