Skip to content

Commit

Permalink
Merge 907d6bd into 55e0dd3
Browse files Browse the repository at this point in the history
  • Loading branch information
afederigo committed Apr 6, 2017
2 parents 55e0dd3 + 907d6bd commit 2968080
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 480 deletions.
31 changes: 31 additions & 0 deletions cmd/lncli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,37 @@ func connectPeer(ctx *cli.Context) error {
return nil
}

var disconnectCommand = cli.Command{
Name: "disconnect",
Usage: "disconnect a remote lnd peer identified by pubKey",
ArgsUsage: "<pubkey>",
Action: disconnectPeer,
}

func disconnectPeer(ctx *cli.Context) error {
ctxb := context.Background()
client, cleanUp := getClient(ctx)
defer cleanUp()

pubKey := ctx.Args().First()
if pubKey == "" {
return fmt.Errorf("target address expected in format: " +
"pubkey")
}

req := &lnrpc.DisconnectPeerRequest{
PubKey: pubKey,
}

lnid, err := client.DisconnectPeer(ctxb, req)
if err != nil {
return err
}

printRespJSON(lnid)
return nil
}

// TODO(roasbeef): change default number of confirmations
var openChannelCommand = cli.Command{
Name: "openchannel",
Expand Down
1 change: 1 addition & 0 deletions cmd/lncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func main() {
debugLevelCommand,
decodePayReqComamnd,
listChainTxnsCommand,
disconnectCommand,
}

if err := app.Run(os.Args); err != nil {
Expand Down
56 changes: 56 additions & 0 deletions lnd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,29 @@ func assertNumChannelsPending(ctxt context.Context, t *harnessTest,
}
}

//assertNumConnections asserts number current connections between two peers
func assertNumConnections(ctxt context.Context, t *harnessTest,
alice, bob *lightningNode, expected int) {
aliceNumPeers, err := alice.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
if err != nil {
t.Fatalf("unable to fetch alice's node (%v) list peers %v",
alice.nodeID, err)
}
bobNumPeers, err := bob.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
if err != nil {
t.Fatalf("unable to fetch bob's node (%v) list peers %v",
bob.nodeID, err)
}
if len(aliceNumPeers.Peers) != expected {
t.Fatalf("number of peers connected to alice is incorrect: expected %v, got %v",
expected, len(aliceNumPeers.Peers))
}
if len(bobNumPeers.Peers) != expected {
t.Fatalf("number of peers connected to bob is incorrect: expected %v, got %v",
expected, len(bobNumPeers.Peers))
}
}

// testBasicChannelFunding performs a test exercising expected behavior from a
// basic funding workflow. The test creates a new channel between Alice and
// Bob, then immediately closes the channel after asserting some expected post
Expand Down Expand Up @@ -291,6 +314,35 @@ func testBasicChannelFunding(net *networkHarness, t *harnessTest) {
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
}

// testDisconnectingTargetPeer performs a test which
// disconnects Alice-peer from Bob-peer and then re-connects them again
func testDisconnectingTargetPeer(net *networkHarness, t *harnessTest) {

ctxb := context.Background()
timeout := time.Duration(time.Second * 10)
ctxt, _ := context.WithTimeout(ctxb, timeout)

// Check existing connection.
assertNumConnections(ctxb, t, net.Alice, net.Bob, 1)

// Disconnect Alice-peer from Bob-peer.
err := net.DisconnectNodes(ctxt, net.Alice, net.Bob)
if err != nil {
t.Fatalf("unable to disconnect Bob's peer from Alice's: err %v", err)
}

// Check zero peer connections.
assertNumConnections(ctxb, t, net.Alice, net.Bob, 0)

// Finally, re-connect both nodes.
if err := net.ConnectNodes(ctxt, net.Alice, net.Bob); err != nil {
t.Fatalf("unable to connect Alice's peer to Bob's: err %v", err)
}

// Check existing connection.
assertNumConnections(ctxb, t, net.Alice, net.Bob, 1)
}

// testFundingPersistence is intended to ensure that the Funding Manager
// persists the state of new channels prior to broadcasting the channel's
// funding transaction. This ensures that the daemon maintains an up-to-date
Expand Down Expand Up @@ -2143,6 +2195,10 @@ var testsCases = []*testCase{
name: "basic funding flow",
test: testBasicChannelFunding,
},
{
name: "disconnecting target peer",
test: testDisconnectingTargetPeer,
},
{
name: "graph topology notifications",
test: testGraphTopologyNotifications,
Expand Down
2 changes: 2 additions & 0 deletions lnrpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ description):
nested-pay-to-witness-key-hash (np2wkh).
* ConnectPeer
* Connects to a peer identified by a public key and host.
* DisconnectPeer
* Disconnects a peer identified by a public key.
* ListPeers
* Lists all available connected peers.
* GetInfo
Expand Down

0 comments on commit 2968080

Please sign in to comment.