66#ifndef BITCOIN_PRIMITIVES_BLOCK_H
77#define BITCOIN_PRIMITIVES_BLOCK_H
88
9+ class CBlockHeader ;
10+ class CBlock ;
11+ class ImmutableBlockHeader ;
12+ class ImmutableBlock ;
13+
14+ #include < consensus/merkle.h>
915#include < primitives/transaction.h>
1016#include < serialize.h>
1117#include < uint256.h>
@@ -34,6 +40,8 @@ class CBlockHeader
3440 SetNull ();
3541 }
3642
43+ CBlockHeader (const ImmutableBlockHeader &header);
44+
3745 SERIALIZE_METHODS (CBlockHeader, obj) { READWRITE (obj.nVersion , obj.hashPrevBlock , obj.hashMerkleRoot , obj.nTime , obj.nBits , obj.nNonce ); }
3846
3947 void SetNull ()
@@ -64,6 +72,66 @@ class CBlockHeader
6472 }
6573};
6674
75+ class ImmutableBlockHeader
76+ {
77+ public:
78+ const int32_t nVersion;
79+ const uint256 hashPrevBlock;
80+ const uint256 hashMerkleRoot;
81+ const uint32_t nTime;
82+ const uint32_t nBits;
83+ const uint32_t nNonce;
84+ private:
85+ mutable std::optional<uint256> hash;
86+ public:
87+ /* * Convert a CBlockHeader into a ImmutableBlockHeader. */
88+ explicit ImmutableBlockHeader (const CBlockHeader& header);
89+
90+ uint256 GetHash () const
91+ {
92+ if (!hash.has_value ())
93+ hash = CBlockHeader (*this ).GetHash ();
94+ return *hash;
95+ }
96+
97+ NodeSeconds Time () const
98+ {
99+ return NodeSeconds{std::chrono::seconds{nTime}};
100+ }
101+
102+ int64_t GetBlockTime () const
103+ {
104+ return (int64_t )nTime;
105+ }
106+ };
107+
108+ class ImmutableBlock : public ImmutableBlockHeader
109+ {
110+ public:
111+ const std::vector<CTransactionRef> vtx;
112+
113+ // Memory-only flags for caching expensive checks
114+ mutable bool fChecked ; // CheckBlock()
115+ mutable bool m_checked_witness_commitment{false }; // CheckWitnessCommitment()
116+ mutable bool m_checked_merkle_root{false }; // CheckMerkleRoot()
117+
118+ explicit ImmutableBlock (const CBlock& block);
119+ explicit ImmutableBlock (CBlock&& block);
120+
121+ CBlockHeader GetBlockHeader () const
122+ {
123+ CBlockHeader block;
124+ block.nVersion = nVersion;
125+ block.hashPrevBlock = hashPrevBlock;
126+ block.hashMerkleRoot = hashMerkleRoot;
127+ block.nTime = nTime;
128+ block.nBits = nBits;
129+ block.nNonce = nNonce;
130+ return block;
131+ }
132+
133+ std::string ToString () const ;
134+ };
67135
68136class CBlock : public CBlockHeader
69137{
@@ -81,6 +149,8 @@ class CBlock : public CBlockHeader
81149 SetNull ();
82150 }
83151
152+ CBlock (const ImmutableBlock &block);
153+
84154 CBlock (const CBlockHeader &header)
85155 {
86156 SetNull ();
0 commit comments