multi: make server side restrictions function generic

pull/331/head
carla 3 years ago
parent 69724757ce
commit 476ae39ce9
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -8,6 +8,7 @@ import (
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/test" "github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/ticker" "github.com/lightningnetwork/lnd/ticker"
@ -92,7 +93,9 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
cfg := &Config{ cfg := &Config{
AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker), AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker),
LoopOutRestrictions: func(context.Context) (*Restrictions, error) { Restrictions: func(context.Context, swap.Type) (*Restrictions,
error) {
return <-testCtx.loopOutRestrictions, nil return <-testCtx.loopOutRestrictions, nil
}, },
ListLoopOut: func() ([]*loopdb.LoopOut, error) { ListLoopOut: func() ([]*loopdb.LoopOut, error) {

@ -188,9 +188,10 @@ type Config struct {
// trigger autoloop in itests. // trigger autoloop in itests.
AutoloopTicker *ticker.Force AutoloopTicker *ticker.Force
// LoopOutRestrictions returns the restrictions that the server applies // Restrictions returns the restrictions that the server applies to
// to loop out swaps. // swaps.
LoopOutRestrictions func(ctx context.Context) (*Restrictions, error) Restrictions func(ctx context.Context, swapType swap.Type) (
*Restrictions, error)
// Lnd provides us with access to lnd's rpc servers. // Lnd provides us with access to lnd's rpc servers.
Lnd *lndclient.LndServices Lnd *lndclient.LndServices
@ -467,7 +468,7 @@ func (m *Manager) GetParameters() Parameters {
// SetParameters updates our current set of parameters if the new parameters // SetParameters updates our current set of parameters if the new parameters
// provided are valid. // provided are valid.
func (m *Manager) SetParameters(ctx context.Context, params Parameters) error { 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 { if err != nil {
return err 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 // Get the current server side restrictions, combined with the client
// set restrictions, if any. // set restrictions, if any.
outRestrictions, err := m.getLoopOutRestrictions(ctx) restrictions, err := m.getSwapRestrictions(ctx, swap.TypeOut)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -647,7 +648,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
balance := newBalances(channel) 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 // We can have nil suggestions in the case where no action is
// required, so we skip over them. // required, so we skip over them.
@ -744,14 +745,14 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
return inBudget, nil return inBudget, nil
} }
// getLoopOutRestrictions queries the server for its latest swap size // getSwapRestrictions queries the server for its latest swap size restrictions,
// restrictions, validates client restrictions (if present) against these // validates client restrictions (if present) against these values and merges
// values and merges the client's custom requirements with the server's limits // the client's custom requirements with the server's limits to produce a single
// to produce a single set of limitations for our swap. // set of limitations for our swap.
func (m *Manager) getLoopOutRestrictions(ctx context.Context) (*Restrictions, func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
error) { *Restrictions, error) {
restrictions, err := m.cfg.LoopOutRestrictions(ctx) restrictions, err := m.cfg.Restrictions(ctx, swapType)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -121,7 +121,7 @@ func newTestConfig() (*Config, *test.LndMockServices) {
) )
return &Config{ return &Config{
LoopOutRestrictions: func(_ context.Context) (*Restrictions, Restrictions: func(_ context.Context, _ swap.Type) (*Restrictions,
error) { error) {
return testRestrictions, nil return testRestrictions, nil
@ -868,7 +868,7 @@ func TestSizeRestrictions(t *testing.T) {
Maximum: 10000, Maximum: 10000,
} }
swap = loop.OutRequest{ outSwap = loop.OutRequest{
OutgoingChanSet: loopdb.ChannelSet{chanID1.ToUint64()}, OutgoingChanSet: loopdb.ChannelSet{chanID1.ToUint64()},
MaxPrepayRoutingFee: prepayFee, MaxPrepayRoutingFee: prepayFee,
MaxMinerFee: defaultMaximumMinerFee, MaxMinerFee: defaultMaximumMinerFee,
@ -967,7 +967,7 @@ func TestSizeRestrictions(t *testing.T) {
// our restrictions endpoint. // our restrictions endpoint.
var callCount int var callCount int
cfg.LoopOutRestrictions = func(_ context.Context) ( cfg.Restrictions = func(_ context.Context, _ swap.Type) (
*Restrictions, error) { *Restrictions, error) {
restrictions := testCase.serverRestrictions[callCount] restrictions := testCase.serverRestrictions[callCount]
@ -981,14 +981,14 @@ func TestSizeRestrictions(t *testing.T) {
// and fee accordingly. // and fee accordingly.
var expectedSwaps []loop.OutRequest var expectedSwaps []loop.OutRequest
if testCase.expectedAmount != 0 { if testCase.expectedAmount != 0 {
swap.Amount = testCase.expectedAmount outSwap.Amount = testCase.expectedAmount
swap.MaxSwapRoutingFee = ppmToSat( outSwap.MaxSwapRoutingFee = ppmToSat(
testCase.expectedAmount, testCase.expectedAmount,
defaultRoutingFeePPM, defaultRoutingFeePPM,
) )
expectedSwaps = append(expectedSwaps, swap) expectedSwaps = append(expectedSwaps, outSwap)
} }
testSuggestSwaps( testSuggestSwaps(

@ -7,6 +7,7 @@ import (
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/ticker" "github.com/lightningnetwork/lnd/ticker"
) )
@ -38,16 +39,27 @@ func getLiquidityManager(client *loop.Client) *liquidity.Manager {
mngrCfg := &liquidity.Config{ mngrCfg := &liquidity.Config{
AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker), AutoloopTicker: ticker.NewForce(liquidity.DefaultAutoloopTicker),
LoopOut: client.LoopOut, LoopOut: client.LoopOut,
LoopOutRestrictions: func(ctx context.Context) ( Restrictions: func(ctx context.Context,
*liquidity.Restrictions, error) { 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 { if err != nil {
return nil, err return nil, err
} }
return liquidity.NewRestrictions( return liquidity.NewRestrictions(
outTerms.MinSwapAmount, outTerms.MaxSwapAmount, inTerms.MinSwapAmount, inTerms.MaxSwapAmount,
), nil ), nil
}, },
Lnd: client.LndServices, Lnd: client.LndServices,

Loading…
Cancel
Save