2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-11 13:11:12 +00:00

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.
This commit is contained in:
carla 2021-04-22 09:28:32 +02:00
parent db56b31b11
commit ab9a662758
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
3 changed files with 27 additions and 26 deletions

View File

@ -414,11 +414,7 @@ func TestCustomSweepConfTarget(t *testing.T) {
// not detected our settle) and settle the off chain htlc, indicating that the // not detected our settle) and settle the off chain htlc, indicating that the
// server successfully settled using the preimage push. In this test, we need // server successfully settled using the preimage push. In this test, we need
// to start with a fee rate that will be too high, then progress to an // to start with a fee rate that will be too high, then progress to an
// acceptable one. We do this by starting with a high confirmation target with // acceptable one.
// a high fee, and setting the default confirmation fee (which our swap will
// drop down to if it is not confirming in time) to a lower fee. This is not
// intuitive (lower confs having lower fees), but it allows up to mock fee
// changes.
func TestPreimagePush(t *testing.T) { func TestPreimagePush(t *testing.T) {
defer test.Guard(t)() defer test.Guard(t)()
@ -426,11 +422,8 @@ func TestPreimagePush(t *testing.T) {
ctx := test.NewContext(t, lnd) ctx := test.NewContext(t, lnd)
server := newServerMock(lnd) server := newServerMock(lnd)
// Start with a high confirmation delta which will have a very high fee
// attached to it.
testReq := *testRequest testReq := *testRequest
testReq.SweepConfTarget = testLoopOutMinOnChainCltvDelta - testReq.SweepConfTarget = 10
DefaultSweepConfTargetDelta - 1
testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta
// We set our mock fee estimate for our target sweep confs to be our // We set our mock fee estimate for our target sweep confs to be our
@ -442,11 +435,6 @@ func TestPreimagePush(t *testing.T) {
), ),
) )
// We set the fee estimate for our default confirmation target very
// low, so that once we drop down to our default confs we will start
// trying to sweep the preimage.
ctx.Lnd.SetFeeEstimate(DefaultSweepConfTarget, 1)
cfg := newSwapConfig( cfg := newSwapConfig(
&lnd.LndServices, newStoreMock(t), server, &lnd.LndServices, newStoreMock(t), server,
) )
@ -520,15 +508,15 @@ func TestPreimagePush(t *testing.T) {
// preimage is not revealed, we also do not expect a preimage push. // preimage is not revealed, we also do not expect a preimage push.
expiryChan <- testTime expiryChan <- testTime
// Now, we notify the height at which the client will start using the // Now we decrease our fees for the swap's confirmation target to less
// default confirmation target. This has the effect of lowering our fees // than the maximum miner fee.
// so that the client still start sweeping. ctx.Lnd.SetFeeEstimate(testReq.SweepConfTarget, chainfee.SatPerKWeight(
defaultConfTargetHeight := ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta - testReq.MaxMinerFee/2,
DefaultSweepConfTargetDelta ))
blockEpochChan <- defaultConfTargetHeight
// This time when we tick the expiry chan, our fees are lower than the // Now when we report a new block and tick our expiry fee timer, and
// swap max, so we expect it to prompt a sweep. // fees are acceptably low so we expect our sweep to be published.
blockEpochChan <- ctx.Lnd.Height + 2
expiryChan <- testTime expiryChan <- testTime
// Expect a signing request for the HTLC success transaction. // Expect a signing request for the HTLC success transaction.

View File

@ -258,5 +258,5 @@ func (s *LndMockServices) DecodeInvoice(request string) (*zpay32.Invoice,
func (s *LndMockServices) SetFeeEstimate(confTarget int32, func (s *LndMockServices) SetFeeEstimate(confTarget int32,
feeEstimate chainfee.SatPerKWeight) { feeEstimate chainfee.SatPerKWeight) {
s.WalletKit.(*mockWalletKit).feeEstimates[confTarget] = feeEstimate s.WalletKit.(*mockWalletKit).setFeeEstimate(confTarget, feeEstimate)
} }

View File

@ -3,6 +3,7 @@ package test
import ( import (
"context" "context"
"errors" "errors"
"sync"
"time" "time"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
@ -21,9 +22,11 @@ import (
var DefaultMockFee = chainfee.SatPerKWeight(10000) var DefaultMockFee = chainfee.SatPerKWeight(10000)
type mockWalletKit struct { type mockWalletKit struct {
lnd *LndMockServices lnd *LndMockServices
keyIndex int32 keyIndex int32
feeEstimates map[int32]chainfee.SatPerKWeight
feeEstimateLock sync.Mutex
feeEstimates map[int32]chainfee.SatPerKWeight
} }
var _ lndclient.WalletKitClient = (*mockWalletKit)(nil) var _ lndclient.WalletKitClient = (*mockWalletKit)(nil)
@ -118,9 +121,19 @@ func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut,
return &tx, nil return &tx, nil
} }
func (m *mockWalletKit) setFeeEstimate(confTarget int32, fee chainfee.SatPerKWeight) {
m.feeEstimateLock.Lock()
defer m.feeEstimateLock.Unlock()
m.feeEstimates[confTarget] = fee
}
func (m *mockWalletKit) EstimateFee(ctx context.Context, confTarget int32) ( func (m *mockWalletKit) EstimateFee(ctx context.Context, confTarget int32) (
chainfee.SatPerKWeight, error) { chainfee.SatPerKWeight, error) {
m.feeEstimateLock.Lock()
defer m.feeEstimateLock.Unlock()
if confTarget <= 1 { if confTarget <= 1 {
return 0, errors.New("conf target must be greater than 1") return 0, errors.New("conf target must be greater than 1")
} }