Skip to content

Commit edac1a4

Browse files
committed
loopin/test: extend test with external htlcs
1 parent a05e466 commit edac1a4

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

loopin_test.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,25 @@ func TestLoopInSuccess(t *testing.T) {
113113
}
114114
}
115115

116-
// TestLoopInTimeout tests the scenario where the server doesn't sweep the htlc
116+
// TestLoopInTimeout tests scenarios where the server doesn't sweep the htlc
117117
// and the client is forced to reclaim the funds using the timeout tx.
118118
func TestLoopInTimeout(t *testing.T) {
119+
testAmt := int64(testLoopInRequest.Amount)
120+
t.Run("internal htlc", func(t *testing.T) {
121+
testLoopInTimeout(t, 0)
122+
})
123+
t.Run("external htlc", func(t *testing.T) {
124+
testLoopInTimeout(t, testAmt)
125+
})
126+
t.Run("external amount too high", func(t *testing.T) {
127+
testLoopInTimeout(t, testAmt+1)
128+
})
129+
t.Run("external amount too low", func(t *testing.T) {
130+
testLoopInTimeout(t, testAmt-1)
131+
})
132+
}
133+
134+
func testLoopInTimeout(t *testing.T, externalValue int64) {
119135
defer test.Guard(t)()
120136

121137
ctx := newLoopInTestContext(t)
@@ -128,9 +144,14 @@ func TestLoopInTimeout(t *testing.T) {
128144
server: ctx.server,
129145
}
130146

147+
req := testLoopInRequest
148+
if externalValue != 0 {
149+
req.ExternalHtlc = true
150+
}
151+
131152
swap, err := newLoopInSwap(
132153
context.Background(), cfg,
133-
height, &testLoopInRequest,
154+
height, &req,
134155
)
135156
if err != nil {
136157
t.Fatal(err)
@@ -152,8 +173,21 @@ func TestLoopInTimeout(t *testing.T) {
152173
ctx.assertState(loopdb.StateHtlcPublished)
153174
ctx.store.assertLoopInState(loopdb.StateHtlcPublished)
154175

155-
// Expect htlc to be published.
156-
htlcTx := <-ctx.lnd.SendOutputsChannel
176+
var htlcTx wire.MsgTx
177+
if externalValue == 0 {
178+
// Expect htlc to be published.
179+
htlcTx = <-ctx.lnd.SendOutputsChannel
180+
} else {
181+
// Create an external htlc publish tx.
182+
htlcTx = wire.MsgTx{
183+
TxOut: []*wire.TxOut{
184+
{
185+
PkScript: swap.htlc.PkScript,
186+
Value: externalValue,
187+
},
188+
},
189+
}
190+
}
157191

158192
// Expect register for htlc conf.
159193
<-ctx.lnd.RegisterConfChannel
@@ -175,8 +209,15 @@ func TestLoopInTimeout(t *testing.T) {
175209
// Let htlc expire.
176210
ctx.blockEpochChan <- swap.LoopInContract.CltvExpiry
177211

178-
// Expect a signing request.
179-
<-ctx.lnd.SignOutputRawChannel
212+
// Expect a signing request for the htlc tx output value.
213+
//
214+
// TODO(joostjager): FIX BUG WHERE WE ALWAYS SIGN FOR THE HTLC AMOUNT.
215+
signReq := <-ctx.lnd.SignOutputRawChannel
216+
if signReq.SignDescriptors[0].Output.Value !=
217+
int64(testLoopInRequest.Amount) {
218+
219+
t.Fatal("invalid signing amount")
220+
}
180221

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

0 commit comments

Comments
 (0)