Skip to content

Commit 99e7075

Browse files
committed
Break circular dependency main ↔ txdb
Break the circular dependency between main and txdb by: - Moving `CBlockFileInfo` from `main.h` to `chain.h`. I think this makes sense, as the other block-file stuff is there too. - Moving `CDiskTxPos` from `main.h` to `txdb.h`. This type seems specific to txdb. - Pass a functor `insertBlockIndex` to `LoadBlockIndexGuts`. This leaves it up to the caller how to insert block indices.
1 parent 73fc922 commit 99e7075

File tree

5 files changed

+87
-89
lines changed

5 files changed

+87
-89
lines changed

src/chain.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,60 @@
1414

1515
#include <vector>
1616

17+
class CBlockFileInfo
18+
{
19+
public:
20+
unsigned int nBlocks; //!< number of blocks stored in file
21+
unsigned int nSize; //!< number of used bytes of block file
22+
unsigned int nUndoSize; //!< number of used bytes in the undo file
23+
unsigned int nHeightFirst; //!< lowest height of block in file
24+
unsigned int nHeightLast; //!< highest height of block in file
25+
uint64_t nTimeFirst; //!< earliest time of block in file
26+
uint64_t nTimeLast; //!< latest time of block in file
27+
28+
ADD_SERIALIZE_METHODS;
29+
30+
template <typename Stream, typename Operation>
31+
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
32+
READWRITE(VARINT(nBlocks));
33+
READWRITE(VARINT(nSize));
34+
READWRITE(VARINT(nUndoSize));
35+
READWRITE(VARINT(nHeightFirst));
36+
READWRITE(VARINT(nHeightLast));
37+
READWRITE(VARINT(nTimeFirst));
38+
READWRITE(VARINT(nTimeLast));
39+
}
40+
41+
void SetNull() {
42+
nBlocks = 0;
43+
nSize = 0;
44+
nUndoSize = 0;
45+
nHeightFirst = 0;
46+
nHeightLast = 0;
47+
nTimeFirst = 0;
48+
nTimeLast = 0;
49+
}
50+
51+
CBlockFileInfo() {
52+
SetNull();
53+
}
54+
55+
std::string ToString() const;
56+
57+
/** update statistics (does not update nSize) */
58+
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
59+
if (nBlocks==0 || nHeightFirst > nHeightIn)
60+
nHeightFirst = nHeightIn;
61+
if (nBlocks==0 || nTimeFirst > nTimeIn)
62+
nTimeFirst = nTimeIn;
63+
nBlocks++;
64+
if (nHeightIn > nHeightLast)
65+
nHeightLast = nHeightIn;
66+
if (nTimeIn > nTimeLast)
67+
nTimeLast = nTimeIn;
68+
}
69+
};
70+
1771
struct CDiskBlockPos
1872
{
1973
int nFile;

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3706,7 +3706,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
37063706
bool static LoadBlockIndexDB()
37073707
{
37083708
const CChainParams& chainparams = Params();
3709-
if (!pblocktree->LoadBlockIndexGuts())
3709+
if (!pblocktree->LoadBlockIndexGuts(InsertBlockIndex))
37103710
return false;
37113711

37123712
boost::this_thread::interruption_point();

src/main.h

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -307,30 +307,6 @@ struct CNodeStateStats {
307307
std::vector<int> vHeightInFlight;
308308
};
309309

310-
struct CDiskTxPos : public CDiskBlockPos
311-
{
312-
unsigned int nTxOffset; // after header
313-
314-
ADD_SERIALIZE_METHODS;
315-
316-
template <typename Stream, typename Operation>
317-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
318-
READWRITE(*(CDiskBlockPos*)this);
319-
READWRITE(VARINT(nTxOffset));
320-
}
321-
322-
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
323-
}
324-
325-
CDiskTxPos() {
326-
SetNull();
327-
}
328-
329-
void SetNull() {
330-
CDiskBlockPos::SetNull();
331-
nTxOffset = 0;
332-
}
333-
};
334310

335311

336312
/**
@@ -469,61 +445,6 @@ bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockI
469445
/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
470446
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
471447

472-
473-
class CBlockFileInfo
474-
{
475-
public:
476-
unsigned int nBlocks; //!< number of blocks stored in file
477-
unsigned int nSize; //!< number of used bytes of block file
478-
unsigned int nUndoSize; //!< number of used bytes in the undo file
479-
unsigned int nHeightFirst; //!< lowest height of block in file
480-
unsigned int nHeightLast; //!< highest height of block in file
481-
uint64_t nTimeFirst; //!< earliest time of block in file
482-
uint64_t nTimeLast; //!< latest time of block in file
483-
484-
ADD_SERIALIZE_METHODS;
485-
486-
template <typename Stream, typename Operation>
487-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
488-
READWRITE(VARINT(nBlocks));
489-
READWRITE(VARINT(nSize));
490-
READWRITE(VARINT(nUndoSize));
491-
READWRITE(VARINT(nHeightFirst));
492-
READWRITE(VARINT(nHeightLast));
493-
READWRITE(VARINT(nTimeFirst));
494-
READWRITE(VARINT(nTimeLast));
495-
}
496-
497-
void SetNull() {
498-
nBlocks = 0;
499-
nSize = 0;
500-
nUndoSize = 0;
501-
nHeightFirst = 0;
502-
nHeightLast = 0;
503-
nTimeFirst = 0;
504-
nTimeLast = 0;
505-
}
506-
507-
CBlockFileInfo() {
508-
SetNull();
509-
}
510-
511-
std::string ToString() const;
512-
513-
/** update statistics (does not update nSize) */
514-
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
515-
if (nBlocks==0 || nHeightFirst > nHeightIn)
516-
nHeightFirst = nHeightIn;
517-
if (nBlocks==0 || nTimeFirst > nTimeIn)
518-
nTimeFirst = nTimeIn;
519-
nBlocks++;
520-
if (nHeightIn > nHeightLast)
521-
nHeightLast = nHeightIn;
522-
if (nTimeIn > nTimeLast)
523-
nTimeLast = nTimeIn;
524-
}
525-
};
526-
527448
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
528449
class CVerifyDB {
529450
public:

src/txdb.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
#include "txdb.h"
77

8-
#include "chain.h"
98
#include "chainparams.h"
109
#include "hash.h"
11-
#include "main.h"
1210
#include "pow.h"
1311
#include "uint256.h"
1412

@@ -177,7 +175,7 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
177175
return true;
178176
}
179177

180-
bool CBlockTreeDB::LoadBlockIndexGuts()
178+
bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex)
181179
{
182180
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
183181

@@ -191,8 +189,8 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
191189
CDiskBlockIndex diskindex;
192190
if (pcursor->GetValue(diskindex)) {
193191
// Construct block index object
194-
CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
195-
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
192+
CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
193+
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
196194
pindexNew->nHeight = diskindex.nHeight;
197195
pindexNew->nFile = diskindex.nFile;
198196
pindexNew->nDataPos = diskindex.nDataPos;

src/txdb.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
#include "coins.h"
1010
#include "dbwrapper.h"
11+
#include "chain.h"
1112

1213
#include <map>
1314
#include <string>
1415
#include <utility>
1516
#include <vector>
1617

17-
class CBlockFileInfo;
18+
#include <boost/function.hpp>
19+
1820
class CBlockIndex;
19-
struct CDiskTxPos;
21+
class CCoinsViewDBCursor;
2022
class uint256;
2123

2224
//! -dbcache default (MiB)
@@ -26,7 +28,30 @@ static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
2628
//! min. -dbcache in (MiB)
2729
static const int64_t nMinDbCache = 4;
2830

29-
class CCoinsViewDBCursor;
31+
struct CDiskTxPos : public CDiskBlockPos
32+
{
33+
unsigned int nTxOffset; // after header
34+
35+
ADD_SERIALIZE_METHODS;
36+
37+
template <typename Stream, typename Operation>
38+
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
39+
READWRITE(*(CDiskBlockPos*)this);
40+
READWRITE(VARINT(nTxOffset));
41+
}
42+
43+
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
44+
}
45+
46+
CDiskTxPos() {
47+
SetNull();
48+
}
49+
50+
void SetNull() {
51+
CDiskBlockPos::SetNull();
52+
nTxOffset = 0;
53+
}
54+
};
3055

3156
/** CCoinsView backed by the coin database (chainstate/) */
3257
class CCoinsViewDB : public CCoinsView
@@ -83,7 +108,7 @@ class CBlockTreeDB : public CDBWrapper
83108
bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
84109
bool WriteFlag(const std::string &name, bool fValue);
85110
bool ReadFlag(const std::string &name, bool &fValue);
86-
bool LoadBlockIndexGuts();
111+
bool LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex);
87112
};
88113

89114
#endif // BITCOIN_TXDB_H

0 commit comments

Comments
 (0)