Skip to content

Commit efee32f

Browse files
committed
Merge #7815: Break circular dependency main ↔ txdb
99e7075 Break circular dependency main ↔ txdb (Wladimir J. van der Laan)
2 parents 006cdf6 + 99e7075 commit efee32f

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
@@ -3705,7 +3705,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
37053705
bool static LoadBlockIndexDB()
37063706
{
37073707
const CChainParams& chainparams = Params();
3708-
if (!pblocktree->LoadBlockIndexGuts())
3708+
if (!pblocktree->LoadBlockIndexGuts(InsertBlockIndex))
37093709
return false;
37103710

37113711
boost::this_thread::interruption_point();

src/main.h

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -310,30 +310,6 @@ struct CNodeStateStats {
310310
std::vector<int> vHeightInFlight;
311311
};
312312

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

338314

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

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

@@ -173,7 +171,7 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
173171
return true;
174172
}
175173

176-
bool CBlockTreeDB::LoadBlockIndexGuts()
174+
bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex)
177175
{
178176
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
179177

@@ -187,8 +185,8 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
187185
CDiskBlockIndex diskindex;
188186
if (pcursor->GetValue(diskindex)) {
189187
// Construct block index object
190-
CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
191-
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
188+
CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
189+
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
192190
pindexNew->nHeight = diskindex.nHeight;
193191
pindexNew->nFile = diskindex.nFile;
194192
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)