Upcoming changes ...
Often there are situations in which a chain of blocks with a later timestamp but with greater trust replaces the earlier found blocks.
I do not like such situations, and I consider this vulnerability. In the updated code, a chain of blocks with a larger time stamp loses its trust.
Code ...
CBlockIndex* pblockindex = NULL;
int nPossibleHeight = pindexNew->pprev->nHeight + 1;
if (fDebug)
printf(" 'AddToBlockIndex()' - The new block pretends to a height %d, block chain height %d\n", nPossibleHeight,
pindexBest->nHeight);
if (nPossibleHeight < pindexBest->nHeight && fHardForkOne)
{
if (nPossibleHeight <= pindexBest->nHeight - nTriggerDepth)
{
if (fDebug)
printf(" 'AddToBlockIndex()' - The new block pretends to a height %d, maximum allowed block height for a competing chain %d\n", nPossibleHeight,
pindexBest->nHeight - nTriggerDepth);
pindexNew->bnChainTrust = 0;
}
pblockindex = FindBlockByHeight(nPossibleHeight);
if (pindexNew->GetBlockTime() > pblockindex->GetBlockTime())
{
if (fDebug)
printf(" 'AddToBlockIndex()' - Generation time of a new block date=%s later than available in the database date=%s\n",
DateTimeStrFormat("%x %H:%M:%S", pindexNew->GetBlockTime()).c_str(), DateTimeStrFormat("%x %H:%M:%S",
pblockindex->GetBlockTime()).c_str());
pindexNew->bnChainTrust = 0;
}
else if (pindexNew->GetBlockTime() < pblockindex->GetBlockTime())
{
if (nPossibleHeight < pindexBest->nHeight && nPossibleHeight > pindexBest->nHeight - nTriggerDepth)
{
if (fDebug)
printf(" 'AddToBlockIndex()' - The generation time of a new block date=%s earlier than the one in the database date=%s\n",
DateTimeStrFormat("%x %H:%M:%S", pindexNew->GetBlockTime()).c_str(), DateTimeStrFormat("%x %H:%M:%S",
pblockindex->GetBlockTime()).c_str());
bnBestChainTrust = pblockindex->pprev->bnChainTrust;
}
}
}