Skip to content

Commit bf5a50f

Browse files
committed
Allow untrimming headers when needed
1 parent bc385cb commit bf5a50f

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

src/chain.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
5050
return CBlockLocator(vHave);
5151
}
5252

53+
void CBlockIndex::untrim() {
54+
if (!trimmed())
55+
return;
56+
CBlockIndex tmp;
57+
const CBlockIndex* pindexfull = untrim_to(&tmp);
58+
assert(pindexfull!=this);
59+
m_trimmed = false;
60+
set_stored();
61+
proof = pindexfull->proof;
62+
m_dynafed_params = pindexfull->m_dynafed_params;
63+
m_signblock_witness = pindexfull->m_signblock_witness;
64+
}
65+
5366
const CBlockIndex *CBlockIndex::untrim_to(CBlockIndex *pindexNew) const
5467
{
5568
return pblocktree->RegenerateFullIndex(this, pindexNew);

src/dynafed.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <dynafed.h>
33
#include <hash.h>
4+
#include <validation.h>
45

56
bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consensus::Params& consensus, DynaFedParamEntry& winning_entry)
67
{
@@ -15,6 +16,7 @@ bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consens
1516
for (int32_t height = next_height - 1; height >= (int32_t)(next_height - consensus.dynamic_epoch_length); --height) {
1617
const CBlockIndex* p_epoch_walk = pindexPrev->GetAncestor(height);
1718
assert(p_epoch_walk);
19+
ForceUntrimHeader(p_epoch_walk);
1820
const DynaFedParamEntry& proposal = p_epoch_walk->dynafed_params().m_proposed;
1921
const uint256 proposal_root = proposal.CalculateRoot();
2022
vote_tally[proposal_root]++;
@@ -60,6 +62,7 @@ DynaFedParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pinde
6062
// may be pre-dynafed params
6163
const CBlockIndex* p_epoch_start = pindexPrev->GetAncestor(epoch_start_height);
6264
assert(p_epoch_start);
65+
ForceUntrimHeader(p_epoch_start);
6366
if (p_epoch_start->dynafed_params().IsNull()) {
6467
// We need to construct the "full" current parameters of pre-dynafed
6568
// consensus
@@ -93,6 +96,7 @@ DynaFedParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPre
9396
{
9497
assert(pindexPrev);
9598

99+
ForceUntrimHeader(pindexPrev);
96100
DynaFedParamEntry entry = ComputeNextBlockFullCurrentParameters(pindexPrev, consensus);
97101

98102
uint32_t next_height = pindexPrev->nHeight+1;

src/pegins.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// ELEMENTS
2727
//
2828

29+
#include <validation.h>
30+
2931
namespace {
3032
static secp256k1_context* secp256k1_ctx_validation;
3133

@@ -487,6 +489,7 @@ std::vector<std::pair<CScript, CScript>> GetValidFedpegScripts(const CBlockIndex
487489
break;
488490
}
489491

492+
ForceUntrimHeader(p_epoch_start);
490493
if (!p_epoch_start->dynafed_params().IsNull()) {
491494
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));
492495
} else {

src/validation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,7 @@ bool CChainState::FlushStateToDisk(
23482348
vBlocks.reserve(setDirtyBlockIndex.size());
23492349
std::set<CBlockIndex*> setTrimmableBlockIndex(setDirtyBlockIndex);
23502350
for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
2351+
(*it)->untrim();
23512352
vBlocks.push_back(*it);
23522353
setDirtyBlockIndex.erase(it++);
23532354
}
@@ -2460,6 +2461,18 @@ static void AppendWarning(bilingual_str& res, const bilingual_str& warn)
24602461
res += warn;
24612462
}
24622463

2464+
void ForceUntrimHeader(const CBlockIndex *pindex_)
2465+
{
2466+
assert(pindex_);
2467+
if (!pindex_->trimmed()) {
2468+
return;
2469+
}
2470+
AssertLockHeld(cs_main);
2471+
CBlockIndex* pindex = const_cast<CBlockIndex*>(pindex_);
2472+
pindex->untrim();
2473+
setDirtyBlockIndex.insert(pindex);
2474+
}
2475+
24632476
void CChainState::UpdateTip(const CBlockIndex* pindexNew)
24642477
{
24652478
// New best block
@@ -2497,11 +2510,13 @@ void CChainState::UpdateTip(const CBlockIndex* pindexNew)
24972510
this->CoinsTip().DynamicMemoryUsage() * (1.0 / (1<<20)), this->CoinsTip().GetCacheSize(),
24982511
!warning_messages.empty() ? strprintf(" warning='%s'", warning_messages.original) : "");
24992512

2513+
ForceUntrimHeader(pindexNew);
25002514
// Do some logging if dynafed parameters changed.
25012515
if (pindexNew->pprev && !pindexNew->dynafed_params().IsNull()) {
25022516
int height = pindexNew->nHeight;
25032517
uint256 hash = pindexNew->GetBlockHash();
25042518
uint256 root = pindexNew->dynafed_params().m_current.CalculateRoot();
2519+
ForceUntrimHeader(pindexNew->pprev);
25052520
if (pindexNew->pprev->dynafed_params().IsNull()) {
25062521
LogPrintf("Dynafed activated in block %d:%s: %s\n", height, hash.GetHex(), root.GetHex());
25072522
} else if (root != pindexNew->pprev->dynafed_params().m_current.CalculateRoot()) {

src/validation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,4 +1068,5 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka
10681068
*/
10691069
const AssumeutxoData* ExpectedAssumeutxo(const int height, const CChainParams& params);
10701070

1071+
void ForceUntrimHeader(const CBlockIndex *pindex_);
10711072
#endif // BITCOIN_VALIDATION_H

0 commit comments

Comments
 (0)