From fa9112aac07dc371bfda437d40eb1b841f36f392 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 2 Feb 2022 13:23:02 +0100 Subject: Remove utxo db upgrade code --- src/init.cpp | 5 +- src/node/chainstate.cpp | 4 +- src/txdb.cpp | 136 ++++-------------------------------------------- src/txdb.h | 4 +- 4 files changed, 18 insertions(+), 131 deletions(-) (limited to 'src') diff --git a/src/init.cpp b/src/init.cpp index bad402e56e..a8b86453fe 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1450,8 +1450,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) strLoadError = _("Error initializing block database"); break; case ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED: - strLoadError = _("Error upgrading chainstate database"); - break; + return InitError(_("Unsupported chainstate database format found. " + "Please restart with -reindex-chainstate. This will " + "rebuild the chainstate database.")); case ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED: strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate."); break; diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index d03b9dcac6..d22f71ac8a 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -90,9 +90,9 @@ std::optional LoadChainstate(bool fReset, chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb); } - // If necessary, upgrade from older database format. + // Refuse to load unsupported database format. // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate - if (!chainstate->CoinsDB().Upgrade()) { + if (chainstate->CoinsDB().NeedsUpgrade()) { return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED; } diff --git a/src/txdb.cpp b/src/txdb.cpp index 9a39e90ccd..81f34b17aa 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -18,7 +17,6 @@ #include static constexpr uint8_t DB_COIN{'C'}; -static constexpr uint8_t DB_COINS{'c'}; static constexpr uint8_t DB_BLOCK_FILES{'f'}; static constexpr uint8_t DB_BLOCK_INDEX{'b'}; @@ -29,6 +27,7 @@ static constexpr uint8_t DB_REINDEX_FLAG{'R'}; static constexpr uint8_t DB_LAST_BLOCK{'l'}; // Keys used in previous version that might still be found in the DB: +static constexpr uint8_t DB_COINS{'c'}; static constexpr uint8_t DB_TXINDEX_BLOCK{'T'}; // uint8_t DB_TXINDEX{'t'} @@ -50,6 +49,15 @@ std::optional CheckLegacyTxindex(CBlockTreeDB& block_tree_db) return std::nullopt; } +bool CCoinsViewDB::NeedsUpgrade() +{ + std::unique_ptr cursor{m_db->NewIterator()}; + // DB_COINS was deprecated in v0.15.0, commit + // 1088b02f0ccd7358d2b7076bb9e122d59d502d02 + cursor->Seek(std::make_pair(DB_COINS, uint256{})); + return cursor->Valid(); +} + namespace { struct CoinEntry { @@ -60,7 +68,7 @@ struct CoinEntry { SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); } }; -} +} // namespace CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) : m_db(std::make_unique(ldb_path, nCacheSize, fMemory, fWipe, true)), @@ -337,125 +345,3 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, return true; } - -namespace { - -//! Legacy class to deserialize pre-pertxout database entries without reindex. -class CCoins -{ -public: - //! whether transaction is a coinbase - bool fCoinBase; - - //! unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped - std::vector vout; - - //! at which height this transaction was included in the active block chain - int nHeight; - - //! empty constructor - CCoins() : fCoinBase(false), vout(0), nHeight(0) { } - - template - void Unserialize(Stream &s) { - unsigned int nCode = 0; - // version - unsigned int nVersionDummy; - ::Unserialize(s, VARINT(nVersionDummy)); - // header code - ::Unserialize(s, VARINT(nCode)); - fCoinBase = nCode & 1; - std::vector vAvail(2, false); - vAvail[0] = (nCode & 2) != 0; - vAvail[1] = (nCode & 4) != 0; - unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1); - // spentness bitmask - while (nMaskCode > 0) { - unsigned char chAvail = 0; - ::Unserialize(s, chAvail); - for (unsigned int p = 0; p < 8; p++) { - bool f = (chAvail & (1 << p)) != 0; - vAvail.push_back(f); - } - if (chAvail != 0) - nMaskCode--; - } - // txouts themself - vout.assign(vAvail.size(), CTxOut()); - for (unsigned int i = 0; i < vAvail.size(); i++) { - if (vAvail[i]) - ::Unserialize(s, Using(vout[i])); - } - // coinbase height - ::Unserialize(s, VARINT_MODE(nHeight, VarIntMode::NONNEGATIVE_SIGNED)); - } -}; - -} - -/** Upgrade the database from older formats. - * - * Currently implemented: from the per-tx utxo model (0.8..0.14.x) to per-txout. - */ -bool CCoinsViewDB::Upgrade() { - std::unique_ptr pcursor(m_db->NewIterator()); - pcursor->Seek(std::make_pair(DB_COINS, uint256())); - if (!pcursor->Valid()) { - return true; - } - - int64_t count = 0; - LogPrintf("Upgrading utxo-set database...\n"); - LogPrintf("[0%%]..."); /* Continued */ - uiInterface.ShowProgress(_("Upgrading UTXO database").translated, 0, true); - size_t batch_size = 1 << 24; - CDBBatch batch(*m_db); - int reportDone = 0; - std::pair key; - std::pair prev_key = {DB_COINS, uint256()}; - while (pcursor->Valid()) { - if (ShutdownRequested()) { - break; - } - if (pcursor->GetKey(key) && key.first == DB_COINS) { - 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 UTXO database").translated, percentageDone, true); - if (reportDone < percentageDone/10) { - // report max. every 10% step - LogPrintf("[%d%%]...", percentageDone); /* Continued */ - reportDone = percentageDone/10; - } - } - CCoins old_coins; - if (!pcursor->GetValue(old_coins)) { - return error("%s: cannot parse CCoins record", __func__); - } - COutPoint outpoint(key.second, 0); - for (size_t i = 0; i < old_coins.vout.size(); ++i) { - if (!old_coins.vout[i].IsNull() && !old_coins.vout[i].scriptPubKey.IsUnspendable()) { - Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight, old_coins.fCoinBase); - outpoint.n = i; - CoinEntry entry(&outpoint); - batch.Write(entry, newcoin); - } - } - batch.Erase(key); - if (batch.SizeEstimate() > batch_size) { - m_db->WriteBatch(batch); - batch.Clear(); - m_db->CompactRange(prev_key, key); - prev_key = key; - } - pcursor->Next(); - } else { - break; - } - } - m_db->WriteBatch(batch); - m_db->CompactRange({DB_COINS, uint256()}, key); - uiInterface.ShowProgress("", 100, false); - LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE"); - return !ShutdownRequested(); -} diff --git a/src/txdb.h b/src/txdb.h index e70f3cd1f2..faa543b412 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -65,8 +65,8 @@ public: bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; std::unique_ptr Cursor() const override; - //! Attempt to update from an older database format. Returns whether an error occurred. - bool Upgrade(); + //! Whether an unsupported database format is used. + bool NeedsUpgrade(); size_t EstimateSize() const override; //! Dynamically alter the underlying leveldb cache size. -- cgit v1.2.3