LCOV - code coverage report
Current view: top level - src/primitives - block.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 27 54 50.0 %
Date: 2023-09-26 12:08:55 Functions: 31 88 35.2 %

          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       14490 :     CBlockHeader()
      33             :     {
      34       14490 :         SetNull();
      35       14490 :     }
      36             : 
      37      131274 :     SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
      38             : 
      39       26711 :     void SetNull()
      40             :     {
      41       26711 :         nVersion = 0;
      42       26711 :         hashPrevBlock.SetNull();
      43       26711 :         hashMerkleRoot.SetNull();
      44       26711 :         nTime = 0;
      45       26711 :         nBits = 0;
      46       26711 :         nNonce = 0;
      47       26711 :     }
      48             : 
      49           0 :     bool IsNull() const
      50             :     {
      51           0 :         return (nBits == 0);
      52             :     }
      53             : 
      54             :     uint256 GetHash() const;
      55             : 
      56        5259 :     NodeSeconds Time() const
      57             :     {
      58        5259 :         return NodeSeconds{std::chrono::seconds{nTime}};
      59             :     }
      60             : 
      61       20837 :     int64_t GetBlockTime() const
      62             :     {
      63       20837 :         return (int64_t)nTime;
      64             :     }
      65             : };
      66             : 
      67             : 
      68             : 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       12204 :     CBlock()
      78             :     {
      79       12204 :         SetNull();
      80       12204 :     }
      81             : 
      82           0 :     CBlock(const CBlockHeader &header)
      83             :     {
      84           0 :         SetNull();
      85           0 :         *(static_cast<CBlockHeader*>(this)) = header;
      86           0 :     }
      87             : 
      88       94677 :     SERIALIZE_METHODS(CBlock, obj)
      89             :     {
      90       31559 :         READWRITE(AsBase<CBlockHeader>(obj), obj.vtx);
      91       31559 :     }
      92             : 
      93       12221 :     void SetNull()
      94             :     {
      95       12221 :         CBlockHeader::SetNull();
      96       12221 :         vtx.clear();
      97       12221 :         fChecked = false;
      98       12221 :     }
      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             : 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