aboutsummaryrefslogtreecommitdiff
path: root/src/txdb.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/txdb.cpp')
-rw-r--r--src/txdb.cpp136
1 files changed, 11 insertions, 125 deletions
diff --git a/src/txdb.cpp b/src/txdb.cpp
index 0dafa3b38a..afcd1985f5 100644
--- a/src/txdb.cpp
+++ b/src/txdb.cpp
@@ -6,7 +6,6 @@
#include <txdb.h>
#include <chain.h>
-#include <node/ui_interface.h>
#include <pow.h>
#include <random.h>
#include <shutdown.h>
@@ -18,7 +17,6 @@
#include <stdint.h>
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<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db)
return std::nullopt;
}
+bool CCoinsViewDB::NeedsUpgrade()
+{
+ std::unique_ptr<CDBIterator> 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<CDBWrapper>(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<CTxOut> 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<typename Stream>
- 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<bool> 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<TxOutCompression>(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<CDBIterator> 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<unsigned char, uint256> key;
- std::pair<unsigned char, uint256> 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();
-}