Assuming a uniform distribution, what is the chance this f gets through? * Here we compute the conditional probability of success for a flow f, given * the knowledge that the liquidity is in the range [a,b) and some amount * x is already committed on another part of the payment. * * The probability equation for x=0 is: * * prob(f) = * * for f =f>=a: (b-f)/(b-a) * for b
| 603 | * shifted by x amount, the new bounds be [MAX(0,a-x),b-x). |
| 604 | */ |
| 605 | double edge_probability(struct amount_msat min, struct amount_msat max, |
| 606 | struct amount_msat in_flight, struct amount_msat f) |
| 607 | { |
| 608 | assert(amount_msat_less_eq(min, max)); |
| 609 | assert(amount_msat_less_eq(in_flight, max)); |
| 610 | |
| 611 | const struct amount_msat one = AMOUNT_MSAT(1); |
| 612 | struct amount_msat B = max; // = max +1 - in_flight |
| 613 | |
| 614 | // one past the last known value, makes computations simpler |
| 615 | if (!amount_msat_accumulate(&B, one)) |
| 616 | goto function_fail; |
| 617 | |
| 618 | // in_flight cannot be greater than max |
| 619 | if (!amount_msat_deduct(&B, in_flight)) |
| 620 | goto function_fail; |
| 621 | |
| 622 | struct amount_msat A = min; // = MAX(0,min-in_flight); |
| 623 | |
| 624 | if (!amount_msat_deduct(&A, in_flight)) |
| 625 | A = AMOUNT_MSAT(0); |
| 626 | |
| 627 | struct amount_msat denominator; // = B-A |
| 628 | |
| 629 | // B cannot be smaller than or equal A |
| 630 | if (!amount_msat_sub(&denominator, B, A) || amount_msat_less_eq(B, A)) |
| 631 | goto function_fail; |
| 632 | |
| 633 | struct amount_msat numerator; // MAX(0,B-f) |
| 634 | |
| 635 | if (!amount_msat_sub(&numerator, B, f)) |
| 636 | numerator = AMOUNT_MSAT(0); |
| 637 | |
| 638 | return amount_msat_less_eq(f, A) |
| 639 | ? 1.0 |
| 640 | : amount_msat_ratio(numerator, denominator); |
| 641 | |
| 642 | function_fail: |
| 643 | return -1; |
| 644 | } |
| 645 | |
| 646 | enum renepay_errorcode |
| 647 | chan_extra_remove_htlc(struct chan_extra_map *chan_extra_map, |