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-04-02 01:23:23 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      386643 :     CBlockHeader()
      37     1546572 :     {
      38      386643 :         SetNull();
      39      386643 :     }
      40             : 
      41     2229821 :     SERIALIZE_METHODS(CBlockHeader, obj) {
      42     1776815 :         READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce);
      43             : 
      44             :         //zerocoin active, header changes to include accumulator checksum
      45     1776815 :         if(obj.nVersion > 3 && obj.nVersion < 7)
      46      498247 :             READWRITE(obj.nAccumulatorCheckpoint);
      47             : 
      48             :         // Sapling active
      49     1776815 :         if (obj.nVersion >= 8)
      50      672456 :             READWRITE(obj.hashFinalSaplingRoot);
      51     1776815 :     }
      52             : 
      53      618590 :     void SetNull()
      54             :     {
      55      618590 :         nVersion = CBlockHeader::CURRENT_VERSION;
      56      618590 :         hashPrevBlock.SetNull();
      57      618590 :         hashMerkleRoot.SetNull();
      58      618590 :         nTime = 0;
      59      618590 :         nBits = 0;
      60      618590 :         nNonce = 0;
      61      618590 :         nAccumulatorCheckpoint.SetNull();
      62      618590 :         hashFinalSaplingRoot.SetNull();
      63      618590 :     }
      64             : 
      65             :     bool IsNull() const
      66             :     {
      67             :         return (nBits == 0);
      68             :     }
      69             : 
      70             :     uint256 GetHash() const;
      71             : 
      72      510642 :     int64_t GetBlockTime() const
      73             :     {
      74      510642 :         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      149675 :     CBlock()
      92      149675 :     {
      93      149675 :         SetNull();
      94      149675 :     }
      95             : 
      96             :     CBlock(const CBlockHeader &header)
      97             :     {
      98             :         SetNull();
      99             :         *(static_cast<CBlockHeader*>(this)) = header;
     100             :     }
     101             : 
     102      683118 :     SERIALIZE_METHODS(CBlock, obj)
     103             :     {
     104      431505 :         READWRITEAS(CBlockHeader, obj);
     105      431505 :         READWRITE(obj.vtx);
     106      431505 :         if(obj.vtx.size() > 1 && obj.vtx[1]->IsCoinStake())
     107       60805 :             READWRITE(obj.vchBlockSig);
     108      431505 :     }
     109             : 
     110      231947 :     void SetNull()
     111             :     {
     112      231947 :         CBlockHeader::SetNull();
     113      231947 :         vtx.clear();
     114      231947 :         fChecked = false;
     115      231947 :         vchBlockSig.clear();
     116      231947 :     }
     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      464943 :     bool IsProofOfStake() const
     135             :     {
     136      464943 :         return (vtx.size() > 1 && vtx[1]->IsCoinStake());
     137             :     }
     138             : 
     139      124782 :     bool IsProofOfWork() const
     140             :     {
     141      124782 :         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        6259 : struct CBlockLocator
     154             : {
     155             :     std::vector<uint256> vHave;
     156             : 
     157        1852 :     CBlockLocator() {}
     158             : 
     159        1681 :     explicit CBlockLocator(const std::vector<uint256>& vHaveIn)
     160        1681 :     {
     161        1681 :         vHave = vHaveIn;
     162        1681 :     }
     163             : 
     164        7568 :     SERIALIZE_METHODS(CBlockLocator, obj)
     165             :     {
     166        3784 :         int nVersion = s.GetVersion();
     167        3784 :         if (!(s.GetType() & SER_GETHASH))
     168        3784 :             READWRITE(nVersion);
     169        3784 :         READWRITE(obj.vHave);
     170        3784 :     }
     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