From c28c5ce619451c75ccbeebf855e699f5d8e4a3f5 Mon Sep 17 00:00:00 2001 From: Pablo Greco Date: Fri, 29 Sep 2023 12:04:59 -0700 Subject: [PATCH] Allow untrimming headers when needed --- src/chain.cpp | 14 ++++++++++++++ src/dynafed.cpp | 4 ++++ src/pegins.cpp | 3 +++ src/validation.cpp | 18 ++++++++++++++++++ src/validation.h | 1 + 5 files changed, 40 insertions(+) diff --git a/src/chain.cpp b/src/chain.cpp index fc36659025..e66d1edbe0 100644 --- a/src/chain.cpp +++ b/src/chain.cpp @@ -56,6 +56,20 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const { return CBlockLocator(vHave); } +void CBlockIndex::untrim() { + if (!trimmed()) + return; + CBlockIndex tmp; + const CBlockIndex *pindexfull = untrim_to(&tmp); + assert(pindexfull!=this); + m_trimmed = false; + set_stored(); + proof = pindexfull->proof; + m_dynafed_params = pindexfull->m_dynafed_params; + m_signblock_witness = pindexfull->m_signblock_witness; + m_pcontext->chainman->m_blockman.m_dirty_blockindex.insert(this); +} + const CBlockIndex *CBlockIndex::untrim_to(CBlockIndex *pindexNew) const { return m_pcontext->chainman->m_blockman.m_block_tree_db->RegenerateFullIndex(this, pindexNew); diff --git a/src/dynafed.cpp b/src/dynafed.cpp index 8ef680e296..29a2029483 100644 --- a/src/dynafed.cpp +++ b/src/dynafed.cpp @@ -1,6 +1,7 @@ #include #include +#include bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consensus::Params& consensus, DynaFedParamEntry& winning_entry) { @@ -15,6 +16,7 @@ bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consens for (int32_t height = next_height - 1; height >= (int32_t)(next_height - consensus.dynamic_epoch_length); --height) { const CBlockIndex* p_epoch_walk = pindexPrev->GetAncestor(height); assert(p_epoch_walk); + ForceUntrimHeader(p_epoch_walk); const DynaFedParamEntry& proposal = p_epoch_walk->dynafed_params().m_proposed; const uint256 proposal_root = proposal.CalculateRoot(); vote_tally[proposal_root]++; @@ -60,6 +62,7 @@ DynaFedParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pinde // may be pre-dynafed params const CBlockIndex* p_epoch_start = pindexPrev->GetAncestor(epoch_start_height); assert(p_epoch_start); + ForceUntrimHeader(p_epoch_start); if (p_epoch_start->dynafed_params().IsNull()) { // We need to construct the "full" current parameters of pre-dynafed // consensus @@ -93,6 +96,7 @@ DynaFedParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPre { assert(pindexPrev); + ForceUntrimHeader(pindexPrev); DynaFedParamEntry entry = ComputeNextBlockFullCurrentParameters(pindexPrev, consensus); uint32_t next_height = pindexPrev->nHeight+1; diff --git a/src/pegins.cpp b/src/pegins.cpp index 370ffcb83b..60e3b3f4b1 100644 --- a/src/pegins.cpp +++ b/src/pegins.cpp @@ -26,6 +26,8 @@ // ELEMENTS // +#include + namespace { static secp256k1_context* secp256k1_ctx_validation; @@ -487,6 +489,7 @@ std::vector> GetValidFedpegScripts(const CBlockIndex break; } + ForceUntrimHeader(p_epoch_start); if (!p_epoch_start->dynafed_params().IsNull()) { fedpegscripts.push_back(std::make_pair(p_epoch_start->dynafed_params().m_current.m_fedpeg_program, p_epoch_start->dynafed_params().m_current.m_fedpegscript)); } else { diff --git a/src/validation.cpp b/src/validation.cpp index 1cb4abf75b..a6bcfb19db 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2573,6 +2573,11 @@ bool CChainState::FlushStateToDisk( { LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk", BCLog::BENCH); + if (node::fTrimHeaders) { + for (std::set::iterator it = setTrimmableBlockIndex.begin(); it != setTrimmableBlockIndex.end(); it++) { + (*it)->untrim(); + } + } if (!m_blockman.WriteBlockIndexDB()) { return AbortNode(state, "Failed to write to block index database"); } @@ -2709,6 +2714,17 @@ static void UpdateTipLog( !warning_messages.empty() ? strprintf(" warning='%s'", warning_messages) : ""); } +void ForceUntrimHeader(const CBlockIndex *pindex_) +{ + assert(pindex_); + if (!pindex_->trimmed()) { + return; + } + AssertLockHeld(cs_main); + CBlockIndex *pindex=const_cast(pindex_); + pindex->untrim(); +} + void CChainState::UpdateTip(const CBlockIndex* pindexNew) { AssertLockHeld(::cs_main); @@ -2754,11 +2770,13 @@ void CChainState::UpdateTip(const CBlockIndex* pindexNew) } UpdateTipLog(coins_tip, pindexNew, m_params, __func__, "", warning_messages.original); + ForceUntrimHeader(pindexNew); // Do some logging if dynafed parameters changed. if (pindexNew->pprev && !pindexNew->dynafed_params().IsNull()) { int height = pindexNew->nHeight; uint256 hash = pindexNew->GetBlockHash(); uint256 root = pindexNew->dynafed_params().m_current.CalculateRoot(); + ForceUntrimHeader(pindexNew->pprev); if (pindexNew->pprev->dynafed_params().IsNull()) { LogPrintf("Dynafed activated in block %d:%s: %s\n", height, hash.GetHex(), root.GetHex()); } else if (root != pindexNew->pprev->dynafed_params().m_current.CalculateRoot()) { diff --git a/src/validation.h b/src/validation.h index a27f927fea..78fa8bea6a 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1023,4 +1023,5 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka */ const AssumeutxoData* ExpectedAssumeutxo(const int height, const CChainParams& params); +void ForceUntrimHeader(const CBlockIndex *pindex_); #endif // BITCOIN_VALIDATION_H