Skip to content

Commit ecab855

Browse files
committed
Merge bitcoin/bitcoin#28195: blockstorage: Drop legacy -txindex check
fae4055 scripted-diff: Rename CBlockTreeDB -> BlockTreeDB (MarcoFalke) faf6303 Fixup style of moved code (MarcoFalke) fa65111 move-only: Move CBlockTreeDB to node/blockstorage (MarcoFalke) fa86855 index: Drop legacy -txindex check (MarcoFalke) fa69148 scripted-diff: Use blocks_path where possible (MarcoFalke) Pull request description: The only reason for the check was to print a warning about an increase in storage use. Now that 22.x is EOL and everyone should have migrated (or decided to not care about storage use), remove the check. Also, a move-only commit is included. (Rebased from bitcoin/bitcoin#22242) ACKs for top commit: TheCharlatan: ACK fae4055, though I lack historical context to really judge the second commit fa86855. stickies-v: ACK fae4055 Tree-SHA512: 9da8f48767ae52d8e8e21c09a40c949cc0838794f1856cc5f58a91acd3f00a3bca818c8082242b3fdc9ca5badb09059570bb3870850d3807b75a8e23b5222da1
2 parents 8c7e735 + fae4055 commit ecab855

14 files changed

+160
-179
lines changed

src/init.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,11 +1555,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15551555
// ********************************************************* Step 8: start indexers
15561556

15571557
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1558-
auto result{WITH_LOCK(cs_main, return CheckLegacyTxindex(*Assert(chainman.m_blockman.m_block_tree_db)))};
1559-
if (!result) {
1560-
return InitError(util::ErrorString(result));
1561-
}
1562-
15631558
g_txindex = std::make_unique<TxIndex>(interfaces::MakeChain(node), cache_sizes.tx_index, false, fReindex);
15641559
node.indexes.emplace_back(g_txindex.get());
15651560
}

src/node/blockstorage.cpp

Lines changed: 111 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,125 @@
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+
// BlockTreeDB::DB_TXINDEX_BLOCK{'T'};
38+
// BlockTreeDB::DB_TXINDEX{'t'}
39+
// BlockTreeDB::ReadFlag("txindex")
40+
41+
bool BlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
42+
{
43+
return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
44+
}
45+
46+
bool BlockTreeDB::WriteReindexing(bool fReindexing)
47+
{
48+
if (fReindexing) {
49+
return Write(DB_REINDEX_FLAG, uint8_t{'1'});
50+
} else {
51+
return Erase(DB_REINDEX_FLAG);
52+
}
53+
}
54+
55+
void BlockTreeDB::ReadReindexing(bool& fReindexing)
56+
{
57+
fReindexing = Exists(DB_REINDEX_FLAG);
58+
}
59+
60+
bool BlockTreeDB::ReadLastBlockFile(int& nFile)
61+
{
62+
return Read(DB_LAST_BLOCK, nFile);
63+
}
64+
65+
bool BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*>>& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo)
66+
{
67+
CDBBatch batch(*this);
68+
for (const auto& [file, info] : fileInfo) {
69+
batch.Write(std::make_pair(DB_BLOCK_FILES, file), *info);
70+
}
71+
batch.Write(DB_LAST_BLOCK, nLastFile);
72+
for (const CBlockIndex* bi : blockinfo) {
73+
batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), CDiskBlockIndex{bi});
74+
}
75+
return WriteBatch(batch, true);
76+
}
77+
78+
bool BlockTreeDB::WriteFlag(const std::string& name, bool fValue)
79+
{
80+
return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
81+
}
82+
83+
bool BlockTreeDB::ReadFlag(const std::string& name, bool& fValue)
84+
{
85+
uint8_t ch;
86+
if (!Read(std::make_pair(DB_FLAG, name), ch)) {
87+
return false;
88+
}
89+
fValue = ch == uint8_t{'1'};
90+
return true;
91+
}
92+
93+
bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
94+
{
95+
AssertLockHeld(::cs_main);
96+
std::unique_ptr<CDBIterator> pcursor(NewIterator());
97+
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
98+
99+
// Load m_block_index
100+
while (pcursor->Valid()) {
101+
if (interrupt) return false;
102+
std::pair<uint8_t, uint256> key;
103+
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
104+
CDiskBlockIndex diskindex;
105+
if (pcursor->GetValue(diskindex)) {
106+
// Construct block index object
107+
CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
108+
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
109+
pindexNew->nHeight = diskindex.nHeight;
110+
pindexNew->nFile = diskindex.nFile;
111+
pindexNew->nDataPos = diskindex.nDataPos;
112+
pindexNew->nUndoPos = diskindex.nUndoPos;
113+
pindexNew->nVersion = diskindex.nVersion;
114+
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
115+
pindexNew->nTime = diskindex.nTime;
116+
pindexNew->nBits = diskindex.nBits;
117+
pindexNew->nNonce = diskindex.nNonce;
118+
pindexNew->nStatus = diskindex.nStatus;
119+
pindexNew->nTx = diskindex.nTx;
120+
121+
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
122+
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
123+
}
124+
125+
pcursor->Next();
126+
} else {
127+
return error("%s: failed to read value", __func__);
128+
}
129+
} else {
130+
break;
131+
}
132+
}
133+
134+
return true;
135+
}
136+
} // namespace kernel
137+
27138
namespace node {
28139
std::atomic_bool fReindex(false);
29140

src/node/blockstorage.h

Lines changed: 29 additions & 2 deletions
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 BlockTreeDB : 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::BlockTreeDB;
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
@@ -185,7 +212,7 @@ class BlockManager
185212
*/
186213
std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
187214

188-
std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
215+
std::unique_ptr<BlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
189216

190217
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
191218
bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

src/node/chainstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ static ChainstateLoadResult CompleteChainstateInitialization(
3737
const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
3838
{
3939
auto& pblocktree{chainman.m_blockman.m_block_tree_db};
40-
// new CBlockTreeDB tries to delete the existing file, which
40+
// new BlockTreeDB tries to delete the existing file, which
4141
// fails if it's still open from the previous loop. Close it first:
4242
pblocktree.reset();
43-
pblocktree = std::make_unique<CBlockTreeDB>(DBParams{
43+
pblocktree = std::make_unique<BlockTreeDB>(DBParams{
4444
.path = chainman.m_options.datadir / "blocks" / "index",
4545
.cache_bytes = static_cast<size_t>(cache_sizes.block_tree_db),
4646
.memory_only = options.block_tree_db_in_memory,

src/test/util/setup_common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include <functional>
6565
#include <stdexcept>
6666

67+
using kernel::BlockTreeDB;
6768
using kernel::ValidationCacheSizes;
6869
using node::ApplyArgsManOptions;
6970
using node::BlockAssembler;
@@ -182,7 +183,7 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
182183
.notifications = chainman_opts.notifications,
183184
};
184185
m_node.chainman = std::make_unique<ChainstateManager>(m_node.kernel->interrupt, chainman_opts, blockman_opts);
185-
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{
186+
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<BlockTreeDB>(DBParams{
186187
.path = m_args.GetDataDirNet() / "blocks" / "index",
187188
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),
188189
.memory_only = true});

src/txdb.cpp

Lines changed: 8 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,25 @@
55

66
#include <txdb.h>
77

8-
#include <chain.h>
8+
#include <coins.h>
9+
#include <dbwrapper.h>
910
#include <logging.h>
10-
#include <pow.h>
11+
#include <primitives/transaction.h>
1112
#include <random.h>
13+
#include <serialize.h>
1214
#include <uint256.h>
13-
#include <util/signalinterrupt.h>
14-
#include <util/translation.h>
1515
#include <util/vector.h>
1616

17-
#include <stdint.h>
17+
#include <cassert>
18+
#include <cstdlib>
19+
#include <iterator>
20+
#include <utility>
1821

1922
static constexpr uint8_t DB_COIN{'C'};
20-
static constexpr uint8_t DB_BLOCK_FILES{'f'};
21-
static constexpr uint8_t DB_BLOCK_INDEX{'b'};
22-
2323
static constexpr uint8_t DB_BEST_BLOCK{'B'};
2424
static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
25-
static constexpr uint8_t DB_FLAG{'F'};
26-
static constexpr uint8_t DB_REINDEX_FLAG{'R'};
27-
static constexpr uint8_t DB_LAST_BLOCK{'l'};
28-
2925
// Keys used in previous version that might still be found in the DB:
3026
static constexpr uint8_t DB_COINS{'c'};
31-
static constexpr uint8_t DB_TXINDEX_BLOCK{'T'};
32-
// uint8_t DB_TXINDEX{'t'}
33-
34-
util::Result<void> CheckLegacyTxindex(CBlockTreeDB& block_tree_db)
35-
{
36-
CBlockLocator ignored{};
37-
if (block_tree_db.Read(DB_TXINDEX_BLOCK, ignored)) {
38-
return util::Error{_("The -txindex upgrade started by a previous version cannot be completed. Restart with the previous version or run a full -reindex.")};
39-
}
40-
bool txindex_legacy_flag{false};
41-
block_tree_db.ReadFlag("txindex", txindex_legacy_flag);
42-
if (txindex_legacy_flag) {
43-
// Disable legacy txindex and warn once about occupied disk space
44-
if (!block_tree_db.WriteFlag("txindex", false)) {
45-
return util::Error{Untranslated("Failed to write block index db flag 'txindex'='0'")};
46-
}
47-
return util::Error{_("The block index db contains a legacy 'txindex'. To clear the occupied disk space, run a full -reindex, otherwise ignore this error. This error message will not be displayed again.")};
48-
}
49-
return {};
50-
}
5127

5228
bool CCoinsViewDB::NeedsUpgrade()
5329
{
@@ -178,25 +154,6 @@ size_t CCoinsViewDB::EstimateSize() const
178154
return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
179155
}
180156

181-
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
182-
return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
183-
}
184-
185-
bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
186-
if (fReindexing)
187-
return Write(DB_REINDEX_FLAG, uint8_t{'1'});
188-
else
189-
return Erase(DB_REINDEX_FLAG);
190-
}
191-
192-
void CBlockTreeDB::ReadReindexing(bool &fReindexing) {
193-
fReindexing = Exists(DB_REINDEX_FLAG);
194-
}
195-
196-
bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
197-
return Read(DB_LAST_BLOCK, nFile);
198-
}
199-
200157
/** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
201158
class CCoinsViewDBCursor: public CCoinsViewCursor
202159
{
@@ -269,71 +226,3 @@ void CCoinsViewDBCursor::Next()
269226
keyTmp.first = entry.key;
270227
}
271228
}
272-
273-
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
274-
CDBBatch batch(*this);
275-
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
276-
batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
277-
}
278-
batch.Write(DB_LAST_BLOCK, nLastFile);
279-
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
280-
batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
281-
}
282-
return WriteBatch(batch, true);
283-
}
284-
285-
bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
286-
return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
287-
}
288-
289-
bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
290-
uint8_t ch;
291-
if (!Read(std::make_pair(DB_FLAG, name), ch))
292-
return false;
293-
fValue = ch == uint8_t{'1'};
294-
return true;
295-
}
296-
297-
bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, const util::SignalInterrupt& interrupt)
298-
{
299-
AssertLockHeld(::cs_main);
300-
std::unique_ptr<CDBIterator> pcursor(NewIterator());
301-
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
302-
303-
// Load m_block_index
304-
while (pcursor->Valid()) {
305-
if (interrupt) return false;
306-
std::pair<uint8_t, uint256> key;
307-
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
308-
CDiskBlockIndex diskindex;
309-
if (pcursor->GetValue(diskindex)) {
310-
// Construct block index object
311-
CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
312-
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
313-
pindexNew->nHeight = diskindex.nHeight;
314-
pindexNew->nFile = diskindex.nFile;
315-
pindexNew->nDataPos = diskindex.nDataPos;
316-
pindexNew->nUndoPos = diskindex.nUndoPos;
317-
pindexNew->nVersion = diskindex.nVersion;
318-
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
319-
pindexNew->nTime = diskindex.nTime;
320-
pindexNew->nBits = diskindex.nBits;
321-
pindexNew->nNonce = diskindex.nNonce;
322-
pindexNew->nStatus = diskindex.nStatus;
323-
pindexNew->nTx = diskindex.nTx;
324-
325-
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
326-
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
327-
}
328-
329-
pcursor->Next();
330-
} else {
331-
return error("%s: failed to read value", __func__);
332-
}
333-
} else {
334-
break;
335-
}
336-
}
337-
338-
return true;
339-
}

0 commit comments

Comments
 (0)