LCOV - code coverage report
Current view: top level - src/primitives - block.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 67 71 94.4 %
Date: 2025-02-23 09:33:43 Functions: 25 25 100.0 %

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

Generated by: LCOV version 1.14