@@ -43,7 +43,7 @@ static u32 delayed_to_us_feerate;
4343static u32 htlc_feerate ;
4444
4545/* The feerate for transactions spending from revoked transactions. */
46- static u32 penalty_feerate ;
46+ static u32 penalty_feerate , max_penalty_feerate ;
4747
4848/* Min and max feerates we ever used */
4949static u32 min_possible_feerate , max_possible_feerate ;
@@ -878,10 +878,19 @@ compute_penalty_output_amount(struct amount_sat initial_amount,
878878 struct amount_sat max_output_amount ;
879879 struct amount_sat output_amount ;
880880 struct amount_sat deducted_amount ;
881+ struct amount_sat min_output_amount , max_fee ;
881882
882883 assert (depth <= max_depth );
883884 assert (depth > 0 );
884885
886+ /* We never pay more than max_penalty_feerate; at some point,
887+ * it's clearly not working. */
888+ max_fee = amount_tx_fee (max_penalty_feerate , weight );
889+ if (!amount_sat_sub (& min_output_amount , initial_amount , max_fee ))
890+ /* We may just donate the whole output as fee, meaning
891+ * we get zero amount. */
892+ min_output_amount = AMOUNT_SAT (0 );
893+
885894 /* The difference between initial_amount, and the fee suggested
886895 * by min_rbf_bump, is the largest allowed output amount.
887896 *
@@ -892,11 +901,7 @@ compute_penalty_output_amount(struct amount_sat initial_amount,
892901 */
893902 if (!amount_sat_sub (& max_output_amount ,
894903 initial_amount , min_rbf_bump (weight , depth - 1 )))
895- /* If min_rbf_bump is larger than the initial_amount,
896- * we should just donate the whole output as fee,
897- * meaning we get 0 output amount.
898- */
899- return AMOUNT_SAT (0 );
904+ return min_output_amount ;
900905
901906 /* Map the depth / max_depth into a number between 0->1. */
902907 double x = (double ) depth / (double ) max_depth ;
@@ -910,9 +915,14 @@ compute_penalty_output_amount(struct amount_sat initial_amount,
910915
911916 /* output_amount = initial_amount - deducted_amount. */
912917 if (!amount_sat_sub (& output_amount ,
913- initial_amount , deducted_amount ))
914- /* If underflow, force to 0. */
915- output_amount = AMOUNT_SAT (0 );
918+ initial_amount , deducted_amount )) {
919+ /* If underflow, force to min. */
920+ output_amount = min_output_amount ;
921+ }
922+
923+ /* If output below min, return min. */
924+ if (amount_sat_less (output_amount , min_output_amount ))
925+ return min_output_amount ;
916926
917927 /* If output exceeds max, return max. */
918928 if (amount_sat_less (max_output_amount , output_amount ))
@@ -3908,6 +3918,7 @@ int main(int argc, char *argv[])
39083918 & delayed_to_us_feerate ,
39093919 & htlc_feerate ,
39103920 & penalty_feerate ,
3921+ & max_penalty_feerate ,
39113922 & dust_limit ,
39123923 & our_broadcast_txid ,
39133924 & scriptpubkey [LOCAL ],
0 commit comments