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.
assert(((flags & KNOWN_WALLET_FLAGS) >> 32) == (flags >> 32));
    // T
    assert(m__flags == 0);
    if (!WalletBatch(GetDatabase()).WriteWalletFlags(flags)) {
        throw stdruntime_error(stdstring(__func__) + " writing  flags failed");
    }
    if (!LoadWalletFlags(flags)) assert(
}
// Helper for producing a max-sized low-S low-R signature (eg 71 bytes)
// or a max-sized low-S signature (e.g. 72 bytes) depending on coin_control
bool DummySignInput(const SigningProvider& provider, CTxIn &tx_in, const CTxOut &txout, const CCoinControl* coin_control)
{
    // F
    const CScript& scriptPubKey = txout.scriptPubKey;
    SignatureData sigdata;
    // U
    // t
    const bool use_max_sig = coin_control && (coin_control->fAllowWatchOnly || coin_control->IsExternalSelected(tx_in.prevout));
    if (!ProduceSignature(provider, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR  DUMMY_SIGNATURE_CREATOR, scriptPubKey, sigdata)) {
        return false;
    }
    UpdateInput(tx_in, sigdata);
    return true;
}
bool FillInputToWeight(CTxIn& txin, int64_t target_weight)
{
    assert(txin.scriptSig.empty());
    assert(txin.scriptWitness.IsNull());
    int64_t txin_weight = GetTransactionInputWeight(txin);
    // D