Merge pull request #41 from ziggie1984/rapidrebalance-amount

pull/45/head
rkfg 2 years ago committed by GitHub
commit 381538d43b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -80,6 +80,12 @@ type cachedNodeInfo struct {
Timestamp time.Time Timestamp time.Time
} }
type rebalanceResult struct {
successfulAttempts int
successfulAmt int64
paidFeeMsat int64
}
type regolancer struct { type regolancer struct {
lnClient lnrpc.LightningClient lnClient lnrpc.LightningClient
routerClient routerrpc.RouterClient 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)) routeCtx, routeCtxCancel := context.WithTimeout(attemptCtx, time.Second*time.Duration(params.TimeoutRoute))
defer routeCtxCancel() 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 err != nil {
if routeCtx.Err() == context.DeadlineExceeded { if routeCtx.Err() == context.DeadlineExceeded {
log.Print(errColor("Timed out looking for a route")) log.Print(errColor("Timed out looking for a route"))
@ -193,19 +199,20 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error,
routeCtxCancel() routeCtxCancel()
for _, route := range routes { for _, route := range routes {
log.Printf("Attempt %s, amount: %s (max fee: %s sat | %s ppm )", 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) r.printRoute(attemptCtx, route)
err = r.pay(attemptCtx, amt, params.MinAmount, route, params.ProbeSteps) err = r.pay(attemptCtx, amt, params.MinAmount, route, params.ProbeSteps)
if err == nil { if err == nil {
if params.AllowRapidRebalance { if params.AllowRapidRebalance {
_, err := tryRapidRebalance(ctx, r, from, to, route, amt) rebalanceResult, _ := tryRapidRebalance(ctx, r, from, to, route, amt, route.TotalFeesMsat)
if err != nil { if rebalanceResult.successfulAttempts > 0 {
log.Printf("Rapid rebalance failed with %s", err) log.Printf("%s rapid rebalances were successful, total amount: %s (fee: %s sat | %s ppm)\n",
} else { hiWhiteColor(rebalanceResult.successfulAttempts), hiWhiteColor(rebalanceResult.successfulAmt),
log.Printf("Finished rapid rebalancing") formatFee(rebalanceResult.paidFeeMsat), formatFeePPM(rebalanceResult.successfulAmt*1000, rebalanceResult.paidFeeMsat))
} }
log.Printf("Finished rapid rebalancing")
} }
return nil, false return nil, false
@ -220,13 +227,14 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error,
err = r.pay(ctx, amt, 0, probedRoute, 0) err = r.pay(ctx, amt, 0, probedRoute, 0)
if err == nil { if err == nil {
if params.AllowRapidRebalance && params.MinAmount > 0 { if params.AllowRapidRebalance && params.MinAmount > 0 {
_, err := tryRapidRebalance(ctx, r, from, to, probedRoute, amt) rebalanceResult, _ := tryRapidRebalance(ctx, r, from, to, probedRoute, amt, feeMsat)
if err != nil { if rebalanceResult.successfulAttempts > 0 {
log.Printf("Rapid rebalance failed with %s", err) log.Printf("%s rapid rebalances were successful, total amount: %s (fee: %s sat | %s ppm)\n",
} else { hiWhiteColor(rebalanceResult.successfulAttempts), hiWhiteColor(rebalanceResult.successfulAmt),
log.Printf("Finished rapid rebalancing") formatFee(rebalanceResult.paidFeeMsat), formatFeePPM(rebalanceResult.successfulAmt*1000, rebalanceResult.paidFeeMsat))
} }
log.Printf("Finished rapid rebalancing")
} }
return nil, false return nil, false
@ -246,25 +254,30 @@ func tryRebalance(ctx context.Context, r *regolancer, attempt *int) (err error,
return nil, true return nil, true
} }
func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, route *lnrpc.Route, amt int64) (successfullAtempts int, err error) { func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64,
route *lnrpc.Route, amt int64, feeMsat int64) (result rebalanceResult,
err error) {
rapidAttempt := 0 result.successfulAttempts = 0
// Include Initial Rebalance
result.successfulAmt = amt
result.paidFeeMsat = feeMsat
for { for {
log.Printf("Rapid rebalance attempt %s", hiWhiteColor(rapidAttempt+1)) log.Printf("Rapid rebalance attempt %s", hiWhiteColor(result.successfulAttempts+1))
cTo, err := r.getChanInfo(ctx, to) cTo, err := r.getChanInfo(ctx, to)
if err != nil { if err != nil {
logErrorF("Error fetching target channel: %s", err) logErrorF("Error fetching target channel: %s", err)
return rapidAttempt, err return result, err
} }
cFrom, err := r.getChanInfo(ctx, from) cFrom, err := r.getChanInfo(ctx, from)
if err != nil { if err != nil {
logErrorF("Error fetching source channel: %s", err) logErrorF("Error fetching source channel: %s", err)
return rapidAttempt, err return result, err
} }
fromPeer, _ := hex.DecodeString(cFrom.Node1Pub) fromPeer, _ := hex.DecodeString(cFrom.Node1Pub)
@ -275,7 +288,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout
if err != nil { if err != nil {
logErrorF("Error fetching source channel: %s", err) logErrorF("Error fetching source channel: %s", err)
return rapidAttempt, err return result, err
} }
toPeer, _ := hex.DecodeString(cTo.Node1Pub) toPeer, _ := hex.DecodeString(cTo.Node1Pub)
@ -287,7 +300,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout
if err != nil { if err != nil {
logErrorF("Error fetching target channel: %s", err) logErrorF("Error fetching target channel: %s", err)
return rapidAttempt, err return result, err
} }
for k := range r.fromChannelId { for k := range r.fromChannelId {
@ -304,8 +317,8 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout
r.fromChannels = r.fromChannels[:0] r.fromChannels = r.fromChannels[:0]
r.toChannels = r.toChannels[:0] r.toChannels = r.toChannels[:0]
r.channels = append(r.channels, toChan.Channels...) r.channels = append(append(r.channels, toChan.Channels...),
r.channels = append(r.channels, fromChan.Channels...) fromChan.Channels...)
for k := range r.failureCache { for k := range r.failureCache {
delete(r.failureCache, k) delete(r.failureCache, k)
@ -319,14 +332,14 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout
if err != nil { if err != nil {
logErrorF("Error selecting channel candidates: %s", err) logErrorF("Error selecting channel candidates: %s", err)
return rapidAttempt, err return result, err
} }
from, to, amt, err = r.pickChannelPair(amt, params.MinAmount, params.RelAmountFrom, params.RelAmountTo) from, to, amt, err = r.pickChannelPair(amt, params.MinAmount, params.RelAmountFrom, params.RelAmountTo)
if err != nil { if err != nil {
log.Printf(errColor("Error during picking channel: %s"), err) log.Printf(errColor("Error during picking channel: %s"), err)
return rapidAttempt, err return result, err
} }
log.Printf("rapid fire starting with amount %s", hiWhiteColor(amt)) log.Printf("rapid fire starting with amount %s", hiWhiteColor(amt))
@ -335,7 +348,7 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout
if err != nil { if err != nil {
log.Printf(errColor("Error building route: %s"), err) log.Printf(errColor("Error building route: %s"), err)
return rapidAttempt, err return result, err
} }
attemptCtx, attemptCancel := context.WithTimeout(ctx, time.Minute*time.Duration(params.TimeoutAttempt)) attemptCtx, attemptCancel := context.WithTimeout(ctx, time.Minute*time.Duration(params.TimeoutAttempt))
@ -348,20 +361,19 @@ func tryRapidRebalance(ctx context.Context, r *regolancer, from, to uint64, rout
if attemptCtx.Err() == context.DeadlineExceeded { if attemptCtx.Err() == context.DeadlineExceeded {
log.Print(errColor("Rapid rebalance attempt timed out")) log.Print(errColor("Rapid rebalance attempt timed out"))
return rapidAttempt, attemptCtx.Err() return result, attemptCtx.Err()
} }
if err != nil { if err != nil {
log.Printf("Rebalance failed with %s", err) log.Printf("Rebalance failed with %s", err)
break break
} else { } else {
rapidAttempt++ result.successfulAttempts++
result.successfulAmt += amt
result.paidFeeMsat += route.TotalFeesMsat
} }
} }
log.Printf("%s rapid rebalances were successful\n", hiWhiteColor(rapidAttempt)) return result, nil
return rapidAttempt, nil
} }
func preflightChecks(params *configParams) error { func preflightChecks(params *configParams) error {

Loading…
Cancel
Save