in #34232 dergoegge reported an assertion fail in SetBumpFeeDiscount.
Context
The bump-fee discount in the savings that we can have when multiple UTXOs that we selected for our transaction share a common unconfirmed ancestor transaction.
We know we can have some savings because we first calculate the summed_bump_fees and then compare to the combined_bump_fee.
For context:
bump_fees: Extra fees that the new transaction must pay to contribute to get his ancestor confirmed.summed_bump_fees: The sum of each input ancestorbump_fee. If we have two inputs with unconfirmed ancestors A and B and both have abump_fee = 100thensummed_bump_fees = 200.combined_bump_fee: Is the totalbump_feessummed of all inputs, but taking into account shared ancestors. If A and B share the transaction ancestor thencombined_bump_fee = 100not200as the transaction must be bumped only once.
If summed_bumpfees > combined_bump_fee we are overestimating the bump_fee as we are counting multiple times the same ancestors so we can discount it using SetBumpFeeDiscount.
Problem
To calculate summed_bump_fees and combined_bump_fee we use two different fresh MiniMiner snapshots of the mempool. Because they are called in different moments the two snapshots of the mempool might be different. An artificial feerate decrease of an ancestor using prioritizesettransaction can make combined_bump_fee > summed_bump_fees creating a negative discount. This cause calling SetBumpFeeDiscount with a negative vaule triggering an assertion discount >= 0.
Fix
This PR fixes it by ensuring not only that a discount exist but also that is greater the 0.
Test
It is hard to manually trigger this race condition. dergoegge coded a patch and test to trigger it that can be used to test the fix. https://github.com/polespinasa/bitcoin/commit/5320e2fd215734ff3b7c59b07108ca5c5f7e075f