Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP implement solo machine client #6115

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions x/ibc/02-client/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ type Header interface {
GetHeight() uint64
}

// message types for the IBC client
const (
TypeMsgCreateClient string = "create_client"
TypeMsgUpdateClient string = "update_client"
TypeMsgSubmitClientMisbehaviour string = "submit_client_misbehaviour"
)

// MsgCreateClient defines the msg interface that the
// CreateClient Handler expects
type MsgCreateClient interface {
Expand All @@ -154,12 +161,14 @@ type ClientType byte
const (
Tendermint ClientType = iota + 1 // 1
Localhost
SoloMachine
Comment on lines 161 to +164
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally they should be sorted according to their ICS number imo

Suggested change
const (
Tendermint ClientType = iota + 1 // 1
Localhost
SoloMachine
const (
SoloMachine ClientType = iota + 1 // 1
Tendermint
Localhost

)

// string representation of the client types
const (
ClientTypeTendermint string = "tendermint"
ClientTypeLocalHost string = "localhost"
ClientTypeTendermint string = "tendermint"
ClientTypeLocalHost string = "localhost"
ClientTypeSoloMachine string = "solomachine"
)

func (ct ClientType) String() string {
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/02-client/keeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.H
return nil, sdkerrors.Wrapf(types.ErrClientNotFound, "cannot update client with ID %s", clientID)
}

// addittion to spec: prevent update if the client is frozen
// addition to spec: prevent update if the client is frozen
if clientState.IsFrozen() {
return nil, sdkerrors.Wrapf(types.ErrClientFrozen, "cannot update client with ID %s", clientID)
}
Expand Down
41 changes: 41 additions & 0 deletions x/ibc/06-solomachine/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package solomachine

// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/ibc/06-solomachine/types/

import (
"github.com/cosmos/cosmos-sdk/x/ibc/06-solomachine/types"
)

const (
SubModuleName = types.SubModuleName
)

var (
// functions aliases
InitializeFromMsg = types.InitializeFromMsg
Initialize = types.Initialize
RegisterCodec = types.RegisterCodec
SetSubModuleCodec = types.SetSubModuleCodec
NewMsgCreateClient = types.NewMsgCreateClient
NewMsgUpdateClient = types.NewMsgUpdateClient
NewMsgSubmitClientMisbehaviour = types.NewMsgSubmitClientMisbehaviour

// variable aliases
SubModuleCdc = types.SubModuleCdc
ErrInvalidHeader = types.ErrInvalidHeader
ErrInvalidSequence = types.ErrInvalidSequence
)

type (
ClientState = types.ClientState
ConsensusState = types.ConsensusState
Evidence = types.Evidence
SignatureAndData = types.SignatureAndData
Header = types.Header
MsgCreateClient = types.MsgCreateClient
MsgUpdateClient = types.MsgUpdateClient
MsgSubmitClientMisbehaviour = types.MsgSubmitClientMisbehaviour
)
28 changes: 28 additions & 0 deletions x/ibc/06-solomachine/client/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cli

import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
)

// GetTxCmd returns the transaction commands for IBC clients
func GetTxCmd(cdc *codec.Codec, storeKey string) *cobra.Command {
ics06SoloMachineTxCmd := &cobra.Command{
Use: "solomachine",
Short: "Solo Machine transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

ics06SoloMachineTxCmd.AddCommand(flags.PostCommands(
GetCmdCreateClient(cdc),
GetCmdUpdateClient(cdc),
GetCmdSubmitMisbehaviour(cdc),
)...)

return ics06SoloMachineTxCmd
}
149 changes: 149 additions & 0 deletions x/ibc/06-solomachine/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package cli

import (
"bufio"
"fmt"
"io/ioutil"
"strings"

"github.com/pkg/errors"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
ibcsmtypes "github.com/cosmos/cosmos-sdk/x/ibc/06-solomachine/types"
)

// GetCmdCreateClient defines the command to create a new IBC Client.
func GetCmdCreateClient(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create [client-id] [path/to/consensus_state.json]",
Short: "create new client with a consensus state",
Long: strings.TrimSpace(fmt.Sprintf(` create new client with specified identifier and consensus state:

Example:
$ %s tx ibc client create [client-id] [path/to/consensus_state.json] --from node0 --home ../node0/<app>cli --chain-id $CID
`, version.ClientName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc).WithBroadcastMode(flags.BroadcastBlock)

clientID := args[0]

var consensusState ibcsmtypes.ConsensusState
if err := cdc.UnmarshalJSON([]byte(args[1]), &consensusState); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &consensusState); err != nil {
return errors.Wrap(err, "error unmarshalling consensus header file")
}
}

msg := ibcsmtypes.NewMsgCreateClient(clientID, consensusState)

if err := msg.ValidateBasic(); err != nil {
return err
}

return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}

// GetCmdUpdateClient defines the command to update a client.
func GetCmdUpdateClient(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "update [client-id] [path/to/header.json]",
Short: "update existing client with a header",
Long: strings.TrimSpace(fmt.Sprintf(`update existing client with a header:

Example:
$ %s tx ibc client update [client-id] [path/to/header.json] --from node0 --home ../node0/<app>cli --chain-id $CID
`, version.ClientName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

clientID := args[0]

var header ibcsmtypes.Header
if err := cdc.UnmarshalJSON([]byte(args[1]), &header); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &header); err != nil {
return errors.Wrap(err, "error unmarshalling header file")
}
}

msg := ibcsmtypes.NewMsgUpdateClient(clientID, header)
if err := msg.ValidateBasic(); err != nil {
return err
}

return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}

// GetCmdSubmitMisbehaviour defines the command to submit a misbehaviour to prevent
// future updates as defined in
// https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#misbehaviour
func GetCmdSubmitMisbehaviour(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "misbehaviour [path/to/evidence.json]",
Short: "submit a client misbehaviour",
Long: strings.TrimSpace(fmt.Sprintf(`submit a client misbehaviour to prevent future updates:

Example:
$ %s tx ibc client misbehaviour [path/to/evidence.json] --from node0 --home ../node0/<app>cli --chain-id $CID
`, version.ClientName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

var ev evidenceexported.Evidence
if err := cdc.UnmarshalJSON([]byte(args[0]), &ev); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[0])
if err != nil {
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &ev); err != nil {
return errors.Wrap(err, "error unmarshalling evidence file")
}
}

msg := ibcsmtypes.NewMsgSubmitClientMisbehaviour(ev, cliCtx.GetFromAddress())
if err := msg.ValidateBasic(); err != nil {
return err
}

return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
40 changes: 40 additions & 0 deletions x/ibc/06-solomachine/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rest

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
ibcsmtypes "github.com/cosmos/cosmos-sdk/x/ibc/06-solomachine/types"
)

// REST client flags
const (
RestClientID = "client-id"
)

// RegisterRoutes - General function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute string) {
registerTxRoutes(cliCtx, r)
}

// CreateClientReq defines the properties of a create client request's body.
type CreateClientReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ClientID string `json:"client_id" yaml:"client_id"`
ConsensusState ibcsmtypes.ConsensusState `json:"consensus_state" yaml:"consensus_state"`
}

// UpdateClientReq defines the properties of an update client request's body.
type UpdateClientReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ClientID string `json:"client_id" yaml:"client_id"`
Header ibcsmtypes.Header `json:"header" yaml:"header"`
}

// SubmitMisbehaviourReq defines the properties of a submit misbehaviour request's body.
type SubmitMisbehaviourReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Evidence evidenceexported.Evidence `json:"evidence" yaml:"evidence"`
}
Loading