lnd_test: make assertActiveHtlcs correctly check the exact pay hashes

This commit is contained in:
Johan T. Halseth 2018-04-13 17:14:42 +02:00
parent daeeca0bc3
commit 4bd45b22eb
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -5341,6 +5341,8 @@ func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest)
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false) closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
} }
// assertActiveHtlcs makes sure all the passed nodes have the _exact_ HTLCs
// matching payHashes on _all_ their channels.
func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error { func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error {
req := &lnrpc.ListChannelsRequest{} req := &lnrpc.ListChannelsRequest{}
ctxb := context.Background() ctxb := context.Background()
@ -5351,27 +5353,31 @@ func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error {
} }
for _, channel := range nodeChans.Channels { for _, channel := range nodeChans.Channels {
if len(channel.PendingHtlcs) == 0 { // Record all payment hashes active for this channel.
return fmt.Errorf("node %x has no htlcs: %v", htlcHashes := make(map[string]struct{})
node.PubKey[:], spew.Sdump(channel)) for _, htlc := range channel.PendingHtlcs {
_, ok := htlcHashes[string(htlc.HashLock)]
if ok {
return fmt.Errorf("duplicate HashLock")
}
htlcHashes[string(htlc.HashLock)] = struct{}{}
} }
for _, htlc := range channel.PendingHtlcs { // Channel should have exactly the payHashes active.
if len(payHashes) != len(htlcHashes) {
return fmt.Errorf("node %x had %v htlcs active, "+
"expected %v", node.PubKey[:],
len(htlcHashes), len(payHashes))
}
var htlcIsMatch bool // Make sure all the payHashes are active.
for _, payHash := range payHashes { for _, payHash := range payHashes {
if bytes.Equal(htlc.HashLock, payHash) { if _, ok := htlcHashes[string(payHash)]; ok {
htlcIsMatch = true
}
}
if htlcIsMatch {
continue continue
} }
return fmt.Errorf("node %x didn't have the "+
return fmt.Errorf("node %x doesn't have expected "+ "payHash %v active", node.PubKey[:],
"payment hashes: %v", node.PubKey[:], payHash)
spew.Sdump(channel.PendingHtlcs))
} }
} }
} }