This has become a double negation now that makes it tougher to read imo. We can get rid of setting the success variable here and directly add in the if condition. Moreover, setting success here seems quite unusual to me, just like it was in the previous code as well. I believe it can be directly updated in the if condition.
I notice that the migration is done only if success is false and then updated. But if success evaluates to true at this line, the migration is skipped and success remains true. To handle this, success can be initially set to true. Also, I believe this is an opportunity to narrow the scope of success here because it’s not used in the previous if block at all.
So, the overall diff is below, which is more than originally intended but I believe it makes this code block more readable. All the unit tests and the migration functional tests pass.
0 res.backup_path = backup_path;
1
2- bool success = false;
3-
4 // Unlock the wallet if needed
5 if (local_wallet->IsLocked() && !local_wallet->Unlock(passphrase)) {
6 if (was_loaded) {
7@@ -4529,14 +4527,14 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
8 }
9 }
10
11+ bool success = true;
12 {
13 LOCK(local_wallet->cs_wallet);
14 // First change to using SQLite
15 if (!local_wallet->MigrateToSQLite(error)) return util::Error{error};
16
17 // Do the migration of keys and scripts for non-blank wallets, and cleanup if it fails
18- success = !HasLegacyRecords(local_wallet.get());
19- if (!success) {
20+ if (HasLegacyRecords(local_wallet.get())) {
21 success = DoMigration(*local_wallet, context, error, res);
22 } else {
23 // Make sure that descriptors flag is actually set