diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..b641496 --- /dev/null +++ b/helper.go @@ -0,0 +1,10 @@ +package main + +func absoluteDeltaPPM(base, amt int64) (deltaPPM int64) { + + deltaPPM = (base - amt) * 1e6 / base + if deltaPPM < 0 { + return -deltaPPM + } + return deltaPPM +} diff --git a/main.go b/main.go index 57a9b01..e1b3eef 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ type configParams struct { Exclude []string `long:"exclude" description:"don't use this node or your channel for routing (can be specified multiple times)" json:"exclude" toml:"exclude"` To []string `long:"to" description:"try only this channel or node as target (should satisfy other constraints too; can be specified multiple times)" json:"to" toml:"to"` From []string `long:"from" description:"try only this channel or node as source (should satisfy other constraints too; can be specified multiple times)" json:"from" toml:"from"` - FailTolerance int64 `long:"fail-tolerance" description:"if a channel failed before during this rebalance but chosen again by lnd, and the forward amount differs by less than this ppm, exclude the channel" json:"fail_tolerance" toml:"fail_tolerance"` + FailTolerance int64 `long:"fail-tolerance" description:"a payment that differs from the prior attempt by this ppm will be cancelled" json:"fail_tolerance" toml:"fail_tolerance"` AllowUnbalanceFrom bool `long:"allow-unbalance-from" description:"let the source channel go below 50% local liquidity, use if you want to drain a channel; you should also set --pfrom to >50" json:"allow_unbalance_from" toml:"allow_unbalance_from"` AllowUnbalanceTo bool `long:"allow-unbalance-to" description:"let the target channel go above 50% local liquidity, use if you want to refill a channel; you should also set --pto to >50" json:"allow_unbalance_to" toml:"allow_unbalance_to"` StatFilename string `short:"s" long:"stat" description:"save successful rebalance information to the specified CSV file" json:"stat" toml:"stat"` @@ -374,6 +374,7 @@ func preflightChecks(params *configParams) error { if params.FailTolerance == 0 { params.FailTolerance = 1000 } + if (params.RelAmountFrom > 0 || params.RelAmountTo > 0) && params.AllowRapidRebalance { return fmt.Errorf("use either relative amounts or rapid rebalance but not both") diff --git a/mission_ctl.go b/mission_ctl.go index 566e00a..16aea01 100644 --- a/mission_ctl.go +++ b/mission_ctl.go @@ -15,7 +15,7 @@ func (r *regolancer) validateRoute(route *lnrpc.Route) error { prevHopPK := r.myPK for _, h := range route.Hops { hopPK := h.PubKey - if fp, ok := r.mcCache[prevHopPK+hopPK]; ok && 1e6-h.AmtToForwardMsat*1e6/fp < params.FailTolerance { + if fp, ok := r.mcCache[prevHopPK+hopPK]; ok && absoluteDeltaPPM(fp, h.AmtToForwardMsat) < params.FailTolerance { from, err := hex.DecodeString(prevHopPK) if err != nil { return err diff --git a/routes.go b/routes.go index 47d4ec9..993f4dc 100644 --- a/routes.go +++ b/routes.go @@ -195,7 +195,8 @@ func (r *regolancer) rebuildRoute(ctx context.Context, route *lnrpc.Route, amoun func (r *regolancer) probeRoute(ctx context.Context, route *lnrpc.Route, goodAmount, badAmount, amount int64, steps int) (maxAmount int64, err error) { - if amount == badAmount || amount == goodAmount || amount == -goodAmount { + if absoluteDeltaPPM(badAmount, amount) <= params.FailTolerance || absoluteDeltaPPM(amount, goodAmount) <= params.FailTolerance || amount == -goodAmount { + bestAmount := hiWhiteColor(goodAmount) if goodAmount <= 0 { bestAmount = hiWhiteColor("unknown")