Skip to content

Commit 1f53701

Browse files
committed
immutableblockheader/immutableblock
1 parent 83a2216 commit 1f53701

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

src/primitives/block.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,25 @@
88
#include <hash.h>
99
#include <tinyformat.h>
1010

11+
CBlockHeader::CBlockHeader(const ImmutableBlockHeader &header) :
12+
nVersion(header.nVersion),
13+
hashPrevBlock(header.hashPrevBlock),
14+
hashMerkleRoot(header.hashMerkleRoot),
15+
nTime(header.nTime),
16+
nBits(header.nBits),
17+
nNonce(header.nNonce)
18+
{}
19+
1120
uint256 CBlockHeader::GetHash() const
1221
{
1322
return (HashWriter{} << *this).GetHash();
1423
}
1524

25+
CBlock::CBlock(const ImmutableBlock &block) :
26+
CBlockHeader(block),
27+
vtx(block.vtx)
28+
{}
29+
1630
std::string CBlock::ToString() const
1731
{
1832
std::stringstream s;
@@ -28,3 +42,34 @@ std::string CBlock::ToString() const
2842
}
2943
return s.str();
3044
}
45+
46+
ImmutableBlockHeader::ImmutableBlockHeader(const CBlockHeader& header) :
47+
nVersion(header.nVersion),
48+
hashPrevBlock(header.hashPrevBlock),
49+
hashMerkleRoot(header.hashMerkleRoot),
50+
nTime(header.nTime),
51+
nBits(header.nBits),
52+
nNonce(header.nNonce)
53+
{}
54+
55+
ImmutableBlock::ImmutableBlock(const CBlock& block) :
56+
ImmutableBlockHeader(block),
57+
vtx(block.vtx)
58+
{
59+
}
60+
61+
std::string ImmutableBlock::ToString() const
62+
{
63+
std::stringstream s;
64+
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
65+
GetHash().ToString(),
66+
nVersion,
67+
hashPrevBlock.ToString(),
68+
hashMerkleRoot.ToString(),
69+
nTime, nBits, nNonce,
70+
vtx.size());
71+
for (const auto& tx : vtx) {
72+
s << " " << tx->ToString() << "\n";
73+
}
74+
return s.str();
75+
}

src/primitives/block.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
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

68136
class 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

Comments
 (0)