Skip to content

Commit

Permalink
add client query/tx
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Jul 18, 2019
1 parent 2681df2 commit f50c833
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 85 deletions.
37 changes: 31 additions & 6 deletions x/ibc/04-channel/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type CLIObject struct {
Cdc *codec.Codec
}

func (man Manager) CLIObject(ctx context.CLIContext, path merkle.Path, chanid, connid string) CLIObject {
func (man Manager) cliObject(path merkle.Path, chanid, connid string) CLIObject {
obj := man.object(connid, chanid)
return CLIObject{
ChanID: chanid,
Expand All @@ -37,13 +37,23 @@ func (man Manager) CLIObject(ctx context.CLIContext, path merkle.Path, chanid, c
return obj.packets.Value(index).Key()
},

Connection: man.connection.CLIObject(ctx, path, connid),

Path: path,
Cdc: obj.channel.Cdc(),
}
}

func (man Manager) CLIQuery(ctx context.CLIContext, path merkle.Path, chanid, connid string) CLIObject {
obj := man.cliObject(path, chanid, connid)
obj.Connection = man.connection.CLIQuery(ctx, path, connid)
return obj
}

func (man Manager) CLIObject(path merkle.Path, chanid, connid, clientid string) CLIObject {
obj := man.cliObject(path, chanid, connid)
obj.Connection = man.connection.CLIObject(path, connid, clientid)
return obj
}

func (obj CLIObject) query(ctx context.CLIContext, key []byte, ptr interface{}) (merkle.Proof, error) {
resp, err := ctx.QueryABCI(obj.Path.RequestQuery(key))
if err != nil {
Expand All @@ -63,6 +73,11 @@ func (obj CLIObject) Channel(ctx context.CLIContext) (res Channel, proof merkle.
return
}

func (obj CLIObject) Available(ctx context.CLIContext) (res bool, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.AvailableKey, &res)
return
}

func (obj CLIObject) SeqSend(ctx context.CLIContext) (res uint64, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.SeqSendKey, &res)
return
Expand All @@ -85,10 +100,20 @@ type CLIHandshakeObject struct {
TimeoutKey []byte
}

func (man Handshaker) CLIObject(ctx context.CLIContext, path merkle.Path, chanid, connid string) CLIHandshakeObject {
func (man Handshaker) CLIQuery(ctx context.CLIContext, path merkle.Path, chanid, connid string) CLIHandshakeObject {
obj := man.object(man.man.object(connid, chanid))
return CLIHandshakeObject{
CLIObject: man.man.CLIQuery(ctx, path, chanid, connid),

StateKey: obj.state.Key(),
TimeoutKey: obj.nextTimeout.Key(),
}
}

func (man Handshaker) CLIObject(path merkle.Path, chanid, connid, clientid string) CLIHandshakeObject {
obj := man.object(man.man.object(connid, chanid))
return CLIHandshakeObject{
CLIObject: man.man.CLIObject(ctx, path, chanid, connid),
CLIObject: man.man.CLIObject(path, chanid, connid, clientid),

StateKey: obj.state.Key(),
TimeoutKey: obj.nextTimeout.Key(),
Expand All @@ -100,7 +125,7 @@ func (obj CLIHandshakeObject) State(ctx context.CLIContext) (res State, proof me
return
}

func (obj CLIHandshakeObject) Timeout(ctx context.CLIContext) (res uint64, proof merkle.Proof, err error) {
func (obj CLIHandshakeObject) NextTimeout(ctx context.CLIContext) (res uint64, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.TimeoutKey, &res)
return
}
32 changes: 24 additions & 8 deletions x/ibc/04-channel/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ import (

"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/client/utils"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection"
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel"
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel/client/utils"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
)

const (
FlagProve = "prove"
)

func object(ctx context.CLIContext, cdc *codec.Codec, storeKey string, version int64, id string) channel.CLIObject {
func object(ctx context.CLIContext, cdc *codec.Codec, storeKey string, version int64, connid, chanid string) channel.CLIObject {
prefix := []byte(strconv.FormatInt(version, 10) + "/")
path := merkle.NewPath([][]byte{[]byte(storeKey)}, prefix)
base := state.NewBase(cdc, sdk.NewKVStoreKey(storeKey), prefix)
climan := client.NewManager(base)
man := channel.NewManager(base, climan)
return man.CLIObject(ctx, path, id)
connman := connection.NewManager(base, climan)
man := channel.NewManager(base, connman)
return man.CLIQuery(ctx, path, connid, chanid)
}

func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
Expand All @@ -56,7 +58,18 @@ func QueryChannel(ctx context.CLIContext, obj channel.CLIObject, prove bool) (re
if err != nil {
return
}
kind, kindp, err := obj.Kind(ctx)
/*
kind, kindp, err := obj.Kind(ctx)
if err != nil {
return
}
*/
seqsend, seqsendp, err := obj.SeqSend(ctx)
if err != nil {
return
}

seqrecv, seqrecvp, err := obj.SeqRecv(ctx)
if err != nil {
return
}
Expand All @@ -65,14 +78,17 @@ func QueryChannel(ctx context.CLIContext, obj channel.CLIObject, prove bool) (re
return utils.NewJSONObject(
conn, connp,
avail, availp,
kind, kindp,
// kind, kindp,
seqsend, seqsendp,
seqrecv, seqrecvp,
), nil
}

return utils.NewJSONObject(
conn, nil,
avail, nil,
kind, nil,
seqsend, nil,
seqrecv, nil,
), nil
}

Expand All @@ -83,7 +99,7 @@ func GetCmdQueryChannel(storeKey string, cdc *codec.Codec) *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
obj := object(ctx, cdc, storeKey, ibc.Version, args[0])
obj := object(ctx, cdc, storeKey, ibc.Version, args[0], args[1])
jsonobj, err := QueryChannel(ctx, obj, viper.GetBool(FlagProve))
if err != nil {
return err
Expand Down
79 changes: 38 additions & 41 deletions x/ibc/04-channel/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection"
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
)
Expand All @@ -33,13 +34,14 @@ const (
FlagFrom2 = "from2"
)

func handshake(ctx context.CLIContext, cdc *codec.Codec, storeKey string, version int64, id string) connection.CLIHandshakeObject {
func handshake(ctx context.CLIContext, cdc *codec.Codec, storeKey string, version int64, connid, chanid string) channel.CLIHandshakeObject {
prefix := []byte(strconv.FormatInt(version, 10) + "/")
path := merkle.NewPath([][]byte{[]byte(storeKey)}, prefix)
base := state.NewBase(cdc, sdk.NewKVStoreKey(storeKey), prefix)
climan := client.NewManager(base)
man := connection.NewHandshaker(connection.NewManager(base, climan))
return man.CLIObject(ctx, path, id)
connman := connection.NewManager(base, climan)
man := channel.NewHandshaker(channel.NewManager(base, connman))
return man.CLIQuery(ctx, path, connid, chanid)
}

func lastheight(ctx context.CLIContext) (uint64, error) {
Expand All @@ -56,12 +58,12 @@ func lastheight(ctx context.CLIContext) (uint64, error) {
return uint64(info.Response.LastBlockHeight), nil
}

func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command {
func GetCmdChannelHandshake(storeKey string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "handshake",
Short: "initiate connection handshake between two chains",
Short: "initiate channel handshake between two chains",
Args: cobra.ExactArgs(4),
// Args: []string{connid1, connfilepath1, connid2, connfilepath2}
// Args: []string{connid1, chanid1, chanfilepath1, connid2, chanid2, chanfilepath2}
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
ctx1 := context.NewCLIContext().
Expand All @@ -75,41 +77,43 @@ func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command
WithFrom(viper.GetString(FlagFrom2))

conn1id := args[0]
conn1bz, err := ioutil.ReadFile(args[1])
chan1id := args[1]
conn1bz, err := ioutil.ReadFile(args[2])
if err != nil {
return err
}
var conn1 connection.Connection
var conn1 channel.Channel
if err := cdc.UnmarshalJSON(conn1bz, &conn1); err != nil {
return err
}

obj1 := handshake(ctx1, cdc, storeKey, ibc.Version, conn1id)
obj1 := handshake(ctx1, cdc, storeKey, ibc.Version, conn1id, chan1id)

conn2id := args[2]
conn2bz, err := ioutil.ReadFile(args[3])
conn2id := args[3]
chan2id := args[4]
conn2bz, err := ioutil.ReadFile(args[5])
if err != nil {
return err
}
var conn2 connection.Connection
var conn2 channel.Channel
if err := cdc.UnmarshalJSON(conn2bz, &conn2); err != nil {
return err
}

obj2 := handshake(ctx2, cdc, storeKey, ibc.Version, conn1id)
obj2 := handshake(ctx2, cdc, storeKey, ibc.Version, conn1id, chan1id)

// TODO: check state and if not Idle continue existing process
height, err := lastheight(ctx2)
if err != nil {
return err
}
nextTimeout := height + 1000 // TODO: parameterize
msginit := connection.MsgOpenInit{
ConnectionID: conn1id,
Connection: conn1,
CounterpartyClient: conn2.Client,
NextTimeout: nextTimeout,
Signer: ctx1.GetFromAddress(),
msginit := channel.MsgOpenInit{
ConnectionID: conn1id,
ChannelID: chan1id,
Channel: conn1,
NextTimeout: nextTimeout,
Signer: ctx1.GetFromAddress(),
}

err = utils.GenerateOrBroadcastMsgs(ctx1, txBldr, []sdk.Msg{msginit})
Expand All @@ -123,7 +127,7 @@ func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command
return err
}
nextTimeout = height + 1000
_, pconn, err := obj1.Connection(ctx1)
_, pconn, err := obj1.Channel(ctx1)
if err != nil {
return err
}
Expand All @@ -135,19 +139,15 @@ func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command
if err != nil {
return err
}
_, pcounter, err := obj1.CounterpartyClient(ctx1)
if err != nil {
return err
}

msgtry := connection.MsgOpenTry{
ConnectionID: conn2id,
Connection: conn2,
CounterpartyClient: conn1.Client,
Timeout: timeout,
NextTimeout: nextTimeout,
Proofs: []commitment.Proof{pconn, pstate, ptimeout, pcounter},
Signer: ctx2.GetFromAddress(),
msgtry := channel.MsgOpenTry{
ConnectionID: conn2id,
ChannelID: chan2id,
Channel: conn2,
Timeout: timeout,
NextTimeout: nextTimeout,
Proofs: []commitment.Proof{pconn, pstate, ptimeout},
Signer: ctx2.GetFromAddress(),
}

err = utils.GenerateOrBroadcastMsgs(ctx2, txBldr, []sdk.Msg{msgtry})
Expand All @@ -161,7 +161,7 @@ func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command
return err
}
nextTimeout = height + 1000
_, pconn, err = obj2.Connection(ctx2)
_, pconn, err = obj2.Channel(ctx2)
if err != nil {
return err
}
Expand All @@ -173,16 +173,12 @@ func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command
if err != nil {
return err
}
_, pcounter, err = obj2.CounterpartyClient(ctx2)
if err != nil {
return err
}

msgack := connection.MsgOpenAck{
msgack := channel.MsgOpenAck{
ConnectionID: conn1id,
ChannelID: chan1id,
Timeout: timeout,
NextTimeout: nextTimeout,
Proofs: []commitment.Proof{pconn, pstate, ptimeout, pcounter},
Proofs: []commitment.Proof{pconn, pstate, ptimeout},
Signer: ctx1.GetFromAddress(),
}

Expand All @@ -201,8 +197,9 @@ func GetCmdConnectionHandshake(storeKey string, cdc *codec.Codec) *cobra.Command
return err
}

msgconfirm := connection.MsgOpenConfirm{
msgconfirm := channel.MsgOpenConfirm{
ConnectionID: conn2id,
ChannelID: chan2id,
Timeout: timeout,
Proofs: []commitment.Proof{pstate, ptimeout},
Signer: ctx2.GetFromAddress(),
Expand Down
Loading

0 comments on commit f50c833

Please sign in to comment.