Skip to content

Commit 197ab3f

Browse files
committed
lntest+itest: remove the usage of ht.AssertActiveHtlcs
The method `AssertActiveHtlcs` is now removed due to it's easy to be misused. To assert a given htlc, use `AssertOutgoingHTLCActive` and `AssertIncomingHTLCActive` instead for ensuring the HTLC exists in the right direction. Although often the case `AssertNumActiveHtlcs` would be enough as it implicitly checks the forwarding behavior for an intermediate node by asserting there are always num_payment*2 HTLCs.
1 parent 69c0d5b commit 197ab3f

File tree

3 files changed

+117
-92
lines changed

3 files changed

+117
-92
lines changed

itest/lnd_hold_invoice_force_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ func testHoldInvoiceForceClose(ht *lntest.HarnessTest) {
5050

5151
// Once the HTLC has cleared, alice and bob should both have a single
5252
// htlc locked in.
53-
ht.AssertActiveHtlcs(alice, payHash[:])
54-
ht.AssertActiveHtlcs(bob, payHash[:])
53+
//
54+
// Alice should have one outgoing HTLCs on channel Alice -> Bob.
55+
ht.AssertOutgoingHTLCActive(alice, chanPoint, payHash[:])
56+
57+
// Bob should have one incoming HTLC on channel Alice -> Bob.
58+
ht.AssertIncomingHTLCActive(bob, chanPoint, payHash[:])
5559

5660
// Get our htlc expiry height and current block height so that we
5761
// can mine the exact number of blocks required to expire the htlc.

itest/lnd_multi-hop_force_close_test.go

Lines changed: 111 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,18 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
328328
}
329329
ht.SendPaymentAssertInflight(alice, req)
330330

331-
// Verify that all nodes in the path now have two HTLC's with the
332-
// proper parameters.
333-
ht.AssertActiveHtlcs(alice, dustPayHash, payHash)
334-
ht.AssertActiveHtlcs(bob, dustPayHash, payHash)
335-
ht.AssertActiveHtlcs(carol, dustPayHash, payHash)
331+
// At this point, all 3 nodes should now have an active channel with
332+
// the created HTLC pending on all of them.
333+
//
334+
// Alice should have two outgoing HTLCs on channel Alice -> Bob.
335+
ht.AssertNumActiveHtlcs(alice, 2)
336+
337+
// Bob should have two incoming HTLCs on channel Alice -> Bob, and two
338+
// outgoing HTLCs on channel Bob -> Carol.
339+
ht.AssertNumActiveHtlcs(bob, 4)
340+
341+
// Carol should have two incoming HTLCs on channel Bob -> Carol.
342+
ht.AssertNumActiveHtlcs(carol, 2)
336343

337344
// We'll now mine enough blocks to trigger Bob's force close the
338345
// channel Bob=>Carol due to the fact that the HTLC is about to
@@ -372,7 +379,7 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
372379
// At this point, Bob should have canceled backwards the dust HTLC that
373380
// we sent earlier. This means Alice should now only have a single HTLC
374381
// on her channel.
375-
ht.AssertActiveHtlcs(alice, payHash)
382+
ht.AssertNumActiveHtlcs(alice, 1)
376383

377384
// With the closing transaction confirmed, we should expect Bob's HTLC
378385
// timeout transaction to be offered to the sweeper due to the expiry
@@ -659,9 +666,18 @@ func runMultiHopReceiverPreimageClaim(ht *lntest.HarnessTest,
659666

660667
// At this point, all 3 nodes should now have an active channel with
661668
// the created HTLC pending on all of them.
662-
ht.AssertActiveHtlcs(alice, payHash[:])
663-
ht.AssertActiveHtlcs(bob, payHash[:])
664-
ht.AssertActiveHtlcs(carol, payHash[:])
669+
// At this point, all 3 nodes should now have an active channel with
670+
// the created HTLCs pending on all of them.
671+
//
672+
// Alice should have one outgoing HTLCs on channel Alice -> Bob.
673+
ht.AssertNumActiveHtlcs(alice, 1)
674+
675+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
676+
// outgoing HTLC on channel Bob -> Carol.
677+
ht.AssertNumActiveHtlcs(bob, 2)
678+
679+
// Carol should have one incoming HTLC on channel Bob -> Carol.
680+
ht.AssertNumActiveHtlcs(carol, 1)
665681

666682
// Wait for Carol to mark invoice as accepted. There is a small gap to
667683
// bridge between adding the htlc to the channel and executing the exit
@@ -1018,11 +1034,20 @@ func runLocalForceCloseBeforeHtlcTimeout(ht *lntest.HarnessTest,
10181034
}
10191035
ht.SendPaymentAssertInflight(alice, req)
10201036

1021-
// Once the HTLC has cleared, all channels in our mini network should
1022-
// have the it locked in.
1023-
ht.AssertActiveHtlcs(alice, payHash)
1024-
ht.AssertActiveHtlcs(bob, payHash)
1025-
ht.AssertActiveHtlcs(carol, payHash)
1037+
// At this point, all 3 nodes should now have an active channel with
1038+
// the created HTLC pending on all of them.
1039+
// At this point, all 3 nodes should now have an active channel with
1040+
// the created HTLCs pending on all of them.
1041+
//
1042+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1043+
ht.AssertNumActiveHtlcs(alice, 1)
1044+
1045+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1046+
// outgoing HTLC on channel Bob -> Carol.
1047+
ht.AssertNumActiveHtlcs(bob, 2)
1048+
1049+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1050+
ht.AssertNumActiveHtlcs(carol, 1)
10261051

10271052
// Now that all parties have the HTLC locked in, we'll immediately
10281053
// force close the Bob -> Carol channel. This should trigger contract
@@ -1347,11 +1372,18 @@ func runRemoteForceCloseBeforeHtlcTimeout(ht *lntest.HarnessTest,
13471372
}
13481373
ht.SendPaymentAssertInflight(alice, req)
13491374

1350-
// Once the HTLC has cleared, all the nodes in our mini network should
1351-
// show that the HTLC has been locked in.
1352-
ht.AssertActiveHtlcs(alice, payHash[:])
1353-
ht.AssertActiveHtlcs(bob, payHash[:])
1354-
ht.AssertActiveHtlcs(carol, payHash[:])
1375+
// At this point, all 3 nodes should now have an active channel with
1376+
// the created HTLCs pending on all of them.
1377+
//
1378+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1379+
ht.AssertNumActiveHtlcs(alice, 1)
1380+
1381+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1382+
// outgoing HTLC on channel Bob -> Carol.
1383+
ht.AssertNumActiveHtlcs(bob, 2)
1384+
1385+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1386+
ht.AssertNumActiveHtlcs(carol, 1)
13551387

13561388
// At this point, we'll now instruct Carol to force close the tx. This
13571389
// will let us exercise that Bob is able to sweep the expired HTLC on
@@ -1608,9 +1640,18 @@ func runLocalClaimIncomingHTLC(ht *lntest.HarnessTest,
16081640

16091641
// At this point, all 3 nodes should now have an active channel with
16101642
// the created HTLC pending on all of them.
1611-
ht.AssertActiveHtlcs(alice, payHash[:])
1612-
ht.AssertActiveHtlcs(bob, payHash[:])
1613-
ht.AssertActiveHtlcs(carol, payHash[:])
1643+
// At this point, all 3 nodes should now have an active channel with
1644+
// the created HTLCs pending on all of them.
1645+
//
1646+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1647+
ht.AssertNumActiveHtlcs(alice, 1)
1648+
1649+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1650+
// outgoing HTLC on channel Bob -> Carol.
1651+
ht.AssertNumActiveHtlcs(bob, 2)
1652+
1653+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1654+
ht.AssertNumActiveHtlcs(carol, 1)
16141655

16151656
// Wait for carol to mark invoice as accepted. There is a small gap to
16161657
// bridge between adding the htlc to the channel and executing the exit
@@ -1919,9 +1960,18 @@ func runLocalClaimIncomingHTLCLeased(ht *lntest.HarnessTest,
19191960

19201961
// At this point, all 3 nodes should now have an active channel with
19211962
// the created HTLC pending on all of them.
1922-
ht.AssertActiveHtlcs(alice, payHash[:])
1923-
ht.AssertActiveHtlcs(bob, payHash[:])
1924-
ht.AssertActiveHtlcs(carol, payHash[:])
1963+
// At this point, all 3 nodes should now have an active channel with
1964+
// the created HTLCs pending on all of them.
1965+
//
1966+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1967+
ht.AssertNumActiveHtlcs(alice, 1)
1968+
1969+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1970+
// outgoing HTLC on channel Bob -> Carol.
1971+
ht.AssertNumActiveHtlcs(bob, 2)
1972+
1973+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1974+
ht.AssertNumActiveHtlcs(carol, 1)
19251975

19261976
// Wait for carol to mark invoice as accepted. There is a small gap to
19271977
// bridge between adding the htlc to the channel and executing the exit
@@ -2273,10 +2323,17 @@ func runLocalPreimageClaim(ht *lntest.HarnessTest,
22732323
ht.SendPaymentAssertInflight(alice, req)
22742324

22752325
// At this point, all 3 nodes should now have an active channel with
2276-
// the created HTLC pending on all of them.
2277-
ht.AssertActiveHtlcs(alice, payHash[:])
2278-
ht.AssertActiveHtlcs(bob, payHash[:])
2279-
ht.AssertActiveHtlcs(carol, payHash[:])
2326+
// the created HTLCs pending on all of them.
2327+
//
2328+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
2329+
ht.AssertNumActiveHtlcs(alice, 1)
2330+
2331+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
2332+
// outgoing HTLC on channel Bob -> Carol.
2333+
ht.AssertNumActiveHtlcs(bob, 2)
2334+
2335+
// Carol should have one incoming HTLC on channel Bob -> Carol.
2336+
ht.AssertNumActiveHtlcs(carol, 1)
22802337

22812338
// Wait for carol to mark invoice as accepted. There is a small gap to
22822339
// bridge between adding the htlc to the channel and executing the exit
@@ -2556,10 +2613,17 @@ func runLocalPreimageClaimLeased(ht *lntest.HarnessTest,
25562613
ht.SendPaymentAssertInflight(alice, req)
25572614

25582615
// At this point, all 3 nodes should now have an active channel with
2559-
// the created HTLC pending on all of them.
2560-
ht.AssertActiveHtlcs(alice, payHash[:])
2561-
ht.AssertActiveHtlcs(bob, payHash[:])
2562-
ht.AssertActiveHtlcs(carol, payHash[:])
2616+
// the created HTLCs pending on all of them.
2617+
//
2618+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
2619+
ht.AssertNumActiveHtlcs(alice, 1)
2620+
2621+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
2622+
// outgoing HTLC on channel Bob -> Carol.
2623+
ht.AssertNumActiveHtlcs(bob, 2)
2624+
2625+
// Carol should have one incoming HTLC on channel Bob -> Carol.
2626+
ht.AssertNumActiveHtlcs(carol, 1)
25632627

25642628
// Wait for carol to mark invoice as accepted. There is a small gap to
25652629
// bridge between adding the htlc to the channel and executing the exit
@@ -3013,11 +3077,20 @@ func runHtlcAggregation(ht *lntest.HarnessTest,
30133077
ht.SendPaymentAssertInflight(carol, req)
30143078
}
30153079

3016-
// At this point, all 3 nodes should now the HTLCs active on their
3017-
// channels.
3018-
ht.AssertActiveHtlcs(alice, payHashes...)
3019-
ht.AssertActiveHtlcs(bob, payHashes...)
3020-
ht.AssertActiveHtlcs(carol, payHashes...)
3080+
// At this point, all 3 nodes should now have an active channel with
3081+
// the created HTLCs pending on all of them.
3082+
//
3083+
// Alice sent numInvoices and received numInvoices payments, she should
3084+
// have numInvoices*2 HTLCs.
3085+
ht.AssertNumActiveHtlcs(alice, numInvoices*2)
3086+
3087+
// Bob should have 2*numInvoices HTLCs on channel Alice -> Bob, and
3088+
// numInvoices*2 HTLCs on channel Bob -> Carol.
3089+
ht.AssertNumActiveHtlcs(bob, numInvoices*4)
3090+
3091+
// Carol sent numInvoices and received numInvoices payments, she should
3092+
// have numInvoices*2 HTLCs.
3093+
ht.AssertNumActiveHtlcs(carol, numInvoices*2)
30213094

30223095
// Wait for Alice and Carol to mark the invoices as accepted. There is
30233096
// a small gap to bridge between adding the htlc to the channel and

lntest/harness_assertion.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,58 +1314,6 @@ func (h *HarnessTest) AssertNumActiveHtlcs(hn *node.HarnessNode, num int) {
13141314
hn.Name())
13151315
}
13161316

1317-
// AssertActiveHtlcs makes sure the node has the _exact_ HTLCs matching
1318-
// payHashes on _all_ their channels.
1319-
func (h *HarnessTest) AssertActiveHtlcs(hn *node.HarnessNode,
1320-
payHashes ...[]byte) {
1321-
1322-
err := wait.NoError(func() error {
1323-
// We require the RPC call to be succeeded and won't wait for
1324-
// it as it's an unexpected behavior.
1325-
req := &lnrpc.ListChannelsRequest{}
1326-
nodeChans := hn.RPC.ListChannels(req)
1327-
1328-
for _, ch := range nodeChans.Channels {
1329-
// Record all payment hashes active for this channel.
1330-
htlcHashes := make(map[string]struct{})
1331-
1332-
for _, htlc := range ch.PendingHtlcs {
1333-
h := hex.EncodeToString(htlc.HashLock)
1334-
_, ok := htlcHashes[h]
1335-
if ok {
1336-
return fmt.Errorf("duplicate HashLock "+
1337-
"in PendingHtlcs: %v",
1338-
ch.PendingHtlcs)
1339-
}
1340-
htlcHashes[h] = struct{}{}
1341-
}
1342-
1343-
// Channel should have exactly the payHashes active.
1344-
if len(payHashes) != len(htlcHashes) {
1345-
return fmt.Errorf("node [%s:%x] had %v "+
1346-
"htlcs active, expected %v",
1347-
hn.Name(), hn.PubKey[:],
1348-
len(htlcHashes), len(payHashes))
1349-
}
1350-
1351-
// Make sure all the payHashes are active.
1352-
for _, payHash := range payHashes {
1353-
h := hex.EncodeToString(payHash)
1354-
if _, ok := htlcHashes[h]; ok {
1355-
continue
1356-
}
1357-
1358-
return fmt.Errorf("node [%s:%x] didn't have: "+
1359-
"the payHash %v active", hn.Name(),
1360-
hn.PubKey[:], h)
1361-
}
1362-
}
1363-
1364-
return nil
1365-
}, DefaultTimeout)
1366-
require.NoError(h, err, "timeout checking active HTLCs")
1367-
}
1368-
13691317
// AssertIncomingHTLCActive asserts the node has a pending incoming HTLC in the
13701318
// given channel. Returns the HTLC if found and active.
13711319
func (h *HarnessTest) AssertIncomingHTLCActive(hn *node.HarnessNode,

0 commit comments

Comments
 (0)