Merge bitcoin/bitcoin#26910: wallet: migrate wallet, exit early if no legacy data exist

6d31900e52 wallet: migrate wallet, exit early if no legacy data exist (furszy)

Pull request description:

  The process first creates a backup file then return an error,
  without removing the recently created file, when notices that
  the db is already running sqlite.

ACKs for top commit:
  john-moffett:
    ACK 6d31900e52
  achow101:
    ACK 6d31900e52
  ishaanam:
    crACK 6d31900e52

Tree-SHA512: 9fb52e80de96e129487ab91bef13647bc4570a782003b1e37940e2a00ca26283fd24ad39bdb63a984ae0a56140b518fd0d74aa2fc59ab04405b2c179b7d3c54a
This commit is contained in:
Andrew Chow 2023-02-01 17:05:25 -05:00
commit fdd363ebd9
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41
2 changed files with 10 additions and 4 deletions

View File

@ -3871,10 +3871,7 @@ std::optional<MigrationData> CWallet::GetDescriptorsForLegacy(bilingual_str& err
AssertLockHeld(cs_wallet); AssertLockHeld(cs_wallet);
LegacyScriptPubKeyMan* legacy_spkm = GetLegacyScriptPubKeyMan(); LegacyScriptPubKeyMan* legacy_spkm = GetLegacyScriptPubKeyMan();
if (!legacy_spkm) { assert(legacy_spkm);
error = _("Error: This wallet is already a descriptor wallet");
return std::nullopt;
}
std::optional<MigrationData> res = legacy_spkm->MigrateToDescriptor(); std::optional<MigrationData> res = legacy_spkm->MigrateToDescriptor();
if (res == std::nullopt) { if (res == std::nullopt) {
@ -4170,6 +4167,11 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error,
util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>&& wallet, WalletContext& context) util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>&& wallet, WalletContext& context)
{ {
// Before anything else, check if there is something to migrate.
if (!wallet->GetLegacyScriptPubKeyMan()) {
return util::Error{_("Error: This wallet is already a descriptor wallet")};
}
MigrationResult res; MigrationResult res;
bilingual_str error; bilingual_str error;
std::vector<bilingual_str> warnings; std::vector<bilingual_str> warnings;

View File

@ -163,6 +163,10 @@ class WalletMigrationTest(BitcoinTestFramework):
assert_equal(basic2.getbalance(), basic2_balance) assert_equal(basic2.getbalance(), basic2_balance)
self.assert_list_txs_equal(basic2.listtransactions(), basic2_txs) self.assert_list_txs_equal(basic2.listtransactions(), basic2_txs)
# Now test migration on a descriptor wallet
self.log.info("Test \"nothing to migrate\" when the user tries to migrate a wallet with no legacy data")
assert_raises_rpc_error(-4, "Error: This wallet is already a descriptor wallet", basic2.migratewallet)
def test_multisig(self): def test_multisig(self):
default = self.nodes[0].get_wallet_rpc(self.default_wallet_name) default = self.nodes[0].get_wallet_rpc(self.default_wallet_name)