Skip to content

Commit 45e9fcb

Browse files
committed
Extreme trimming on startup
1 parent 7b20b41 commit 45e9fcb

File tree

3 files changed

+4
-45
lines changed

3 files changed

+4
-45
lines changed

src/txdb.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -308,41 +308,6 @@ bool CBlockTreeDB::WritePAKList(const std::vector<std::vector<unsigned char> >&
308308
return Write(std::make_pair(DB_PAK, uint256S("1")), offline_list) && Write(std::make_pair(DB_PAK, uint256S("2")), online_list) && Write(std::make_pair(DB_PAK, uint256S("3")), reject);
309309
}
310310

311-
/** Note that we only get a conservative (lower) estimate of the max header height here,
312-
* obtained by sampling the first 10,000 headers on disk (which are in random order) and
313-
* taking the highest block we see. */
314-
bool CBlockTreeDB::WalkBlockIndexGutsForMaxHeight(int* nHeight) {
315-
std::unique_ptr<CDBIterator> pcursor(NewIterator());
316-
*nHeight = 0;
317-
int i = 0;
318-
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
319-
while (pcursor->Valid()) {
320-
if (ShutdownRequested()) return false;
321-
std::pair<uint8_t, uint256> key;
322-
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
323-
i++;
324-
if (i > 10'000) {
325-
// Under the (accurate) assumption that the headers on disk are effectively in random height order,
326-
// we have a good-enough (conservative) estimate of the max height very quickly, and don't need to
327-
// waste more time. Shortcutting like this will cause us to keep a few extra headers, which is fine.
328-
break;
329-
}
330-
CDiskBlockIndex diskindex;
331-
if (pcursor->GetValue(diskindex)) {
332-
if (diskindex.nHeight > *nHeight) {
333-
*nHeight = diskindex.nHeight;
334-
}
335-
pcursor->Next();
336-
} else {
337-
return error("%s: failed to read value", __func__);
338-
}
339-
} else {
340-
break;
341-
}
342-
}
343-
return true;
344-
}
345-
346311
const CBlockIndex *CBlockTreeDB::RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const
347312
{
348313
if(!pindexTrimmed->trimmed()) {

src/txdb.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class CBlockTreeDB : public CDBWrapper
8888
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex, int trimBelowHeight);
8989
// ELEMENTS:
9090
const CBlockIndex* RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const;
91-
bool WalkBlockIndexGutsForMaxHeight(int* nHeight);
9291
bool ReadPAKList(std::vector<std::vector<unsigned char> >& offline_list, std::vector<std::vector<unsigned char> >& online_list, bool& reject);
9392
bool WritePAKList(const std::vector<std::vector<unsigned char> >& offline_list, const std::vector<std::vector<unsigned char> >& online_list, bool reject);
9493
};

src/validation.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4161,15 +4161,7 @@ bool BlockManager::LoadBlockIndex(
41614161
{
41624162
int trim_below_height = 0;
41634163
if (fTrimHeaders) {
4164-
int max_height = 0;
4165-
if (!blocktree.WalkBlockIndexGutsForMaxHeight(&max_height)) {
4166-
LogPrintf("LoadBlockIndex: Failed to WalkBlockIndexGutsForMaxHeight.\n");
4167-
return false;
4168-
}
4169-
4170-
int must_keep_headers = (consensus_params.total_valid_epochs + 2) * consensus_params.dynamic_epoch_length;
4171-
int extra_headers_buffer = consensus_params.dynamic_epoch_length * 2; // XXX arbitrary
4172-
trim_below_height = max_height - must_keep_headers - extra_headers_buffer;
4164+
trim_below_height = std::numeric_limits<int>::max();
41734165
}
41744166
if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, trim_below_height))
41754167
return false;
@@ -4218,6 +4210,9 @@ bool BlockManager::LoadBlockIndex(
42184210
pindexBestHeader = pindex;
42194211
}
42204212

4213+
if (pindexBestHeader) {
4214+
ForceUntrimHeader(pindexBestHeader);
4215+
}
42214216
return true;
42224217
}
42234218

0 commit comments

Comments
 (0)