You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
regolancer/routes.go

215 lines
6.7 KiB
Go

2 years ago
package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"math/rand"
2 years ago
"time"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
2 years ago
)
func (r *regolancer) getChanInfo(ctx context.Context, chanId uint64) (*lnrpc.ChannelEdge, error) {
if c, ok := r.chanCache[chanId]; ok {
return c, nil
}
c, err := r.lnClient.GetChanInfo(ctx, &lnrpc.ChanInfoRequest{ChanId: chanId})
if err != nil {
return nil, err
}
r.chanCache[chanId] = c
return c, nil
}
func (r *regolancer) calcFeeMsat(ctx context.Context, to uint64, amtMsat int64, ratio float64) (feeMsat int64,
lastPKstr string, err error) {
c, err := r.getChanInfo(ctx, to)
2 years ago
if err != nil {
return 0, "", err
2 years ago
}
lastPKstr = c.Node1Pub
2 years ago
policy := c.Node2Policy
if lastPKstr == r.myPK {
lastPKstr = c.Node2Pub
policy = c.Node1Policy
}
feeMsat = int64(float64(policy.FeeBaseMsat+amtMsat*policy.FeeRateMilliMsat) * ratio / 1e6)
return
}
func (r *regolancer) getRoutes(ctx context.Context, from, to uint64, amtMsat int64, ratio float64) ([]*lnrpc.Route, int64, error) {
routeCtx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
feeMsat, lastPKstr, err := r.calcFeeMsat(routeCtx, to, amtMsat, ratio)
if err != nil {
return nil, 0, err
}
2 years ago
lastPK, err := hex.DecodeString(lastPKstr)
if err != nil {
return nil, 0, err
2 years ago
}
routes, err := r.lnClient.QueryRoutes(routeCtx, &lnrpc.QueryRoutesRequest{
2 years ago
PubKey: r.myPK,
OutgoingChanId: from,
LastHopPubkey: lastPK,
AmtMsat: amtMsat,
UseMissionControl: true,
FeeLimit: &lnrpc.FeeLimit{Limit: &lnrpc.FeeLimit_FixedMsat{FixedMsat: feeMsat}},
2 years ago
})
if err != nil {
return nil, 0, err
2 years ago
}
return routes.Routes, feeMsat, nil
2 years ago
}
func (r *regolancer) getNodeInfo(ctx context.Context, pk string) (*lnrpc.NodeInfo, error) {
2 years ago
if nodeInfo, ok := r.nodeCache[pk]; ok {
return nodeInfo, nil
}
nodeInfo, err := r.lnClient.GetNodeInfo(ctx, &lnrpc.NodeInfoRequest{PubKey: pk})
2 years ago
if err == nil {
r.nodeCache[pk] = nodeInfo
}
return nodeInfo, err
}
func (r *regolancer) printRoute(ctx context.Context, route *lnrpc.Route) {
2 years ago
if len(route.Hops) == 0 {
return
}
errs := ""
fmt.Printf("%s %s\n", faintWhiteColor("Total fee:"), hiWhiteColor(route.TotalFeesMsat/1000))
2 years ago
for i, hop := range route.Hops {
nodeInfo, err := r.getNodeInfo(ctx, hop.PubKey)
2 years ago
if err != nil {
errs = errs + err.Error() + "\n"
continue
}
fee := hiWhiteColorF("%-6s", "")
if i > 0 {
fee = hiWhiteColorF("%-6d", route.Hops[i-1].FeeMsat)
2 years ago
}
fmt.Printf("%s %s %s\n", faintWhiteColor(hop.ChanId), fee, cyanColor(nodeInfo.Node.Alias))
}
if errs != "" {
fmt.Println(errColor(errs))
}
}
func (r *regolancer) rebuildRoute(ctx context.Context, route *lnrpc.Route, amount int64) (*lnrpc.Route, error) {
pks := [][]byte{}
for _, h := range route.Hops {
pk, _ := hex.DecodeString(h.PubKey)
pks = append(pks, pk)
}
resultRoute, err := r.routerClient.BuildRoute(ctx, &routerrpc.BuildRouteRequest{
AmtMsat: amount * 1000,
OutgoingChanId: route.Hops[0].ChanId,
HopPubkeys: pks,
FinalCltvDelta: 144,
})
2 years ago
if err != nil {
return nil, err
}
return resultRoute.Route, err
}
func (r *regolancer) probeRoute(ctx context.Context, route *lnrpc.Route, goodAmount, badAmount, amount int64,
steps int, ratio float64) (maxAmount int64, err error) {
probedRoute, err := r.rebuildRoute(ctx, route, amount)
if err != nil {
return 0, err
}
maxFeeMsat, _, err := r.calcFeeMsat(ctx, probedRoute.Hops[len(probedRoute.Hops)-1].ChanId, amount*1000, ratio)
if err != nil {
return 0, err
}
if probedRoute.TotalFeesMsat > maxFeeMsat {
if steps == 1 {
bestAmount := hiWhiteColor(goodAmount)
if goodAmount <= 0 {
bestAmount = hiWhiteColor("unknown")
goodAmount = 0
}
log.Printf("%s requires too high fee %s (max allowed is %s), best amount is %s", hiWhiteColor(amount),
hiWhiteColor(probedRoute.TotalFeesMsat/1000), hiWhiteColor(maxFeeMsat/1000), bestAmount)
return goodAmount, nil
}
nextAmount := amount + (badAmount-amount)/2
log.Printf("%s requires too high fee %s (max allowed is %s), increasing amount to %s, %s steps left",
hiWhiteColor(amount), hiWhiteColor(probedRoute.TotalFeesMsat/1000), hiWhiteColor(maxFeeMsat/1000),
hiWhiteColor(nextAmount), hiWhiteColor(steps-1))
// returning negative amount as "good", it's a special case which means this is
// rather the lower bound and the actual good amount is still unknown
return r.probeRoute(ctx, route, -amount, badAmount, nextAmount, steps-1, ratio)
}
fakeHash := make([]byte, 32)
rand.Read(fakeHash)
result, err := r.routerClient.SendToRouteV2(ctx,
&routerrpc.SendToRouteRequest{
PaymentHash: fakeHash,
Route: probedRoute,
})
if err != nil {
return
}
if result.Status == lnrpc.HTLCAttempt_SUCCEEDED {
return 0, fmt.Errorf("this should never happen")
}
if result.Status == lnrpc.HTLCAttempt_FAILED {
if result.Failure.Code == lnrpc.Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS { // payment can succeed
if steps == 1 || amount == badAmount {
log.Printf("best amount is %s", hiWhiteColor(amount))
return amount, nil
}
nextAmount := amount + (badAmount-amount)/2
log.Printf("%s is good enough, trying amount %s, %s steps left",
hiWhiteColor(amount), hiWhiteColor(nextAmount), hiWhiteColor(steps-1))
return r.probeRoute(ctx, route, amount, badAmount, nextAmount, steps-1, ratio)
}
if result.Failure.Code == lnrpc.Failure_TEMPORARY_CHANNEL_FAILURE {
if steps == 1 {
bestAmount := hiWhiteColor(goodAmount)
if goodAmount <= 0 {
bestAmount = hiWhiteColor("unknown")
goodAmount = 0
}
log.Printf("%s is too much, best amount is %s", hiWhiteColor(amount), bestAmount)
return goodAmount, nil
}
var nextAmount int64
if goodAmount >= 0 {
nextAmount = amount + (goodAmount-amount)/2
} else {
nextAmount = amount - (goodAmount+amount)/2
}
log.Printf("%s is too much, lowering amount to %s, %s steps left",
hiWhiteColor(amount), hiWhiteColor(nextAmount), hiWhiteColor(steps-1))
return r.probeRoute(ctx, route, goodAmount, amount, nextAmount, steps-1, ratio)
}
if result.Failure.Code == lnrpc.Failure_FEE_INSUFFICIENT {
log.Printf("Fee insufficient, retrying...")
return r.probeRoute(ctx, route, goodAmount, badAmount, amount, steps, ratio)
}
}
return 0, fmt.Errorf("unknown error: %+v", result)
}
func (r *regolancer) addFailedRoute(from, to uint64) {
t := time.Now().Add(time.Hour)
r.failureCache[fmt.Sprintf("%d-%d", from, to)] = &t
for k, v := range r.failureCache {
if v.Before(time.Now()) {
delete(r.failureCache, k)
}
}
}
func (r *regolancer) isFailedRoute(from, to uint64) bool {
_, ok := r.failureCache[fmt.Sprintf("%d-%d", from, to)]
return ok
}