Skip to content

Commit e202c05

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 ensure the HTLC exists in the right direction. Although often the case `AssertNumActiveHtlcs` would be enough has it implicitly checks the forwarding behavior for an intermediate node by asserting there are always num_payment*2 HTLCs.
1 parent b662fee commit e202c05

3 files changed

+117
-92
lines changed

itest/lnd_hold_invoice_force_test.go

+6-2
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

+111-38
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,18 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
320320
}
321321
ht.SendPaymentAssertInflight(alice, req)
322322

323-
// Verify that all nodes in the path now have two HTLC's with the
324-
// proper parameters.
325-
ht.AssertActiveHtlcs(alice, dustPayHash, payHash)
326-
ht.AssertActiveHtlcs(bob, dustPayHash, payHash)
327-
ht.AssertActiveHtlcs(carol, dustPayHash, payHash)
323+
// At this point, all 3 nodes should now have an active channel with
324+
// the created HTLC pending on all of them.
325+
//
326+
// Alice should have two outgoing HTLCs on channel Alice -> Bob.
327+
ht.AssertNumActiveHtlcs(alice, 2)
328+
329+
// Bob should have two incoming HTLCs on channel Alice -> Bob, and two
330+
// outgoing HTLCs on channel Bob -> Carol.
331+
ht.AssertNumActiveHtlcs(bob, 4)
332+
333+
// Carol should have two incoming HTLCs on channel Bob -> Carol.
334+
ht.AssertNumActiveHtlcs(carol, 2)
328335

329336
// We'll now mine enough blocks to trigger Bob's force close the
330337
// channel Bob=>Carol due to the fact that the HTLC is about to
@@ -364,7 +371,7 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
364371
// At this point, Bob should have canceled backwards the dust HTLC that
365372
// we sent earlier. This means Alice should now only have a single HTLC
366373
// on her channel.
367-
ht.AssertActiveHtlcs(alice, payHash)
374+
ht.AssertNumActiveHtlcs(alice, 1)
368375

369376
// With the closing transaction confirmed, we should expect Bob's HTLC
370377
// timeout transaction to be offered to the sweeper due to the expiry
@@ -651,9 +658,18 @@ func runMultiHopReceiverPreimageClaim(ht *lntest.HarnessTest,
651658

652659
// At this point, all 3 nodes should now have an active channel with
653660
// the created HTLC pending on all of them.
654-
ht.AssertActiveHtlcs(alice, payHash[:])
655-
ht.AssertActiveHtlcs(bob, payHash[:])
656-
ht.AssertActiveHtlcs(carol, payHash[:])
661+
// At this point, all 3 nodes should now have an active channel with
662+
// the created HTLCs pending on all of them.
663+
//
664+
// Alice should have one outgoing HTLCs on channel Alice -> Bob.
665+
ht.AssertNumActiveHtlcs(alice, 1)
666+
667+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
668+
// outgoing HTLC on channel Bob -> Carol.
669+
ht.AssertNumActiveHtlcs(bob, 2)
670+
671+
// Carol should have one incoming HTLC on channel Bob -> Carol.
672+
ht.AssertNumActiveHtlcs(carol, 1)
657673

658674
// Wait for Carol to mark invoice as accepted. There is a small gap to
659675
// bridge between adding the htlc to the channel and executing the exit
@@ -1010,11 +1026,20 @@ func runLocalForceCloseBeforeHtlcTimeout(ht *lntest.HarnessTest,
10101026
}
10111027
ht.SendPaymentAssertInflight(alice, req)
10121028

1013-
// Once the HTLC has cleared, all channels in our mini network should
1014-
// have the it locked in.
1015-
ht.AssertActiveHtlcs(alice, payHash)
1016-
ht.AssertActiveHtlcs(bob, payHash)
1017-
ht.AssertActiveHtlcs(carol, payHash)
1029+
// At this point, all 3 nodes should now have an active channel with
1030+
// the created HTLC pending on all of them.
1031+
// At this point, all 3 nodes should now have an active channel with
1032+
// the created HTLCs pending on all of them.
1033+
//
1034+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1035+
ht.AssertNumActiveHtlcs(alice, 1)
1036+
1037+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1038+
// outgoing HTLC on channel Bob -> Carol.
1039+
ht.AssertNumActiveHtlcs(bob, 2)
1040+
1041+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1042+
ht.AssertNumActiveHtlcs(carol, 1)
10181043

10191044
// Now that all parties have the HTLC locked in, we'll immediately
10201045
// force close the Bob -> Carol channel. This should trigger contract
@@ -1339,11 +1364,18 @@ func runRemoteForceCloseBeforeHtlcTimeout(ht *lntest.HarnessTest,
13391364
}
13401365
ht.SendPaymentAssertInflight(alice, req)
13411366

1342-
// Once the HTLC has cleared, all the nodes in our mini network should
1343-
// show that the HTLC has been locked in.
1344-
ht.AssertActiveHtlcs(alice, payHash[:])
1345-
ht.AssertActiveHtlcs(bob, payHash[:])
1346-
ht.AssertActiveHtlcs(carol, payHash[:])
1367+
// At this point, all 3 nodes should now have an active channel with
1368+
// the created HTLCs pending on all of them.
1369+
//
1370+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1371+
ht.AssertNumActiveHtlcs(alice, 1)
1372+
1373+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1374+
// outgoing HTLC on channel Bob -> Carol.
1375+
ht.AssertNumActiveHtlcs(bob, 2)
1376+
1377+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1378+
ht.AssertNumActiveHtlcs(carol, 1)
13471379

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

16011633
// At this point, all 3 nodes should now have an active channel with
16021634
// the created HTLC pending on all of them.
1603-
ht.AssertActiveHtlcs(alice, payHash[:])
1604-
ht.AssertActiveHtlcs(bob, payHash[:])
1605-
ht.AssertActiveHtlcs(carol, payHash[:])
1635+
// At this point, all 3 nodes should now have an active channel with
1636+
// the created HTLCs pending on all of them.
1637+
//
1638+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1639+
ht.AssertNumActiveHtlcs(alice, 1)
1640+
1641+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1642+
// outgoing HTLC on channel Bob -> Carol.
1643+
ht.AssertNumActiveHtlcs(bob, 2)
1644+
1645+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1646+
ht.AssertNumActiveHtlcs(carol, 1)
16061647

16071648
// Wait for carol to mark invoice as accepted. There is a small gap to
16081649
// bridge between adding the htlc to the channel and executing the exit
@@ -1903,9 +1944,18 @@ func runLocalClaimIncomingHTLCLeased(ht *lntest.HarnessTest,
19031944

19041945
// At this point, all 3 nodes should now have an active channel with
19051946
// the created HTLC pending on all of them.
1906-
ht.AssertActiveHtlcs(alice, payHash[:])
1907-
ht.AssertActiveHtlcs(bob, payHash[:])
1908-
ht.AssertActiveHtlcs(carol, payHash[:])
1947+
// At this point, all 3 nodes should now have an active channel with
1948+
// the created HTLCs pending on all of them.
1949+
//
1950+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
1951+
ht.AssertNumActiveHtlcs(alice, 1)
1952+
1953+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
1954+
// outgoing HTLC on channel Bob -> Carol.
1955+
ht.AssertNumActiveHtlcs(bob, 2)
1956+
1957+
// Carol should have one incoming HTLC on channel Bob -> Carol.
1958+
ht.AssertNumActiveHtlcs(carol, 1)
19091959

19101960
// Wait for carol to mark invoice as accepted. There is a small gap to
19111961
// bridge between adding the htlc to the channel and executing the exit
@@ -2257,10 +2307,17 @@ func runLocalPreimageClaim(ht *lntest.HarnessTest,
22572307
ht.SendPaymentAssertInflight(alice, req)
22582308

22592309
// At this point, all 3 nodes should now have an active channel with
2260-
// the created HTLC pending on all of them.
2261-
ht.AssertActiveHtlcs(alice, payHash[:])
2262-
ht.AssertActiveHtlcs(bob, payHash[:])
2263-
ht.AssertActiveHtlcs(carol, payHash[:])
2310+
// the created HTLCs pending on all of them.
2311+
//
2312+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
2313+
ht.AssertNumActiveHtlcs(alice, 1)
2314+
2315+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
2316+
// outgoing HTLC on channel Bob -> Carol.
2317+
ht.AssertNumActiveHtlcs(bob, 2)
2318+
2319+
// Carol should have one incoming HTLC on channel Bob -> Carol.
2320+
ht.AssertNumActiveHtlcs(carol, 1)
22642321

22652322
// Wait for carol to mark invoice as accepted. There is a small gap to
22662323
// bridge between adding the htlc to the channel and executing the exit
@@ -2540,10 +2597,17 @@ func runLocalPreimageClaimLeased(ht *lntest.HarnessTest,
25402597
ht.SendPaymentAssertInflight(alice, req)
25412598

25422599
// At this point, all 3 nodes should now have an active channel with
2543-
// the created HTLC pending on all of them.
2544-
ht.AssertActiveHtlcs(alice, payHash[:])
2545-
ht.AssertActiveHtlcs(bob, payHash[:])
2546-
ht.AssertActiveHtlcs(carol, payHash[:])
2600+
// the created HTLCs pending on all of them.
2601+
//
2602+
// Alice should have one outgoing HTLC on channel Alice -> Bob.
2603+
ht.AssertNumActiveHtlcs(alice, 1)
2604+
2605+
// Bob should have one incoming HTLC on channel Alice -> Bob, and one
2606+
// outgoing HTLC on channel Bob -> Carol.
2607+
ht.AssertNumActiveHtlcs(bob, 2)
2608+
2609+
// Carol should have one incoming HTLC on channel Bob -> Carol.
2610+
ht.AssertNumActiveHtlcs(carol, 1)
25472611

25482612
// Wait for carol to mark invoice as accepted. There is a small gap to
25492613
// bridge between adding the htlc to the channel and executing the exit
@@ -2997,11 +3061,20 @@ func runHtlcAggregation(ht *lntest.HarnessTest,
29973061
ht.SendPaymentAssertInflight(carol, req)
29983062
}
29993063

3000-
// At this point, all 3 nodes should now the HTLCs active on their
3001-
// channels.
3002-
ht.AssertActiveHtlcs(alice, payHashes...)
3003-
ht.AssertActiveHtlcs(bob, payHashes...)
3004-
ht.AssertActiveHtlcs(carol, payHashes...)
3064+
// At this point, all 3 nodes should now have an active channel with
3065+
// the created HTLCs pending on all of them.
3066+
//
3067+
// Alice sent numInvoices and received numInvoices payments, she should
3068+
// have numInvoices*2 HTLCs.
3069+
ht.AssertNumActiveHtlcs(alice, numInvoices*2)
3070+
3071+
// Bob should have 2*numInvoices HTLCs on channel Alice -> Bob, and
3072+
// numInvoices*2 HTLCs on channel Bob -> Carol.
3073+
ht.AssertNumActiveHtlcs(bob, numInvoices*4)
3074+
3075+
// Carol sent numInvoices and received numInvoices payments, she should
3076+
// have numInvoices*2 HTLCs.
3077+
ht.AssertNumActiveHtlcs(carol, numInvoices*2)
30053078

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

lntest/harness_assertion.go

-52
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)