Skip to content

Commit

Permalink
Merge pull request #1389 from Bluetegu/noprivate-describegraph-1037
Browse files Browse the repository at this point in the history
Add --noprivate flag to describegraph rpc to filter out private channels
  • Loading branch information
Roasbeef committed Oct 17, 2018
2 parents 7755370 + 2f3e2f9 commit 4c0ca37
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 186 deletions.
38 changes: 38 additions & 0 deletions channeldb/db_test.go
Expand Up @@ -33,3 +33,41 @@ func TestOpenWithCreate(t *testing.T) {
t.Fatalf("channeldb failed to create data directory")
}
}

// TestWipe tests that the database wipe operation completes successfully
// and that the buckets are deleted. It also checks that attempts to fetch
// information while the buckets are not set return the correct errors.
func TestWipe(t *testing.T) {
t.Parallel()

// First, create a temporary directory to be used for the duration of
// this test.
tempDirName, err := ioutil.TempDir("", "channeldb")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
defer os.RemoveAll(tempDirName)

// Next, open thereby creating channeldb for the first time.
dbPath := filepath.Join(tempDirName, "cdb")
cdb, err := Open(dbPath)
if err != nil {
t.Fatalf("unable to create channeldb: %v", err)
}
defer cdb.Close()

if err := cdb.Wipe(); err != nil {
t.Fatalf("unable to wipe channeldb: %v", err)
}
// Check correct errors are returned
_, err = cdb.FetchAllOpenChannels()
if err != ErrNoActiveChannels {
t.Fatalf("fetching open channels: expected '%v' instead got '%v'",
ErrNoActiveChannels, err)
}
_, err = cdb.FetchClosedChannels(false)
if err != ErrNoClosedChannels {
t.Fatalf("fetching closed channels: expected '%v' instead got '%v'",
ErrNoClosedChannels, err)
}
}
14 changes: 12 additions & 2 deletions cmd/lncli/commands.go
Expand Up @@ -2467,15 +2467,25 @@ var describeGraphCommand = cli.Command{
Category: "Peers",
Description: "Prints a human readable version of the known channel " +
"graph from the PoV of the node",
Usage: "Describe the network graph.",
Usage: "Describe the network graph.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "include_unannounced",
Usage: "If set, unannounced channels will be included in the " +
"graph. Unannounced channels are both private channels, and " +
"public channels that are not yet announced to the network.",
},
},
Action: actionDecorator(describeGraph),
}

func describeGraph(ctx *cli.Context) error {
client, cleanUp := getClient(ctx)
defer cleanUp()

req := &lnrpc.ChannelGraphRequest{}
req := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: ctx.Bool("include_unannounced"),
}

graph, err := client.DescribeGraph(context.Background(), req)
if err != nil {
Expand Down
156 changes: 146 additions & 10 deletions lnd_test.go
Expand Up @@ -870,7 +870,9 @@ func assertChannelPolicy(t *harnessTest, node *lntest.HarnessNode,
advertisingNode string, expectedPolicy *lnrpc.RoutingPolicy,
chanPoints ...*lnrpc.ChannelPoint) {

descReq := &lnrpc.ChannelGraphRequest{}
descReq := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: true,
}
chanGraph, err := node.DescribeGraph(context.Background(), descReq)
if err != nil {
t.Fatalf("unable to query for alice's graph: %v", err)
Expand Down Expand Up @@ -1496,7 +1498,9 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
}

// Alice should now have 1 edge in her graph.
req := &lnrpc.ChannelGraphRequest{}
req := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: true,
}
chanGraph, err := net.Alice.DescribeGraph(ctxb, req)
if err != nil {
t.Fatalf("unable to query for alice's routing table: %v", err)
Expand Down Expand Up @@ -1538,7 +1542,9 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {

// Since the fundingtx was reorged out, Alice should now have no edges
// in her graph.
req = &lnrpc.ChannelGraphRequest{}
req = &lnrpc.ChannelGraphRequest{
IncludeUnannounced: true,
}
chanGraph, err = net.Alice.DescribeGraph(ctxb, req)
if err != nil {
t.Fatalf("unable to query for alice's routing table: %v", err)
Expand Down Expand Up @@ -4122,6 +4128,116 @@ func testSendToRouteErrorPropagation(net *lntest.NetworkHarness, t *harnessTest)
closeChannelAndAssert(ctxt, t, net, carol, chanPointCarol, false)
}

// testUnannouncedChannels checks unannounced channels are not returned by
// describeGraph RPC request unless explicity asked for.
func testUnannouncedChannels(net *lntest.NetworkHarness, t *harnessTest) {
timeout := time.Duration(time.Second * 5)
ctb := context.Background()
amount := maxBtcFundingAmount

// Open a channel between Alice and Bob, ensuring the
// channel has been opened properly.
ctx, _ := context.WithTimeout(ctb, timeout)
chanOpenUpdate, err := net.OpenChannel(
ctx, net.Alice, net.Bob,
lntest.OpenChannelParams{
Amt: amount,
},
)
if err != nil {
t.Fatalf("unable to open channel: %v", err)
}

// Mine 2 blocks, and check that the channel is opened but not yet
// announced to the network.
mineBlocks(t, net, 2)

// One block is enough to make the channel ready for use, since the
// nodes have defaultNumConfs=1 set.
ctx, _ = context.WithTimeout(ctb, timeout)
fundingChanPoint, err := net.WaitForChannelOpen(ctx, chanOpenUpdate)
if err != nil {
t.Fatalf("error while waiting for channel open: %v", err)
}

// Alice should have 1 edge in her graph.
req := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: true,
}
ctx, _ = context.WithTimeout(ctb, timeout)
chanGraph, err := net.Alice.DescribeGraph(ctx, req)
if err != nil {
t.Fatalf("unable to query alice's graph: %v", err)
}

numEdges := len(chanGraph.Edges)
if numEdges != 1 {
t.Fatalf("expected to find 1 edge in the graph, found %d", numEdges)
}

// Channels should not be announced yet, hence Alice should have no
// announced edges in her graph.
req.IncludeUnannounced = false
ctx, _ = context.WithTimeout(ctb, timeout)
chanGraph, err = net.Alice.DescribeGraph(ctx, req)
if err != nil {
t.Fatalf("unable to query alice's graph: %v", err)
}

numEdges = len(chanGraph.Edges)
if numEdges != 0 {
t.Fatalf("expected to find 0 announced edges in the graph, found %d",
numEdges)
}

// Mine 4 more blocks, and check that the channel is now announced.
mineBlocks(t, net, 4)

// Give the network a chance to learn that auth proof is confirmed.
var predErr error
err = lntest.WaitPredicate(func() bool {
// The channel should now be announced. Check that Alice has 1
// announced edge.
req.IncludeUnannounced = false
ctx, _ = context.WithTimeout(ctb, timeout)
chanGraph, err = net.Alice.DescribeGraph(ctx, req)
if err != nil {
predErr = fmt.Errorf("unable to query alice's graph: %v", err)
return false
}

numEdges = len(chanGraph.Edges)
if numEdges != 1 {
predErr = fmt.Errorf("expected to find 1 announced edge in "+
"the graph, found %d", numEdges)
return false
}
return true
}, time.Second*15)
if err != nil {
t.Fatalf("%v", predErr)
}

// The channel should now be announced. Check that Alice has 1 announced
// edge.
req.IncludeUnannounced = false
ctx, _ = context.WithTimeout(ctb, timeout)
chanGraph, err = net.Alice.DescribeGraph(ctx, req)
if err != nil {
t.Fatalf("unable to query alice's graph: %v", err)
}

numEdges = len(chanGraph.Edges)
if numEdges != 1 {
t.Fatalf("expected to find 1 announced edge in the graph, found %d",
numEdges)
}

// Close the channel used during the test.
ctx, _ = context.WithTimeout(ctb, timeout)
closeChannelAndAssert(ctx, t, net, net.Alice, fundingChanPoint, false)
}

// testPrivateChannels tests that a private channel can be used for
// routing by the two endpoints of the channel, but is not known by
// the rest of the nodes in the graph.
Expand Down Expand Up @@ -4432,8 +4548,10 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
// nodes know about. Carol and Alice should know about 4, while
// Bob and Dave should only know about 3, since one channel is
// private.
numChannels := func(node *lntest.HarnessNode) int {
req := &lnrpc.ChannelGraphRequest{}
numChannels := func(node *lntest.HarnessNode, includeUnannounced bool) int {
req := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: includeUnannounced,
}
ctxt, _ := context.WithTimeout(ctxb, timeout)
chanGraph, err := node.DescribeGraph(ctxt, req)
if err != nil {
Expand All @@ -4444,25 +4562,37 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {

var predErr error
err = lntest.WaitPredicate(func() bool {
aliceChans := numChannels(net.Alice)
aliceChans := numChannels(net.Alice, true)
if aliceChans != 4 {
predErr = fmt.Errorf("expected Alice to know 4 edges, "+
"had %v", aliceChans)
return false
}
bobChans := numChannels(net.Bob)
alicePubChans := numChannels(net.Alice, false)
if alicePubChans != 3 {
predErr = fmt.Errorf("expected Alice to know 3 public edges, "+
"had %v", alicePubChans)
return false
}
bobChans := numChannels(net.Bob, true)
if bobChans != 3 {
predErr = fmt.Errorf("expected Bob to know 3 edges, "+
"had %v", bobChans)
return false
}
carolChans := numChannels(carol)
carolChans := numChannels(carol, true)
if carolChans != 4 {
predErr = fmt.Errorf("expected Carol to know 4 edges, "+
"had %v", carolChans)
return false
}
daveChans := numChannels(dave)
carolPubChans := numChannels(carol, false)
if carolPubChans != 3 {
predErr = fmt.Errorf("expected Carol to know 3 public edges, "+
"had %v", carolPubChans)
return false
}
daveChans := numChannels(dave, true)
if daveChans != 3 {
predErr = fmt.Errorf("expected Dave to know 3 edges, "+
"had %v", daveChans)
Expand Down Expand Up @@ -5781,7 +5911,9 @@ func testGarbageCollectLinkNodes(net *lntest.NetworkHarness, t *harnessTest) {

// Finally, we'll ensure that Bob and Carol no longer show in Alice's
// channel graph.
describeGraphReq := &lnrpc.ChannelGraphRequest{}
describeGraphReq := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: true,
}
channelGraph, err := net.Alice.DescribeGraph(ctxb, describeGraphReq)
if err != nil {
t.Fatalf("unable to query for alice's channel graph: %v", err)
Expand Down Expand Up @@ -12184,6 +12316,10 @@ var testsCases = []*testCase{
name: "send to route error propagation",
test: testSendToRouteErrorPropagation,
},
{
name: "unannounced channels",
test: testUnannouncedChannels,
},
{
name: "private channels",
test: testPrivateChannels,
Expand Down

0 comments on commit 4c0ca37

Please sign in to comment.