Skip to content

Commit 96f395e

Browse files
authored
Merge pull request #305 from carlaKC/autoloop-forcetick
liquidity: add force tick endpoint to trigger autoloop in tests
2 parents 62be092 + 87b02b7 commit 96f395e

File tree

10 files changed

+309
-5
lines changed

10 files changed

+309
-5
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ sudo: required
2424
script:
2525
- export GO111MODULE=on
2626
- make lint unit build mod-check
27+
- make tags=dev
2728

2829
after_script:
2930
- echo "Uploading to termbin.com..." && find *.log | xargs -I{} sh -c "cat {} | nc termbin.com 9999 | xargs -r0 printf '{} uploaded to %s'"

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ GOMOD := GO111MODULE=on go mod
1111

1212
COMMIT := $(shell git describe --abbrev=40 --dirty)
1313
LDFLAGS := -ldflags "-X $(PKG)/build.Commit=$(COMMIT)"
14+
DEV_TAGS = dev
1415

1516
GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
1617
GOLIST := go list $(PKG)/... | grep -v '/vendor/'
@@ -64,10 +65,10 @@ mod-check:
6465

6566
build:
6667
@$(call print, "Building debug loop and loopd.")
67-
$(GOBUILD) -o loop-debug $(LDFLAGS) $(PKG)/cmd/loop
68-
$(GOBUILD) -o loopd-debug $(LDFLAGS) $(PKG)/cmd/loopd
68+
$(GOBUILD) -tags="$(DEV_TAGS)" -o loop-debug $(LDFLAGS) $(PKG)/cmd/loop
69+
$(GOBUILD) -tags="$(DEV_TAGS)" -o loopd-debug $(LDFLAGS) $(PKG)/cmd/loopd
6970

7071
install:
7172
@$(call print, "Installing loop and loopd.")
72-
$(GOINSTALL) $(LDFLAGS) $(PKG)/cmd/loop
73-
$(GOINSTALL) $(LDFLAGS) $(PKG)/cmd/loopd
73+
$(GOINSTALL) -tags="${tags}" $(LDFLAGS) $(PKG)/cmd/loop
74+
$(GOINSTALL) -tags="${tags}" $(LDFLAGS) $(PKG)/cmd/loopd

liquidity/liquidity.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ func (m *Manager) autoloop(ctx context.Context) error {
453453
return nil
454454
}
455455

456+
// ForceAutoLoop force-ticks our auto-out ticker.
457+
func (m *Manager) ForceAutoLoop(ctx context.Context) error {
458+
select {
459+
case m.cfg.AutoOutTicker.Force <- m.cfg.Clock.Now():
460+
return nil
461+
462+
case <-ctx.Done():
463+
return ctx.Err()
464+
}
465+
}
466+
456467
// SuggestSwaps returns a set of swap suggestions based on our current liquidity
457468
// balance for the set of rules configured for the manager, failing if there are
458469
// no rules set. It takes an autoOut boolean that indicates whether the

loopd/daemon.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ func (d *Daemon) startWebServers() error {
197197
d.grpcServer = grpc.NewServer(serverOpts...)
198198
looprpc.RegisterSwapClientServer(d.grpcServer, d)
199199

200+
// Register our debug server if it is compiled in.
201+
d.registerDebugServer()
202+
200203
// Next, start the gRPC server listening for HTTP/2 connections.
201204
log.Infof("Starting gRPC listener")
202205
serverTLSCfg, restClientCreds, err := getTLSConfig(d.cfg)
@@ -355,6 +358,7 @@ func (d *Daemon) initialize() error {
355358

356359
// Now finally fully initialize the swap client RPC server instance.
357360
d.swapClientServer = swapClientServer{
361+
network: lndclient.Network(d.cfg.Network),
358362
impl: swapclient,
359363
liquidityMgr: getLiquidityManager(swapclient),
360364
lnd: &d.lnd.LndServices,

loopd/macaroons.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ func (d *Daemon) startMacaroonService() error {
164164
// We only generate one default macaroon that contains all
165165
// existing permissions (equivalent to the admin.macaroon in
166166
// lnd). Custom macaroons can be created through the bakery
167-
// RPC.
167+
// RPC. Add our debug permissions if required.
168+
allPermissions = append(allPermissions, debugPermissions...)
168169
loopMac, err := d.macaroonService.Oven.NewMacaroon(
169170
ctx, bakery.LatestVersion, nil, allPermissions...,
170171
)
@@ -196,6 +197,12 @@ func (d *Daemon) stopMacaroonService() error {
196197
// macaroonInterceptor creates gRPC server options with the macaroon security
197198
// interceptors.
198199
func (d *Daemon) macaroonInterceptor() []grpc.ServerOption {
200+
// Add our debug permissions to our main set of required permissions
201+
// if compiled in.
202+
for endpoint, perm := range debugRequiredPermissions {
203+
RequiredPermissions[endpoint] = perm
204+
}
205+
199206
unaryInterceptor := d.macaroonService.UnaryServerInterceptor(
200207
RequiredPermissions,
201208
)

loopd/register_default.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build !dev
2+
3+
package loopd
4+
5+
import "gopkg.in/macaroon-bakery.v2/bakery"
6+
7+
var (
8+
debugRequiredPermissions = map[string][]bakery.Op{}
9+
debugPermissions []bakery.Op
10+
)
11+
12+
// registerDebugServer is our default debug server registration function, which
13+
// excludes debug functionality.
14+
func (d *Daemon) registerDebugServer() {}

loopd/swapclient_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434

3535
// swapClientServer implements the grpc service exposed by loopd.
3636
type swapClientServer struct {
37+
network lndclient.Network
3738
impl *loop.Client
3839
liquidityMgr *liquidity.Manager
3940
lnd *lndclient.LndServices

loopd/swapclient_server_debug.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// +build dev
2+
3+
package loopd
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"gopkg.in/macaroon-bakery.v2/bakery"
10+
11+
"github.com/lightninglabs/lndclient"
12+
"github.com/lightninglabs/loop/looprpc"
13+
)
14+
15+
var (
16+
debugRequiredPermissions = map[string][]bakery.Op{
17+
"/looprpc.Debug/ForceAutoLoop": {{
18+
Entity: "debug",
19+
Action: "write",
20+
}},
21+
}
22+
23+
debugPermissions = []bakery.Op{
24+
{
25+
Entity: "debug",
26+
Action: "write",
27+
},
28+
}
29+
)
30+
31+
// registerDebugServer registers the debug server.
32+
func (d *Daemon) registerDebugServer() {
33+
looprpc.RegisterDebugServer(d.grpcServer, d)
34+
}
35+
36+
// ForceAutoLoop triggers our liquidity manager to dispatch an automated swap,
37+
// if one is suggested. This endpoint is only for testing purposes and cannot be
38+
// used on mainnet.
39+
func (s *swapClientServer) ForceAutoLoop(ctx context.Context,
40+
_ *looprpc.ForceAutoLoopRequest) (*looprpc.ForceAutoLoopResponse, error) {
41+
42+
if s.network == lndclient.NetworkMainnet {
43+
return nil, fmt.Errorf("force autoloop not allowed on mainnet")
44+
}
45+
46+
err := s.liquidityMgr.ForceAutoLoop(ctx)
47+
return &looprpc.ForceAutoLoopResponse{}, err
48+
}

looprpc/debug.pb.go

Lines changed: 194 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

looprpc/debug.proto

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
syntax = "proto3";
2+
3+
package looprpc;
4+
5+
/*
6+
Debug is a service that exposes endpoints intended for testing purposes. These
7+
endpoints should not operate on mainnet, and should only be included if loop is
8+
built with the dev build tag.
9+
*/
10+
service Debug {
11+
/*
12+
ForceAutoLoop is intended for *testing purposes only* and will not work on
13+
mainnet. This endpoint ticks our autoloop timer, triggering automated
14+
dispatch of a swap if one is suggested.
15+
*/
16+
rpc ForceAutoLoop(ForceAutoLoopRequest) returns (ForceAutoLoopResponse){}
17+
}
18+
19+
message ForceAutoLoopRequest {
20+
}
21+
22+
message ForceAutoLoopResponse {
23+
}

0 commit comments

Comments
 (0)