Skip to content

Commit 9af6148

Browse files
authored
Merge pull request #331 from carlaKC/autoloop-0-refactoring
autoloop: refactoring to prepare for loopin
2 parents d85cc01 + 68a5336 commit 9af6148

File tree

13 files changed

+290
-259
lines changed

13 files changed

+290
-259
lines changed

cmd/loop/liquidity.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ var setParamsCommand = cli.Command{
225225
"included in suggestions.",
226226
},
227227
cli.BoolFlag{
228-
Name: "autoout",
228+
Name: "autoloop",
229229
Usage: "set to true to enable automated dispatch " +
230-
"of loop out swaps, limited to the budget " +
231-
"set by autobudget",
230+
"of swaps, limited to the budget set by " +
231+
"autobudget",
232232
},
233233
cli.Uint64Flag{
234234
Name: "autobudget",
@@ -338,18 +338,18 @@ func setParams(ctx *cli.Context) error {
338338
flagSet = true
339339
}
340340

341-
if ctx.IsSet("autoout") {
342-
params.AutoLoopOut = ctx.Bool("autoout")
341+
if ctx.IsSet("autoloop") {
342+
params.Autoloop = ctx.Bool("autoloop")
343343
flagSet = true
344344
}
345345

346346
if ctx.IsSet("autobudget") {
347-
params.AutoOutBudgetSat = ctx.Uint64("autobudget")
347+
params.AutoloopBudgetSat = ctx.Uint64("autobudget")
348348
flagSet = true
349349
}
350350

351351
if ctx.IsSet("budgetstart") {
352-
params.AutoOutBudgetStartSec = ctx.Uint64("budgetstart")
352+
params.AutoloopBudgetStartSec = ctx.Uint64("budgetstart")
353353
flagSet = true
354354
}
355355

docs/autoloop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ budget of your choosing.
66
The autoloop functionality is disabled by default, and can be enabled using the
77
following command:
88
```
9-
loop setparams --autoout=true
9+
loop setparams --autoloop=true
1010
```
1111

1212
Swaps that are dispatched by the autolooper can be identified in the output of

labels/labels.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"fmt"
66
"strings"
7+
8+
"github.com/lightninglabs/loop/swap"
79
)
810

911
const (
@@ -17,6 +19,10 @@ const (
1719
// autoOut is the label used for loop out swaps that are automatically
1820
// dispatched.
1921
autoOut = "autoloop-out"
22+
23+
// autoIn is the label used for loop in swaps that are automatically
24+
// dispatched.
25+
autoIn = "autoloop-in"
2026
)
2127

2228
var (
@@ -28,10 +34,14 @@ var (
2834
ErrReservedPrefix = errors.New("label contains reserved prefix")
2935
)
3036

31-
// AutoOutLabel returns a label with the reserved prefix that identifies
32-
// automatically dispatched loop outs.
33-
func AutoOutLabel() string {
34-
return fmt.Sprintf("%v: %v", Reserved, autoOut)
37+
// AutoloopLabel returns a label with the reserved prefix that identifies
38+
// automatically dispatched swaps depending on the type of swap being executed.
39+
func AutoloopLabel(swapType swap.Type) string {
40+
if swapType == swap.TypeOut {
41+
return fmt.Sprintf("%v: %v", Reserved, autoOut)
42+
}
43+
44+
return fmt.Sprintf("%v: %v", Reserved, autoIn)
3545
}
3646

3747
// Validate checks that a label is of appropriate length and is not in our list

liquidity/autoloop_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/lightninglabs/loop"
99
"github.com/lightninglabs/loop/labels"
1010
"github.com/lightninglabs/loop/loopdb"
11+
"github.com/lightninglabs/loop/swap"
1112
"github.com/lightninglabs/loop/test"
1213
"github.com/lightningnetwork/lnd/lntypes"
1314
"github.com/lightningnetwork/lnd/lnwire"
@@ -75,7 +76,7 @@ func TestAutoLoopEnabled(t *testing.T) {
7576
// is set to allow exactly 2 swaps at the prices that we set in our
7677
// test quotes.
7778
params := Parameters{
78-
AutoOut: true,
79+
Autoloop: true,
7980
AutoFeeBudget: 40066,
8081
AutoFeeStartDate: testTime,
8182
MaxAutoInFlight: 2,
@@ -145,7 +146,7 @@ func TestAutoLoopEnabled(t *testing.T) {
145146
MaxMinerFee: params.MaximumMinerFee,
146147
SweepConfTarget: params.SweepConfTarget,
147148
OutgoingChanSet: loopdb.ChannelSet{chanID1.ToUint64()},
148-
Label: labels.AutoOutLabel(),
149+
Label: labels.AutoloopLabel(swap.TypeOut),
149150
Initiator: autoloopSwapInitiator,
150151
}
151152

@@ -161,7 +162,7 @@ func TestAutoLoopEnabled(t *testing.T) {
161162
MaxMinerFee: params.MaximumMinerFee,
162163
SweepConfTarget: params.SweepConfTarget,
163164
OutgoingChanSet: loopdb.ChannelSet{chanID2.ToUint64()},
164-
Label: labels.AutoOutLabel(),
165+
Label: labels.AutoloopLabel(swap.TypeOut),
165166
Initiator: autoloopSwapInitiator,
166167
}
167168

liquidity/autoloop_testcontext_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/lightninglabs/lndclient"
99
"github.com/lightninglabs/loop"
1010
"github.com/lightninglabs/loop/loopdb"
11+
"github.com/lightninglabs/loop/swap"
1112
"github.com/lightninglabs/loop/test"
1213
"github.com/lightningnetwork/lnd/clock"
1314
"github.com/lightningnetwork/lnd/ticker"
@@ -91,8 +92,10 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
9192
testCtx.lnd.Channels = channels
9293

9394
cfg := &Config{
94-
AutoOutTicker: ticker.NewForce(DefaultAutoOutTicker),
95-
LoopOutRestrictions: func(context.Context) (*Restrictions, error) {
95+
AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker),
96+
Restrictions: func(context.Context, swap.Type) (*Restrictions,
97+
error) {
98+
9699
return <-testCtx.loopOutRestrictions, nil
97100
},
98101
ListLoopOut: func() ([]*loopdb.LoopOut, error) {
@@ -182,7 +185,7 @@ func (c *autoloopTestCtx) autoloop(minAmt, maxAmt btcutil.Amount,
182185
expectedSwaps []loopOutRequestResp) {
183186

184187
// Tick our autoloop ticker to force assessing whether we want to loop.
185-
c.manager.cfg.AutoOutTicker.Force <- testTime
188+
c.manager.cfg.AutoloopTicker.Force <- testTime
186189

187190
// Send a mocked response from the server with the swap size limits.
188191
c.loopOutRestrictions <- NewRestrictions(minAmt, maxAmt)

liquidity/liquidity.go

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/lightninglabs/loop"
4747
"github.com/lightninglabs/loop/labels"
4848
"github.com/lightninglabs/loop/loopdb"
49+
"github.com/lightninglabs/loop/swap"
4950
"github.com/lightningnetwork/lnd/clock"
5051
"github.com/lightningnetwork/lnd/funding"
5152
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -96,9 +97,9 @@ const (
9697
// suggestions as a dry-run).
9798
defaultMaxInFlight = 1
9899

99-
// DefaultAutoOutTicker is the default amount of time between automated
100-
// loop out checks.
101-
DefaultAutoOutTicker = time.Minute * 10
100+
// DefaultAutoloopTicker is the default amount of time between automated
101+
// swap checks.
102+
DefaultAutoloopTicker = time.Minute * 10
102103

103104
// autoloopSwapInitiator is the value we send in the initiator field of
104105
// a swap request when issuing an automatic swap.
@@ -182,14 +183,15 @@ var (
182183
// Config contains the external functionality required to run the
183184
// liquidity manager.
184185
type Config struct {
185-
// AutoOutTicker determines how often we should check whether we want
186-
// to dispatch an automated loop out. We use a force ticker so that
187-
// we can trigger autoloop in itests.
188-
AutoOutTicker *ticker.Force
186+
// AutoloopTicker determines how often we should check whether we want
187+
// to dispatch an automated swap. We use a force ticker so that we can
188+
// trigger autoloop in itests.
189+
AutoloopTicker *ticker.Force
189190

190-
// LoopOutRestrictions returns the restrictions that the server applies
191-
// to loop out swaps.
192-
LoopOutRestrictions func(ctx context.Context) (*Restrictions, error)
191+
// Restrictions returns the restrictions that the server applies to
192+
// swaps.
193+
Restrictions func(ctx context.Context, swapType swap.Type) (
194+
*Restrictions, error)
193195

194196
// Lnd provides us with access to lnd's rpc servers.
195197
Lnd *lndclient.LndServices
@@ -220,8 +222,8 @@ type Config struct {
220222
// Parameters is a set of parameters provided by the user which guide
221223
// how we assess liquidity.
222224
type Parameters struct {
223-
// AutoOut enables automatic dispatch of loop out swaps.
224-
AutoOut bool
225+
// Autoloop enables automatic dispatch of swaps.
226+
Autoloop bool
225227

226228
// AutoFeeBudget is the total amount we allow to be spent on
227229
// automatically dispatched swaps. Once this budget has been used, we
@@ -431,12 +433,12 @@ type Manager struct {
431433
// We run this loop even if automated swaps are not currently enabled rather
432434
// than managing starting and stopping the ticker as our parameters are updated.
433435
func (m *Manager) Run(ctx context.Context) error {
434-
m.cfg.AutoOutTicker.Resume()
435-
defer m.cfg.AutoOutTicker.Stop()
436+
m.cfg.AutoloopTicker.Resume()
437+
defer m.cfg.AutoloopTicker.Stop()
436438

437439
for {
438440
select {
439-
case <-m.cfg.AutoOutTicker.Ticks():
441+
case <-m.cfg.AutoloopTicker.Ticks():
440442
if err := m.autoloop(ctx); err != nil {
441443
log.Errorf("autoloop failed: %v", err)
442444
}
@@ -466,7 +468,7 @@ func (m *Manager) GetParameters() Parameters {
466468
// SetParameters updates our current set of parameters if the new parameters
467469
// provided are valid.
468470
func (m *Manager) SetParameters(ctx context.Context, params Parameters) error {
469-
restrictions, err := m.cfg.LoopOutRestrictions(ctx)
471+
restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
470472
if err != nil {
471473
return err
472474
}
@@ -510,6 +512,15 @@ func (m *Manager) autoloop(ctx context.Context) error {
510512
}
511513

512514
for _, swap := range swaps {
515+
// If we don't actually have dispatch of swaps enabled, log
516+
// suggestions.
517+
if !m.params.Autoloop {
518+
log.Debugf("recommended autoloop: %v sats over "+
519+
"%v", swap.Amount, swap.OutgoingChanSet)
520+
521+
continue
522+
}
523+
513524
// Create a copy of our range var so that we can reference it.
514525
swap := swap
515526
loopOut, err := m.cfg.LoopOut(ctx, &swap)
@@ -528,7 +539,7 @@ func (m *Manager) autoloop(ctx context.Context) error {
528539
// ForceAutoLoop force-ticks our auto-out ticker.
529540
func (m *Manager) ForceAutoLoop(ctx context.Context) error {
530541
select {
531-
case m.cfg.AutoOutTicker.Force <- m.cfg.Clock.Now():
542+
case m.cfg.AutoloopTicker.Force <- m.cfg.Clock.Now():
532543
return nil
533544

534545
case <-ctx.Done():
@@ -538,11 +549,11 @@ func (m *Manager) ForceAutoLoop(ctx context.Context) error {
538549

539550
// SuggestSwaps returns a set of swap suggestions based on our current liquidity
540551
// balance for the set of rules configured for the manager, failing if there are
541-
// no rules set. It takes an autoOut boolean that indicates whether the
552+
// no rules set. It takes an autoloop boolean that indicates whether the
542553
// suggestions are being used for our internal autolooper. This boolean is used
543554
// to determine the information we add to our swap suggestion and whether we
544555
// return any suggestions.
545-
func (m *Manager) SuggestSwaps(ctx context.Context, autoOut bool) (
556+
func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
546557
[]loop.OutRequest, error) {
547558

548559
m.paramsLock.Lock()
@@ -587,7 +598,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoOut bool) (
587598

588599
// Get the current server side restrictions, combined with the client
589600
// set restrictions, if any.
590-
outRestrictions, err := m.getLoopOutRestrictions(ctx)
601+
restrictions, err := m.getSwapRestrictions(ctx, swap.TypeOut)
591602
if err != nil {
592603
return nil, err
593604
}
@@ -646,7 +657,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoOut bool) (
646657

647658
balance := newBalances(channel)
648659

649-
suggestion := rule.suggestSwap(balance, outRestrictions)
660+
suggestion := rule.suggestSwap(balance, restrictions)
650661

651662
// We can have nil suggestions in the case where no action is
652663
// required, so we skip over them.
@@ -681,7 +692,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoOut bool) (
681692
}
682693

683694
outRequest, err := m.makeLoopOutRequest(
684-
ctx, suggestion, quote, autoOut,
695+
ctx, suggestion, quote, autoloop,
685696
)
686697
if err != nil {
687698
return nil, err
@@ -728,29 +739,17 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoOut bool) (
728739
}
729740
}
730741

731-
// If we are getting suggestions for automatically dispatched swaps,
732-
// and they are not enabled in our parameters, we just log the swap
733-
// suggestions and return an empty set of suggestions.
734-
if autoOut && !m.params.AutoOut {
735-
for _, swap := range inBudget {
736-
log.Debugf("recommended autoloop: %v sats over "+
737-
"%v", swap.Amount, swap.OutgoingChanSet)
738-
}
739-
740-
return nil, nil
741-
}
742-
743742
return inBudget, nil
744743
}
745744

746-
// getLoopOutRestrictions queries the server for its latest swap size
747-
// restrictions, validates client restrictions (if present) against these
748-
// values and merges the client's custom requirements with the server's limits
749-
// to produce a single set of limitations for our swap.
750-
func (m *Manager) getLoopOutRestrictions(ctx context.Context) (*Restrictions,
751-
error) {
745+
// getSwapRestrictions queries the server for its latest swap size restrictions,
746+
// validates client restrictions (if present) against these values and merges
747+
// the client's custom requirements with the server's limits to produce a single
748+
// set of limitations for our swap.
749+
func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
750+
*Restrictions, error) {
752751

753-
restrictions, err := m.cfg.LoopOutRestrictions(ctx)
752+
restrictions, err := m.cfg.Restrictions(ctx, swapType)
754753
if err != nil {
755754
return nil, err
756755
}
@@ -791,7 +790,7 @@ func (m *Manager) getLoopOutRestrictions(ctx context.Context) (*Restrictions,
791790
// non-auto requests, because the client api will set it anyway).
792791
func (m *Manager) makeLoopOutRequest(ctx context.Context,
793792
suggestion *LoopOutRecommendation, quote *loop.LoopOutQuote,
794-
autoOut bool) (loop.OutRequest, error) {
793+
autoloop bool) (loop.OutRequest, error) {
795794

796795
prepayMaxFee := ppmToSat(
797796
quote.PrepayAmount, m.params.MaximumPrepayRoutingFeePPM,
@@ -815,8 +814,8 @@ func (m *Manager) makeLoopOutRequest(ctx context.Context,
815814
Initiator: autoloopSwapInitiator,
816815
}
817816

818-
if autoOut {
819-
request.Label = labels.AutoOutLabel()
817+
if autoloop {
818+
request.Label = labels.AutoloopLabel(swap.TypeOut)
820819

821820
addr, err := m.cfg.Lnd.WalletKit.NextAddr(ctx)
822821
if err != nil {
@@ -882,7 +881,7 @@ func (m *Manager) checkExistingAutoLoops(ctx context.Context,
882881
var summary existingAutoLoopSummary
883882

884883
for _, out := range loopOuts {
885-
if out.Contract.Label != labels.AutoOutLabel() {
884+
if out.Contract.Label != labels.AutoloopLabel(swap.TypeOut) {
886885
continue
887886
}
888887

0 commit comments

Comments
 (0)