loopout: add test for checking preimage reveal after timeout

pull/366/head
yyforyongyu 3 years ago
parent 8c3206c785
commit 2feb629a46
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868

@ -593,3 +593,108 @@ func TestPreimagePush(t *testing.T) {
require.NoError(t, <-errChan)
}
// TestNotPublishPreimageAfterTimeout validates that when the on-chain HTLC has
// timed out, the client won't sweep the HTLC anymore by publishing the
// preimage.
func TestNotPublishPreimageAfterTimeout(t *testing.T) {
defer test.Guard(t)()
lnd := test.NewMockLnd()
ctx := test.NewContext(t, lnd)
server := newServerMock(lnd)
testReq := *testRequest
// Set on-chain HTLC CLTV.
testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta
// We set our max miner fee to be 0 so that our fee rate is above what
// we are willing to pay. The sweep will never succeed and will be
// retried.
testReq.MaxMinerFee = 0
// Setup the cfg using mock server and init a loop out request.
cfg := newSwapConfig(
&lnd.LndServices, newStoreMock(t), server,
)
initResult, err := newLoopOutSwap(
context.Background(), cfg, ctx.Lnd.Height, &testReq,
)
require.NoError(t, err)
swap := initResult.swap
// Set up the required dependencies to execute the swap.
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
blockEpochChan := make(chan interface{})
statusChan := make(chan SwapInfo)
expiryChan := make(chan time.Time)
timerFactory := func(_ time.Duration) <-chan time.Time {
return expiryChan
}
errChan := make(chan error)
go func() {
err := swap.execute(context.Background(), &executeConfig{
statusChan: statusChan,
blockEpochChan: blockEpochChan,
timerFactory: timerFactory,
sweeper: sweeper,
}, ctx.Lnd.Height)
if err != nil {
log.Error(err)
}
errChan <- err
}()
// The swap should be found in its initial state.
cfg.store.(*storeMock).assertLoopOutStored()
state := <-statusChan
require.Equal(t, loopdb.StateInitiated, state.State)
// We'll then pay both the swap and prepay invoice, which should trigger
// the server to publish the on-chain HTLC.
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
signalSwapPaymentResult(nil)
signalPrepaymentResult(nil)
// Notify the confirmation notification for the HTLC.
ctx.AssertRegisterConf(false, defaultConfirmations)
// Advance the block height to get the HTLC confirmed.
blockEpochChan <- ctx.Lnd.Height + 1
htlcTx := wire.NewMsgTx(2)
htlcTx.AddTxOut(&wire.TxOut{
Value: int64(swap.AmountRequested),
PkScript: swap.htlc.PkScript,
})
ctx.NotifyConf(htlcTx)
// The client should then register for a spend of the HTLC and attempt
// to sweep it using the custom confirmation target.
ctx.AssertRegisterSpendNtfn(swap.htlc.PkScript)
// Assert that we made a query to track our payment, as required for
// preimage push tracking.
ctx.AssertTrackPayment()
// Tick the expiry channel. Because our max miner fee is 0, we won't
// attempt a sweep at this point.
expiryChan <- testTime
// Advance the block height to the point where we would do timeout
// instead of pushing the preimage.
blockEpochChan <- testReq.Expiry
// We should see our swap marked as failed.
cfg.store.(*storeMock).assertLoopOutState(loopdb.StateFailSweepTimeout)
status := <-statusChan
require.Equal(
t, status.State, loopdb.StateFailSweepTimeout,
)
require.NoError(t, <-errChan)
}

Loading…
Cancel
Save