diff --git a/go.mod b/go.mod index dba1d73..d2ea086 100644 --- a/go.mod +++ b/go.mod @@ -83,6 +83,7 @@ require ( github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 // indirect github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mholt/archiver/v3 v3.5.0 // indirect github.com/miekg/dns v1.1.43 // indirect @@ -95,6 +96,7 @@ require ( github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.26.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect github.com/rogpeppe/fastuuid v1.2.0 // indirect github.com/sirupsen/logrus v1.7.0 // indirect github.com/soheilhy/cmux v0.1.5 // indirect diff --git a/go.sum b/go.sum index ca3f2c8..e02bd71 100644 --- a/go.sum +++ b/go.sum @@ -473,6 +473,8 @@ github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mholt/archiver/v3 v3.5.0 h1:nE8gZIrw66cu4osS/U7UW7YDuGMHssxKutU8IfWxwWE= @@ -549,6 +551,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= diff --git a/info.go b/info.go new file mode 100644 index 0000000..38ad861 --- /dev/null +++ b/info.go @@ -0,0 +1,95 @@ +package main + +import ( + "context" + "fmt" + "strings" + + "github.com/lightningnetwork/lnd/lnrpc" + "github.com/mattn/go-runewidth" +) + +func printBooleanOption(name string, value bool) { + v := "disabled" + if value { + v = "enabled" + } + fmt.Printf("%s: %s\n", name, hiWhiteColor(v)) +} + +func (r *regolancer) printChannelInfo(ctx context.Context, channel *lnrpc.Channel) error { + nodeInfo, err := r.getNodeInfo(ctx, channel.RemotePubkey) + if err != nil { + return err + } + alias := runewidth.FillRight(runewidth.Truncate(nodeInfo.Node.Alias, 25, ""), 25) + balance := runewidth.FillRight(strings.Repeat("|", int(channel.LocalBalance*15/channel.Capacity)), 15) + balancePct := channel.LocalBalance * 100 / channel.Capacity + fmt.Printf("%s [%s] %d%% ", alias, cyanColor(balance), balancePct) + return nil +} + +func (r *regolancer) info(ctx context.Context) error { + fromIdx := 0 + toIdx := 0 + sep := strings.Repeat("—", 98) + fmt.Printf("%s\nFrom %s channels %33s To %s channels\n%s\n", sep, hiWhiteColor(len(r.fromChannels)), "", hiWhiteColor(len(r.toChannels)), sep) + for { + if fromIdx < len(r.fromChannels) { + channel := r.fromChannels[fromIdx] + err := r.printChannelInfo(ctx, channel) + if err != nil { + return err + } + fromIdx++ + } else { + fmt.Print(strings.Repeat(" ", 51)) + } + if toIdx < len(r.toChannels) { + channel := r.toChannels[toIdx] + err := r.printChannelInfo(ctx, channel) + if err != nil { + return err + } + toIdx++ + } + fmt.Println() + if fromIdx >= len(r.fromChannels) && toIdx >= len(r.toChannels) { + break + } + } + fmt.Println(sep) + fmt.Printf("Min amount: %s sat\n", formatAmt(params.MinAmount)) + if params.Amount > 0 { + fmt.Printf("Amount: %s sat\n", formatAmt(params.Amount)) + } else { + if params.RelAmountFrom > 0 { + fmt.Printf("Relative amount from: %s%%\n", formatAmt(int64(params.RelAmountFrom*100))) + } + if params.RelAmountTo > 0 { + fmt.Printf("Relative amount to: %s%%\n", formatAmt(int64(params.RelAmountTo*100))) + } + } + if params.FeeLimitPPM > 0 { + fmt.Printf("Max fee: %s ppm", formatAmt(int64(params.FeeLimitPPM))) + } else if params.EconRatio > 0 { + fmt.Printf("Max fee: %s%% of target channel ppm", formatAmt(int64(params.EconRatio*100))) + if params.EconRatioMaxPPM > 0 { + fmt.Printf(" (but <= %s ppm)", formatAmt(int64(params.EconRatioMaxPPM))) + } + } + fmt.Println() + fmt.Printf("Fail tolerance: %s ppm\n", formatAmt(int64(params.FailTolerance))) + printBooleanOption("Rapid rebalance", params.AllowRapidRebalance) + printBooleanOption("Lost profit accounting", params.LostProfit) + if params.ProbeSteps > 0 { + fmt.Printf("Probing steps: %s\n", hiWhiteColor(params.ProbeSteps)) + } + fmt.Printf("Node cache size: %s records, life time: %s days %s hours %s minutes\n", hiWhiteColor(len(r.nodeCache)), hiWhiteColor(params.NodeCacheLifetime/1440), hiWhiteColor(params.NodeCacheLifetime%1440/60), hiWhiteColor(params.NodeCacheLifetime%60)) + printBooleanOption("Show node cache hits", params.NodeCacheInfo) + fmt.Printf("Total rebalance timeout: %s hours %s minutes\n", hiWhiteColor(params.TimeoutRebalance/60), hiWhiteColor(params.TimeoutRebalance%60)) + fmt.Printf("Single attempt timeout: %s minutes\n", hiWhiteColor(params.TimeoutAttempt)) + fmt.Printf("Info query timeout: %s seconds\n", hiWhiteColor(params.TimeoutInfo)) + fmt.Printf("Route query timeout: %s seconds\n", hiWhiteColor(params.TimeoutRoute)) + return nil +} diff --git a/main.go b/main.go index ad11efd..74bfd87 100644 --- a/main.go +++ b/main.go @@ -61,6 +61,7 @@ type configParams struct { TimeoutRoute int `long:"timeout-route" description:"max channel selection and route query time in seconds" json:"timeout_route" toml:"timeout_route"` StatFilename string `rego-grouping:"Others" short:"s" long:"stat" description:"save successful rebalance information to the specified CSV file" json:"stat" toml:"stat"` Version bool `short:"v" long:"version" description:"show program version and exit"` + Info bool `long:"info" description:"show rebalance information"` Help bool `short:"h" long:"help" description:"Show this help message"` } @@ -594,7 +595,6 @@ func main() { if len(r.toChannels) == 0 { log.Fatal("No target channels selected") } - infoCtxCancel() attempt := 1 err = r.loadNodeCache(params.NodeCacheFilename, params.NodeCacheLifetime, @@ -603,6 +603,14 @@ func main() { logErrorF("%s", err) } defer r.saveNodeCache(params.NodeCacheFilename, params.NodeCacheLifetime) + if params.Info { + err = r.info(infoCtx) + if err != nil { + log.Fatal(err) + } + return + } + infoCtxCancel() stopChan := make(chan os.Signal, 1) signal.Notify(stopChan, os.Interrupt) go func() {