Skip to content

Commit e34653e

Browse files
authored
Merge pull request #556 from GeorgeTsagk/autoloop-expose-last-refresh
2 parents 55845ff + aca6428 commit e34653e

10 files changed

+378
-305
lines changed

liquidity/autoloop_test.go

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestAutoLoopDisabled(t *testing.T) {
2828
}
2929

3030
params := defaultParameters
31+
params.AutoloopBudgetLastRefresh = testBudgetStart
3132
params.ChannelRules = map[lnwire.ShortChannelID]*SwapRule{
3233
chanID1: chanRule,
3334
}
@@ -95,12 +96,13 @@ func TestAutoLoopEnabled(t *testing.T) {
9596
// autoloop budget is set to allow exactly 2 swaps at the prices
9697
// that we set in our test quotes.
9798
params = Parameters{
98-
Autoloop: true,
99-
AutoFeeBudget: 40066,
100-
AutoFeeRefreshPeriod: testBudgetRefresh,
101-
MaxAutoInFlight: 2,
102-
FailureBackOff: time.Hour,
103-
SweepConfTarget: 10,
99+
Autoloop: true,
100+
AutoFeeBudget: 40066,
101+
AutoFeeRefreshPeriod: testBudgetRefresh,
102+
AutoloopBudgetLastRefresh: testBudgetStart,
103+
MaxAutoInFlight: 2,
104+
FailureBackOff: time.Hour,
105+
SweepConfTarget: 10,
104106
FeeLimit: NewFeeCategoryLimit(
105107
swapFeePPM, routeFeePPM, prepayFeePPM, maxMiner,
106108
prepayAmount, 20000,
@@ -353,13 +355,14 @@ func TestAutoloopAddress(t *testing.T) {
353355
// Create some dummy parameters for autoloop and also specify an
354356
// destination address.
355357
params = Parameters{
356-
Autoloop: true,
357-
AutoFeeBudget: 40066,
358-
DestAddr: addr,
359-
AutoFeeRefreshPeriod: testBudgetRefresh,
360-
MaxAutoInFlight: 2,
361-
FailureBackOff: time.Hour,
362-
SweepConfTarget: 10,
358+
Autoloop: true,
359+
AutoFeeBudget: 40066,
360+
DestAddr: addr,
361+
AutoFeeRefreshPeriod: testBudgetRefresh,
362+
AutoloopBudgetLastRefresh: testBudgetStart,
363+
MaxAutoInFlight: 2,
364+
FailureBackOff: time.Hour,
365+
SweepConfTarget: 10,
363366
FeeLimit: NewFeeCategoryLimit(
364367
swapFeePPM, routeFeePPM, prepayFeePPM, maxMiner,
365368
prepayAmount, 20000,
@@ -523,12 +526,13 @@ func TestCompositeRules(t *testing.T) {
523526
swapFeePPM, routeFeePPM, prepayFeePPM, maxMiner,
524527
prepayAmount, 20000,
525528
),
526-
Autoloop: true,
527-
AutoFeeBudget: 100000,
528-
AutoFeeRefreshPeriod: testBudgetRefresh,
529-
MaxAutoInFlight: 2,
530-
FailureBackOff: time.Hour,
531-
SweepConfTarget: 10,
529+
Autoloop: true,
530+
AutoFeeBudget: 100000,
531+
AutoFeeRefreshPeriod: testBudgetRefresh,
532+
AutoloopBudgetLastRefresh: testBudgetStart,
533+
MaxAutoInFlight: 2,
534+
FailureBackOff: time.Hour,
535+
SweepConfTarget: 10,
532536
ChannelRules: map[lnwire.ShortChannelID]*SwapRule{
533537
chanID1: chanRule,
534538
},
@@ -715,13 +719,14 @@ func TestAutoLoopInEnabled(t *testing.T) {
715719
peer2MaxFee = ppmToSat(peer2ExpectedAmt, swapFeePPM)
716720

717721
params = Parameters{
718-
Autoloop: true,
719-
AutoFeeBudget: peer1MaxFee + peer2MaxFee + 1,
720-
AutoFeeRefreshPeriod: testBudgetRefresh,
721-
MaxAutoInFlight: 2,
722-
FailureBackOff: time.Hour,
723-
FeeLimit: NewFeePortion(swapFeePPM),
724-
ChannelRules: make(map[lnwire.ShortChannelID]*SwapRule),
722+
Autoloop: true,
723+
AutoFeeBudget: peer1MaxFee + peer2MaxFee + 1,
724+
AutoFeeRefreshPeriod: testBudgetRefresh,
725+
AutoloopBudgetLastRefresh: testBudgetStart,
726+
MaxAutoInFlight: 2,
727+
FailureBackOff: time.Hour,
728+
FeeLimit: NewFeePortion(swapFeePPM),
729+
ChannelRules: make(map[lnwire.ShortChannelID]*SwapRule),
725730
PeerRules: map[route.Vertex]*SwapRule{
726731
peer1: rule,
727732
peer2: rule,
@@ -898,12 +903,13 @@ func TestAutoloopBothTypes(t *testing.T) {
898903
loopInMaxFee = ppmToSat(loopInAmount, swapFeePPM)
899904

900905
params = Parameters{
901-
Autoloop: true,
902-
AutoFeeBudget: loopOutMaxFee + loopInMaxFee + 1,
903-
AutoFeeRefreshPeriod: testBudgetRefresh,
904-
MaxAutoInFlight: 2,
905-
FailureBackOff: time.Hour,
906-
FeeLimit: NewFeePortion(swapFeePPM),
906+
Autoloop: true,
907+
AutoFeeBudget: loopOutMaxFee + loopInMaxFee + 1,
908+
AutoFeeRefreshPeriod: testBudgetRefresh,
909+
AutoloopBudgetLastRefresh: testBudgetStart,
910+
MaxAutoInFlight: 2,
911+
FailureBackOff: time.Hour,
912+
FeeLimit: NewFeePortion(swapFeePPM),
907913
ChannelRules: map[lnwire.ShortChannelID]*SwapRule{
908914
chanID1: outRule,
909915
},
@@ -1041,12 +1047,13 @@ func TestAutoLoopRecurringBudget(t *testing.T) {
10411047
maxMiner = btcutil.Amount(20000)
10421048

10431049
params = Parameters{
1044-
Autoloop: true,
1045-
AutoFeeBudget: 36000,
1046-
AutoFeeRefreshPeriod: time.Hour * 3,
1047-
MaxAutoInFlight: 2,
1048-
FailureBackOff: time.Hour,
1049-
SweepConfTarget: 10,
1050+
Autoloop: true,
1051+
AutoFeeBudget: 36000,
1052+
AutoFeeRefreshPeriod: time.Hour * 3,
1053+
AutoloopBudgetLastRefresh: testBudgetStart,
1054+
MaxAutoInFlight: 2,
1055+
FailureBackOff: time.Hour,
1056+
SweepConfTarget: 10,
10501057
FeeLimit: NewFeeCategoryLimit(
10511058
swapFeePPM, routeFeePPM, prepayFeePPM, maxMiner,
10521059
prepayAmount, 20000,

liquidity/autoloop_testcontext_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
129129
testCtx.lnd.Channels = channels
130130

131131
cfg := &Config{
132-
AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker),
133-
AutoloopBudgetLastRefresh: testBudgetStart,
132+
AutoloopTicker: ticker.NewForce(DefaultAutoloopTicker),
134133
Restrictions: func(_ context.Context, swapType swap.Type) (*Restrictions,
135134
error) {
136135

liquidity/liquidity.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ type Config struct {
166166
// trigger autoloop in itests.
167167
AutoloopTicker *ticker.Force
168168

169-
// AutoloopBudgetLastRefresh is the last time at which we refreshed
170-
// our budget.
171-
AutoloopBudgetLastRefresh time.Time
172-
173169
// Restrictions returns the restrictions that the server applies to
174170
// swaps.
175171
Restrictions func(ctx context.Context, swapType swap.Type) (
@@ -301,7 +297,7 @@ func (m *Manager) GetParameters() Parameters {
301297
func (m *Manager) SetParameters(ctx context.Context,
302298
req *clientrpc.LiquidityParameters) error {
303299

304-
params, err := rpcToParameters(req)
300+
params, err := RpcToParameters(req)
305301
if err != nil {
306302
return err
307303
}
@@ -743,7 +739,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
743739
}
744740
} else {
745741
refreshTime := m.params.AutoFeeRefreshPeriod -
746-
time.Since(m.cfg.AutoloopBudgetLastRefresh)
742+
time.Since(m.params.AutoloopBudgetLastRefresh)
747743

748744
log.Infof("Swap fee exceeds budget, remaining budget: "+
749745
"%v, swap fee %v, next budget refresh: %v",
@@ -929,7 +925,7 @@ func (m *Manager) checkExistingAutoLoops(ctx context.Context,
929925
mSatToSatoshis(prepay.Value),
930926
)
931927
} else if out.LastUpdateTime().After(
932-
m.cfg.AutoloopBudgetLastRefresh,
928+
m.params.AutoloopBudgetLastRefresh,
933929
) {
934930

935931
summary.spentFees += out.State().Cost.Total()
@@ -943,7 +939,7 @@ func (m *Manager) checkExistingAutoLoops(ctx context.Context,
943939

944940
pending := in.State().State.Type() == loopdb.StateTypePending
945941
inBudget := !in.LastUpdateTime().
946-
Before(m.cfg.AutoloopBudgetLastRefresh)
942+
Before(m.params.AutoloopBudgetLastRefresh)
947943

948944
// If an autoloop is in a pending state, we always count it in
949945
// our current budget, and record the worst-case fees for it,
@@ -1054,11 +1050,23 @@ func (m *Manager) currentSwapTraffic(loopOut []*loopdb.LoopOut,
10541050
// budget refresh is greater than our configured refresh period. If so, the last
10551051
// refresh timestamp.
10561052
func (m *Manager) refreshAutoloopBudget(ctx context.Context) {
1057-
if time.Since(m.cfg.AutoloopBudgetLastRefresh) >
1053+
if time.Since(m.params.AutoloopBudgetLastRefresh) >
10581054
m.params.AutoFeeRefreshPeriod {
10591055

10601056
log.Debug("Refreshing autoloop budget")
1061-
m.cfg.AutoloopBudgetLastRefresh = m.cfg.Clock.Now()
1057+
m.params.AutoloopBudgetLastRefresh = m.cfg.Clock.Now()
1058+
1059+
paramsRpc, err := ParametersToRpc(m.params)
1060+
if err != nil {
1061+
log.Errorf("Error converting parameters to rpc: %v",
1062+
err)
1063+
return
1064+
}
1065+
1066+
err = m.saveParams(paramsRpc)
1067+
if err != nil {
1068+
log.Errorf("Error saving parameters: %v", err)
1069+
}
10621070
}
10631071
}
10641072

liquidity/liquidity_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ func newTestConfig() (*Config, *test.LndMockServices) {
148148

149149
return testRestrictions, nil
150150
},
151-
Lnd: &lnd.LndServices,
152-
Clock: clock.NewTestClock(testTime),
153-
AutoloopBudgetLastRefresh: testBudgetStart,
151+
Lnd: &lnd.LndServices,
152+
Clock: clock.NewTestClock(testTime),
154153
ListLoopOut: func() ([]*loopdb.LoopOut, error) {
155154
return nil, nil
156155
},
@@ -572,6 +571,7 @@ func TestRestrictedSuggestions(t *testing.T) {
572571
lnd.Channels = testCase.channels
573572

574573
params := defaultParameters
574+
params.AutoloopBudgetLastRefresh = testBudgetStart
575575
if testCase.chanRules != nil {
576576
params.ChannelRules = testCase.chanRules
577577
}
@@ -652,6 +652,7 @@ func TestSweepFeeLimit(t *testing.T) {
652652
}
653653

654654
params := defaultParameters
655+
params.AutoloopBudgetLastRefresh = testBudgetStart
655656
params.FeeLimit = defaultFeeCategoryLimit()
656657

657658
// Set our budget to cover a single swap with these
@@ -794,6 +795,7 @@ func TestSuggestSwaps(t *testing.T) {
794795
lnd.Channels = testCase.channels
795796

796797
params := defaultParameters
798+
params.AutoloopBudgetLastRefresh = testBudgetStart
797799
if testCase.rules != nil {
798800
params.ChannelRules = testCase.rules
799801
}
@@ -901,6 +903,7 @@ func TestFeeLimits(t *testing.T) {
901903

902904
// Set our params to use individual fee limits.
903905
params := defaultParameters
906+
params.AutoloopBudgetLastRefresh = testBudgetStart
904907
params.FeeLimit = defaultFeeCategoryLimit()
905908

906909
// Set our budget to cover a single swap with these
@@ -1108,6 +1111,7 @@ func TestFeeBudget(t *testing.T) {
11081111
}
11091112
params.AutoFeeBudget = testCase.budget
11101113
params.AutoFeeRefreshPeriod = testBudgetRefresh
1114+
params.AutoloopBudgetLastRefresh = testBudgetStart
11111115
params.MaxAutoInFlight = 2
11121116
params.FeeLimit = NewFeeCategoryLimit(
11131117
defaultSwapFeePPM, defaultRoutingFeePPM,
@@ -1272,6 +1276,7 @@ func TestInFlightLimit(t *testing.T) {
12721276
}
12731277

12741278
params := defaultParameters
1279+
params.AutoloopBudgetLastRefresh = testBudgetStart
12751280

12761281
if testCase.peerRules != nil {
12771282
params.PeerRules = testCase.peerRules
@@ -1431,6 +1436,7 @@ func TestSizeRestrictions(t *testing.T) {
14311436
}
14321437

14331438
params := defaultParameters
1439+
params.AutoloopBudgetLastRefresh = testBudgetStart
14341440
params.ClientRestrictions = testCase.clientRestrictions
14351441
params.ChannelRules = map[lnwire.ShortChannelID]*SwapRule{
14361442
chanID1: chanRule,
@@ -1593,6 +1599,7 @@ func TestFeePercentage(t *testing.T) {
15931599
}
15941600

15951601
params := defaultParameters
1602+
params.AutoloopBudgetLastRefresh = testBudgetStart
15961603
params.FeeLimit = NewFeePortion(testCase.feePPM)
15971604
params.ChannelRules = map[lnwire.ShortChannelID]*SwapRule{
15981605
chanID1: chanRule,
@@ -1765,6 +1772,7 @@ func TestBudgetWithLoopin(t *testing.T) {
17651772
params := defaultParameters
17661773
params.AutoFeeBudget = budget
17671774
params.AutoFeeRefreshPeriod = testBudgetRefresh
1775+
params.AutoloopBudgetLastRefresh = testBudgetStart
17681776

17691777
params.FeeLimit = NewFeePortion(testPPM)
17701778
params.ChannelRules = map[lnwire.ShortChannelID]*SwapRule{
@@ -1821,6 +1829,7 @@ func testSuggestSwaps(t *testing.T, setup *testSuggestSwapsSetup,
18211829
}
18221830

18231831
params := defaultParameters
1832+
params.AutoloopBudgetLastRefresh = testBudgetStart
18241833
params.ChannelRules = map[lnwire.ShortChannelID]*SwapRule{
18251834
chanID1: chanRule,
18261835
chanID2: chanRule,

0 commit comments

Comments
 (0)