Skip to content

Commit 98bdbcd

Browse files
committed
tests: extend tests to also test resuming with HTLC v2
1 parent 133f3ca commit 98bdbcd

File tree

2 files changed

+98
-28
lines changed

2 files changed

+98
-28
lines changed

client_test.go

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/btcsuite/btcutil"
1313
"github.com/lightninglabs/lndclient"
1414
"github.com/lightninglabs/loop/loopdb"
15+
"github.com/lightninglabs/loop/swap"
1516
"github.com/lightninglabs/loop/test"
1617
"github.com/lightningnetwork/lnd/lnrpc"
1718
"github.com/lightningnetwork/lnd/lntypes"
@@ -68,7 +69,7 @@ func TestSuccess(t *testing.T) {
6869

6970
testSuccess(ctx, testRequest.Amount, info.SwapHash,
7071
signalPrepaymentResult, signalSwapPaymentResult, false,
71-
confIntent,
72+
confIntent, swap.HtlcV2,
7273
)
7374
}
7475

@@ -150,22 +151,46 @@ func TestResume(t *testing.T) {
150151

151152
defaultConfs := loopdb.DefaultLoopOutHtlcConfirmations
152153

153-
t.Run("not expired", func(t *testing.T) {
154-
testResume(t, defaultConfs, false, false, true)
155-
})
156-
t.Run("not expired, custom confirmations", func(t *testing.T) {
157-
testResume(t, 3, false, false, true)
158-
})
159-
t.Run("expired not revealed", func(t *testing.T) {
160-
testResume(t, defaultConfs, true, false, false)
161-
})
162-
t.Run("expired revealed", func(t *testing.T) {
163-
testResume(t, defaultConfs, true, true, true)
164-
})
154+
storedVersion := []loopdb.ProtocolVersion{
155+
loopdb.ProtocolVersionUnrecorded,
156+
loopdb.ProtocolVersionHtlcV2,
157+
}
158+
159+
for _, version := range storedVersion {
160+
version := version
161+
162+
t.Run(version.String(), func(t *testing.T) {
163+
t.Run("not expired", func(t *testing.T) {
164+
testResume(
165+
t, defaultConfs, false, false, true,
166+
version,
167+
)
168+
})
169+
t.Run("not expired, custom confirmations",
170+
func(t *testing.T) {
171+
testResume(
172+
t, 3, false, false, true,
173+
version,
174+
)
175+
})
176+
t.Run("expired not revealed", func(t *testing.T) {
177+
testResume(
178+
t, defaultConfs, true, false, false,
179+
version,
180+
)
181+
})
182+
t.Run("expired revealed", func(t *testing.T) {
183+
testResume(
184+
t, defaultConfs, true, true, true,
185+
version,
186+
)
187+
})
188+
})
189+
}
165190
}
166191

167192
func testResume(t *testing.T, confs uint32, expired, preimageRevealed,
168-
expectSuccess bool) {
193+
expectSuccess bool, protocolVersion loopdb.ProtocolVersion) {
169194

170195
defer test.Guard(t)()
171196

@@ -222,6 +247,7 @@ func testResume(t *testing.T, confs uint32, expired, preimageRevealed,
222247
SenderKey: senderKey,
223248
MaxSwapFee: 60000,
224249
MaxMinerFee: 50000,
250+
ProtocolVersion: protocolVersion,
225251
},
226252
},
227253
Loop: loopdb.Loop{
@@ -250,6 +276,15 @@ func testResume(t *testing.T, confs uint32, expired, preimageRevealed,
250276
// Expect client to register for our expected number of confirmations.
251277
confIntent := ctx.AssertRegisterConf(preimageRevealed, int32(confs))
252278

279+
// Assert that the loopout htlc equals to the expected one.
280+
scriptVersion := GetHtlcScriptVersion(protocolVersion)
281+
htlc, err := swap.NewHtlc(
282+
scriptVersion, pendingSwap.Contract.CltvExpiry, senderKey,
283+
receiverKey, hash, swap.HtlcP2WSH, &chaincfg.TestNet3Params,
284+
)
285+
require.NoError(t, err)
286+
require.Equal(t, htlc.PkScript, confIntent.PkScript)
287+
253288
signalSwapPaymentResult(nil)
254289
signalPrepaymentResult(nil)
255290

@@ -267,13 +302,14 @@ func testResume(t *testing.T, confs uint32, expired, preimageRevealed,
267302
func(r error) {},
268303
func(r error) {},
269304
preimageRevealed,
270-
confIntent,
305+
confIntent, scriptVersion,
271306
)
272307
}
273308

274309
func testSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
275310
signalPrepaymentResult, signalSwapPaymentResult func(error),
276-
preimageRevealed bool, confIntent *test.ConfRegistration) {
311+
preimageRevealed bool, confIntent *test.ConfRegistration,
312+
scriptVersion swap.ScriptVersion) {
277313

278314
htlcOutpoint := ctx.publishHtlc(confIntent.PkScript, amt)
279315

@@ -304,8 +340,13 @@ func testSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
304340
ctx.T.Fatalf("client not sweeping from htlc tx")
305341
}
306342

343+
preImageIndex := 1
344+
if scriptVersion == swap.HtlcV2 {
345+
preImageIndex = 0
346+
}
347+
307348
// Check preimage.
308-
clientPreImage := sweepTx.TxIn[0].Witness[1]
349+
clientPreImage := sweepTx.TxIn[0].Witness[preImageIndex]
309350
clientPreImageHash := sha256.Sum256(clientPreImage)
310351
if clientPreImageHash != hash {
311352
ctx.T.Fatalf("incorrect preimage")

loopin_test.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,48 @@ func testLoopInTimeout(t *testing.T,
282282

283283
// TestLoopInResume tests resuming swaps in various states.
284284
func TestLoopInResume(t *testing.T) {
285-
t.Run("initiated", func(t *testing.T) {
286-
testLoopInResume(t, loopdb.StateInitiated, false)
287-
})
285+
storedVersion := []loopdb.ProtocolVersion{
286+
loopdb.ProtocolVersionUnrecorded,
287+
loopdb.ProtocolVersionHtlcV2,
288+
}
288289

289-
t.Run("initiated expired", func(t *testing.T) {
290-
testLoopInResume(t, loopdb.StateInitiated, true)
291-
})
290+
htlcVersion := []swap.ScriptVersion{
291+
swap.HtlcV1,
292+
swap.HtlcV2,
293+
}
292294

293-
t.Run("htlc published", func(t *testing.T) {
294-
testLoopInResume(t, loopdb.StateHtlcPublished, false)
295-
})
295+
for i, version := range storedVersion {
296+
version := version
297+
scriptVersion := htlcVersion[i]
298+
299+
t.Run(version.String(), func(t *testing.T) {
300+
t.Run("initiated", func(t *testing.T) {
301+
testLoopInResume(
302+
t, loopdb.StateInitiated, false,
303+
version, scriptVersion,
304+
)
305+
})
306+
307+
t.Run("initiated expired", func(t *testing.T) {
308+
testLoopInResume(
309+
t, loopdb.StateInitiated, true,
310+
version, scriptVersion,
311+
)
312+
})
313+
314+
t.Run("htlc published", func(t *testing.T) {
315+
testLoopInResume(
316+
t, loopdb.StateHtlcPublished, false,
317+
version, scriptVersion,
318+
)
319+
})
320+
})
321+
}
296322
}
297323

298-
func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
324+
func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
325+
storedVersion loopdb.ProtocolVersion, scriptVersion swap.ScriptVersion) {
326+
299327
defer test.Guard(t)()
300328

301329
ctx := newLoopInTestContext(t)
@@ -314,6 +342,7 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
314342
SenderKey: senderKey,
315343
MaxSwapFee: 60000,
316344
MaxMinerFee: 50000,
345+
ProtocolVersion: storedVersion,
317346
},
318347
}
319348
pendSwap := &loopdb.LoopIn{
@@ -331,7 +360,7 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
331360
}
332361

333362
htlc, err := swap.NewHtlc(
334-
swap.HtlcV1, contract.CltvExpiry, contract.SenderKey,
363+
scriptVersion, contract.CltvExpiry, contract.SenderKey,
335364
contract.ReceiverKey, testPreimage.Hash(), swap.HtlcNP2WSH,
336365
cfg.lnd.ChainParams,
337366
)

0 commit comments

Comments
 (0)