Skip to content

Commit c62a8d0

Browse files
committed
wallet: Keep txs that belong to both watchonly and migrated wallets
It is possible for a transaction that has an output that belongs to the mgirated wallet, and another output that belongs to the watchonly wallet. Such transaction should appear in both wallets during migration.
1 parent 71cb28e commit c62a8d0

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/wallet/wallet.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,30 +3958,33 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
39583958
}
39593959
}
39603960
for (const auto& [_pos, wtx] : wtxOrdered) {
3961-
if (!IsMine(*wtx->tx) && !IsFromMe(*wtx->tx)) {
3962-
// Check it is the watchonly wallet's
3963-
// solvable_wallet doesn't need to be checked because transactions for those scripts weren't being watched for
3964-
if (data.watchonly_wallet) {
3965-
LOCK(data.watchonly_wallet->cs_wallet);
3966-
if (data.watchonly_wallet->IsMine(*wtx->tx) || data.watchonly_wallet->IsFromMe(*wtx->tx)) {
3967-
// Add to watchonly wallet
3968-
const uint256& hash = wtx->GetHash();
3969-
const CWalletTx& to_copy_wtx = *wtx;
3970-
if (!data.watchonly_wallet->LoadToWallet(hash, [&](CWalletTx& ins_wtx, bool new_tx) EXCLUSIVE_LOCKS_REQUIRED(data.watchonly_wallet->cs_wallet) {
3971-
if (!new_tx) return false;
3972-
ins_wtx.SetTx(to_copy_wtx.tx);
3973-
ins_wtx.CopyFrom(to_copy_wtx);
3974-
return true;
3975-
})) {
3976-
error = strprintf(_("Error: Could not add watchonly tx %s to watchonly wallet"), wtx->GetHash().GetHex());
3977-
return false;
3978-
}
3979-
watchonly_batch->WriteTx(data.watchonly_wallet->mapWallet.at(hash));
3980-
// Mark as to remove from this wallet
3961+
// Check it is the watchonly wallet's
3962+
// solvable_wallet doesn't need to be checked because transactions for those scripts weren't being watched for
3963+
bool is_mine = IsMine(*wtx->tx) || IsFromMe(*wtx->tx);
3964+
if (data.watchonly_wallet) {
3965+
LOCK(data.watchonly_wallet->cs_wallet);
3966+
if (data.watchonly_wallet->IsMine(*wtx->tx) || data.watchonly_wallet->IsFromMe(*wtx->tx)) {
3967+
// Add to watchonly wallet
3968+
const uint256& hash = wtx->GetHash();
3969+
const CWalletTx& to_copy_wtx = *wtx;
3970+
if (!data.watchonly_wallet->LoadToWallet(hash, [&](CWalletTx& ins_wtx, bool new_tx) EXCLUSIVE_LOCKS_REQUIRED(data.watchonly_wallet->cs_wallet) {
3971+
if (!new_tx) return false;
3972+
ins_wtx.SetTx(to_copy_wtx.tx);
3973+
ins_wtx.CopyFrom(to_copy_wtx);
3974+
return true;
3975+
})) {
3976+
error = strprintf(_("Error: Could not add watchonly tx %s to watchonly wallet"), wtx->GetHash().GetHex());
3977+
return false;
3978+
}
3979+
watchonly_batch->WriteTx(data.watchonly_wallet->mapWallet.at(hash));
3980+
// Mark as to remove from the migrated wallet only if it does not also belong to it
3981+
if (!is_mine) {
39813982
txids_to_delete.push_back(hash);
3982-
continue;
39833983
}
3984+
continue;
39843985
}
3986+
}
3987+
if (!is_mine) {
39853988
// Both not ours and not in the watchonly wallet
39863989
error = strprintf(_("Error: Transaction %s in wallet cannot be identified to belong to migrated wallets"), wtx->GetHash().GetHex());
39873990
return false;

0 commit comments

Comments
 (0)