Skip to content

Commit 969bf95

Browse files
committed
testframework: verify that route is constructed
Route may not be built even when channel is active. Since this causes flaky test, Added to check route construction in advance.
1 parent e5cbb6b commit 969bf95

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

testframework/clightning.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,11 @@ func (n *CLightningNode) OpenChannel(remote LightningNode, capacity, pushAmt uin
371371
if err != nil {
372372
return false, fmt.Errorf("IsChannelActive() %w", err)
373373
}
374-
return remoteActive && localActive, nil
374+
hasRoute, err := n.HasRoute(remote.Id(), scid)
375+
if err != nil {
376+
return false, nil
377+
}
378+
return remoteActive && localActive && hasRoute, nil
375379
}, TIMEOUT)
376380
if err != nil {
377381
return "", fmt.Errorf("error waiting for active channel: %w", err)
@@ -386,6 +390,15 @@ func (n *CLightningNode) OpenChannel(remote LightningNode, capacity, pushAmt uin
386390
return scid, nil
387391
}
388392

393+
// HasRoute check the route is constructed
394+
func (n *CLightningNode) HasRoute(remote, scid string) (bool, error) {
395+
routes, err := n.Rpc.GetRoute(remote, 1, 1, 0, n.Info.Id, 0, nil, 1)
396+
if err != nil {
397+
return false, fmt.Errorf("GetRoute() %w", err)
398+
}
399+
return len(routes) > 0, nil
400+
}
401+
389402
func (n *CLightningNode) IsBlockHeightSynced() (bool, error) {
390403
r, err := n.bitcoin.Rpc.Call("getblockcount")
391404
if err != nil {

testframework/lightning.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type LightningNode interface {
2121
IsBlockHeightSynced() (bool, error)
2222
IsChannelActive(scid string) (bool, error)
2323
IsConnected(peer LightningNode) (bool, error)
24+
HasRoute(remote, scid string) (bool, error)
2425

2526
AddInvoice(amtSat uint64, desc, label string) (payreq string, err error)
2627
PayInvoice(payreq string) error

testframework/lnd.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414

1515
"github.com/elementsproject/peerswap/lightning"
1616
"github.com/lightningnetwork/lnd/lnrpc"
17+
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
1718
"github.com/lightningnetwork/lnd/lnwire"
19+
"github.com/lightningnetwork/lnd/routing/route"
1820
)
1921

2022
func getLndConfig() map[string]string {
@@ -375,7 +377,11 @@ func (n *LndNode) OpenChannel(peer LightningNode, capacity, pushAmt uint64, conn
375377
return false, fmt.Errorf("IsChannelActive() %w", err)
376378
}
377379
}
378-
return remoteActive && localActive, nil
380+
hasRoute, err := n.HasRoute(peer.Id(), scid)
381+
if err != nil {
382+
return false, nil
383+
}
384+
return remoteActive && localActive && hasRoute, nil
379385
}, TIMEOUT)
380386
if err != nil {
381387
return "", fmt.Errorf("error waiting for active channel: %w", err)
@@ -390,6 +396,39 @@ func (n *LndNode) OpenChannel(peer LightningNode, capacity, pushAmt uint64, conn
390396
return scid, nil
391397
}
392398

399+
// HasRoute check the route is constructed
400+
func (n *LndNode) HasRoute(remote, scid string) (bool, error) {
401+
chsRes, err := n.Rpc.ListChannels(context.Background(), &lnrpc.ListChannelsRequest{})
402+
if err != nil {
403+
return false, fmt.Errorf("ListChannels() %w", err)
404+
}
405+
var channel *lnrpc.Channel
406+
for _, ch := range chsRes.GetChannels() {
407+
channelShortId := lnwire.NewShortChanIDFromInt(ch.ChanId)
408+
if channelShortId.String() == lightning.Scid(scid).LndStyle() {
409+
channel = ch
410+
}
411+
}
412+
if channel.GetChanId() == 0 {
413+
return false, fmt.Errorf("could not find a channel with scid: %s", scid)
414+
}
415+
v, err := route.NewVertexFromStr(channel.GetRemotePubkey())
416+
if err != nil {
417+
return false, fmt.Errorf("NewVertexFromStr() %w", err)
418+
}
419+
420+
routeres, err := n.RpcV2.BuildRoute(context.Background(), &routerrpc.BuildRouteRequest{
421+
AmtMsat: 1,
422+
FinalCltvDelta: 9,
423+
OutgoingChanId: channel.GetChanId(),
424+
HopPubkeys: [][]byte{v[:]},
425+
})
426+
if err != nil {
427+
return false, fmt.Errorf("BuildRoute() %w", err)
428+
}
429+
return len(routeres.GetRoute().Hops) > 0, nil
430+
}
431+
393432
func (n *LndNode) IsBlockHeightSynced() (bool, error) {
394433
r, err := n.bitcoin.Rpc.Call("getblockcount")
395434
if err != nil {

0 commit comments

Comments
 (0)