@@ -3908,6 +3908,14 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
3908
3908
// Check if the transactions in the wallet are still ours. Either they belong here, or they belong in the watchonly wallet.
3909
3909
// We need to go through these in the tx insertion order so that lookups to spends works.
3910
3910
std::vector<uint256> txids_to_delete;
3911
+ std::unique_ptr<WalletBatch> watchonly_batch;
3912
+ if (data.watchonly_wallet ) {
3913
+ watchonly_batch = std::make_unique<WalletBatch>(data.watchonly_wallet ->GetDatabase ());
3914
+ // Copy the next tx order pos to the watchonly wallet
3915
+ LOCK (data.watchonly_wallet ->cs_wallet );
3916
+ data.watchonly_wallet ->nOrderPosNext = nOrderPosNext;
3917
+ watchonly_batch->WriteOrderPosNext (data.watchonly_wallet ->nOrderPosNext );
3918
+ }
3911
3919
for (const auto & [_pos, wtx] : wtxOrdered) {
3912
3920
if (!IsMine (*wtx->tx ) && !IsFromMe (*wtx->tx )) {
3913
3921
// Check it is the watchonly wallet's
@@ -3916,12 +3924,20 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
3916
3924
LOCK (data.watchonly_wallet ->cs_wallet );
3917
3925
if (data.watchonly_wallet ->IsMine (*wtx->tx ) || data.watchonly_wallet ->IsFromMe (*wtx->tx )) {
3918
3926
// Add to watchonly wallet
3919
- if (!data.watchonly_wallet ->AddToWallet (wtx->tx , wtx->m_state )) {
3920
- error = _ (" Error: Could not add watchonly tx to watchonly wallet" );
3927
+ const uint256& hash = wtx->GetHash ();
3928
+ const CWalletTx& to_copy_wtx = *wtx;
3929
+ if (!data.watchonly_wallet ->LoadToWallet (hash, [&](CWalletTx& ins_wtx, bool new_tx) EXCLUSIVE_LOCKS_REQUIRED (data.watchonly_wallet ->cs_wallet ) {
3930
+ if (!new_tx) return false ;
3931
+ ins_wtx.SetTx (to_copy_wtx.tx );
3932
+ ins_wtx.CopyFrom (to_copy_wtx);
3933
+ return true ;
3934
+ })) {
3935
+ error = strprintf (_ (" Error: Could not add watchonly tx %s to watchonly wallet" ), wtx->GetHash ().GetHex ());
3921
3936
return false ;
3922
3937
}
3938
+ watchonly_batch->WriteTx (data.watchonly_wallet ->mapWallet .at (hash));
3923
3939
// Mark as to remove from this wallet
3924
- txids_to_delete.push_back (wtx-> GetHash () );
3940
+ txids_to_delete.push_back (hash );
3925
3941
continue ;
3926
3942
}
3927
3943
}
@@ -3930,6 +3946,7 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
3930
3946
return false ;
3931
3947
}
3932
3948
}
3949
+ watchonly_batch.reset (); // Flush
3933
3950
// Do the removes
3934
3951
if (txids_to_delete.size () > 0 ) {
3935
3952
std::vector<uint256> deleted_txids;
0 commit comments