Skip to content

Commit 7ba1821

Browse files
committed
liquidity: fail suggest swaps when no rules are set
In an effort to surface more information about why autoloop is not executing, we add an error when suggest swaps is called with no rules. In other cases we can surface a reason enum with each rule that is set, but in the case where we have no rules, there are no results to accompany with reasons.
1 parent 67f4171 commit 7ba1821

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

cmd/loop/liquidity.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strconv"
78

89
"github.com/lightninglabs/loop/liquidity"
910
"github.com/lightninglabs/loop/looprpc"
1011
"github.com/urfave/cli"
12+
"google.golang.org/grpc/codes"
13+
"google.golang.org/grpc/status"
1114
)
1215

1316
var getLiquidityParamsCommand = cli.Command{
@@ -411,11 +414,22 @@ func suggestSwap(ctx *cli.Context) error {
411414
resp, err := client.SuggestSwaps(
412415
context.Background(), &looprpc.SuggestSwapsRequest{},
413416
)
414-
if err != nil {
417+
if err == nil {
418+
printJSON(resp)
419+
return nil
420+
}
421+
422+
// If we got an error because no rules are set, we want to display a
423+
// friendly message.
424+
rpcErr, ok := status.FromError(err)
425+
if !ok {
415426
return err
416427
}
417428

418-
printJSON(resp)
429+
if rpcErr.Code() != codes.FailedPrecondition {
430+
return err
431+
}
419432

420-
return nil
433+
return errors.New("no rules set for autolooper, please set rules " +
434+
"using the setrule command")
421435
}

liquidity/liquidity.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ var (
178178
// less than the server minimum.
179179
ErrMinLessThanServer = errors.New("minimum swap amount is less than " +
180180
"server minimum")
181+
182+
// ErrNoRules is returned when no rules are set for swap suggestions.
183+
ErrNoRules = errors.New("no rules set for autoloop")
181184
)
182185

183186
// Config contains the external functionality required to run the
@@ -439,7 +442,14 @@ func (m *Manager) Run(ctx context.Context) error {
439442
for {
440443
select {
441444
case <-m.cfg.AutoloopTicker.Ticks():
442-
if err := m.autoloop(ctx); err != nil {
445+
err := m.autoloop(ctx)
446+
switch err {
447+
case ErrNoRules:
448+
log.Debugf("No rules configured for autoloop")
449+
450+
case nil:
451+
452+
default:
443453
log.Errorf("autoloop failed: %v", err)
444454
}
445455

@@ -562,7 +572,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
562572
// If we have no rules set, exit early to avoid unnecessary calls to
563573
// lnd and the server.
564574
if len(m.params.ChannelRules) == 0 {
565-
return nil, nil
575+
return nil, ErrNoRules
566576
}
567577

568578
// If our start date is in the future, we interpret this as meaning that

liquidity/liquidity_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,12 @@ func TestSuggestSwaps(t *testing.T) {
496496
name string
497497
rules map[lnwire.ShortChannelID]*ThresholdRule
498498
swaps []loop.OutRequest
499+
err error
499500
}{
500501
{
501502
name: "no rules",
502503
rules: map[lnwire.ShortChannelID]*ThresholdRule{},
504+
err: ErrNoRules,
503505
},
504506
{
505507
name: "loop out",
@@ -534,7 +536,7 @@ func TestSuggestSwaps(t *testing.T) {
534536

535537
testSuggestSwaps(
536538
t, newSuggestSwapsSetup(cfg, lnd, params),
537-
testCase.swaps, nil,
539+
testCase.swaps, testCase.err,
538540
)
539541
})
540542
}

loopd/swapclient_server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"github.com/lightningnetwork/lnd/lnwire"
2222
"github.com/lightningnetwork/lnd/queue"
2323
"github.com/lightningnetwork/lnd/routing/route"
24+
"google.golang.org/grpc/codes"
25+
"google.golang.org/grpc/status"
2426
)
2527

2628
const (
@@ -703,7 +705,13 @@ func (s *swapClientServer) SuggestSwaps(ctx context.Context,
703705
_ *looprpc.SuggestSwapsRequest) (*looprpc.SuggestSwapsResponse, error) {
704706

705707
swaps, err := s.liquidityMgr.SuggestSwaps(ctx, false)
706-
if err != nil {
708+
switch err {
709+
case liquidity.ErrNoRules:
710+
return nil, status.Error(codes.FailedPrecondition, err.Error())
711+
712+
case nil:
713+
714+
default:
707715
return nil, err
708716
}
709717

release_notes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ This file tracks release notes for the loop client.
3131
been renamed to `Autoloop`, `AutoloopBudgetSat` and `AutoloopBudgetStartSec`.
3232
* The `autoout` flag for enabling automatic dispatch of loop out swaps has been
3333
renamed to `autoloop` so that it can cover loop out and loop in.
34+
* The `SuggestSwaps` rpc call will now fail with a `FailedPrecondition` grpc
35+
error code if no rules are configured for the autolooper. Previously the rpc
36+
would return an empty response.
3437

3538
#### Bug Fixes

0 commit comments

Comments
 (0)