Skip to content

Commit 79fcd30

Browse files
committed
merge bitcoin#24236: Remove utxo db upgrade code
1 parent d13ff52 commit 79fcd30

File tree

9 files changed

+92
-134
lines changed

9 files changed

+92
-134
lines changed

ci/test/00_setup_env_native_qt5.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export TEST_RUNNER_EXTRA="--previous-releases --coverage --extended --exclude fe
1414
export RUN_UNIT_TESTS_SEQUENTIAL="true"
1515
export RUN_UNIT_TESTS="false"
1616
export GOAL="install"
17-
export PREVIOUS_RELEASES_TO_DOWNLOAD="v0.15.0.0 v0.16.1.1 v0.17.0.3 v18.2.2 v19.3.0 v20.0.1"
17+
export PREVIOUS_RELEASES_TO_DOWNLOAD="v0.12.1.5 v0.15.0.0 v0.16.1.1 v0.17.0.3 v18.2.2 v19.3.0 v20.0.1"
1818
export BITCOIN_CONFIG="--enable-zmq --with-libs=no --enable-reduce-exports --disable-fuzz-binary LDFLAGS=-static-libstdc++ --with-boost-process"

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,8 +2037,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
20372037
strLoadError = _("Error initializing block database");
20382038
break;
20392039
case ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED:
2040-
strLoadError = _("Error upgrading chainstate database");
2041-
break;
2040+
return InitError(_("Unsupported chainstate database format found. "
2041+
"Please restart with -reindex-chainstate. This will "
2042+
"rebuild the chainstate database."));
20422043
case ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED:
20432044
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
20442045
break;

src/node/chainstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
156156
chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb);
157157
}
158158

159-
// If necessary, upgrade from older database format.
159+
// Refuse to load unsupported database format.
160160
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
161-
if (!chainstate->CoinsDB().Upgrade()) {
161+
if (chainstate->CoinsDB().NeedsUpgrade()) {
162162
return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
163163
}
164164

src/txdb.cpp

Lines changed: 11 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <txdb.h>
77

88
#include <chain.h>
9-
#include <node/interface_ui.h>
109
#include <pow.h>
1110
#include <random.h>
1211
#include <shutdown.h>
@@ -18,7 +17,6 @@
1817
#include <stdint.h>
1918

2019
static constexpr uint8_t DB_COIN{'C'};
21-
static constexpr uint8_t DB_COINS{'c'};
2220
static constexpr uint8_t DB_BLOCK_FILES{'f'};
2321
static constexpr uint8_t DB_ADDRESSINDEX{'a'};
2422
static constexpr uint8_t DB_ADDRESSUNSPENTINDEX{'u'};
@@ -33,6 +31,7 @@ static constexpr uint8_t DB_REINDEX_FLAG{'R'};
3331
static constexpr uint8_t DB_LAST_BLOCK{'l'};
3432

3533
// Keys used in previous version that might still be found in the DB:
34+
static constexpr uint8_t DB_COINS{'c'};
3635
static constexpr uint8_t DB_TXINDEX_BLOCK{'T'};
3736
// uint8_t DB_TXINDEX{'t'}
3837

@@ -54,6 +53,15 @@ std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db)
5453
return std::nullopt;
5554
}
5655

56+
bool CCoinsViewDB::NeedsUpgrade()
57+
{
58+
std::unique_ptr<CDBIterator> cursor{m_db->NewIterator()};
59+
// DB_COINS was deprecated in v0.15.0, commit
60+
// 1088b02f0ccd7358d2b7076bb9e122d59d502d02
61+
cursor->Seek(std::make_pair(DB_COINS, uint256{}));
62+
return cursor->Valid();
63+
}
64+
5765
namespace {
5866

5967
struct CoinEntry {
@@ -64,7 +72,7 @@ struct CoinEntry {
6472
SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
6573
};
6674

67-
}
75+
} // namespace
6876

6977
CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) :
7078
m_db(std::make_unique<CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe, true)),
@@ -473,125 +481,3 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
473481

474482
return true;
475483
}
476-
477-
namespace {
478-
479-
//! Legacy class to deserialize pre-pertxout database entries without reindex.
480-
class CCoins
481-
{
482-
public:
483-
//! whether transaction is a coinbase
484-
bool fCoinBase;
485-
486-
//! unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
487-
std::vector<CTxOut> vout;
488-
489-
//! at which height this transaction was included in the active block chain
490-
int nHeight;
491-
492-
//! empty constructor
493-
CCoins() : fCoinBase(false), vout(0), nHeight(0) { }
494-
495-
template<typename Stream>
496-
void Unserialize(Stream &s) {
497-
unsigned int nCode = 0;
498-
// version
499-
unsigned int nVersionDummy;
500-
::Unserialize(s, VARINT(nVersionDummy));
501-
// header code
502-
::Unserialize(s, VARINT(nCode));
503-
fCoinBase = nCode & 1;
504-
std::vector<bool> vAvail(2, false);
505-
vAvail[0] = (nCode & 2) != 0;
506-
vAvail[1] = (nCode & 4) != 0;
507-
unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
508-
// spentness bitmask
509-
while (nMaskCode > 0) {
510-
unsigned char chAvail = 0;
511-
::Unserialize(s, chAvail);
512-
for (unsigned int p = 0; p < 8; p++) {
513-
bool f = (chAvail & (1 << p)) != 0;
514-
vAvail.push_back(f);
515-
}
516-
if (chAvail != 0)
517-
nMaskCode--;
518-
}
519-
// txouts themself
520-
vout.assign(vAvail.size(), CTxOut());
521-
for (unsigned int i = 0; i < vAvail.size(); i++) {
522-
if (vAvail[i])
523-
::Unserialize(s, Using<TxOutCompression>(vout[i]));
524-
}
525-
// coinbase height
526-
::Unserialize(s, VARINT_MODE(nHeight, VarIntMode::NONNEGATIVE_SIGNED));
527-
}
528-
};
529-
530-
}
531-
532-
/** Upgrade the database from older formats.
533-
*
534-
* Currently implemented: from the per-tx utxo model (0.8..0.14.x) to per-txout.
535-
*/
536-
bool CCoinsViewDB::Upgrade() {
537-
std::unique_ptr<CDBIterator> pcursor(m_db->NewIterator());
538-
pcursor->Seek(std::make_pair(DB_COINS, uint256()));
539-
if (!pcursor->Valid()) {
540-
return true;
541-
}
542-
543-
int64_t count = 0;
544-
LogPrintf("Upgrading utxo-set database...\n");
545-
LogPrintf("[0%%]..."); /* Continued */
546-
uiInterface.ShowProgress(_("Upgrading UTXO database").translated, 0, true);
547-
size_t batch_size = 1 << 24;
548-
CDBBatch batch(*m_db);
549-
int reportDone = 0;
550-
std::pair<unsigned char, uint256> key;
551-
std::pair<unsigned char, uint256> prev_key = {DB_COINS, uint256()};
552-
while (pcursor->Valid()) {
553-
if (ShutdownRequested()) {
554-
break;
555-
}
556-
if (pcursor->GetKey(key) && key.first == DB_COINS) {
557-
if (count++ % 256 == 0) {
558-
uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1);
559-
int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
560-
uiInterface.ShowProgress(_("Upgrading UTXO database").translated, percentageDone, true);
561-
if (reportDone < percentageDone/10) {
562-
// report max. every 10% step
563-
LogPrintf("[%d%%]...", percentageDone); /* Continued */
564-
reportDone = percentageDone/10;
565-
}
566-
}
567-
CCoins old_coins;
568-
if (!pcursor->GetValue(old_coins)) {
569-
return error("%s: cannot parse CCoins record", __func__);
570-
}
571-
COutPoint outpoint(key.second, 0);
572-
for (size_t i = 0; i < old_coins.vout.size(); ++i) {
573-
if (!old_coins.vout[i].IsNull() && !old_coins.vout[i].scriptPubKey.IsUnspendable()) {
574-
Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight, old_coins.fCoinBase);
575-
outpoint.n = i;
576-
CoinEntry entry(&outpoint);
577-
batch.Write(entry, newcoin);
578-
}
579-
}
580-
batch.Erase(key);
581-
if (batch.SizeEstimate() > batch_size) {
582-
m_db->WriteBatch(batch);
583-
batch.Clear();
584-
m_db->CompactRange(prev_key, key);
585-
prev_key = key;
586-
}
587-
pcursor->Next();
588-
} else {
589-
break;
590-
}
591-
}
592-
m_db->WriteBatch(batch);
593-
m_db->CompactRange({DB_COINS, uint256()}, key);
594-
uiInterface.ShowProgress("", 100, false);
595-
LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
596-
return !ShutdownRequested();
597-
}

src/txdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class CCoinsViewDB final : public CCoinsView
6868
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase = true) override;
6969
std::unique_ptr<CCoinsViewCursor> Cursor() const override;
7070

71-
//! Attempt to update from an older database format. Returns whether an error occurred.
72-
bool Upgrade();
71+
//! Whether an unsupported database format is used.
72+
bool NeedsUpgrade();
7373
size_t EstimateSize() const override;
7474

7575
//! Dynamically alter the underlying leveldb cache size.

test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test/functional/test_runner.py --extended
103103
In order to run backwards compatibility tests, download the previous node binaries:
104104

105105
```
106-
test/get_previous_releases.py -b v19.3.0 v18.2.2 v0.17.0.3 v0.16.1.1 v0.15.0.0
106+
test/get_previous_releases.py -b v19.3.0 v18.2.2 v0.17.0.3 v0.16.1.1 v0.15.0.0 v0.12.1.5
107107
```
108108

109109
By default, up to 4 tests will be run in parallel by test_runner. To specify
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2022 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test that unsupported utxo db causes an init error.
6+
7+
Previous releases are required by this test, see test/README.md.
8+
"""
9+
10+
import shutil
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import assert_equal
14+
15+
16+
class UnsupportedUtxoDbTest(BitcoinTestFramework):
17+
def set_test_params(self):
18+
self.setup_clean_chain = True
19+
self.num_nodes = 2
20+
21+
def skip_test_if_missing_module(self):
22+
self.skip_if_no_previous_releases()
23+
24+
def setup_network(self):
25+
self.add_nodes(
26+
self.num_nodes,
27+
versions=[
28+
120105, # Last release with previous utxo db format
29+
None, # For MiniWallet, without migration code
30+
],
31+
)
32+
33+
def run_test(self):
34+
self.log.info("Create previous version (v0.12.1.5) utxo db")
35+
self.start_node(0)
36+
# 'generatetoaddress' was introduced in v0.12.3, use legacy 'generate' call
37+
block = self.nodes[0].rpc.generate(1)[-1]
38+
assert_equal(self.nodes[0].getbestblockhash(), block)
39+
assert_equal(self.nodes[0].gettxoutsetinfo()["total_amount"], 500)
40+
self.stop_nodes()
41+
42+
self.log.info("Check init error")
43+
legacy_utxos_dir = self.nodes[0].chain_path / "chainstate"
44+
legacy_blocks_dir = self.nodes[0].chain_path / "blocks"
45+
recent_utxos_dir = self.nodes[1].chain_path / "chainstate"
46+
recent_blocks_dir = self.nodes[1].chain_path / "blocks"
47+
shutil.copytree(legacy_utxos_dir, recent_utxos_dir)
48+
shutil.copytree(legacy_blocks_dir, recent_blocks_dir)
49+
self.nodes[1].assert_start_raises_init_error(
50+
expected_msg="Error: Unsupported chainstate database format found. "
51+
"Please restart with -reindex-chainstate. "
52+
"This will rebuild the chainstate database.",
53+
)
54+
55+
self.log.info("Drop legacy utxo db")
56+
self.start_node(1, extra_args=["-reindex-chainstate", "-txindex=0"])
57+
assert_equal(self.nodes[1].getbestblockhash(), block)
58+
assert_equal(self.nodes[1].gettxoutsetinfo()["total_amount"], 500)
59+
60+
61+
if __name__ == "__main__":
62+
UnsupportedUtxoDbTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@
368368
'p2p_sendtxrcncl.py',
369369
'rpc_scantxoutset.py',
370370
'feature_txindex_compatibility.py',
371+
'feature_unsupported_utxo_db.py',
371372
'feature_logging.py',
372373
'feature_anchors.py',
373374
'feature_coinstatsindex.py',

test/get_previous_releases.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import sys
2020
import hashlib
2121

22-
2322
SHA256_SUMS = {
2423
"d1f7121a7d7bdd4077709284076860389d6a0f4481a934ad9acb85cae3d7b83e": "dashcore-20.0.1-aarch64-linux-gnu.tar.gz",
2524
"37375229e5ab18d7050b729fb016df24acdd72d60bc3fa074270d89030a27827": "dashcore-20.0.1-arm-linux-gnueabihf.tar.gz",
@@ -92,6 +91,15 @@
9291
"f532bc7e0360e80908eb6b9c3aeec7e0037e70e25dee3b040dbbf7a124e05619": "dashcore-0.15.0.0-win64-setup.exe",
9392
"3ba6ff98113af30319fb1499d132d993633380476f9980443d630d21a40e0efb": "dashcore-0.15.0.0-win64.zip",
9493
"4cc0815ebd595f3d0134a8df9e6224cbe3d79398a5a899b60ca5f4ab8a576160": "dashcore-0.15.0.0-x86_64-linux-gnu.tar.gz",
94+
#
95+
"060c86587176ffcea5615ea46aa080152ea5ebd035d2a7e0340d312345c2eee0": "dashcore-0.12.1.5-RPi2.tar.gz",
96+
"369dee3e2ae8854592ca696fd5fd7314bc8bbdd865920407bc96d59acb06f81d": "dashcore-0.12.1.5-linux32.tar.gz",
97+
"81692699b99d64b50d1bb1db427f6a0d68c84b98d0e0152f854cfbe048c08fc4": "dashcore-0.12.1.5-linux64.tar.gz",
98+
"b4514d4a705cc1adb400ec0c69630612fe394508decd7bf3edef068021fc47b5": "dashcore-0.12.1.5-osx.dmg",
99+
"35eb7bd9f5883d986b32ae84ddd074566c9e01a438bddd0d147c4a1e96b81b02": "dashcore-0.12.1.5-win32-setup.exe",
100+
"2ea20e8671dcae6ed4f980b8b7dcaa1024961e0267956f4e8bd9eea31b0030e4": "dashcore-0.12.1.5-win32.zip",
101+
"db276b7697777203e98f93e1f498bdab0016f2a96f6454a56c51846120aae0b7": "dashcore-0.12.1.5-win64-setup.exe",
102+
"15ee0d8405609aaf70c31ff320b0468c86e7c6bc1f061ae90d6295a540576c39": "dashcore-0.12.1.5-win64.zip",
95103
}
96104

97105

0 commit comments

Comments
 (0)