From 98068a105d2f767d14832aa15f068c5220ec9df0 Mon Sep 17 00:00:00 2001 From: ziggie Date: Sat, 10 Dec 2022 00:00:01 +0100 Subject: [PATCH 1/4] add amount when rapid rebalancing --- main.go | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 50aa281..775fe69 100644 --- a/main.go +++ b/main.go @@ -199,13 +199,12 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, if err == nil { if params.AllowRapidRebalance { - _, err := tryRapidRebalance(ctx, r, from, to, route, amt) + attempt, rebalanceAmt, _ := tryRapidRebalance(ctx, r, from, to, route, amt) - if err != nil { - log.Printf("Rapid rebalance failed with %s", err) - } else { - log.Printf("Finished rapid rebalancing") + if attempt > 0 { + log.Printf("%s rapid rebalances were successful, total amount rebalanced: %s sats \n", hiWhiteColor(attempt), hiWhiteColor(rebalanceAmt)) } + log.Printf("Finished rapid rebalancing") } return nil, false @@ -220,13 +219,12 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, err = r.pay(ctx, amt, 0, probedRoute, 0) if err == nil { if params.AllowRapidRebalance && params.MinAmount > 0 { - _, err := tryRapidRebalance(ctx, r, from, to, probedRoute, amt) + attempt, rebalanceAmt, _ := tryRapidRebalance(ctx, r, from, to, probedRoute, amt) - if err != nil { - log.Printf("Rapid rebalance failed with %s", err) - } else { - log.Printf("Finished rapid rebalancing") + if attempt > 0 { + log.Printf("%s rapid rebalances were successful, total amount rebalanced: %s sats \n", hiWhiteColor(attempt), hiWhiteColor(rebalanceAmt)) } + log.Printf("Finished rapid rebalancing") } return nil, false @@ -246,25 +244,23 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, return nil, true } -func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, route *lnrpc.Route, amt int64) (successfullAtempts int, err error) { - - rapidAttempt := 0 +func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, route *lnrpc.Route, amt int64) (successfulAttempts int, successfulAmt int64, err error) { for { - log.Printf("Rapid rebalance attempt %s", hiWhiteColor(rapidAttempt+1)) + log.Printf("Rapid rebalance attempt %s", hiWhiteColor(successfulAttempts+1)) cTo, err := r.getChanInfo(ctx, to) if err != nil { logErrorF("Error fetching target channel: %s", err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } cFrom, err := r.getChanInfo(ctx, from) if err != nil { logErrorF("Error fetching source channel: %s", err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } fromPeer, _ := hex.DecodeString(cFrom.Node1Pub) @@ -275,7 +271,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching source channel: %s", err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } toPeer, _ := hex.DecodeString(cTo.Node1Pub) @@ -287,7 +283,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching target channel: %s", err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } for k := range r.fromChannelId { @@ -319,14 +315,14 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error selecting channel candidates: %s", err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } from, to, amt, err = r.pickChannelPair(amt, params.MinAmount, params.RelAmountFrom, params.RelAmountTo) if err != nil { log.Printf(errColor("Error during picking channel: %s"), err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } log.Printf("rapid fire starting with amount %s", hiWhiteColor(amt)) @@ -335,7 +331,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { log.Printf(errColor("Error building route: %s"), err) - return rapidAttempt, err + return successfulAttempts, successfulAmt, err } attemptCtx, attemptCancel := context.WithTimeout(ctx, time.Minute*time.Duration(params.TimeoutAttempt)) @@ -348,20 +344,18 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if attemptCtx.Err() == context.DeadlineExceeded { log.Print(errColor("Rapid rebalance attempt timed out")) - return rapidAttempt, attemptCtx.Err() + return successfulAttempts, successfulAmt, attemptCtx.Err() } if err != nil { log.Printf("Rebalance failed with %s", err) break } else { - rapidAttempt++ + successfulAttempts++ + successfulAmt += amt } - } - log.Printf("%s rapid rebalances were successful\n", hiWhiteColor(rapidAttempt)) - return rapidAttempt, nil - + return successfulAttempts, successfulAmt, nil } func preflightChecks(params *configParams) error { From 5a3a41afae11bb1c3b6ae7cd20f861420c062745 Mon Sep 17 00:00:00 2001 From: ziggie Date: Sun, 11 Dec 2022 19:40:49 +0100 Subject: [PATCH 2/4] add fee output --- main.go | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 775fe69..20c340f 100644 --- a/main.go +++ b/main.go @@ -80,6 +80,12 @@ type cachedNodeInfo struct { Timestamp time.Time } +type rebalanceResult struct { + successfulAttempts int + successfulAmt int64 + paidFeeMsat int64 +} + type regolancer struct { lnClient lnrpc.LightningClient routerClient routerrpc.RouterClient @@ -181,7 +187,7 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, } routeCtx, routeCtxCancel := context.WithTimeout(attemptCtx, time.Second*time.Duration(params.TimeoutRoute)) defer routeCtxCancel() - routes, fee, err := r.getRoutes(routeCtx, from, to, amt*1000) + routes, feeMsat, err := r.getRoutes(routeCtx, from, to, amt*1000) if err != nil { if routeCtx.Err() == context.DeadlineExceeded { log.Print(errColor("Timed out looking for a route")) @@ -193,16 +199,18 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, routeCtxCancel() for _, route := range routes { log.Printf("Attempt %s, amount: %s (max fee: %s sat | %s ppm )", - hiWhiteColorF("#%d", *attempt), hiWhiteColor(amt), formatFee(fee), formatFeePPM(amt*1000, fee)) + hiWhiteColorF("#%d", *attempt), hiWhiteColor(amt), formatFee(feeMsat), formatFeePPM(amt*1000, feeMsat)) r.printRoute(attemptCtx, route) err = r.pay(attemptCtx, amt, params.MinAmount, route, params.ProbeSteps) if err == nil { if params.AllowRapidRebalance { - attempt, rebalanceAmt, _ := tryRapidRebalance(ctx, r, from, to, route, amt) + rebalanceResult, _ := tryRapidRebalance(ctx, r, from, to, route, amt, feeMsat) - if attempt > 0 { - log.Printf("%s rapid rebalances were successful, total amount rebalanced: %s sats \n", hiWhiteColor(attempt), hiWhiteColor(rebalanceAmt)) + if rebalanceResult.successfulAttempts > 0 { + log.Printf("%s rapid rebalances were successful, total amount: %s (fee: %s sat | %s ppm)\n", + hiWhiteColor(rebalanceResult.successfulAttempts), hiWhiteColor(rebalanceResult.successfulAmt), + formatFee(rebalanceResult.paidFeeMsat), formatFeePPM(rebalanceResult.successfulAmt*1000, rebalanceResult.paidFeeMsat)) } log.Printf("Finished rapid rebalancing") } @@ -219,10 +227,12 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, err = r.pay(ctx, amt, 0, probedRoute, 0) if err == nil { if params.AllowRapidRebalance && params.MinAmount > 0 { - attempt, rebalanceAmt, _ := tryRapidRebalance(ctx, r, from, to, probedRoute, amt) + rebalanceResult, _ := tryRapidRebalance(ctx, r, from, to, probedRoute, amt, feeMsat) - if attempt > 0 { - log.Printf("%s rapid rebalances were successful, total amount rebalanced: %s sats \n", hiWhiteColor(attempt), hiWhiteColor(rebalanceAmt)) + if rebalanceResult.successfulAttempts > 0 { + log.Printf("%s rapid rebalances were successful, total amount: %s (fee: %s sat | %s ppm)\n", + hiWhiteColor(rebalanceResult.successfulAttempts), hiWhiteColor(rebalanceResult.successfulAmt), + formatFee(rebalanceResult.paidFeeMsat), formatFeePPM(rebalanceResult.successfulAmt*1000, rebalanceResult.paidFeeMsat)) } log.Printf("Finished rapid rebalancing") } @@ -244,7 +254,12 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, return nil, true } -func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, route *lnrpc.Route, amt int64) (successfulAttempts int, successfulAmt int64, err error) { +func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, route *lnrpc.Route, amt int64, feeMsat int64) (rebalanceResult, error) { + + successfulAttempts := 0 + // Include Initial Rebalance + successfulAmt := amt + paidFeeMsat := feeMsat for { @@ -254,13 +269,13 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching target channel: %s", err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } cFrom, err := r.getChanInfo(ctx, from) if err != nil { logErrorF("Error fetching source channel: %s", err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } fromPeer, _ := hex.DecodeString(cFrom.Node1Pub) @@ -271,7 +286,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching source channel: %s", err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } toPeer, _ := hex.DecodeString(cTo.Node1Pub) @@ -283,7 +298,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching target channel: %s", err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } for k := range r.fromChannelId { @@ -315,14 +330,14 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error selecting channel candidates: %s", err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } from, to, amt, err = r.pickChannelPair(amt, params.MinAmount, params.RelAmountFrom, params.RelAmountTo) if err != nil { log.Printf(errColor("Error during picking channel: %s"), err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } log.Printf("rapid fire starting with amount %s", hiWhiteColor(amt)) @@ -331,7 +346,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { log.Printf(errColor("Error building route: %s"), err) - return successfulAttempts, successfulAmt, err + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err } attemptCtx, attemptCancel := context.WithTimeout(ctx, time.Minute*time.Duration(params.TimeoutAttempt)) @@ -344,7 +359,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if attemptCtx.Err() == context.DeadlineExceeded { log.Print(errColor("Rapid rebalance attempt timed out")) - return successfulAttempts, successfulAmt, attemptCtx.Err() + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, attemptCtx.Err() } if err != nil { @@ -353,9 +368,10 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout } else { successfulAttempts++ successfulAmt += amt + paidFeeMsat += route.GetTotalFeesMsat() } } - return successfulAttempts, successfulAmt, nil + return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, nil } func preflightChecks(params *configParams) error { From 7cb051fc97ad6e47228a1794443b30db290cdcc5 Mon Sep 17 00:00:00 2001 From: rkfg Date: Sat, 17 Dec 2022 13:48:49 +0300 Subject: [PATCH 3/4] Simplify rapid rebalance result --- main.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 20c340f..839b24d 100644 --- a/main.go +++ b/main.go @@ -254,28 +254,30 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, return nil, true } -func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, route *lnrpc.Route, amt int64, feeMsat int64) (rebalanceResult, error) { +func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, + route *lnrpc.Route, amt int64, feeMsat int64) (result rebalanceResult, + err error) { - successfulAttempts := 0 + result.successfulAttempts = 0 // Include Initial Rebalance - successfulAmt := amt - paidFeeMsat := feeMsat + result.successfulAmt = amt + result.paidFeeMsat = feeMsat for { - log.Printf("Rapid rebalance attempt %s", hiWhiteColor(successfulAttempts+1)) + log.Printf("Rapid rebalance attempt %s", hiWhiteColor(result.successfulAttempts+1)) cTo, err := r.getChanInfo(ctx, to) if err != nil { logErrorF("Error fetching target channel: %s", err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } cFrom, err := r.getChanInfo(ctx, from) if err != nil { logErrorF("Error fetching source channel: %s", err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } fromPeer, _ := hex.DecodeString(cFrom.Node1Pub) @@ -286,7 +288,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching source channel: %s", err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } toPeer, _ := hex.DecodeString(cTo.Node1Pub) @@ -298,7 +300,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error fetching target channel: %s", err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } for k := range r.fromChannelId { @@ -315,8 +317,8 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout r.fromChannels = r.fromChannels[:0] r.toChannels = r.toChannels[:0] - r.channels = append(r.channels, toChan.Channels...) - r.channels = append(r.channels, fromChan.Channels...) + r.channels = append(append(r.channels, toChan.Channels...), + fromChan.Channels...) for k := range r.failureCache { delete(r.failureCache, k) @@ -330,14 +332,14 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { logErrorF("Error selecting channel candidates: %s", err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } from, to, amt, err = r.pickChannelPair(amt, params.MinAmount, params.RelAmountFrom, params.RelAmountTo) if err != nil { log.Printf(errColor("Error during picking channel: %s"), err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } log.Printf("rapid fire starting with amount %s", hiWhiteColor(amt)) @@ -346,7 +348,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if err != nil { log.Printf(errColor("Error building route: %s"), err) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, err + return result, err } attemptCtx, attemptCancel := context.WithTimeout(ctx, time.Minute*time.Duration(params.TimeoutAttempt)) @@ -359,19 +361,19 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout if attemptCtx.Err() == context.DeadlineExceeded { log.Print(errColor("Rapid rebalance attempt timed out")) - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, attemptCtx.Err() + return result, attemptCtx.Err() } if err != nil { log.Printf("Rebalance failed with %s", err) break } else { - successfulAttempts++ - successfulAmt += amt - paidFeeMsat += route.GetTotalFeesMsat() + result.successfulAttempts++ + result.successfulAmt += amt + result.paidFeeMsat += route.GetTotalFeesMsat() } } - return rebalanceResult{successfulAttempts, successfulAmt, paidFeeMsat}, nil + return result, nil } func preflightChecks(params *configParams) error { From 91acf210865c2d0e92649e10e40860d9ba9db310 Mon Sep 17 00:00:00 2001 From: rkfg Date: Sat, 17 Dec 2022 15:07:03 +0300 Subject: [PATCH 4/4] Fix initial fee in rapid rebalance --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 839b24d..5d6281a 100644 --- a/main.go +++ b/main.go @@ -205,7 +205,7 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error, if err == nil { if params.AllowRapidRebalance { - rebalanceResult, _ := tryRapidRebalance(ctx, r, from, to, route, amt, feeMsat) + rebalanceResult, _ := tryRapidRebalance(ctx, r, from, to, route, amt, route.TotalFeesMsat) if rebalanceResult.successfulAttempts > 0 { log.Printf("%s rapid rebalances were successful, total amount: %s (fee: %s sat | %s ppm)\n", @@ -370,7 +370,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, } else { result.successfulAttempts++ result.successfulAmt += amt - result.paidFeeMsat += route.GetTotalFeesMsat() + result.paidFeeMsat += route.TotalFeesMsat } } return result, nil