Skip to content

Commit

Permalink
Improve Bitcloud Core
Browse files Browse the repository at this point in the history
Fix for Assertion `hashPrevBlock == view.GetBestBlock()' failed.
  • Loading branch information
Chris authored Jan 21, 2018
1 parent 91f1da0 commit 296eaef
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3743,6 +3743,71 @@ bool static LoadBlockIndexDB()
LogPrintf("%s: Last shutdown was prepared: %s\n", __func__, fLastShutdownWasPrepared);

//Check for inconsistency with block file info and internal state
if (!fLastShutdownWasPrepared && !GetBoolArg("-forcestart", false) && !GetBoolArg("-reindex", false)) {
unsigned int nHeightLastBlockFile = vinfoBlockFile[nLastBlockFile].nHeightLast + 1;
if (vSortedByHeight.size() > nHeightLastBlockFile && pcoinsTip->GetBestBlock() != vSortedByHeight[nHeightLastBlockFile].second->GetBlockHash()) {
//The database is in a state where a block has been accepted and written to disk, but the
//transaction database (pcoinsTip) was not flushed to disk, and is therefore not in sync with
//the block index database.
string strError = "";
if (!mapBlockIndex.count(pcoinsTip->GetBestBlock())) {
strError = "The wallet has been not been closed gracefully, causing the transaction database to be out of sync with the block database";
return false;
}
LogPrintf("%s : pcoinstip synced to block height %d, block index height %d\n", __func__,
mapBlockIndex[pcoinsTip->GetBestBlock()]->nHeight, vSortedByHeight.size());

//get the index associated with the point in the chain that pcoinsTip is synced to
CBlockIndex *pindexLastMeta = vSortedByHeight[vinfoBlockFile[nLastBlockFile].nHeightLast + 1].second;
CBlockIndex *pindex = vSortedByHeight[0].second;
unsigned int nSortedPos = 0;
for (unsigned int i = 0; i < vSortedByHeight.size(); i++) {
nSortedPos = i;
if (vSortedByHeight[i].first == mapBlockIndex[pcoinsTip->GetBestBlock()]->nHeight + 1) {
pindex = vSortedByHeight[i].second;
break;
}
}

// Start at the last block that was successfully added to the txdb (pcoinsTip) and manually add all transactions that occurred for each block up until
// the best known block from the block index db.
CCoinsViewCache view(pcoinsTip);
while (nSortedPos < vSortedByHeight.size()) {
CBlock block;
if (!ReadBlockFromDisk(block, pindex)) {
strError = "The wallet has been not been closed gracefully and has caused corruption of blocks stored to disk. Data directory is in an unusable state";
return false;
}

vector<CTxUndo> vtxundo;
vtxundo.reserve(block.vtx.size() - 1);
uint256 hashBlock = block.GetHash();
for (unsigned int i = 0; i < block.vtx.size(); i++) {
CValidationState state;
CTxUndo undoDummy;
if (i > 0)
vtxundo.push_back(CTxUndo());
UpdateCoins(block.vtx[i], state, view, i == 0 ? undoDummy : vtxundo.back(), pindex->nHeight);
view.SetBestBlock(hashBlock);
}

if(pindex->nHeight >= pindexLastMeta->nHeight)
break;

pindex = vSortedByHeight[++nSortedPos].second;
}

// Save the updates to disk
if (!view.Flush() || !pcoinsTip->Flush())
LogPrintf("%s : failed to flush view\n", __func__);

LogPrintf("%s: Last block properly recorded: #%d %s\n", __func__, pindexLastMeta->nHeight,
pindexLastMeta->GetBlockHash().ToString().c_str());
LogPrintf("%s : pcoinstip=%d %s\n", __func__, mapBlockIndex[pcoinsTip->GetBestBlock()]->nHeight,
pcoinsTip->GetBestBlock().GetHex());
}
}
/*
if (!fLastShutdownWasPrepared && !GetBoolArg("-forcestart", false) && !GetBoolArg("-reindex", false) && (vSortedByHeight.size() != vinfoBlockFile[nLastBlockFile].nHeightLast + 1) && (vinfoBlockFile[nLastBlockFile].nHeightLast != 0)) {
//The database is in a state where a block has been accepted and written to disk, but not
//all of the block has perculated through the code. The block and the index should both be
Expand Down Expand Up @@ -3821,7 +3886,7 @@ bool static LoadBlockIndexDB()
}
LogPrintf("Passed corruption fix\n");
}

*/
// Check whether we need to continue reindexing
bool fReindexing = false;
pblocktree->ReadReindexing(fReindexing);
Expand Down

0 comments on commit 296eaef

Please sign in to comment.