Browse Source

multi: make server side restrictions function generic

pull/331/head
carla 7 months ago
parent
commit
476ae39ce9
No known key found for this signature in database GPG Key ID: 4CA7FE54A6213C91
4 changed files with 40 additions and 24 deletions
  1. +4
    -1
      liquidity/autoloop_testcontext_test.go
  2. +14
    -13
      liquidity/liquidity.go
  3. +6
    -6
      liquidity/liquidity_test.go
  4. +16
    -4
      loopd/utils.go

+ 4
- 1
liquidity/autoloop_testcontext_test.go View File

@ -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) {

+ 14
- 13
liquidity/liquidity.go View File

@ -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
}

+ 6
- 6
liquidity/liquidity_test.go View File

@ -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(

+ 16
- 4
loopd/utils.go View File

@ -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,

Loading…
Cancel
Save