From 476ae39ce93468a0549a758d948a132138416e48 Mon Sep 17 00:00:00 2001 From: carla Date: Wed, 3 Feb 2021 08:54:50 +0200 Subject: [PATCH] multi: make server side restrictions function generic --- liquidity/autoloop_testcontext_test.go | 5 ++++- liquidity/liquidity.go | 27 +++++++++++++------------- liquidity/liquidity_test.go | 12 ++++++------ loopd/utils.go | 20 +++++++++++++++---- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/liquidity/autoloop_testcontext_test.go b/liquidity/autoloop_testcontext_test.go index 39c601a..ec40053 100644 --- a/liquidity/autoloop_testcontext_test.go +++ b/liquidity/autoloop_testcontext_test.go @@ -8,6 +8,7 @@ import ( "github.com/lightninglabs/lndclient" "github.com/lightninglabs/loop" "github.com/lightninglabs/loop/loopdb" + "github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/test" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/ticker" @@ -92,7 +93,9 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters, cfg := &Config{ AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker), - LoopOutRestrictions: func(context.Context) (*Restrictions, error) { + Restrictions: func(context.Context, swap.Type) (*Restrictions, + error) { + return <-testCtx.loopOutRestrictions, nil }, ListLoopOut: func() ([]*loopdb.LoopOut, error) { diff --git a/liquidity/liquidity.go b/liquidity/liquidity.go index 3c717fe..9fd12da 100644 --- a/liquidity/liquidity.go +++ b/liquidity/liquidity.go @@ -188,9 +188,10 @@ type Config struct { // trigger autoloop in itests. AutoloopTicker *ticker.Force - // LoopOutRestrictions returns the restrictions that the server applies - // to loop out swaps. - LoopOutRestrictions func(ctx context.Context) (*Restrictions, error) + // Restrictions returns the restrictions that the server applies to + // swaps. + Restrictions func(ctx context.Context, swapType swap.Type) ( + *Restrictions, error) // Lnd provides us with access to lnd's rpc servers. Lnd *lndclient.LndServices @@ -467,7 +468,7 @@ func (m *Manager) GetParameters() Parameters { // SetParameters updates our current set of parameters if the new parameters // provided are valid. func (m *Manager) SetParameters(ctx context.Context, params Parameters) error { - restrictions, err := m.cfg.LoopOutRestrictions(ctx) + restrictions, err := m.cfg.Restrictions(ctx, swap.TypeOut) if err != nil { return err } @@ -588,7 +589,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) ( // Get the current server side restrictions, combined with the client // set restrictions, if any. - outRestrictions, err := m.getLoopOutRestrictions(ctx) + restrictions, err := m.getSwapRestrictions(ctx, swap.TypeOut) if err != nil { return nil, err } @@ -647,7 +648,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) ( balance := newBalances(channel) - suggestion := rule.suggestSwap(balance, outRestrictions) + suggestion := rule.suggestSwap(balance, restrictions) // We can have nil suggestions in the case where no action is // required, so we skip over them. @@ -744,14 +745,14 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) ( return inBudget, nil } -// getLoopOutRestrictions queries the server for its latest swap size -// restrictions, validates client restrictions (if present) against these -// values and merges the client's custom requirements with the server's limits -// to produce a single set of limitations for our swap. -func (m *Manager) getLoopOutRestrictions(ctx context.Context) (*Restrictions, - error) { +// getSwapRestrictions queries the server for its latest swap size restrictions, +// validates client restrictions (if present) against these values and merges +// the client's custom requirements with the server's limits to produce a single +// set of limitations for our swap. +func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) ( + *Restrictions, error) { - restrictions, err := m.cfg.LoopOutRestrictions(ctx) + restrictions, err := m.cfg.Restrictions(ctx, swapType) if err != nil { return nil, err } diff --git a/liquidity/liquidity_test.go b/liquidity/liquidity_test.go index 93b1cc6..0872394 100644 --- a/liquidity/liquidity_test.go +++ b/liquidity/liquidity_test.go @@ -121,7 +121,7 @@ func newTestConfig() (*Config, *test.LndMockServices) { ) return &Config{ - LoopOutRestrictions: func(_ context.Context) (*Restrictions, + Restrictions: func(_ context.Context, _ swap.Type) (*Restrictions, error) { return testRestrictions, nil @@ -868,7 +868,7 @@ func TestSizeRestrictions(t *testing.T) { Maximum: 10000, } - swap = loop.OutRequest{ + outSwap = loop.OutRequest{ OutgoingChanSet: loopdb.ChannelSet{chanID1.ToUint64()}, MaxPrepayRoutingFee: prepayFee, MaxMinerFee: defaultMaximumMinerFee, @@ -967,7 +967,7 @@ func TestSizeRestrictions(t *testing.T) { // our restrictions endpoint. var callCount int - cfg.LoopOutRestrictions = func(_ context.Context) ( + cfg.Restrictions = func(_ context.Context, _ swap.Type) ( *Restrictions, error) { restrictions := testCase.serverRestrictions[callCount] @@ -981,14 +981,14 @@ func TestSizeRestrictions(t *testing.T) { // and fee accordingly. var expectedSwaps []loop.OutRequest if testCase.expectedAmount != 0 { - swap.Amount = testCase.expectedAmount + outSwap.Amount = testCase.expectedAmount - swap.MaxSwapRoutingFee = ppmToSat( + outSwap.MaxSwapRoutingFee = ppmToSat( testCase.expectedAmount, defaultRoutingFeePPM, ) - expectedSwaps = append(expectedSwaps, swap) + expectedSwaps = append(expectedSwaps, outSwap) } testSuggestSwaps( diff --git a/loopd/utils.go b/loopd/utils.go index 5338846..59e8cca 100644 --- a/loopd/utils.go +++ b/loopd/utils.go @@ -7,6 +7,7 @@ import ( "github.com/lightninglabs/lndclient" "github.com/lightninglabs/loop" "github.com/lightninglabs/loop/liquidity" + "github.com/lightninglabs/loop/swap" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/ticker" ) @@ -38,16 +39,27 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager { mngrCfg := &liquidity.Config{ AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker), LoopOut: client.LoopOut, - LoopOutRestrictions: func(ctx context.Context) ( - *liquidity.Restrictions, error) { + Restrictions: func(ctx context.Context, + swapType swap.Type) (*liquidity.Restrictions, error) { - outTerms, err := client.Server.GetLoopOutTerms(ctx) + if swapType == swap.TypeOut { + outTerms, err := client.Server.GetLoopOutTerms(ctx) + if err != nil { + return nil, err + } + + return liquidity.NewRestrictions( + outTerms.MinSwapAmount, outTerms.MaxSwapAmount, + ), nil + } + + inTerms, err := client.Server.GetLoopInTerms(ctx) if err != nil { return nil, err } return liquidity.NewRestrictions( - outTerms.MinSwapAmount, outTerms.MaxSwapAmount, + inTerms.MinSwapAmount, inTerms.MaxSwapAmount, ), nil }, Lnd: client.LndServices,