Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

Fee Budgets: A Possible Path Towards Unified Cost Functions For Lightning Pathfinding Problems

6 messages, Aug 21 to 30, 2021. Started by ZmnSCPxj.

ZmnSCPxj (4), Stefan Richter (2)

Permalink First message in the Wayback Machine

ZmnSCPxj permalink original

Subject: Fee Budgets: A Possible Path Towards Unified Cost Functions For Lightning Pathfinding Problems

Introduction ============

What is the cost of a failed LN payment?

Presumably, if a user wants to pay in exchange for something, that user values that thing higher than the Bitcoin they spend on that thing. This is a central assumption of free market economics, that all transactions are voluntary and that all participants in the transaction get more utility out of the transaction than what they put in.

Note that ***value is subjective***. For example, a farmer values the food they sell less than the buyer of that food, because the farmer has leet farming skillz that actually let them **grow food** from literal shit, sunlight, and water, and presumably the buyer does not have those leet skillz0rs to convert literal shit to food using sunlight and water (otherwise they would be growing their own food). This applies for all production, given that you puny humans have such limited time to learn and train leet skillz.

Thus, for a buyer, there is a difference in value between the product they are buying, and the BTC they are sacrificing to the elder gods (i.e. the payment network and the seller) in order to get the product. The buyer must value the product more than the BTC.

This difference, then, is the cost of a failed payment. If the attempt to pay fails, then obviously the seller will not be willing to send the product (as it can receive no money for it) and the buyer loses the (buyer-subjective) value of the product minus the value of the BTC they wanted to use to pay.

This difference in value, while subjective, is quantifiable (consider how judges at a beauty contest must convert their subjective judgment of beauty to a number; indeed, horny humans do this all the time at bars, suggesting that even judgment-impaired humans intuitively understand that subjective values can be quantified). And that quantifiable subjective value can be measured in units of bitcoin.

Thus, this difference in value is the cost of failure of an LN payment. And due to the relationship of failure and success, the cost of failure is the value of success.

Pickhardt-Richter Payments ==========================

Why is the cost of failure/value of success even relevant?

In [a 2021 paper](https://arxiv.org/abs/2107.05322) Pickhardt and Richter present a method of estimating the probability of payment success, and using that probability-of-success as a cost function for a generalization of pathfinding algorithms (specifically minimum cost flow).

Of course, probabilities of success are not the only concern that actual pathfinding algorithms need to worry about. Another two concerns are:

* Actual fees (measured in bitcoin units).
* Actual total cltv-delta (measured in blocks).

It is possible to convert total cltv-delta to a "Fee". Basically, the cost of the total cltv-delta is the value of your funds being locked and unuseable for that many blocks. This can be represented as an expected annual return on investment if those funds were instead locked into some investment. In C-Lightning this is the `riskfactor` parameter.

This implies that total cltv-delta can be converted to an equivalent amount of BTCs.

Now, the issue is, how can we convert probability of success to some equivalent amount of BTCs?

This is why the value of success --- i.e. the cost of payment failure --- is relevant.

By multiplying the cost of failure by the probability of failure, we can acquire a bitcoins-measured quantity that can be added directly to expected fees.

Fee Budgets ===========

Long ago, some weird rando with an unpronouncable name decided to add a "fee budget" to his implementation of C-Lightning pay algorithm (back when C-Lightning did not even have a `pay` algorithm). For some reason (possibly dark ritual), that rando managed to get that code into the actual C-Lightning, and the fee budget --- known as `maxfeepercent` --- has been retained to this day, even though none of the original code has survived (dark rituals tend to consume anything involved in their rites, I would not be surprised if that includes source code).

Now consider --- how would a buyer assign a fee budget for a particular payment?

As we noted, a rational buyer will only buy if they believe the value of the product being bought is higher than the value of the BTCs they sacrifice to buy that product. This difference is the cost of failure (equivalent to value of success).

And a rational buyer will be willing to pay, as fee, any value up to this cost of failure/value of success.

For example, if the buyer is not willing to pay more than half the cost of failure, then if there is no way to succeed payments at half the cost of failure, then the payment simply fails and the buyer loses the entire cost of failure. Logically, the buyer must be willing to pay, as fees, up to the cost of failure.

Similarly, if the buyer is willing to pay up to twice the cost of failure, then if it succeeds only by paying up to twice the cost of failure, even if the payment pushes through and the buyer gets the product, the buyer still lost the cost of failure because it paid more fees than the value of the payment success was.

Logically, then, the buyer must specify as fee budget, its expected value from acquiring the product minus the price of the product.

Thus, it so happens that the fee budget is, in fact, the value of payment success/cost of payment failure, subjectively determined by the payer, quantified, and provided to the C-Lightning payment algorithm!

Unified Cost Function =====================

The cost of failure is then:

    fee_budget * (1 - success_probability)

The `fee_budget` is the above fee budget, an input from the user. `success_probability` is the estimate as determined using the Pickhardt-Richter algorithm.

The cost of a channel that charges `fee` is:

    fee + fee_budget * (1 - success_probability)

However, we should note that the above cost function is really the expected cost for the *entire payment*. In particular, `success_probability` is multiplicative along a path, whereas `fee` is additive.

When we consider multipath payments, we should also observe that the `success_probability` of each sub-payment are multiplied together (since all sub-payments must succeed) while `fee` is again additive in nature.

Because of this, there is probably no *existing* pathfinding algorithm which can actually *use* this cost function. Every pathfinding algorithm uses addition to compute total costs.

The Pickhardt-Richter paper gets around this by using the logarithm of the probability. As addition of logarithms is equivalent to multiplication (`log A + log B = log (A * B)`), this converts the addition operations of existing pathfinding algos to multiplication of the probabilities.

This technique cannot work with the above unified cost function, unfortunately.

However, we can consider that, if we neglect `fee`, and use only the logarithms of the cost function, then the `fee_budget` term is effectively a constant cost for all channels on the network. That is, `log (fee_budget * success_probability) = log fee_budget + log success_probability` for all channels on the network, and we can subtract `log fee_budget` on all channels without changing the result of any pathfinding algorithms (provided we do not get into zero or negative costs). Thus, this instance of the Pickhard-Richter technique is equivalent to neglecting both the `fee` and the `fee_budget` in our unified cost function.

Generalized `#zerobasefee` ==========================

If `fee` is small compared to `fee_budget`, then we can consider the effect of the `fee` term to be negligible compared to `fee_budget` term.

Here is how the above cost function looks like, with `fee_budget` distributed:

    fee + fee_budget - fee_budget * success_probability

If `fee_budget` is very much higher than `fee` then we can neglect `fee` as an approximation.

    fee_budget - fee_budget * success_probability

Since `fee_budget` is constant for all paths and for all channels, we can just use the negative logarithm of `success_probability` as the cost function.

As a heuristic, we can *ignore* fees and just use nagetive log probability, as suggested in the Pickhardt-Richter paper, *after* pruning channels whose fees are very high (i.e. prune channels whose `fee`, say, exceeds 1% of the `fee_budget`).

One way to view `#zerobasefee` is that this is a specialized instance of the above general heuristic, that we can prune channels with non-zero base fees in order to operate a pathfinding algorithm that uses only addition for edge costs and use negative log probability for edge costs.

We can instead consider that *small enough* base fees, which are negligible compared to the `fee_budget`, should not be pruned, and not specifically that all non-zero base fees should be pruned. That is, `#zerobasefee` assumes that a 1-sat base fee is *not* negligible compared to the `fee_budget`.

However, for large enough payments, the `fee_budget` may be large enough that a 1 satoshi base fee is actually negligible compared to the `fee_budget` term. This implies that `#zerobasefee` is a specific heuristic, one that potentially could be generalized.

As a corollary, the higher `success_probability` is, the more small fees matter (since `success_probability` subtracts from `fee_budget`). And higher `success_probability` arises from larger channels in general. This leads to the counterintuition that larger channels should charge lower fees, since logically the `#lowbasefee` pruning level should be lower at higher `success_probability`.

Alternative Pathfinding? ========================

As noted, practically every pathfinding algorithm assumes that costs along every edge are always added together.

For quantities that must be multiplied --- such as probabilities --- we can use the logarithm trick to convert the addition to a multiplication.

However, as noted, the unified cost function has quantities that, in order to combine, must be added (fees) and multiplied (probabilities).

In essence, every pathfinding algorithm cannot use this unified cost function, as they assume cost functions that are trivially monoid.

Or is it?

For something like the family of pathfinding algorithms Greedy, A\*, and Dijkstra, the only operations needed on costs are:

* Addition (actually, a monoidal operation).
* Comparison.

Rather than "add", perhaps a better term would be to "aggregate" the costs.

    class (Monoid type) where
         zero :: type
         `<*>` :: type -> type -> type
         -- laws where
         --     forall (a :: type) => zero <*> a = a
         --     forall (a :: type) => a <*> zero = a
         --     forall (a :: type, b :: type) => a <*> b = b <*> a
         --     forall (a :: type, b :: type, c :: type) => (a <*> b) <*> c = a <*> (b <*> c)

In the above, `<*>` is an "aggregate" operation that replaces the simple addition `+` traditionally used in pathfinding algorithms. For typical numeric types, for example, you could derive an addition monoid or a multiplication monoid.

We can consider a type that is both `Monoid` and `Ord`:

    -- This will be defined by an actual run of the
    -- pathfinding algorithm.
    feeBudget :: Integer
    feeBudget = undefined
    data UnifiedCost = UnifiedCost { fee :: Integer
                                   , successProbability :: Rational
                                   }
    -- addition
    instance (Monoid UnifiedCost) where
        zero = UnifiedCost { fee = 0
                           , successProbability = 0
                           }
        a <*> b = UnifiedCost { fee = fee a + fee b
                              , successProbability = successProbability a
                                                   * successProbability b
                              }
    -- comparison
    computeCost :: UnifiedCost -> Rational
    computeCost c = toRational (fee c)
                  + ( toRational feeBudget
                    * (1.0 - successProbability c))
    instance (Eq UnifiedCost) where
        a == b = computeCost a == computeCost b
    instance (Ord UnifiedCost) where
        compare a b = compare (computeCost a) (computeCost b)
    -- laws where
    --    forall (a :: UnifiedCost, b :: UnifiedCost) => a <*> b >= a
    --    forall (a :: UnifiedCost, b :: UnifiedCost) => a <*> b >= b
    -- -- laws could be checked with QuickCheck
    -- -- though we should ensure `0 <= successProbability <= 1`

That type would work well to replace the type of the cost in the Dijkstra-A\*-Greedy family of algortihms; they only need comparison and a monoid operation, but the actual structure of the cost type is immaterial to the algorithm.

In particular, `feeBudget` is constant for an entire run of pathfinding algorithms.

The question is whether minimum cost flow algos could work with the above limited type. I probably need to go actually study those algos.

ZmnSCPxj in reply to ZmnSCPxj permalink original
Quoted text (2 lines)
>     Alternative Pathfinding?
>     ========================

Or to put this section more succinctly: Why should cost be a number?

What operations do the minimum cost flow algorithms demand of this thing called "cost", and can we provide those operations using something which is not a number but is instead a different structure? What is the minimal interface that the mincostflow algo demands of this "cost" datatype?

Regards, ZmnSCPxj

Stefan Richter in reply to ZmnSCPxj permalink original

Hi Zmn! That is some amazing lateral thinking you have been applying there. I'm quite certain I haven't understood everything fully, but it has been highly entertaining to read. Will have to give it a closer read when I get some time.

As a first impression, here are some preliminary observations: While I highly like the Haskell-style datatype, and the algorithm we use does mostly use Dijkstra pathfinding, I think what is really important in your definition is the computeCost definition. This is what we would call the cost function IIUC, and in order to be able to solve min-cost flow problems it generally has to be separable and convex. I believe your datatype merely hides the fact that it is neither.

Intuitively, I think that any cost function that implies a fixed cost (that is, independent of the amount, though it might be different for every edge) per edge is concave and in theory problematic for min-cost flow algorithms because you could reduce some kind of NP-hard selection problem to it. I believe that applies to most if not all of your ideas in the text. Again, I think we should think more about how much of a problem that is in practice, because we do have tools like approximation and parameterized algorithms, as well as heuristics, and I also believe that, say, a moderate base fee will not change the optimal flow much, because this will always prefer large Htlcs anyway in order to optimize probability.

I am really grateful that you have been taking the time to read and understand our paper and have been thinking further in this fascinating way. I am certain good things will come of it in time.

Cheers,
  Stefan

ZmnSCPxj <ZmnSCPxj at protonmail.com> schrieb am Sa., 21. Aug. 2021, 03:49:

Quoted text (16 lines)
>
> >     Alternative Pathfinding?
> >     ========================
>
>
> Or to put this section more succinctly: Why should cost be a number?
>
> What operations do the minimum cost flow algorithms demand of this thing
> called "cost", and can we provide those operations using something which is
> not a number but is instead a different structure?
> What is the minimal interface that the mincostflow algo demands of this
> "cost" datatype?
>
> Regards,
> ZmnSCPxj
>
ZmnSCPxj in reply to Stefan Richter permalink original

Good morning Stefan,

Quoted text (3 lines)
> Hi Zmn! That is some amazing lateral thinking you have been applying there. I'm quite certain I haven't understood everything fully, but it has been highly entertaining to read. Will have to give it a closer read when I get some time.
>
> As a first impression, here are some preliminary observations: While I highly like the Haskell-style datatype, and the algorithm we use does mostly use Dijkstra pathfinding, I think what is really important in your definition is the computeCost definition. This is what we would call the cost function IIUC, and in order to be able to solve min-cost flow problems it generally has to be separable and convex. I believe your datatype merely hides the fact that it is neither.

Well, it really depends on what min flow cost algorithms actually assume of the "numbers" being used.

For instance, it is well known that the Dijkstra-A\*-Greedy family of algorithms do not handle "negative costs". What it really means is that the algorithms assume:

    a + b >= a
    a + b >= b

This holds if `a` and `b` are naturals (0 or positive), but not if they are integers. 1 + -1 = 0, and 0 >= 1 is not true, thus the type for costs in those algorithms cannot be integer types, they have to be naturals. However if you restrict the type to naturals, `a + b >= a` holds, and thus Dijkstra and its family of algorithms work.

Thus, if you are going to use Dijkstra-A\*-Greedy, you "only" need to have the following "operations":

    `+` :: Cost -> Cost -> Cost
    `<` :: Cost -> Cost -> Bool
    zero :: Cost

With the following derived operations:

    a > b = b < a
    a >= b = not (a < b)
    a <= b = not (b < a)
    a == b = (a >= b) && (a <= b)
    a /= b = (a < b) || (a > b)

And following the laws:

    forall (a :: Cost) => a + zero == a
    forall (a :: Cost) => zero + a == a
    forall (a :: Cost, b :: Cost) => a + b == b + a
    forall (a :: Cost, b :: Cost, c :: Cost) => (a + b) + c == a + (b + c)
    forall (a :: Cost, b :: Cost) => a + b >= a
    forall (a :: Cost, b :: Cost) => a + b >= b

As a non-mathist I have no idea what "separable" and "convex" actually mean. Basic search for "convex" and "concave" tends to show up information in geometry, which I think is not related (though it is possible there is some extension of the geometric concept to pure number theory?). And definitions on "separable" are not understandable by me, either.

What exactly are the operations involved, and what are the laws those operations must follow, for the data type to be "separable" and "convex" (vs."concave")?

I guess my problem as well is that I cannot find easy-to-understand algorithms for min cost flow --- I can find discussions on the min cost flow "problem", and some allusions to solutions to that problem, but once I try looking into algorithms it gets quite a bit more complicated.

Basically: do I need these operations?

    `*` :: Cost -> Cost -> Cost
    `/` :: Cost -> Cost -> Cost --- or Maybe Cost

If not, then why cannot `type Cost = UnifiedCost`?

For example, this page: https://www.topcoder.com/thrive/articles/Minimum%20Cost%20Flow%20Part%20Two:%20Algorithms

Includes this pseudocode:

    Transform network G by adding source and sink
    Initial flow x is zero
    while ( Gx contains a path from s to t ) do
        Find any shortest path P from s to t
        Augment current flow x along P
        update Gx

If "find any shortest path" is implemented using Dijkstra-A\*-Greedy, then that does not require `Cost` to be an actual numeric type, they just require a type that provides `+`, `<`, and `zero`, all of which follow the laws I pointed out, *and no more than those*. `UnifiedCost` follows those laws (tough note that my definition of `zero` has a bug, `successProbability` should be `1.0` not `0`).

In short --- the output of the cost function is a `UnifiedCost` structure and ***not*** a number (in the traditional sense).

Basically, I am deconstructing numbers here and trying to figure out what makes them tick, and seeing if I can use a different type to provide the "tick".

Regards, ZmnSCPxj

Stefan Richter in reply to ZmnSCPxj permalink original

Good Morning Zmn!

If you'd like to understand the min-cost flow problem and algorithms better, I would really recommend the textbook we have been citing throughout the paper.

The algorithm you have found has a few shortcomings. It'll only work for the linear min-cost flow problem, and it is very slow. In reality, we need to deal with convex cost functions, and the algorithm we have used so far uses an approach called capacity scaling in order to be much faster. It is indeed complex enough that it has taken us about two months to understand and implement it, discovering a nice heuristic in the process of making mistakes.

Separable in this context means that you can simply add up the costs of the edges to get the total costs. On second thought, your definition would probably work here , by redefining adding up.

Convex here means that for any two amounts x, y, the cost function f in the interval x, y does not lie below the line connecting the two points (x, f(x)) and (y, f(y)). The intuition here is that a linear approximation never overestimates the real cost. I guess one would need a more involved definition for your more complex coordinates.

Cheers,
  Stefan

ZmnSCPxj <ZmnSCPxj at protonmail.com> schrieb am Di., 24. Aug. 2021, 01:06:

Quoted text (110 lines)
> Good morning Stefan,
>
> > Hi Zmn! That is some amazing lateral thinking you have been applying
> there. I'm quite certain I haven't understood everything fully, but it has
> been highly entertaining to read. Will have to give it a closer read when I
> get some time.
> >
> > As a first impression, here are some preliminary observations: While I
> highly like the Haskell-style datatype, and the algorithm we use does
> mostly use Dijkstra pathfinding, I think what is really important in your
> definition is the computeCost definition. This is what we would call the
> cost function IIUC, and in order to be able to solve min-cost flow problems
> it generally has to be separable and convex. I believe your datatype merely
> hides the fact that it is neither.
>
> Well, it really depends on what min flow cost algorithms actually assume
> of the "numbers" being used.
>
> For instance, it is well known that the Dijkstra-A\*-Greedy family of
> algorithms do not handle "negative costs".
> What it really means is that the algorithms assume:
>
>     a + b >= a
>     a + b >= b
>
> This holds if `a` and `b` are naturals (0 or positive), but not if they
> are integers.
> 1 + -1 = 0, and 0 >= 1 is not true, thus the type for costs in those
> algorithms cannot be integer types, they have to be naturals.
> However if you restrict the type to naturals,  `a + b >= a` holds, and
> thus Dijkstra and its family of algorithms work.
>
> Thus, if you are going to use Dijkstra-A\*-Greedy, you "only" need to have
> the following "operations":
>
>     `+` :: Cost -> Cost -> Cost
>     `<` :: Cost -> Cost -> Bool
>     zero :: Cost
>
> With the following derived operations:
>
>     a > b = b < a
>     a >= b = not (a < b)
>     a <= b = not (b < a)
>     a == b = (a >= b) && (a <= b)
>     a /= b = (a < b) || (a > b)
>
> And following the laws:
>
>     forall (a :: Cost) => a + zero == a
>     forall (a :: Cost) => zero + a == a
>     forall (a :: Cost, b :: Cost) => a + b == b + a
>     forall (a :: Cost, b :: Cost, c :: Cost) => (a + b) + c == a + (b + c)
>     forall (a :: Cost, b :: Cost) => a + b >= a
>     forall (a :: Cost, b :: Cost) => a + b >= b
>
> As a non-mathist I have no idea what "separable" and "convex" actually
> mean.
> Basic search for "convex" and "concave" tends to show up information in
> geometry, which I think is not related (though it is possible there is some
> extension of the geometric concept to pure number theory?).
> And definitions on "separable" are not understandable by me, either.
>
> What exactly are the operations involved, and what are the laws those
> operations must follow, for the data type to be "separable" and "convex"
> (vs."concave")?
>
> I guess my problem as well is that I cannot find easy-to-understand
> algorithms for min cost flow --- I can find discussions on the min cost
> flow "problem", and some allusions to solutions to that problem, but once I
> try looking into algorithms it gets quite a bit more complicated.
>
> Basically: do I need these operations?
>
>     `*` :: Cost -> Cost -> Cost
>     `/` :: Cost -> Cost -> Cost --- or Maybe Cost
>
> If not, then why cannot `type Cost = UnifiedCost`?
>
>
> For example, this page:
> https://www.topcoder.com/thrive/articles/Minimum%20Cost%20Flow%20Part%20Two:%20Algorithms
>
> Includes this pseudocode:
>
>     Transform network G by adding source and sink
>     Initial flow x is zero
>     while ( Gx contains a path from s to t ) do
>         Find any shortest path P from s to t
>         Augment current flow x along P
>         update Gx
>
> If "find any shortest path" is implemented using Dijkstra-A\*-Greedy, then
> that does not require `Cost` to be an actual numeric type, they just
> require a type that provides `+`, `<`, and `zero`, all of which follow the
> laws I pointed out, *and no more than those*.
> `UnifiedCost` follows those laws (tough note that my definition of `zero`
> has a bug, `successProbability` should be `1.0` not `0`).
>
> In short --- the output of the cost function is a `UnifiedCost` structure
> and ***not*** a number (in the traditional sense).
>
> Basically, I am deconstructing numbers here and trying to figure out what
> makes them tick, and seeing if I can use a different type to provide the
> "tick".
>
>
> Regards,
> ZmnSCPxj
>
ZmnSCPxj in reply to Stefan Richter permalink original

Good morning Stefan,

Quoted text (9 lines)
> Good Morning Zmn!
>
> If you'd like to understand  the min-cost flow problem and algorithms better, I would really recommend the textbook we have been citing throughout the paper.
>
> The algorithm you have found has a few shortcomings. It'll only work for the linear min-cost flow problem, and it is very slow. In reality, we need to deal with convex cost functions, and the algorithm we have used so far uses an approach called capacity scaling in order to be much faster. It is indeed complex enough that it has taken us about two months to understand and implement it, discovering a nice heuristic in the process of making mistakes.
>
> Separable in this context means that you can simply add up the costs of the edges to get the total costs. On second thought, your definition would probably work here , by redefining adding up.
>
> Convex here means that for any two amounts x, y, the cost function f in the interval x, y does not lie below the line connecting the two points (x, f(x)) and (y, f(y)). The intuition here is that a linear approximation never overestimates the real cost. I guess one would need a more involved definition for your more complex coordinates.

Hmm, that does require that we define "subtract" and "multiply" operations, with certain additional rules. This is probably doable, especially since I think we do not need to multiply a `UnifiedCost` by another `UnifiedCost`, only a `UnifiedCost` by a `Rational`.

***HOWEVER*** I realized later that the `#zerobasefee` issue might not actually be a problem for the ***mincostflow*** algorithm, but rather a problem for the ***disect*** algorithm that converts a flow solution to an actual set of sub-payments. In that case, this effort is actually a dead end; it seems to me that, for the mincostflow algo at least, you can trivially add base fees, the issue is that once the disect algorithm gets its hands on the solution, base fees are problematic.

In addition, if you tell me that it took you two months to do the algo, then that is an even greater concern --- this is possible to do with a small number of operations, but if we need to add more, like "subtract" and "multiply" operations, then the risk involved in the software engineering also increases, and the expected implementation time might take much, much longer due to complexity.

For example, in CLBOSS I have a Dijkstra implementation that has inversion-of-control *and* allows users to parametrize "addition" and "comparison" operations, it is just a few dozen lines of code. However, the complexity of the code implementation would greatly increase if there is also a need to fill in "multiply" and "subtract" operations as well --- consider testing and other code quality needs.

*And* if my conjecture is right --- that the `#zerobasefee` requirement is really a requirement of the ***disect*** algorithm rather than the ***mincostflow*** algorithm then the effort here would be pointless, we should focus more on the disect algo, which is why the new thread yet again.

Regards, ZmnSCPxj