Skip to content

Commit f9253ae

Browse files
committed
blockstorage: Move CBlockFileInfo to blocktreestorage
This eliminates the circular dependency between the blockstorage and the blocktreestorage modules again.
1 parent e38b6f1 commit f9253ae

5 files changed

Lines changed: 50 additions & 51 deletions

File tree

src/kernel/blocktreestorage.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#include <chain.h>
88
#include <crc32c/include/crc32c/crc32c.h>
9+
#include <crypto/common.h>
910
#include <kernel/cs_main.h>
1011
#include <logging.h>
11-
#include <node/blockstorage.h>
1212
#include <pow.h>
1313
#include <serialize.h>
1414
#include <span.h>
@@ -87,6 +87,11 @@ struct BlockFileInfoWrapper : CBlockFileInfo {
8787
}
8888
};
8989

90+
std::string CBlockFileInfo::ToString() const
91+
{
92+
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
93+
}
94+
9095
static FilePosition CalculateBlockFileInfoPosition(int file_index)
9196
{
9297
assert(file_index >= 0);

src/kernel/blocktreestorage.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <kernel/cs_main.h>
99
#include <serialize.h>
10-
#include <streams.h>
1110
#include <sync.h>
1211
#include <util/fs.h>
1312

@@ -30,8 +29,6 @@ class SignalInterrupt;
3029

3130
namespace kernel {
3231

33-
class CBlockFileInfo;
34-
3532
// Checksums are calculated from the serialized value and its position in the
3633
// file. This protects against out of order data and allows the same checksum
3734
// from the log file record to be used for the actual data files.
@@ -81,6 +78,50 @@ class BlockTreeStoreError : public std::runtime_error
8178
explicit BlockTreeStoreError(const std::string& msg) : std::runtime_error(msg) {}
8279
};
8380

81+
class CBlockFileInfo
82+
{
83+
public:
84+
uint32_t nBlocks{}; //!< number of blocks stored in file
85+
uint32_t nSize{}; //!< number of used bytes of block file
86+
uint32_t nUndoSize{}; //!< number of used bytes in the undo file
87+
uint32_t nHeightFirst{}; //!< lowest height of block in file
88+
uint32_t nHeightLast{}; //!< highest height of block in file
89+
uint64_t nTimeFirst{}; //!< earliest time of block in file
90+
uint64_t nTimeLast{}; //!< latest time of block in file
91+
92+
// Note: The SERIALIZE_METHODS here use VARINT encoding for compatibility with
93+
// the legacy leveldb block tree db, used during migration in CreateAndMigrateBlockTree.
94+
// BlockFileInfoWrapper uses fixed-width encoding for the new flat file storage.
95+
SERIALIZE_METHODS(CBlockFileInfo, obj)
96+
{
97+
READWRITE(VARINT(obj.nBlocks));
98+
READWRITE(VARINT(obj.nSize));
99+
READWRITE(VARINT(obj.nUndoSize));
100+
READWRITE(VARINT(obj.nHeightFirst));
101+
READWRITE(VARINT(obj.nHeightLast));
102+
READWRITE(VARINT(obj.nTimeFirst));
103+
READWRITE(VARINT(obj.nTimeLast));
104+
}
105+
106+
CBlockFileInfo() = default;
107+
108+
std::string ToString() const;
109+
110+
/** update statistics (does not update nSize) */
111+
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
112+
{
113+
if (nBlocks == 0 || nHeightFirst > nHeightIn)
114+
nHeightFirst = nHeightIn;
115+
if (nBlocks == 0 || nTimeFirst > nTimeIn)
116+
nTimeFirst = nTimeIn;
117+
nBlocks++;
118+
if (nHeightIn > nHeightLast)
119+
nHeightLast = nHeightIn;
120+
if (nTimeIn > nTimeLast)
121+
nTimeLast = nTimeIn;
122+
}
123+
};
124+
84125
class BlockTreeStore
85126
{
86127
public:

src/node/blockstorage.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <util/signalinterrupt.h>
3939
#include <util/strencodings.h>
4040
#include <util/syserror.h>
41-
#include <util/time.h>
4241
#include <util/translation.h>
4342
#include <validation.h>
4443

@@ -137,10 +136,6 @@ bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, s
137136
return true;
138137
}
139138

140-
std::string CBlockFileInfo::ToString() const
141-
{
142-
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
143-
}
144139
} // namespace kernel
145140

146141
namespace node {

src/node/blockstorage.h

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <kernel/cs_main.h>
1616
#include <kernel/messagestartchars.h>
1717
#include <primitives/block.h>
18-
#include <serialize.h>
1918
#include <streams.h>
2019
#include <sync.h>
2120
#include <uint256.h>
@@ -57,46 +56,6 @@ class SignalInterrupt;
5756
} // namespace util
5857

5958
namespace kernel {
60-
class CBlockFileInfo
61-
{
62-
public:
63-
uint32_t nBlocks{}; //!< number of blocks stored in file
64-
uint32_t nSize{}; //!< number of used bytes of block file
65-
uint32_t nUndoSize{}; //!< number of used bytes in the undo file
66-
uint32_t nHeightFirst{}; //!< lowest height of block in file
67-
uint32_t nHeightLast{}; //!< highest height of block in file
68-
uint64_t nTimeFirst{}; //!< earliest time of block in file
69-
uint64_t nTimeLast{}; //!< latest time of block in file
70-
71-
SERIALIZE_METHODS(CBlockFileInfo, obj)
72-
{
73-
READWRITE(VARINT(obj.nBlocks));
74-
READWRITE(VARINT(obj.nSize));
75-
READWRITE(VARINT(obj.nUndoSize));
76-
READWRITE(VARINT(obj.nHeightFirst));
77-
READWRITE(VARINT(obj.nHeightLast));
78-
READWRITE(VARINT(obj.nTimeFirst));
79-
READWRITE(VARINT(obj.nTimeLast));
80-
}
81-
82-
CBlockFileInfo() = default;
83-
84-
std::string ToString() const;
85-
86-
/** update statistics (does not update nSize) */
87-
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
88-
{
89-
if (nBlocks == 0 || nHeightFirst > nHeightIn)
90-
nHeightFirst = nHeightIn;
91-
if (nBlocks == 0 || nTimeFirst > nTimeIn)
92-
nTimeFirst = nTimeIn;
93-
nBlocks++;
94-
if (nHeightIn > nHeightLast)
95-
nHeightLast = nHeightIn;
96-
if (nTimeIn > nTimeLast)
97-
nTimeLast = nTimeIn;
98-
}
99-
};
10059

10160
/** Access to the legacy block database (blocks/index/) used during migration*/
10261
class BlockTreeDB : public CDBWrapper

test/lint/lint-circular-dependencies.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"wallet/wallet -> wallet/walletdb -> wallet/wallet",
2323
"kernel/coinstats -> validation -> kernel/coinstats",
2424
"versionbits -> versionbits_impl -> versionbits",
25-
"kernel/blocktreestorage -> node/blockstorage -> kernel/blocktreestorage",
2625

2726
# Temporary, removed in followup https://github.com/bitcoin/bitcoin/pull/24230
2827
"index/base -> node/context -> net_processing -> index/blockfilterindex -> index/base",

0 commit comments

Comments
 (0)