From 21ca2d833b85673c482803b1c382bbe948b2c44a Mon Sep 17 00:00:00 2001 From: Satoshi Nakamoto Date: Mon, 16 Aug 2010 20:54:47 +0000 Subject: [PATCH] erase the bad chain after disconnecting it --- db.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/db.cpp b/db.cpp index ded78d345..d83392e52 100644 --- a/db.cpp +++ b/db.cpp @@ -460,9 +460,12 @@ bool CTxDB::LoadBlockIndex() ReadBestInvalidWork(bnBestInvalidWork); // Verify blocks in the best chain + vector vChain; + vector vBad; CBlockIndex* pindexFork = NULL; for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev) { + vChain.push_back(pindex); CBlock block; if (!block.ReadFromDisk(pindex)) return error("LoadBlockIndex() : block.ReadFromDisk failed"); @@ -470,6 +473,7 @@ bool CTxDB::LoadBlockIndex() { printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str()); pindexFork = pindex->pprev; + vBad = vChain; } } if (pindexFork) @@ -480,6 +484,14 @@ bool CTxDB::LoadBlockIndex() return error("LoadBlockIndex() : block.ReadFromDisk failed"); CTxDB txdb; block.SetBestChain(txdb, pindexFork); + + // Delete the bad chain + foreach(CBlockIndex* pindex, vBad) + { + txdb.EraseBlockIndex(pindex->GetBlockHash()); + mapBlockIndex.erase(pindex->GetBlockHash()); + delete pindex; + } } return true;