Skip to content

Commit abaf524

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#7815: Break circular dependency main ↔ txdb
99e7075 Break circular dependency main ↔ txdb (Wladimir J. van der Laan)
1 parent 2e54bd2 commit abaf524

File tree

5 files changed

+89
-90
lines changed

5 files changed

+89
-90
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/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 "validation.h"
1210
#include "pow.h"
1311
#include "uint256.h"
1412

@@ -305,7 +303,7 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
305303
return true;
306304
}
307305

308-
bool CBlockTreeDB::LoadBlockIndexGuts()
306+
bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex)
309307
{
310308
boost::scoped_ptr<CDBIterator> pcursor(NewIterator());
311309

@@ -319,8 +317,8 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
319317
CDiskBlockIndex diskindex;
320318
if (pcursor->GetValue(diskindex)) {
321319
// Construct block index object
322-
CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
323-
pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
320+
CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
321+
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
324322
pindexNew->nHeight = diskindex.nHeight;
325323
pindexNew->nFile = diskindex.nFile;
326324
pindexNew->nDataPos = diskindex.nDataPos;

src/txdb.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
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;
22+
class uint256;
23+
2024
struct CAddressUnspentKey;
2125
struct CAddressUnspentValue;
2226
struct CAddressIndexKey;
@@ -26,7 +30,6 @@ struct CTimestampIndexKey;
2630
struct CTimestampIndexIteratorKey;
2731
struct CSpentIndexKey;
2832
struct CSpentIndexValue;
29-
class uint256;
3033

3134
//! -dbcache default (MiB)
3235
static const int64_t nDefaultDbCache = 100;
@@ -35,7 +38,30 @@ static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
3538
//! min. -dbcache in (MiB)
3639
static const int64_t nMinDbCache = 4;
3740

38-
class CCoinsViewDBCursor;
41+
struct CDiskTxPos : public CDiskBlockPos
42+
{
43+
unsigned int nTxOffset; // after header
44+
45+
ADD_SERIALIZE_METHODS;
46+
47+
template <typename Stream, typename Operation>
48+
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
49+
READWRITE(*(CDiskBlockPos*)this);
50+
READWRITE(VARINT(nTxOffset));
51+
}
52+
53+
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
54+
}
55+
56+
CDiskTxPos() {
57+
SetNull();
58+
}
59+
60+
void SetNull() {
61+
CDiskBlockPos::SetNull();
62+
nTxOffset = 0;
63+
}
64+
};
3965

4066
/** CCoinsView backed by the coin database (chainstate/) */
4167
class CCoinsViewDB : public CCoinsView
@@ -104,7 +130,7 @@ class CBlockTreeDB : public CDBWrapper
104130
bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &vect);
105131
bool WriteFlag(const std::string &name, bool fValue);
106132
bool ReadFlag(const std::string &name, bool &fValue);
107-
bool LoadBlockIndexGuts();
133+
bool LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex);
108134
};
109135

110136
#endif // BITCOIN_TXDB_H

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3701,7 +3701,7 @@ CBlockIndex * InsertBlockIndex(uint256 hash)
37013701
bool static LoadBlockIndexDB()
37023702
{
37033703
const CChainParams& chainparams = Params();
3704-
if (!pblocktree->LoadBlockIndexGuts())
3704+
if (!pblocktree->LoadBlockIndexGuts(InsertBlockIndex))
37053705
return false;
37063706

37073707
boost::this_thread::interruption_point();

src/validation.h

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -583,30 +583,6 @@ struct CAddressIndexIteratorHeightKey {
583583
}
584584
};
585585

586-
struct CDiskTxPos : public CDiskBlockPos
587-
{
588-
unsigned int nTxOffset; // after header
589-
590-
ADD_SERIALIZE_METHODS;
591-
592-
template <typename Stream, typename Operation>
593-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
594-
READWRITE(*(CDiskBlockPos*)this);
595-
READWRITE(VARINT(nTxOffset));
596-
}
597-
598-
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
599-
}
600-
601-
CDiskTxPos() {
602-
SetNull();
603-
}
604-
605-
void SetNull() {
606-
CDiskBlockPos::SetNull();
607-
nTxOffset = 0;
608-
}
609-
};
610586

611587

612588
/**
@@ -752,61 +728,6 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
752728
/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
753729
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
754730

755-
756-
class CBlockFileInfo
757-
{
758-
public:
759-
unsigned int nBlocks; //! number of blocks stored in file
760-
unsigned int nSize; //! number of used bytes of block file
761-
unsigned int nUndoSize; //! number of used bytes in the undo file
762-
unsigned int nHeightFirst; //! lowest height of block in file
763-
unsigned int nHeightLast; //! highest height of block in file
764-
uint64_t nTimeFirst; //! earliest time of block in file
765-
uint64_t nTimeLast; //! latest time of block in file
766-
767-
ADD_SERIALIZE_METHODS;
768-
769-
template <typename Stream, typename Operation>
770-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
771-
READWRITE(VARINT(nBlocks));
772-
READWRITE(VARINT(nSize));
773-
READWRITE(VARINT(nUndoSize));
774-
READWRITE(VARINT(nHeightFirst));
775-
READWRITE(VARINT(nHeightLast));
776-
READWRITE(VARINT(nTimeFirst));
777-
READWRITE(VARINT(nTimeLast));
778-
}
779-
780-
void SetNull() {
781-
nBlocks = 0;
782-
nSize = 0;
783-
nUndoSize = 0;
784-
nHeightFirst = 0;
785-
nHeightLast = 0;
786-
nTimeFirst = 0;
787-
nTimeLast = 0;
788-
}
789-
790-
CBlockFileInfo() {
791-
SetNull();
792-
}
793-
794-
std::string ToString() const;
795-
796-
/** update statistics (does not update nSize) */
797-
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
798-
if (nBlocks==0 || nHeightFirst > nHeightIn)
799-
nHeightFirst = nHeightIn;
800-
if (nBlocks==0 || nTimeFirst > nTimeIn)
801-
nTimeFirst = nTimeIn;
802-
nBlocks++;
803-
if (nHeightIn > nHeightLast)
804-
nHeightLast = nHeightIn;
805-
if (nTimeIn > nTimeLast)
806-
nTimeLast = nTimeIn;
807-
}
808-
};
809-
810731
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
811732
class CVerifyDB {
812733
public:

0 commit comments

Comments
 (0)