Skip to content

Commit

Permalink
Add flow control
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Sep 4, 2023
1 parent 950bac5 commit 70b75f4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ From a "make money routing" perspective, theoretically, these most distant nodes

**Passively manage channel liquidity**

Set channel fees based on the channel's current liquidity. The idea here is to encourage passive channel re-balancing through fees. If a channel has a too much local liquidity (high), fees are lowered in order to encourage relatively more outbound transactions. Visa versa for a channel with too little local liquidity (low).
Set channel fees based on the channel's current liquidity. The idea here is to encourage passive channel rebalancing through fees. If a channel has a too much local liquidity (high), fees are lowered in order to encourage relatively more outbound transactions. Visa versa for a channel with too little local liquidity (low).

The global `-liquidity-thresholds` flag determines how channels are grouped into liquidity buckets, while the `-liquidity-fees` flag determines the fee settings applied to those groups. For example, if thresholds are set to `80,20` and fees set to `5,50,500`, then channels with over 80% local liquidity will have a 5 PPM fee, channels between 80% and 20% local liquidity will have a 50 PPM fee, and channels with less than 20% liquidity will have a 500 PPM fee.

Expand All @@ -61,6 +61,8 @@ The `-liquidity-thresholds`, `-liquidity-fees`, and `-liquidity-stickiness` are

`fees` follows the [zero-base-fee movement](http://www.rene-pickhardt.de/). I am honestly not sure if this is financially sound, but I appreciate the simpler mental model of only thinking in ppm.

`fees` also automatically applies some [flow control](https://blog.bitmex.com/the-power-of-htlc_maximum_msat-as-a-control-valve-for-better-flow-control-improved-reliability-and-lower-expected-payment-failure-rates-on-the-lightning-network/) to channels in order to encourage more rebalancing.

## rebalance

**Actively manage channel liquidity**
Expand Down
8 changes: 8 additions & 0 deletions lightning/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func (s Satoshi) BTC() float64 {
return float64(s) / 100000000
}

// BTC value of Satoshi.
func (s Satoshi) Millis() MilliSatoshi {
return MilliSatoshi(s * 1000)
}

// MilliSatoshi unit of bitcoin.
type MilliSatoshi int64

// FeePPM is the channel fee in part per million.
type FeePPM float64

Expand Down
3 changes: 2 additions & 1 deletion lightning/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (l LndClient) ListChannels(ctx context.Context) (Channels, error) {
}

// SetFees for channel with rate in ppm.
func (l LndClient) SetFees(ctx context.Context, channelID ChannelID, fee FeePPM) error {
func (l LndClient) SetFees(ctx context.Context, channelID ChannelID, fee FeePPM, maxHTLC MilliSatoshi) error {
ce, err := l.c.GetChanInfo(ctx, uint64(channelID))
if err != nil {
return err
Expand All @@ -250,6 +250,7 @@ func (l LndClient) SetFees(ctx context.Context, channelID ChannelID, fee FeePPM)
BaseFeeMsat: 0,
FeeRate: fee.Rate(),
TimeLockDelta: 80,
MaxHtlcMsat: uint64(maxHTLC),
}
return l.c.UpdateChanPolicy(ctx, req, outpoint)
}
Expand Down
6 changes: 4 additions & 2 deletions raiju.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type lightninger interface {
GetChannel(ctx context.Context, channelID lightning.ChannelID) (lightning.Channel, error)
ListChannels(ctx context.Context) (lightning.Channels, error)
SendPayment(ctx context.Context, invoice lightning.Invoice, outChannelID lightning.ChannelID, lastHopPubKey lightning.PubKey, maxFee lightning.FeePPM) (lightning.Satoshi, error)
SetFees(ctx context.Context, channelID lightning.ChannelID, fee lightning.FeePPM) error
SetFees(ctx context.Context, channelID lightning.ChannelID, fee lightning.FeePPM, maxHTLC lightning.MilliSatoshi) error
SubscribeChannelUpdates(ctx context.Context) (<-chan lightning.Channels, <-chan error, error)
}

Expand Down Expand Up @@ -308,8 +308,10 @@ func (r Raiju) setFees(ctx context.Context, channels lightning.Channels) (map[li
// update channel fees based on liquidity, but only change if necessary
for _, c := range channels {
fee := r.f.Fee(c)
// flow control, broadcast to the network the max payment size to forward through this channel.
maxPayment := c.LocalBalance.Millis() / 2
if c.LocalFee != fee && !c.Private {
err := r.l.SetFees(ctx, c.ChannelID, fee)
err := r.l.SetFees(ctx, c.ChannelID, fee, maxPayment)
if err != nil {
return map[lightning.ChannelID]lightning.FeePPM{}, err
}
Expand Down

0 comments on commit 70b75f4

Please sign in to comment.