Skip to content

Commit

Permalink
unit: adjust for incorrect amount sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
hieblmi committed Jan 30, 2024
1 parent f54c86f commit 1e5be4f
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions loopin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,15 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
// Assert that the swap is failed in case of an invalid amount.
invalidAmt := externalValue != 0 && externalValue != int64(req.Amount)
if invalidAmt {
ctx.assertState(loopdb.StateFailIncorrectHtlcAmt)
ctx.store.AssertLoopInState(loopdb.StateFailIncorrectHtlcAmt)

require.NoError(t, <-errChan)
return
handleInvalidAmountTimeout(t, ctx, inSwap, htlcTx, errChan)
} else {
handleValidAmountTimeout(t, ctx, inSwap, htlcTx, cost, errChan)
}
}

func handleValidAmountTimeout(t *testing.T, ctx *loopInTestContext,
inSwap *loopInSwap, htlcTx wire.MsgTx, cost loopdb.SwapCost,
errChan chan error) {

// Client starts listening for spend of htlc.
<-ctx.lnd.RegisterSpendChannel
Expand Down Expand Up @@ -336,6 +339,48 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
require.NoError(t, <-errChan)
}

func handleInvalidAmountTimeout(t *testing.T, ctx *loopInTestContext,
inSwap *loopInSwap, htlcTx wire.MsgTx, errChan chan error) {

// The htlc is expected to be confirmed at this stage so we can time it
// out.
ctx.blockEpochChan <- inSwap.LoopInContract.CltvExpiry

// Expect a signing request for the htlc tx output value.
signReq := <-ctx.lnd.SignOutputRawChannel
require.Equal(
t, htlcTx.TxOut[0].Value,
signReq.SignDescriptors[0].Output.Value,
"invalid signing amount",
)

// Expect timeout tx to be published.
timeoutTx := <-ctx.lnd.TxPublishChannel

// Client starts listening for spend of htlc.
<-ctx.lnd.RegisterSpendChannel

// Client starts listening for swap invoice updates.
ctx.assertSubscribeInvoice(ctx.server.swapHash)

// Confirm timeout tx.
ctx.lnd.SpendChannel <- &chainntnfs.SpendDetail{
SpendingTx: timeoutTx,
SpenderInputIndex: 0,
}

// Check that the saved state indicates that the invalid htlc amount was
// swept.
ctx.assertState(loopdb.StateFailIncorrectHtlcAmtSwept)
ctx.store.AssertLoopInState(loopdb.StateFailIncorrectHtlcAmtSwept)

// Now that timeout tx has confirmed, the client should be able to
// safely cancel the swap invoice.
<-ctx.lnd.FailInvoiceChannel

require.NoError(t, <-errChan)
}

// TestLoopInResume tests resuming swaps in various states.
func TestLoopInResume(t *testing.T) {
storedVersion := []loopdb.ProtocolVersion{
Expand Down

0 comments on commit 1e5be4f

Please sign in to comment.