Follow-up work to my comment in #25239.
Guarding and alerting the user about a wallet invalid state during chain synchronization.
Explanation
if the AddToWallet
tx write fails, the method returns a wtx nullptr
without removing the recently added transaction from the wallet’s map.
Which makes that AddToWalletIfInvolvingMe
return false (even when the tx is on the wallet’s map already), –> which makes SyncTransaction
skip the MarkInputsDirty
call –> which leads to a wallet invalid state where the inputs of this new transaction are not marked dirty, while the transaction that spends them still exist on the in-memory wallet tx map.
Plus, as we only store the arriving transaction inside AddToWalletIfInvolvingMe
when we synchronize/scan block/s from the chain and nowhere else, it makes sense to treat the transaction db write error as a runtime error to notify the user about the problem. Otherwise, the user will lose all the not stored transactions after a wallet shutdown (without be able to recover them automatically on the next startup because the chain sync would be above the block where the txs arrived).
Note: On purpose, the first commit adds test coverage for it. Showing how the wallet can end up in an invalid state. The second commit corrects it with the proposed solution.