diff --git a/src/chain.h b/src/chain.h index 0daa00edb7..a73d0d9215 100644 --- a/src/chain.h +++ b/src/chain.h @@ -218,6 +218,7 @@ protected: bool m_trimmed{false}; bool m_trimmed_dynafed_block{false}; + bool m_stored_lvl{false}; friend class CBlockTreeDB; @@ -228,19 +229,28 @@ public: // Irrevocably remove blocksigning and dynafed-related stuff from this // in-memory copy of the block header. - void trim() { + bool trim() { assert_untrimmed(); + if (!m_stored_lvl) { + // We can't trim in-memory data if it's not on disk yet, but we can if it's already been recovered once + return false; + } m_trimmed = true; m_trimmed_dynafed_block = !m_dynafed_params.value().IsNull(); proof = std::nullopt; m_dynafed_params = std::nullopt; m_signblock_witness = std::nullopt; + return true; } + void untrim(); inline bool trimmed() const { return m_trimmed; } + inline void set_stored() { + m_stored_lvl = true; + } inline void assert_untrimmed() const { assert(!m_trimmed); } @@ -507,6 +517,9 @@ public: // For compatibility with elements 0.14 based chains if (g_signed_blocks) { + if (!ser_action.ForRead()) { + obj.assert_untrimmed(); + } if (is_dyna) { READWRITE(obj.m_dynafed_params.value()); READWRITE(obj.m_signblock_witness.value().stack); diff --git a/src/txdb.cpp b/src/txdb.cpp index 4ebd1c4f08..16e8b23d2d 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -396,6 +396,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, pindexNew->nStatus = diskindex.nStatus; pindexNew->nTx = diskindex.nTx; + pindexNew->set_stored(); n_total++; if (diskindex.nHeight >= trimBelowHeight) { n_untrimmed++; diff --git a/src/validation.cpp b/src/validation.cpp index 0b18345215..c74363a218 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2577,6 +2577,11 @@ bool CChainState::FlushStateToDisk( return AbortNode(state, "Failed to write to block index database"); } + // This should be done inside WriteBatchSync, but CBlockIndex is const there + for (std::set::iterator it = setTrimmableBlockIndex.begin(); it != setTrimmableBlockIndex.end(); it++) { + (*it)->set_stored(); + } + if (node::fTrimHeaders) { LogPrintf("Flushing block index, trimming headers, setTrimmableBlockIndex.size(): %d\n", setTrimmableBlockIndex.size()); int trim_height = m_chain.Height() - node::nMustKeepFullHeaders;