cmd/lncli: implement async+sync calls to [open|close]channel

This commit is contained in:
Olaoluwa Osuntokun 2016-07-07 15:35:06 -07:00
parent 07166fe88b
commit a4e92fac8e
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
@ -191,6 +192,10 @@ var OpenChannelCommand = cli.Command{
Usage: "the number of confirmations required before the " + Usage: "the number of confirmations required before the " +
"channel is considered 'open'", "channel is considered 'open'",
}, },
cli.BoolFlag{
Name: "block",
Usage: "block and wait until the channel is fully open",
},
}, },
Action: openChannel, Action: openChannel,
} }
@ -207,23 +212,38 @@ func openChannel(ctx *cli.Context) error {
NumConfs: uint32(ctx.Int("num_confs")), NumConfs: uint32(ctx.Int("num_confs")),
} }
resp, err := client.OpenChannel(ctxb, req) stream, err := client.OpenChannel(ctxb, req)
if err != nil { if err != nil {
return err return err
} }
txid, err := wire.NewShaHash(resp.ChannelPoint.FundingTxid) if !ctx.Bool("block") {
if err != nil { return nil
return err }
for {
resp, err := stream.Recv()
if err == io.EOF {
return nil
} else if err != nil {
return err
}
txid, err := wire.NewShaHash(resp.ChannelPoint.FundingTxid)
if err != nil {
return err
}
index := resp.ChannelPoint.OutputIndex
printRespJson(struct {
ChannelPoint string `json:"channel_point"`
}{
ChannelPoint: fmt.Sprintf("%v:%v", txid, index),
},
)
} }
index := resp.ChannelPoint.OutputIndex
printRespJson(struct {
ChannelPoint string `json:"channel_point"`
}{
ChannelPoint: fmt.Sprintf("%v:%v", txid, index),
},
)
return nil return nil
} }
@ -253,6 +273,10 @@ var CloseChannelCommand = cli.Command{
Usage: "after the time limit has passed, attempted an " + Usage: "after the time limit has passed, attempted an " +
"uncooperative closure", "uncooperative closure",
}, },
cli.BoolFlag{
Name: "block",
Usage: "block until the channel is closed",
},
}, },
Action: closeChannel, Action: closeChannel,
} }
@ -273,12 +297,25 @@ func closeChannel(ctx *cli.Context) error {
}, },
} }
resp, err := client.CloseChannel(ctxb, req) stream, err := client.CloseChannel(ctxb, req)
if err != nil { if err != nil {
return err return err
} }
printRespJson(resp) if !ctx.Bool("block") {
return nil
}
for {
resp, err := stream.Recv()
if err == io.EOF {
return nil
} else if err != nil {
return err
}
printRespJson(resp)
}
return nil return nil
} }