This PR is a reattempt of #19645, which has been abandoned.
Currently, an existing mempool transaction can’t be replaced by a transaction that’s identical except for its witness (same txid, different wtxid); the request is rejected as a duplicate. This PR changes this behavior such that the mempool will replace the existing transaction if the new transaction has a smaller witness by at least 5% (an anti-Dos measure). The replacement transaction can’t increase the fee (since its inputs and outputs cannot change), but it necessarily has a higher fee rate than the existing transaction, since its overall size is lower. Miners should always prefer the replacement transaction.
An important difference between what this PR implements and the existing RBF (replace-by-fee, BIP125) is that the replacement can occur even if the original transaction is not marked as replaceable. This is because the replacement transaction will pay the same outputs as the transaction being replaced (identical txids ensures this), so the recipients of the existing transaction have no reason to fear not being paid by the replacement transaction. Unlike RBF, all of the existing transaction’s descendants, if any, remain in the mempool.
Initially, we’ll limit this to single transactions (not packages); this can be reconsidered later.