Skip to content

Commit

Permalink
testframework: verify that route is constructed
Browse files Browse the repository at this point in the history
Route may not be built even when channel is active.
Since this causes flaky test,
Added to check route construction in advance.
  • Loading branch information
YusukeShimizu committed Nov 26, 2023
1 parent e5cbb6b commit 5c30387
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
15 changes: 14 additions & 1 deletion testframework/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ func (n *CLightningNode) OpenChannel(remote LightningNode, capacity, pushAmt uin
if err != nil {
return false, fmt.Errorf("IsChannelActive() %w", err)
}
return remoteActive && localActive, nil
hasRoute, err := n.HasRoute(remote.Id(), scid)
if err != nil {
return false, nil
}
return remoteActive && localActive && hasRoute, nil
}, TIMEOUT)
if err != nil {
return "", fmt.Errorf("error waiting for active channel: %w", err)
Expand All @@ -386,6 +390,15 @@ func (n *CLightningNode) OpenChannel(remote LightningNode, capacity, pushAmt uin
return scid, nil
}

// HasRoute check the route is constructed
func (n *CLightningNode) HasRoute(remote, scid string) (bool, error) {
routes, err := n.Rpc.GetRoute(remote, 1, 1, 0, n.Info.Id, 0, nil, 1)
if err != nil {
return false, fmt.Errorf("GetRoute() %w", err)
}
return len(routes) > 0, nil
}

func (n *CLightningNode) IsBlockHeightSynced() (bool, error) {
r, err := n.bitcoin.Rpc.Call("getblockcount")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions testframework/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type LightningNode interface {
IsBlockHeightSynced() (bool, error)
IsChannelActive(scid string) (bool, error)
IsConnected(peer LightningNode) (bool, error)
HasRoute(remote, scid string) (bool, error)

AddInvoice(amtSat uint64, desc, label string) (payreq string, err error)
PayInvoice(payreq string) error
Expand Down
41 changes: 40 additions & 1 deletion testframework/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (

"github.com/elementsproject/peerswap/lightning"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
)

func getLndConfig() map[string]string {
Expand Down Expand Up @@ -375,7 +377,11 @@ func (n *LndNode) OpenChannel(peer LightningNode, capacity, pushAmt uint64, conn
return false, fmt.Errorf("IsChannelActive() %w", err)
}
}
return remoteActive && localActive, nil
hasRoute, err := n.HasRoute(peer.Id(), scid)
if err != nil {
return false, nil
}
return remoteActive && localActive && hasRoute, nil
}, TIMEOUT)
if err != nil {
return "", fmt.Errorf("error waiting for active channel: %w", err)
Expand All @@ -390,6 +396,39 @@ func (n *LndNode) OpenChannel(peer LightningNode, capacity, pushAmt uint64, conn
return scid, nil
}

// HasRoute check the route is constructed
func (n *LndNode) HasRoute(remote, scid string) (bool, error) {
chsRes, err := n.Rpc.ListChannels(context.Background(), &lnrpc.ListChannelsRequest{})
if err != nil {
return false, fmt.Errorf("ListChannels() %w", err)
}
var channel *lnrpc.Channel
for _, ch := range chsRes.GetChannels() {
channelShortId := lnwire.NewShortChanIDFromInt(ch.ChanId)
if channelShortId.String() == lightning.Scid(scid).LndStyle() {
channel = ch
}
}
if channel.GetChanId() == 0 {
return false, fmt.Errorf("could not find a channel with scid: %s", scid)
}
v, err := route.NewVertexFromStr(channel.GetRemotePubkey())
if err != nil {
return false, fmt.Errorf("NewVertexFromStr() %w", err)
}

routeres, err := n.RpcV2.BuildRoute(context.Background(), &routerrpc.BuildRouteRequest{
AmtMsat: channel.GetLocalBalance() * 1000 / 2,
FinalCltvDelta: 9,
OutgoingChanId: channel.GetChanId(),
HopPubkeys: [][]byte{v[:]},
})
if err != nil {
return false, fmt.Errorf("BuildRoute() %w", err)
}
return len(routeres.GetRoute().Hops) > 0, nil
}

func (n *LndNode) IsBlockHeightSynced() (bool, error) {
r, err := n.bitcoin.Rpc.Call("getblockcount")
if err != nil {
Expand Down

0 comments on commit 5c30387

Please sign in to comment.