Skip to content

Commit 476ae39

Browse files
committed
multi: make server side restrictions function generic
1 parent 6972475 commit 476ae39

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

liquidity/autoloop_testcontext_test.go

Lines changed: 4 additions & 1 deletion
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"
@@ -92,7 +93,9 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
9293

9394
cfg := &Config{
9495
AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker),
95-
LoopOutRestrictions: func(context.Context) (*Restrictions, error) {
96+
Restrictions: func(context.Context, swap.Type) (*Restrictions,
97+
error) {
98+
9699
return <-testCtx.loopOutRestrictions, nil
97100
},
98101
ListLoopOut: func() ([]*loopdb.LoopOut, error) {

liquidity/liquidity.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ type Config struct {
188188
// trigger autoloop in itests.
189189
AutoloopTicker *ticker.Force
190190

191-
// LoopOutRestrictions returns the restrictions that the server applies
192-
// to loop out swaps.
193-
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)
194195

195196
// Lnd provides us with access to lnd's rpc servers.
196197
Lnd *lndclient.LndServices
@@ -467,7 +468,7 @@ func (m *Manager) GetParameters() Parameters {
467468
// SetParameters updates our current set of parameters if the new parameters
468469
// provided are valid.
469470
func (m *Manager) SetParameters(ctx context.Context, params Parameters) error {
470-
restrictions, err := m.cfg.LoopOutRestrictions(ctx)
471+
restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut)
471472
if err != nil {
472473
return err
473474
}
@@ -588,7 +589,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
588589

589590
// Get the current server side restrictions, combined with the client
590591
// set restrictions, if any.
591-
outRestrictions, err := m.getLoopOutRestrictions(ctx)
592+
restrictions, err := m.getSwapRestrictions(ctx, swap.TypeOut)
592593
if err != nil {
593594
return nil, err
594595
}
@@ -647,7 +648,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
647648

648649
balance := newBalances(channel)
649650

650-
suggestion := rule.suggestSwap(balance, outRestrictions)
651+
suggestion := rule.suggestSwap(balance, restrictions)
651652

652653
// We can have nil suggestions in the case where no action is
653654
// required, so we skip over them.
@@ -744,14 +745,14 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
744745
return inBudget, nil
745746
}
746747

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

754-
restrictions, err := m.cfg.LoopOutRestrictions(ctx)
755+
restrictions, err := m.cfg.Restrictions(ctx, swapType)
755756
if err != nil {
756757
return nil, err
757758
}

liquidity/liquidity_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func newTestConfig() (*Config, *test.LndMockServices) {
121121
)
122122

123123
return &Config{
124-
LoopOutRestrictions: func(_ context.Context) (*Restrictions,
124+
Restrictions: func(_ context.Context, _ swap.Type) (*Restrictions,
125125
error) {
126126

127127
return testRestrictions, nil
@@ -868,7 +868,7 @@ func TestSizeRestrictions(t *testing.T) {
868868
Maximum: 10000,
869869
}
870870

871-
swap = loop.OutRequest{
871+
outSwap = loop.OutRequest{
872872
OutgoingChanSet: loopdb.ChannelSet{chanID1.ToUint64()},
873873
MaxPrepayRoutingFee: prepayFee,
874874
MaxMinerFee: defaultMaximumMinerFee,
@@ -967,7 +967,7 @@ func TestSizeRestrictions(t *testing.T) {
967967
// our restrictions endpoint.
968968
var callCount int
969969

970-
cfg.LoopOutRestrictions = func(_ context.Context) (
970+
cfg.Restrictions = func(_ context.Context, _ swap.Type) (
971971
*Restrictions, error) {
972972

973973
restrictions := testCase.serverRestrictions[callCount]
@@ -981,14 +981,14 @@ func TestSizeRestrictions(t *testing.T) {
981981
// and fee accordingly.
982982
var expectedSwaps []loop.OutRequest
983983
if testCase.expectedAmount != 0 {
984-
swap.Amount = testCase.expectedAmount
984+
outSwap.Amount = testCase.expectedAmount
985985

986-
swap.MaxSwapRoutingFee = ppmToSat(
986+
outSwap.MaxSwapRoutingFee = ppmToSat(
987987
testCase.expectedAmount,
988988
defaultRoutingFeePPM,
989989
)
990990

991-
expectedSwaps = append(expectedSwaps, swap)
991+
expectedSwaps = append(expectedSwaps, outSwap)
992992
}
993993

994994
testSuggestSwaps(

loopd/utils.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/lightninglabs/lndclient"
88
"github.com/lightninglabs/loop"
99
"github.com/lightninglabs/loop/liquidity"
10+
"github.com/lightninglabs/loop/swap"
1011
"github.com/lightningnetwork/lnd/clock"
1112
"github.com/lightningnetwork/lnd/ticker"
1213
)
@@ -38,16 +39,27 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager {
3839
mngrCfg := &liquidity.Config{
3940
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
4041
LoopOut: client.LoopOut,
41-
LoopOutRestrictions: func(ctx context.Context) (
42-
*liquidity.Restrictions, error) {
42+
Restrictions: func(ctx context.Context,
43+
swapType swap.Type) (*liquidity.Restrictions, error) {
4344

44-
outTerms, err := client.Server.GetLoopOutTerms(ctx)
45+
if swapType == swap.TypeOut {
46+
outTerms, err := client.Server.GetLoopOutTerms(ctx)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
return liquidity.NewRestrictions(
52+
outTerms.MinSwapAmount, outTerms.MaxSwapAmount,
53+
), nil
54+
}
55+
56+
inTerms, err := client.Server.GetLoopInTerms(ctx)
4557
if err != nil {
4658
return nil, err
4759
}
4860

4961
return liquidity.NewRestrictions(
50-
outTerms.MinSwapAmount, outTerms.MaxSwapAmount,
62+
inTerms.MinSwapAmount, inTerms.MaxSwapAmount,
5163
), nil
5264
},
5365
Lnd: client.LndServices,

0 commit comments

Comments
 (0)