Skip to content

Commit ab9a662

Browse files
committed
loop/test: simplify preimage push test to be less dependent on height
Our preimage push test previously relied on our dropping down to the default sweep conf target to mock a drop in chain fees. This makes our test dependent on height, which makes changes to our sweep logic regarding when we reveal our preimage break this test. In this commit that logic is replaced with simply locking our mock and updating fees on the fly.
1 parent db56b31 commit ab9a662

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

loopout_test.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -414,23 +414,16 @@ func TestCustomSweepConfTarget(t *testing.T) {
414414
// not detected our settle) and settle the off chain htlc, indicating that the
415415
// server successfully settled using the preimage push. In this test, we need
416416
// to start with a fee rate that will be too high, then progress to an
417-
// acceptable one. We do this by starting with a high confirmation target with
418-
// a high fee, and setting the default confirmation fee (which our swap will
419-
// drop down to if it is not confirming in time) to a lower fee. This is not
420-
// intuitive (lower confs having lower fees), but it allows up to mock fee
421-
// changes.
417+
// acceptable one.
422418
func TestPreimagePush(t *testing.T) {
423419
defer test.Guard(t)()
424420

425421
lnd := test.NewMockLnd()
426422
ctx := test.NewContext(t, lnd)
427423
server := newServerMock(lnd)
428424

429-
// Start with a high confirmation delta which will have a very high fee
430-
// attached to it.
431425
testReq := *testRequest
432-
testReq.SweepConfTarget = testLoopOutMinOnChainCltvDelta -
433-
DefaultSweepConfTargetDelta - 1
426+
testReq.SweepConfTarget = 10
434427
testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta
435428

436429
// We set our mock fee estimate for our target sweep confs to be our
@@ -442,11 +435,6 @@ func TestPreimagePush(t *testing.T) {
442435
),
443436
)
444437

445-
// We set the fee estimate for our default confirmation target very
446-
// low, so that once we drop down to our default confs we will start
447-
// trying to sweep the preimage.
448-
ctx.Lnd.SetFeeEstimate(DefaultSweepConfTarget, 1)
449-
450438
cfg := newSwapConfig(
451439
&lnd.LndServices, newStoreMock(t), server,
452440
)
@@ -520,15 +508,15 @@ func TestPreimagePush(t *testing.T) {
520508
// preimage is not revealed, we also do not expect a preimage push.
521509
expiryChan <- testTime
522510

523-
// Now, we notify the height at which the client will start using the
524-
// default confirmation target. This has the effect of lowering our fees
525-
// so that the client still start sweeping.
526-
defaultConfTargetHeight := ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta -
527-
DefaultSweepConfTargetDelta
528-
blockEpochChan <- defaultConfTargetHeight
511+
// Now we decrease our fees for the swap's confirmation target to less
512+
// than the maximum miner fee.
513+
ctx.Lnd.SetFeeEstimate(testReq.SweepConfTarget, chainfee.SatPerKWeight(
514+
testReq.MaxMinerFee/2,
515+
))
529516

530-
// This time when we tick the expiry chan, our fees are lower than the
531-
// swap max, so we expect it to prompt a sweep.
517+
// Now when we report a new block and tick our expiry fee timer, and
518+
// fees are acceptably low so we expect our sweep to be published.
519+
blockEpochChan <- ctx.Lnd.Height + 2
532520
expiryChan <- testTime
533521

534522
// Expect a signing request for the HTLC success transaction.

test/lnd_services_mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,5 @@ func (s *LndMockServices) DecodeInvoice(request string) (*zpay32.Invoice,
258258
func (s *LndMockServices) SetFeeEstimate(confTarget int32,
259259
feeEstimate chainfee.SatPerKWeight) {
260260

261-
s.WalletKit.(*mockWalletKit).feeEstimates[confTarget] = feeEstimate
261+
s.WalletKit.(*mockWalletKit).setFeeEstimate(confTarget, feeEstimate)
262262
}

test/walletkit_mock.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test
33
import (
44
"context"
55
"errors"
6+
"sync"
67
"time"
78

89
"github.com/btcsuite/btcd/chaincfg"
@@ -21,9 +22,11 @@ import (
2122
var DefaultMockFee = chainfee.SatPerKWeight(10000)
2223

2324
type mockWalletKit struct {
24-
lnd *LndMockServices
25-
keyIndex int32
26-
feeEstimates map[int32]chainfee.SatPerKWeight
25+
lnd *LndMockServices
26+
keyIndex int32
27+
28+
feeEstimateLock sync.Mutex
29+
feeEstimates map[int32]chainfee.SatPerKWeight
2730
}
2831

2932
var _ lndclient.WalletKitClient = (*mockWalletKit)(nil)
@@ -118,9 +121,19 @@ func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut,
118121
return &tx, nil
119122
}
120123

124+
func (m *mockWalletKit) setFeeEstimate(confTarget int32, fee chainfee.SatPerKWeight) {
125+
m.feeEstimateLock.Lock()
126+
defer m.feeEstimateLock.Unlock()
127+
128+
m.feeEstimates[confTarget] = fee
129+
}
130+
121131
func (m *mockWalletKit) EstimateFee(ctx context.Context, confTarget int32) (
122132
chainfee.SatPerKWeight, error) {
123133

134+
m.feeEstimateLock.Lock()
135+
defer m.feeEstimateLock.Unlock()
136+
124137
if confTarget <= 1 {
125138
return 0, errors.New("conf target must be greater than 1")
126139
}

0 commit comments

Comments
 (0)