Reduce memory usage of CBlockIndex

Ported code from https://github.com/zcash/zcash/pull/6192 with various changes needed
for the Hush codebase.
This commit is contained in:
Duke
2023-04-13 23:30:23 -04:00
parent 053e9156a7
commit e8dc755f06
9 changed files with 187 additions and 52 deletions

View File

@@ -19,12 +19,57 @@
******************************************************************************/
#include "chain.h"
#include "main.h"
#include "txdb.h"
using namespace std;
/**
* CChain implementation
*/
void CBlockIndex::TrimSolution()
{
AssertLockHeld(cs_main);
// We can correctly trim a solution as soon as the block index entry has been added
// to leveldb. Updates to the block index entry (to update validity status) will be
// handled by re-reading the solution from the existing db entry. It does not help to
// try to avoid these reads by gating trimming on the validity status: the re-reads are
// efficient anyway because of caching in leveldb, and most of them are unavoidable.
if (HasSolution()) {
std::vector<unsigned char> empty;
nSolution.swap(empty);
}
}
CBlockHeader CBlockIndex::GetBlockHeader() const
{
AssertLockHeld(cs_main);
CBlockHeader header;
header.nVersion = nVersion;
if (pprev) {
header.hashPrevBlock = pprev->GetBlockHash();
}
header.hashMerkleRoot = hashMerkleRoot;
// Hush does not have this, maybe some day
// header.hashBlockCommitments = hashBlockCommitments;
header.nTime = nTime;
header.nBits = nBits;
header.nNonce = nNonce;
if (HasSolution()) {
header.nSolution = nSolution;
} else {
CDiskBlockIndex dbindex;
if (!pblocktree->ReadDiskBlockIndex(GetBlockHash(), dbindex)) {
LogPrintf("%s: Failed to read index entry", __func__);
throw std::runtime_error("Failed to read index entry");
}
header.nSolution = dbindex.GetSolution();
}
return header;
}
void CChain::SetTip(CBlockIndex *pindex) {
lastTip = pindex;
if (pindex == NULL) {