refactor: wallet, simplify addressbook migration

Same process written in a cleaner manner.
Removing code duplication.
This commit is contained in:
furszy 2023-02-26 20:30:02 -03:00
parent d0943315b1
commit 595bbe6e81
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623

View File

@ -4009,52 +4009,41 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
// Check the address book data in the same way we did for transactions // Check the address book data in the same way we did for transactions
std::vector<CTxDestination> dests_to_delete; std::vector<CTxDestination> dests_to_delete;
for (const auto& addr_pair : m_address_book) { for (const auto& [dest, record] : m_address_book) {
// Labels applied to receiving addresses should go based on IsMine // Ensure "receive" entries that are no longer part of the original wallet are transferred to another wallet
if (addr_pair.second.purpose == AddressPurpose::RECEIVE) { // Entries for everything else ("send") will be cloned to all wallets.
if (!IsMine(addr_pair.first)) { bool require_transfer = record.purpose == AddressPurpose::RECEIVE && !IsMine(dest);
// Check the address book data is the watchonly wallet's bool copied = false;
if (data.watchonly_wallet) { for (auto& wallet : {data.watchonly_wallet, data.solvable_wallet}) {
LOCK(data.watchonly_wallet->cs_wallet); if (!wallet) continue;
if (data.watchonly_wallet->IsMine(addr_pair.first)) {
// Add to the watchonly. Copy the entire address book entry
data.watchonly_wallet->m_address_book[addr_pair.first] = addr_pair.second;
dests_to_delete.push_back(addr_pair.first);
continue;
}
}
if (data.solvable_wallet) {
LOCK(data.solvable_wallet->cs_wallet);
if (data.solvable_wallet->IsMine(addr_pair.first)) {
// Add to the solvable. Copy the entire address book entry
data.solvable_wallet->m_address_book[addr_pair.first] = addr_pair.second;
dests_to_delete.push_back(addr_pair.first);
continue;
}
}
// Skip invalid/non-watched scripts that will not be migrated LOCK(wallet->cs_wallet);
if (not_migrated_dests.count(addr_pair.first) > 0) { if (require_transfer && !wallet->IsMine(dest)) continue;
dests_to_delete.push_back(addr_pair.first);
continue;
}
// Not ours, not in watchonly wallet, and not in solvable // Copy the entire address book entry
error = _("Error: Address book data in wallet cannot be identified to belong to migrated wallets"); wallet->m_address_book[dest] = record;
return false;
copied = true;
// Only delete 'receive' records that are no longer part of the original wallet
if (require_transfer) {
dests_to_delete.push_back(dest);
break;
} }
} else { }
// Labels for everything else ("send") should be cloned to all
if (data.watchonly_wallet) { // Fail immediately if we ever found an entry that was ours and cannot be transferred
LOCK(data.watchonly_wallet->cs_wallet); // to any of the created wallets (watch-only, solvable).
// Add to the watchonly. Copy the entire address book entry // Means that no inferred descriptor maps to the stored entry. Which mustn't happen.
data.watchonly_wallet->m_address_book[addr_pair.first] = addr_pair.second; if (require_transfer && !copied) {
}
if (data.solvable_wallet) { // Skip invalid/non-watched scripts that will not be migrated
LOCK(data.solvable_wallet->cs_wallet); if (not_migrated_dests.count(dest) > 0) {
// Add to the solvable. Copy the entire address book entry dests_to_delete.push_back(dest);
data.solvable_wallet->m_address_book[addr_pair.first] = addr_pair.second; continue;
} }
error = _("Error: Address book data in wallet cannot be identified to belong to migrated wallets");
return false;
} }
} }