Skip to content

Commit 4b47709

Browse files
committed
Extreme trimming on startup
1 parent 7f95a75 commit 4b47709

File tree

3 files changed

+4
-45
lines changed

3 files changed

+4
-45
lines changed

src/node/blockstorage.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,7 @@ bool BlockManager::LoadBlockIndex(
223223
{
224224
int trim_below_height = 0;
225225
if (fTrimHeaders) {
226-
int max_height = 0;
227-
if (!m_block_tree_db->WalkBlockIndexGutsForMaxHeight(&max_height)) {
228-
LogPrintf("LoadBlockIndex: Failed to WalkBlockIndexGutsForMaxHeight.\n");
229-
return false;
230-
}
231-
232-
int must_keep_headers = (consensus_params.total_valid_epochs + 2) * consensus_params.dynamic_epoch_length;
233-
int extra_headers_buffer = consensus_params.dynamic_epoch_length * 2; // XXX arbitrary
234-
trim_below_height = max_height - must_keep_headers - extra_headers_buffer;
226+
trim_below_height = std::numeric_limits<int>::max();
235227
}
236228
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, trim_below_height)) {
237229
return false;
@@ -337,6 +329,9 @@ bool BlockManager::LoadBlockIndex(
337329
}
338330
}
339331

332+
if (pindexBestHeader) {
333+
ForceUntrimHeader(pindexBestHeader);
334+
}
340335
return true;
341336
}
342337

src/txdb.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -331,41 +331,6 @@ bool CBlockTreeDB::WritePAKList(const std::vector<std::vector<unsigned char> >&
331331
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);
332332
}
333333

334-
/** Note that we only get a conservative (lower) estimate of the max header height here,
335-
* obtained by sampling the first 10,000 headers on disk (which are in random order) and
336-
* taking the highest block we see. */
337-
bool CBlockTreeDB::WalkBlockIndexGutsForMaxHeight(int* nHeight) {
338-
std::unique_ptr<CDBIterator> pcursor(NewIterator());
339-
*nHeight = 0;
340-
int i = 0;
341-
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
342-
while (pcursor->Valid()) {
343-
if (ShutdownRequested()) return false;
344-
std::pair<uint8_t, uint256> key;
345-
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
346-
i++;
347-
if (i > 10'000) {
348-
// Under the (accurate) assumption that the headers on disk are effectively in random height order,
349-
// we have a good-enough (conservative) estimate of the max height very quickly, and don't need to
350-
// waste more time. Shortcutting like this will cause us to keep a few extra headers, which is fine.
351-
break;
352-
}
353-
CDiskBlockIndex diskindex;
354-
if (pcursor->GetValue(diskindex)) {
355-
if (diskindex.nHeight > *nHeight) {
356-
*nHeight = diskindex.nHeight;
357-
}
358-
pcursor->Next();
359-
} else {
360-
return error("%s: failed to read value", __func__);
361-
}
362-
} else {
363-
break;
364-
}
365-
}
366-
return true;
367-
}
368-
369334
const CBlockIndex *CBlockTreeDB::RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const
370335
{
371336
if(!pindexTrimmed->trimmed()) {

src/txdb.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ class CBlockTreeDB : public CDBWrapper
9292
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
9393
// ELEMENTS:
9494
const CBlockIndex *RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const;
95-
bool WalkBlockIndexGutsForMaxHeight(int* nHeight);
9695
bool ReadPAKList(std::vector<std::vector<unsigned char> >& offline_list, std::vector<std::vector<unsigned char> >& online_list, bool& reject);
9796
bool WritePAKList(const std::vector<std::vector<unsigned char> >& offline_list, const std::vector<std::vector<unsigned char> >& online_list, bool reject);
9897
};

0 commit comments

Comments
 (0)