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 371538 : CBlockHeader() 37 1486152 : { 38 371538 : SetNull(); 39 371538 : } 40 : 41 2214843 : SERIALIZE_METHODS(CBlockHeader, obj) { 42 1773070 : READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); 43 : 44 : //zerocoin active, header changes to include accumulator checksum 45 1773070 : if(obj.nVersion > 3 && obj.nVersion < 7) 46 457475 : READWRITE(obj.nAccumulatorCheckpoint); 47 : 48 : // Sapling active 49 1773070 : if (obj.nVersion >= 8) 50 709540 : READWRITE(obj.hashFinalSaplingRoot); 51 1773070 : } 52 : 53 612713 : void SetNull() 54 : { 55 612713 : nVersion = CBlockHeader::CURRENT_VERSION; 56 612713 : hashPrevBlock.SetNull(); 57 612713 : hashMerkleRoot.SetNull(); 58 612713 : nTime = 0; 59 612713 : nBits = 0; 60 612713 : nNonce = 0; 61 612713 : nAccumulatorCheckpoint.SetNull(); 62 612713 : hashFinalSaplingRoot.SetNull(); 63 612713 : } 64 : 65 : bool IsNull() const 66 : { 67 : return (nBits == 0); 68 : } 69 : 70 : uint256 GetHash() const; 71 : 72 532730 : int64_t GetBlockTime() const 73 : { 74 532730 : 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 155843 : CBlock() 92 155843 : { 93 155843 : SetNull(); 94 155843 : } 95 : 96 : CBlock(const CBlockHeader &header) 97 : { 98 : SetNull(); 99 : *(static_cast<CBlockHeader*>(this)) = header; 100 : } 101 : 102 707110 : SERIALIZE_METHODS(CBlock, obj) 103 : { 104 445408 : READWRITEAS(CBlockHeader, obj); 105 445408 : READWRITE(obj.vtx); 106 445408 : if(obj.vtx.size() > 1 && obj.vtx[1]->IsCoinStake()) 107 67540 : READWRITE(obj.vchBlockSig); 108 445408 : } 109 : 110 241175 : void SetNull() 111 : { 112 241175 : CBlockHeader::SetNull(); 113 241175 : vtx.clear(); 114 241175 : fChecked = false; 115 241175 : vchBlockSig.clear(); 116 241175 : } 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 476026 : bool IsProofOfStake() const 135 : { 136 476026 : return (vtx.size() > 1 && vtx[1]->IsCoinStake()); 137 : } 138 : 139 128726 : bool IsProofOfWork() const 140 : { 141 128726 : 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 6235 : struct CBlockLocator 154 : { 155 : std::vector<uint256> vHave; 156 : 157 1846 : CBlockLocator() {} 158 : 159 1675 : explicit CBlockLocator(const std::vector<uint256>& vHaveIn) 160 1675 : { 161 1675 : vHave = vHaveIn; 162 1675 : } 163 : 164 7544 : SERIALIZE_METHODS(CBlockLocator, obj) 165 : { 166 3772 : int nVersion = s.GetVersion(); 167 3772 : if (!(s.GetType() & SER_GETHASH)) 168 3772 : READWRITE(nVersion); 169 3772 : READWRITE(obj.vHave); 170 3772 : } 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