Allow untrimming headers when needed

This commit is contained in:
Pablo Greco 2023-09-29 12:04:59 -07:00 committed by James Dorfman
parent 4259278ff5
commit c28c5ce619
5 changed files with 40 additions and 0 deletions

View file

@ -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);

View file

@ -1,6 +1,7 @@
#include <dynafed.h>
#include <hash.h>
#include <validation.h>
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;

View file

@ -26,6 +26,8 @@
// ELEMENTS
//
#include <validation.h>
namespace {
static secp256k1_context* secp256k1_ctx_validation;
@ -487,6 +489,7 @@ std::vector<std::pair<CScript, CScript>> 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 {

View file

@ -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<CBlockIndex*>::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<CBlockIndex*>(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()) {

View file

@ -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