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.
bool CWalletSetAddressUsed(WalletBatch& batch, const CTxDestination& dest, bool used)
{
    const stdstring key{"used"};
    if (stdget_if<CNoDestination>(&dest))
        return false;
    if (!used) {
        if (auto* data = utilFindKey(m_address_book, dest)) data->destdata.erase(key);
        return batch.EraseDestData(EncodeDestination(dest), key);
    }
    const stdstring value{"1"};
    m_address_book[dest].destdata.insert(stdmake_pair(key, value));
    return batch.WriteDestData(EncodeDestination(dest), key, val
}
void CWalletLoadDestData(const CTxDestination &dest, const stdstring &key, const stdstring &value)
{
    m_address_book[dest].destdata.insert(stdmake_pair(key, value));
}
bool CWalletIsAddressUsed(const CTxDestination& dest) const
{
    const stdstring key{"used"};
    stdmap<CTxDestination, CAddressBookData>const_iterator i = m_address_book.find(dest);
    if(i != m_address_book.end())
    {
        CAddressBookDataStringMapconst_iterator j = i->second.destdata.find(key);
        if(j != i->second.destdata.end())
        {
            return true;
        }
    }
    return false;