Skip to content

Commit

Permalink
Extreme trimming on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
psgreco committed Oct 2, 2023
1 parent 7f95a75 commit 4b47709
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 45 deletions.
13 changes: 4 additions & 9 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,7 @@ bool BlockManager::LoadBlockIndex(
{
int trim_below_height = 0;
if (fTrimHeaders) {
int max_height = 0;
if (!m_block_tree_db->WalkBlockIndexGutsForMaxHeight(&max_height)) {
LogPrintf("LoadBlockIndex: Failed to WalkBlockIndexGutsForMaxHeight.\n");
return false;
}

int must_keep_headers = (consensus_params.total_valid_epochs + 2) * consensus_params.dynamic_epoch_length;
int extra_headers_buffer = consensus_params.dynamic_epoch_length * 2; // XXX arbitrary
trim_below_height = max_height - must_keep_headers - extra_headers_buffer;
trim_below_height = std::numeric_limits<int>::max();
}
if (!m_block_tree_db->LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }, trim_below_height)) {
return false;
Expand Down Expand Up @@ -337,6 +329,9 @@ bool BlockManager::LoadBlockIndex(
}
}

if (pindexBestHeader) {
ForceUntrimHeader(pindexBestHeader);
}
return true;
}

Expand Down
35 changes: 0 additions & 35 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,41 +331,6 @@ bool CBlockTreeDB::WritePAKList(const std::vector<std::vector<unsigned char> >&
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);
}

/** Note that we only get a conservative (lower) estimate of the max header height here,
* obtained by sampling the first 10,000 headers on disk (which are in random order) and
* taking the highest block we see. */
bool CBlockTreeDB::WalkBlockIndexGutsForMaxHeight(int* nHeight) {
std::unique_ptr<CDBIterator> pcursor(NewIterator());
*nHeight = 0;
int i = 0;
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
while (pcursor->Valid()) {
if (ShutdownRequested()) return false;
std::pair<uint8_t, uint256> key;
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
i++;
if (i > 10'000) {
// Under the (accurate) assumption that the headers on disk are effectively in random height order,
// we have a good-enough (conservative) estimate of the max height very quickly, and don't need to
// waste more time. Shortcutting like this will cause us to keep a few extra headers, which is fine.
break;
}
CDiskBlockIndex diskindex;
if (pcursor->GetValue(diskindex)) {
if (diskindex.nHeight > *nHeight) {
*nHeight = diskindex.nHeight;
}
pcursor->Next();
} else {
return error("%s: failed to read value", __func__);
}
} else {
break;
}
}
return true;
}

const CBlockIndex *CBlockTreeDB::RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const
{
if(!pindexTrimmed->trimmed()) {
Expand Down
1 change: 0 additions & 1 deletion src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class CBlockTreeDB : public CDBWrapper
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
// ELEMENTS:
const CBlockIndex *RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const;
bool WalkBlockIndexGutsForMaxHeight(int* nHeight);
bool ReadPAKList(std::vector<std::vector<unsigned char> >& offline_list, std::vector<std::vector<unsigned char> >& online_list, bool& reject);
bool WritePAKList(const std::vector<std::vector<unsigned char> >& offline_list, const std::vector<std::vector<unsigned char> >& online_list, bool reject);
};
Expand Down

0 comments on commit 4b47709

Please sign in to comment.