LCOV - code coverage report
Current view: top level - src/primitives - block.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 29 56 51.8 %
Date: 2023-11-10 23:46:46 Functions: 30 88 34.1 %
Branches: 1 4 25.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 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.
       5                 :            : 
       6                 :            : #ifndef BITCOIN_PRIMITIVES_BLOCK_H
       7                 :            : #define BITCOIN_PRIMITIVES_BLOCK_H
       8                 :            : 
       9                 :            : #include <primitives/transaction.h>
      10                 :            : #include <serialize.h>
      11                 :            : #include <uint256.h>
      12                 :            : #include <util/time.h>
      13                 :            : 
      14                 :            : /** Nodes collect new transactions into a block, hash them into a hash tree,
      15                 :            :  * and scan through nonce values to make the block's hash satisfy proof-of-work
      16                 :            :  * requirements.  When they solve the proof-of-work, they broadcast the block
      17                 :            :  * to everyone and the block is added to the block chain.  The first transaction
      18                 :            :  * in the block is a special one that creates a new coin owned by the creator
      19                 :            :  * of the block.
      20                 :            :  */
      21                 :            : class CBlockHeader
      22                 :            : {
      23                 :            : public:
      24                 :            :     // header
      25                 :            :     int32_t nVersion;
      26                 :            :     uint256 hashPrevBlock;
      27                 :            :     uint256 hashMerkleRoot;
      28                 :            :     uint32_t nTime;
      29                 :            :     uint32_t nBits;
      30                 :            :     uint32_t nNonce;
      31                 :            : 
      32                 :        312 :     CBlockHeader()
      33                 :            :     {
      34                 :        312 :         SetNull();
      35                 :        312 :     }
      36                 :            : 
      37                 :      11268 :     SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
      38                 :            : 
      39                 :        776 :     void SetNull()
      40                 :            :     {
      41                 :        776 :         nVersion = 0;
      42                 :        776 :         hashPrevBlock.SetNull();
      43                 :        776 :         hashMerkleRoot.SetNull();
      44                 :        776 :         nTime = 0;
      45                 :        776 :         nBits = 0;
      46                 :        776 :         nNonce = 0;
      47                 :        776 :     }
      48                 :            : 
      49                 :        151 :     bool IsNull() const
      50                 :            :     {
      51                 :        151 :         return (nBits == 0);
      52                 :            :     }
      53                 :            : 
      54                 :            :     uint256 GetHash() const;
      55                 :            : 
      56                 :        300 :     NodeSeconds Time() const
      57                 :            :     {
      58                 :        300 :         return NodeSeconds{std::chrono::seconds{nTime}};
      59                 :            :     }
      60                 :            : 
      61                 :       1051 :     int64_t GetBlockTime() const
      62                 :            :     {
      63                 :       1051 :         return (int64_t)nTime;
      64                 :            :     }
      65                 :            : };
      66                 :            : 
      67                 :            : 
      68                 :          0 : class CBlock : public CBlockHeader
      69                 :            : {
      70                 :            : public:
      71                 :            :     // network and disk
      72                 :            :     std::vector<CTransactionRef> vtx;
      73                 :            : 
      74                 :            :     // memory only
      75                 :            :     mutable bool fChecked;
      76                 :            : 
      77                 :        312 :     CBlock()
      78                 :            :     {
      79         [ +  - ]:        312 :         SetNull();
      80                 :        312 :     }
      81                 :            : 
      82                 :          0 :     CBlock(const CBlockHeader &header)
      83                 :            :     {
      84         [ #  # ]:          0 :         SetNull();
      85                 :          0 :         *(static_cast<CBlockHeader*>(this)) = header;
      86                 :          0 :     }
      87                 :            : 
      88                 :       5868 :     SERIALIZE_METHODS(CBlock, obj)
      89                 :            :     {
      90                 :       1956 :         READWRITE(AsBase<CBlockHeader>(obj), obj.vtx);
      91                 :       1956 :     }
      92                 :            : 
      93                 :        464 :     void SetNull()
      94                 :            :     {
      95                 :        464 :         CBlockHeader::SetNull();
      96                 :        464 :         vtx.clear();
      97                 :        464 :         fChecked = false;
      98                 :        464 :     }
      99                 :            : 
     100                 :          0 :     CBlockHeader GetBlockHeader() const
     101                 :            :     {
     102                 :          0 :         CBlockHeader block;
     103                 :          0 :         block.nVersion       = nVersion;
     104                 :          0 :         block.hashPrevBlock  = hashPrevBlock;
     105                 :          0 :         block.hashMerkleRoot = hashMerkleRoot;
     106                 :          0 :         block.nTime          = nTime;
     107                 :          0 :         block.nBits          = nBits;
     108                 :          0 :         block.nNonce         = nNonce;
     109                 :          0 :         return block;
     110                 :            :     }
     111                 :            : 
     112                 :            :     std::string ToString() const;
     113                 :            : };
     114                 :            : 
     115                 :            : /** Describes a place in the block chain to another node such that if the
     116                 :            :  * other node doesn't have the same branch, it can find a recent common trunk.
     117                 :            :  * The further back it is, the further before the fork it may be.
     118                 :            :  */
     119                 :          0 : struct CBlockLocator
     120                 :            : {
     121                 :            :     /** Historically CBlockLocator's version field has been written to network
     122                 :            :      * streams as the negotiated protocol version and to disk streams as the
     123                 :            :      * client version, but the value has never been used.
     124                 :            :      *
     125                 :            :      * Hard-code to the highest protocol version ever written to a network stream.
     126                 :            :      * SerParams can be used if the field requires any meaning in the future,
     127                 :            :      **/
     128                 :            :     static constexpr int DUMMY_VERSION = 70016;
     129                 :            : 
     130                 :            :     std::vector<uint256> vHave;
     131                 :            : 
     132                 :          0 :     CBlockLocator() {}
     133                 :            : 
     134                 :          0 :     explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}
     135                 :            : 
     136                 :          0 :     SERIALIZE_METHODS(CBlockLocator, obj)
     137                 :            :     {
     138                 :          0 :         int nVersion = DUMMY_VERSION;
     139                 :          0 :         READWRITE(nVersion);
     140                 :          0 :         READWRITE(obj.vHave);
     141                 :          0 :     }
     142                 :            : 
     143                 :          0 :     void SetNull()
     144                 :            :     {
     145                 :          0 :         vHave.clear();
     146                 :          0 :     }
     147                 :            : 
     148                 :          0 :     bool IsNull() const
     149                 :            :     {
     150                 :          0 :         return vHave.empty();
     151                 :            :     }
     152                 :            : };
     153                 :            : 
     154                 :            : #endif // BITCOIN_PRIMITIVES_BLOCK_H

Generated by: LCOV version 1.14