2022-08-20 14:11:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-27 14:40:27 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-09-03 00:50:00 +00:00
|
|
|
"log"
|
2022-08-20 14:11:45 +00:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2022-10-07 01:18:32 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-08-20 14:11:45 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2022-10-07 01:18:32 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2022-08-20 14:11:45 +00:00
|
|
|
)
|
|
|
|
|
2022-08-27 14:40:27 +00:00
|
|
|
func formatChannelPair(a, b uint64) string {
|
|
|
|
return fmt.Sprintf("%d-%d", a, b)
|
|
|
|
}
|
|
|
|
|
2022-08-20 19:19:48 +00:00
|
|
|
func (r *regolancer) getChannels(ctx context.Context) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
|
2022-08-20 14:11:45 +00:00
|
|
|
defer cancel()
|
|
|
|
channels, err := r.lnClient.ListChannels(ctx, &lnrpc.ListChannelsRequest{ActiveOnly: true, PublicOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.channels = channels.Channels
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-21 08:26:50 +00:00
|
|
|
func makeChanSet(chanIds []uint64) (result map[uint64]struct{}) {
|
|
|
|
result = map[uint64]struct{}{}
|
|
|
|
for _, cid := range chanIds {
|
|
|
|
result[cid] = struct{}{}
|
2022-08-20 18:12:19 +00:00
|
|
|
}
|
2022-08-21 08:26:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *regolancer) getChannelCandidates(fromPerc, toPerc, amount int64) error {
|
2022-08-20 14:11:45 +00:00
|
|
|
for _, c := range r.channels {
|
2022-08-21 08:26:50 +00:00
|
|
|
if _, ok := r.excludeBoth[c.ChanId]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-01 15:37:56 +00:00
|
|
|
if c.LocalBalance < c.Capacity*toPerc/100 && (params.AllowUnbalanceTo ||
|
|
|
|
c.LocalBalance+amount < c.Capacity/2) {
|
2022-08-21 08:26:50 +00:00
|
|
|
if _, ok := r.excludeIn[c.ChanId]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-08 12:31:45 +00:00
|
|
|
if _, ok := r.toChannelId[c.ChanId]; ok || len(r.toChannelId) == 0 {
|
2022-08-21 10:20:52 +00:00
|
|
|
r.toChannels = append(r.toChannels, c)
|
|
|
|
}
|
2022-08-20 14:11:45 +00:00
|
|
|
}
|
2022-10-01 15:37:56 +00:00
|
|
|
if c.RemoteBalance < c.Capacity*fromPerc/100 &&
|
|
|
|
(params.AllowUnbalanceFrom ||
|
|
|
|
c.RemoteBalance+amount < c.Capacity/2) {
|
2022-08-21 08:26:50 +00:00
|
|
|
if _, ok := r.excludeOut[c.ChanId]; ok {
|
2022-08-20 18:12:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-08 12:31:45 +00:00
|
|
|
if _, ok := r.fromChannelId[c.ChanId]; ok || len(r.fromChannelId) == 0 {
|
2022-08-21 10:20:52 +00:00
|
|
|
r.fromChannels = append(r.fromChannels, c)
|
|
|
|
}
|
2022-08-20 14:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-27 14:40:27 +00:00
|
|
|
for _, fc := range r.fromChannels {
|
|
|
|
for _, tc := range r.toChannels {
|
2022-10-08 14:00:55 +00:00
|
|
|
if fc.RemotePubkey != tc.RemotePubkey {
|
|
|
|
pair := [2]*lnrpc.Channel{fc, tc}
|
|
|
|
r.channelPairs[formatChannelPair(pair[0].ChanId, pair[1].ChanId)] = pair
|
|
|
|
}
|
2022-08-27 14:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-20 14:11:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(args ...int64) (result int64) {
|
|
|
|
result = math.MaxInt64
|
|
|
|
for _, a := range args {
|
|
|
|
if a < result {
|
|
|
|
result = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-08 15:31:02 +00:00
|
|
|
func (r *regolancer) pickChannelPair(amount, minAmount int64,
|
|
|
|
relFromAmount, relToAmount float64) (from uint64, to uint64, maxAmount int64, err error) {
|
2022-08-27 14:40:27 +00:00
|
|
|
if len(r.channelPairs) == 0 {
|
2022-09-03 00:50:00 +00:00
|
|
|
if !r.routeFound {
|
|
|
|
return 0, 0, 0, errors.New("no routes")
|
|
|
|
}
|
|
|
|
log.Print(errColor("No channel pairs left, expiring all failed routes"))
|
|
|
|
// expire all failed routes
|
|
|
|
for k, v := range r.failureCache {
|
|
|
|
r.channelPairs[k] = v.channelPair
|
|
|
|
delete(r.failureCache, k)
|
|
|
|
}
|
|
|
|
r.routeFound = false
|
2022-08-27 14:40:27 +00:00
|
|
|
}
|
2022-08-20 19:19:48 +00:00
|
|
|
var fromChan, toChan *lnrpc.Channel
|
2022-08-27 14:40:27 +00:00
|
|
|
idx := rand.Int31n(int32(len(r.channelPairs)))
|
|
|
|
var pair [2]*lnrpc.Channel
|
|
|
|
for _, pair = range r.channelPairs {
|
|
|
|
if idx == 0 {
|
2022-08-20 19:19:48 +00:00
|
|
|
break
|
|
|
|
}
|
2022-08-27 14:40:27 +00:00
|
|
|
idx--
|
2022-08-20 19:19:48 +00:00
|
|
|
}
|
2022-08-27 14:40:27 +00:00
|
|
|
fromChan = pair[0]
|
|
|
|
toChan = pair[1]
|
2022-08-20 14:11:45 +00:00
|
|
|
maxFrom := fromChan.Capacity/2 - fromChan.RemoteBalance
|
2022-10-01 15:37:56 +00:00
|
|
|
if params.AllowUnbalanceFrom {
|
|
|
|
maxFrom = fromChan.LocalBalance
|
|
|
|
}
|
2022-10-08 15:31:02 +00:00
|
|
|
if relFromAmount > 0 {
|
|
|
|
maxFrom = min(maxFrom, int64(float64(fromChan.Capacity)*relFromAmount)-fromChan.RemoteBalance)
|
|
|
|
}
|
2022-08-20 14:11:45 +00:00
|
|
|
maxTo := toChan.Capacity/2 - toChan.LocalBalance
|
2022-10-01 15:37:56 +00:00
|
|
|
if params.AllowUnbalanceTo {
|
|
|
|
maxTo = toChan.RemoteBalance
|
|
|
|
}
|
2022-10-08 15:31:02 +00:00
|
|
|
if relToAmount > 0 {
|
|
|
|
maxTo = min(maxTo, int64(float64(toChan.Capacity)*relToAmount)-toChan.LocalBalance)
|
|
|
|
}
|
2022-08-20 14:11:45 +00:00
|
|
|
if amount == 0 {
|
|
|
|
maxAmount = min(maxFrom, maxTo)
|
|
|
|
} else {
|
|
|
|
maxAmount = min(maxFrom, maxTo, amount)
|
|
|
|
}
|
2022-09-11 14:36:10 +00:00
|
|
|
if maxAmount < minAmount {
|
|
|
|
r.addFailedRoute(fromChan.ChanId, toChan.ChanId)
|
2022-10-08 15:31:02 +00:00
|
|
|
return r.pickChannelPair(amount, minAmount, relFromAmount, relToAmount)
|
2022-09-11 14:36:10 +00:00
|
|
|
}
|
2022-08-27 14:40:27 +00:00
|
|
|
for k, v := range r.failureCache {
|
|
|
|
if v.expiration.Before(time.Now()) {
|
|
|
|
r.channelPairs[k] = v.channelPair
|
|
|
|
delete(r.failureCache, k)
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 19:19:48 +00:00
|
|
|
return fromChan.ChanId, toChan.ChanId, maxAmount, nil
|
2022-08-20 14:11:45 +00:00
|
|
|
}
|
2022-08-27 14:40:27 +00:00
|
|
|
|
|
|
|
func (r *regolancer) addFailedRoute(from, to uint64) {
|
2022-08-27 16:13:09 +00:00
|
|
|
t := time.Now().Add(time.Minute * 5)
|
2022-08-27 14:40:27 +00:00
|
|
|
k := formatChannelPair(from, to)
|
|
|
|
r.failureCache[k] = failedRoute{channelPair: r.channelPairs[k], expiration: &t}
|
|
|
|
delete(r.channelPairs, k)
|
|
|
|
}
|
2022-10-07 01:18:32 +00:00
|
|
|
|
|
|
|
func parseScid(chanId string) int64 {
|
|
|
|
|
|
|
|
elements := strings.Split(strings.ToLower(chanId), "x")
|
|
|
|
|
|
|
|
blockHeight, err := strconv.ParseInt(elements[0], 10, 24)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: not able to parse Blockheight of ShortChannelID %s, %s ", chanId, err)
|
|
|
|
}
|
|
|
|
txIndex, err := strconv.ParseInt(elements[1], 10, 24)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: not able to parse TxIndex of ShortChannelID %s, %s ", chanId, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
txPosition, err := strconv.ParseInt(elements[2], 10, 32)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: not able to parse txPosition of ShortChannelID %s, %s ", chanId, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var scId lnwire.ShortChannelID
|
|
|
|
scId.BlockHeight = uint32(blockHeight)
|
|
|
|
scId.TxIndex = uint32(txIndex)
|
|
|
|
scId.TxPosition = uint16(txPosition)
|
|
|
|
|
|
|
|
return int64(scId.ToUint64())
|
|
|
|
|
|
|
|
}
|