diff --git a/src/txdb.cpp b/src/txdb.cpp --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -5,7 +5,6 @@ #include -#include #include #include #include @@ -463,6 +462,9 @@ } bool CBlockTreeDB::Upgrade(const Consensus::Params ¶ms) { + // This method used to add the block size to pre-0.22.8 block index + // databases. This is no longer supported as of 0.25.5, but the method is + // kept to update the version number in the database. std::unique_ptr pcursor(NewIterator()); uint64_t version = 0; @@ -476,92 +478,22 @@ return true; } - CDBBatch batch(*this); - pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256())); - if (!pcursor->Valid()) { - // The DB is empty, so just write the version number and consider the - // upgrade done. - batch.Write("version", uint64_t(CLIENT_VERSION)); - WriteBatch(batch); - return true; - } - - int64_t count = 0; - LogPrintf("Upgrading block index database...\n"); - int reportDone = -1; - std::pair key = {DB_BLOCK_INDEX, uint256()}; - while (pcursor->Valid()) { - if (ShutdownRequested()) { - break; - } - - if (!pcursor->GetKey(key) || key.first != DB_BLOCK_INDEX) { - break; - } - - if (count++ % 256 == 0) { - uint32_t high = - 0x100 * *key.second.begin() + *(key.second.begin() + 1); - int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5); - uiInterface.ShowProgress( - _("Upgrading block index database").translated, percentageDone, - true); - if (reportDone < percentageDone / 10) { - // report max. every 10% step - LogPrintfToBeContinued("[%d%%]...", percentageDone); - reportDone = percentageDone / 10; - } - } - - // Read the block index entry and update it. - CDiskBlockIndex diskindex; - if (!pcursor->GetValue(diskindex)) { - return error("%s: cannot parse CDiskBlockIndex record", __func__); - } - - // The block hash needs to be usable. - BlockHash blockhash = diskindex.GetBlockHash(); - diskindex.phashBlock = &blockhash; - - bool mustUpdate = false; - - // We must update the block index to add the size. - if (CLIENT_VERSION >= CDiskBlockIndex::TRACK_SIZE_VERSION && - version < CDiskBlockIndex::TRACK_SIZE_VERSION && - diskindex.nTx > 0 && diskindex.nSize == 0) { - if (!diskindex.nStatus.hasData()) { - // The block was pruned, we need a full reindex. - LogPrintf("\nThe block %s is pruned. The block index cannot be " - "upgraded and reindexing is required.\n", - blockhash.GetHex()); - return false; - } - - CBlock block; - if (!ReadBlockFromDisk(block, &diskindex, params)) { - // Failed to read the block from disk, even though it is marked - // that we have data for this block. - return false; - } - mustUpdate = true; - diskindex.nSize = ::GetSerializeSize(block, PROTOCOL_VERSION); - } - - if (mustUpdate) { - batch.Write(std::make_pair(DB_BLOCK_INDEX, blockhash), diskindex); - } - - pcursor->Next(); + // The DB is not empty, and the version is either non-existent or too old. + // The node requires a reindex. + if (pcursor->Valid() && version < CDiskBlockIndex::TRACK_SIZE_VERSION) { + LogPrintf( + "\nThe database is too old. The block index cannot be upgraded " + "and reindexing is required.\n"); + return false; } - // Upgrade is done, now let's update the version number. + // The DB is empty or recent enough. + // Just write the new version number and consider the upgrade done. + CDBBatch batch(*this); + LogPrintf("Updating the block index database version to %d\n", + CLIENT_VERSION); batch.Write("version", uint64_t(CLIENT_VERSION)); - - WriteBatch(batch); - CompactRange({DB_BLOCK_INDEX, uint256()}, key); - uiInterface.ShowProgress("", 100, false); - LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE"); - return !ShutdownRequested(); + return WriteBatch(batch); }