When a reorg lowers the tip, removeForReorg can evict BIP68 transactions that are still valid at the new tip, together with all their descendants.
A transaction that spends an unconfirmed parent gets its lockpoints cached with that input at height tip+1. After invalidateblock, TestLockPointValidity can still pass on those cached lockpoints (always when every input was unconfirmed, since maxInputBlock is then genesis; and with mixed confirmed and unconfirmed inputs when the confirmed input's block survives), so CheckSequenceLocksAtTip is evaluated against stale values and the transaction is removed.
The fix, suggested by ismaelsadeeq, is to never evict on cached lockpoints alone: if they are not valid at the new tip, or the cached sequence-lock check fails, recalculate them with CalculateLockPointsAtTip and evict only if the fresh result also fails. This covers height-based and time-based locks alike, at the cost of some recalculations that turn out to be unnecessary.
test_reorg_stale_lockpoints() in feature_bip68_sequence.py covers four cases across a reorg: a BIP68-disabled child, a BIP68-enabled child of an unconfirmed parent, a child with mixed confirmed and unconfirmed inputs, and a time-based zero lock where the reorg lowers the median time past (test by Bicaru20). The test fails on master, and the mixed-input case also fails on the earlier genesis-only version of this fix.
test_reorg_unsatisfied_lock_evicted() (by instagibbs) is the counterpart: it checks that transactions whose relative lock is genuinely unsatisfied after the reorg are still evicted, together with their descendants.
Reproducer for unpatched nodes: https://gist.github.com/javierpmateos/c55d365973adbf488a852dc5e0b77dec
Same class of issue fixed for TRUC in #33504.
Closes #35007