Skip to content

Commit fa65111

Browse files
author
MarcoFalke
committed
move-only: Move CBlockTreeDB to node/blockstorage
The block index (CBlockTreeDB) is required to write and read blocks, so move it to blockstorage. This allows to drop the txdb.h include from `node/blockstorage.h`. Can be reviewed with: --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent fa86855 commit fa65111

File tree

6 files changed

+132
-132
lines changed

6 files changed

+132
-132
lines changed

src/node/blockstorage.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chain.h>
88
#include <clientversion.h>
99
#include <consensus/validation.h>
10+
#include <dbwrapper.h>
1011
#include <flatfile.h>
1112
#include <hash.h>
1213
#include <kernel/chainparams.h>
@@ -15,15 +16,116 @@
1516
#include <reverse_iterator.h>
1617
#include <signet.h>
1718
#include <streams.h>
19+
#include <sync.h>
1820
#include <undo.h>
1921
#include <util/batchpriority.h>
2022
#include <util/fs.h>
2123
#include <util/signalinterrupt.h>
24+
#include <util/translation.h>
2225
#include <validation.h>
2326

2427
#include <map>
2528
#include <unordered_map>
2629

30+
namespace kernel {
31+
static constexpr uint8_t DB_BLOCK_FILES{'f'};
32+
static constexpr uint8_t DB_BLOCK_INDEX{'b'};
33+
static constexpr uint8_t DB_FLAG{'F'};
34+
static constexpr uint8_t DB_REINDEX_FLAG{'R'};
35+
static constexpr uint8_t DB_LAST_BLOCK{'l'};
36+
// Keys used in previous version that might still be found in the DB:
37+
// CBlockTreeDB::DB_TXINDEX_BLOCK{'T'};
38+
// CBlockTreeDB::DB_TXINDEX{'t'}
39+
// CBlockTreeDB::ReadFlag("txindex")
40+
41+
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
42+
return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
43+
}
44+
45+
bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
46+
if (fReindexing)
47+
return Write(DB_REINDEX_FLAG, uint8_t{'1'});
48+
else
49+
return Erase(DB_REINDEX_FLAG);
50+
}
51+
52+
void CBlockTreeDB::ReadReindexing(bool &fReindexing) {
53+
fReindexing = Exists(DB_REINDEX_FLAG);
54+
}
55+
56+
bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
57+
return Read(DB_LAST_BLOCK, nFile);
58+
}
59+
60+
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
61+
CDBBatch batch(*this);
62+
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
63+
batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
64+
}
65+
batch.Write(DB_LAST_BLOCK, nLastFile);
66+
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
67+
batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
68+
}
69+
return WriteBatch(batch, true);
70+
}
71+
72+
bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
73+
return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
74+
}
75+
76+
bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
77+
uint8_t ch;
78+
if (!Read(std::make_pair(DB_FLAG, name), ch))
79+
return false;
80+
fValue = ch == uint8_t{'1'};
81+
return true;
82+
}
83+
84+
bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
85+
{
86+
AssertLockHeld(::cs_main);
87+
std::unique_ptr<CDBIterator> pcursor(NewIterator());
88+
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
89+
90+
// Load m_block_index
91+
while (pcursor->Valid()) {
92+
if (interrupt) return false;
93+
std::pair<uint8_t, uint256> key;
94+
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
95+
CDiskBlockIndex diskindex;
96+
if (pcursor->GetValue(diskindex)) {
97+
// Construct block index object
98+
CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
99+
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
100+
pindexNew->nHeight = diskindex.nHeight;
101+
pindexNew->nFile = diskindex.nFile;
102+
pindexNew->nDataPos = diskindex.nDataPos;
103+
pindexNew->nUndoPos = diskindex.nUndoPos;
104+
pindexNew->nVersion = diskindex.nVersion;
105+
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
106+
pindexNew->nTime = diskindex.nTime;
107+
pindexNew->nBits = diskindex.nBits;
108+
pindexNew->nNonce = diskindex.nNonce;
109+
pindexNew->nStatus = diskindex.nStatus;
110+
pindexNew->nTx = diskindex.nTx;
111+
112+
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
113+
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
114+
}
115+
116+
pcursor->Next();
117+
} else {
118+
return error("%s: failed to read value", __func__);
119+
}
120+
} else {
121+
break;
122+
}
123+
}
124+
125+
return true;
126+
}
127+
} // namespace kernel
128+
27129
namespace node {
28130
std::atomic_bool fReindex(false);
29131

src/node/blockstorage.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@
77

88
#include <attributes.h>
99
#include <chain.h>
10+
#include <dbwrapper.h>
1011
#include <kernel/blockmanager_opts.h>
1112
#include <kernel/chainparams.h>
1213
#include <kernel/cs_main.h>
1314
#include <protocol.h>
1415
#include <sync.h>
15-
#include <txdb.h>
1616
#include <util/fs.h>
17+
#include <util/hasher.h>
1718

1819
#include <atomic>
1920
#include <cstdint>
21+
#include <functional>
22+
#include <limits>
23+
#include <map>
24+
#include <memory>
25+
#include <set>
26+
#include <string>
2027
#include <unordered_map>
28+
#include <utility>
2129
#include <vector>
2230

2331
class BlockValidationState;
@@ -36,7 +44,26 @@ namespace util {
3644
class SignalInterrupt;
3745
} // namespace util
3846

47+
namespace kernel {
48+
/** Access to the block database (blocks/index/) */
49+
class CBlockTreeDB : public CDBWrapper
50+
{
51+
public:
52+
using CDBWrapper::CDBWrapper;
53+
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*>>& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
54+
bool ReadBlockFileInfo(int nFile, CBlockFileInfo& info);
55+
bool ReadLastBlockFile(int& nFile);
56+
bool WriteReindexing(bool fReindexing);
57+
void ReadReindexing(bool& fReindexing);
58+
bool WriteFlag(const std::string& name, bool fValue);
59+
bool ReadFlag(const std::string& name, bool& fValue);
60+
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
61+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
62+
};
63+
} // namespace kernel
64+
3965
namespace node {
66+
using kernel::CBlockTreeDB;
4067

4168
/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */
4269
static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB

src/test/util/setup_common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include <functional>
6464
#include <stdexcept>
6565

66+
using kernel::CBlockTreeDB;
6667
using kernel::ValidationCacheSizes;
6768
using node::ApplyArgsManOptions;
6869
using node::BlockAssembler;

src/txdb.cpp

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,25 @@
55

66
#include <txdb.h>
77

8-
#include <chain.h>
98
#include <coins.h>
109
#include <dbwrapper.h>
11-
#include <kernel/cs_main.h>
1210
#include <logging.h>
13-
#include <pow.h>
1411
#include <primitives/transaction.h>
1512
#include <random.h>
1613
#include <serialize.h>
17-
#include <sync.h>
1814
#include <uint256.h>
19-
#include <util/signalinterrupt.h>
20-
#include <util/translation.h>
2115
#include <util/vector.h>
2216

2317
#include <cassert>
2418
#include <cstdlib>
2519
#include <iterator>
20+
#include <utility>
2621

2722
static constexpr uint8_t DB_COIN{'C'};
28-
static constexpr uint8_t DB_BLOCK_FILES{'f'};
29-
static constexpr uint8_t DB_BLOCK_INDEX{'b'};
30-
3123
static constexpr uint8_t DB_BEST_BLOCK{'B'};
3224
static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
33-
static constexpr uint8_t DB_FLAG{'F'};
34-
static constexpr uint8_t DB_REINDEX_FLAG{'R'};
35-
static constexpr uint8_t DB_LAST_BLOCK{'l'};
36-
3725
// Keys used in previous version that might still be found in the DB:
3826
static constexpr uint8_t DB_COINS{'c'};
39-
// CBlockTreeDB::DB_TXINDEX_BLOCK{'T'};
40-
// CBlockTreeDB::DB_TXINDEX{'t'}
41-
// CBlockTreeDB::ReadFlag("txindex")
4227

4328
bool CCoinsViewDB::NeedsUpgrade()
4429
{
@@ -166,25 +151,6 @@ size_t CCoinsViewDB::EstimateSize() const
166151
return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
167152
}
168153

169-
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
170-
return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
171-
}
172-
173-
bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
174-
if (fReindexing)
175-
return Write(DB_REINDEX_FLAG, uint8_t{'1'});
176-
else
177-
return Erase(DB_REINDEX_FLAG);
178-
}
179-
180-
void CBlockTreeDB::ReadReindexing(bool &fReindexing) {
181-
fReindexing = Exists(DB_REINDEX_FLAG);
182-
}
183-
184-
bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
185-
return Read(DB_LAST_BLOCK, nFile);
186-
}
187-
188154
/** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
189155
class CCoinsViewDBCursor: public CCoinsViewCursor
190156
{
@@ -257,71 +223,3 @@ void CCoinsViewDBCursor::Next()
257223
keyTmp.first = entry.key;
258224
}
259225
}
260-
261-
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
262-
CDBBatch batch(*this);
263-
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
264-
batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
265-
}
266-
batch.Write(DB_LAST_BLOCK, nLastFile);
267-
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
268-
batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
269-
}
270-
return WriteBatch(batch, true);
271-
}
272-
273-
bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
274-
return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
275-
}
276-
277-
bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
278-
uint8_t ch;
279-
if (!Read(std::make_pair(DB_FLAG, name), ch))
280-
return false;
281-
fValue = ch == uint8_t{'1'};
282-
return true;
283-
}
284-
285-
bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
286-
{
287-
AssertLockHeld(::cs_main);
288-
std::unique_ptr<CDBIterator> pcursor(NewIterator());
289-
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
290-
291-
// Load m_block_index
292-
while (pcursor->Valid()) {
293-
if (interrupt) return false;
294-
std::pair<uint8_t, uint256> key;
295-
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
296-
CDiskBlockIndex diskindex;
297-
if (pcursor->GetValue(diskindex)) {
298-
// Construct block index object
299-
CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
300-
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
301-
pindexNew->nHeight = diskindex.nHeight;
302-
pindexNew->nFile = diskindex.nFile;
303-
pindexNew->nDataPos = diskindex.nDataPos;
304-
pindexNew->nUndoPos = diskindex.nUndoPos;
305-
pindexNew->nVersion = diskindex.nVersion;
306-
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
307-
pindexNew->nTime = diskindex.nTime;
308-
pindexNew->nBits = diskindex.nBits;
309-
pindexNew->nNonce = diskindex.nNonce;
310-
pindexNew->nStatus = diskindex.nStatus;
311-
pindexNew->nTx = diskindex.nTx;
312-
313-
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
314-
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
315-
}
316-
317-
pcursor->Next();
318-
} else {
319-
return error("%s: failed to read value", __func__);
320-
}
321-
} else {
322-
break;
323-
}
324-
}
325-
326-
return true;
327-
}

src/txdb.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,12 @@
1414

1515
#include <cstddef>
1616
#include <cstdint>
17-
#include <functional>
1817
#include <memory>
1918
#include <optional>
20-
#include <string>
21-
#include <utility>
2219
#include <vector>
2320

24-
class CBlockFileInfo;
25-
class CBlockIndex;
2621
class COutPoint;
2722
class uint256;
28-
namespace Consensus {
29-
struct Params;
30-
};
31-
namespace util {
32-
class SignalInterrupt;
33-
} // namespace util
3423

3524
//! -dbcache default (MiB)
3625
static const int64_t nDefaultDbCache = 450;
@@ -88,20 +77,4 @@ class CCoinsViewDB final : public CCoinsView
8877
std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
8978
};
9079

91-
/** Access to the block database (blocks/index/) */
92-
class CBlockTreeDB : public CDBWrapper
93-
{
94-
public:
95-
using CDBWrapper::CDBWrapper;
96-
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
97-
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
98-
bool ReadLastBlockFile(int &nFile);
99-
bool WriteReindexing(bool fReindexing);
100-
void ReadReindexing(bool &fReindexing);
101-
bool WriteFlag(const std::string &name, bool fValue);
102-
bool ReadFlag(const std::string &name, bool &fValue);
103-
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
104-
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
105-
};
106-
10780
#endif // BITCOIN_TXDB_H

src/validation.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#include <vector>
4848

4949
class Chainstate;
50-
class CBlockTreeDB;
5150
class CTxMemPool;
5251
class ChainstateManager;
5352
struct ChainTxData;

0 commit comments

Comments
 (0)