When a block reward is spent together with other outputs in the wallet and that block reward is later orphaned, the wallet does not show the other funds as available anymore.  Even a `-rescan` is not enough - only `-zapwallettxes` or an explicit `abandontransaction` "recovers" the funds.  I prepared a [test case](https://github.com/domob1812/bitcoin/blob/wallet-bug-invalidated-block-rewards/test/functional/wallet_bug.py) that shows this behaviour.

Is this a known issue or the expected behaviour?  While this situation is something that will presumably not occur in practice (as it requires a long reorg), I wonder if that transaction should be automatically abandoned as it seems clear it won't be able to be confirmed anymore.
stdunique_ptr<WalletDatabase> new_db = MakeDatabase(db_dir, opts, db_status, error);
    assert(new_db); // This is to prevent doing anything further with this . The original file was deleted, but a backup exists.
    m_databreset();
    m_database = stdmove(new_db);
    // Write existing records into the new DB
    batch = m_database->MakeBatch();
    bool began = batch->TxnBegin();
    assert(began); // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution.
    for (const auto& [key, value]  records) {
        DataStream ss_key{key};
        DataStream ss_value{value};
        if (!batch->Write(ss_key, ss_value)) {
            batch->TxnAbort();
            m_database->Close();
            fsremove(m_database->Filename());
            assert( // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution.
        }
    }
    bool committed = batch->TxnCommit();
    assert(committed); // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution.
    return true;
}
stdoptional<MigrationData> CWalletGetDescriptorsForLegacy(bilingual_str& error) const
{
    AssertLockHeld(cs_);
    LegacyScriptPubKeyMan* legacy_spkm = GetLegacyScriptPubKeyMan();
    if (!legacy_spkm) {
        error = _("Error This  is already a  ");
        return stdnullopt;
    }