Skip to content

Commit

Permalink
ICS07 follow up changes (#5606)
Browse files Browse the repository at this point in the history
* ADR07 follow up changes

* add assertion checks

* update ICS02 tests

* update ICS07 tests

* add trusting and ubd period to msg

* tests

* more test updates

* ICS07 follow ups (#5631)

* refactor tendermint package to move msgs here

* fix rest of package to accomadate 07 refactor

* added GetHeight to ConsensusState and moved clientstate struct creation to 07-tendermint

* start work on making misbehavior retrieve consensusState LTE misbehavior

* allow misbehavior submission at height not equal to persisted consensusState

* optimize submitMisbehavior by erroring earlier

* cleanup misbehavior and propose lazy fix

* fix bug

* Update x/ibc/02-client/keeper/client.go

Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* address fede review

* add chain-id into clientstate

* address necessary fede review

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

Co-authored-by: Aditya <adityasripal@gmail.com>
  • Loading branch information
fedekunze and AdityaSripal authored Feb 18, 2020
1 parent 5875a96 commit efb28d7
Show file tree
Hide file tree
Showing 55 changed files with 1,398 additions and 877 deletions.
8 changes: 2 additions & 6 deletions x/ibc/02-client/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ var (
ErrRootNotFound = types.ErrRootNotFound
ErrInvalidHeader = types.ErrInvalidHeader
ErrInvalidEvidence = types.ErrInvalidEvidence
NewMsgCreateClient = types.NewMsgCreateClient
NewMsgUpdateClient = types.NewMsgUpdateClient

// variable aliases
SubModuleCdc = types.SubModuleCdc
Expand All @@ -48,8 +46,6 @@ var (
)

type (
Keeper = keeper.Keeper
StakingKeeper = types.StakingKeeper
MsgCreateClient = types.MsgCreateClient
MsgUpdateClient = types.MsgUpdateClient
Keeper = keeper.Keeper
StakingKeeper = types.StakingKeeper
)
18 changes: 0 additions & 18 deletions x/ibc/02-client/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,3 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
)...)
return ics02ClientQueryCmd
}

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

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

return ics02ClientTxCmd
}
23 changes: 0 additions & 23 deletions x/ibc/02-client/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ 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"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
)

// REST client flags
Expand All @@ -18,24 +15,4 @@ const (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute string) {
registerQueryRoutes(cliCtx, r)
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 exported.ConsensusState `json:"consensus_state" yaml:"consensus_state"`
}

// UpdateClientReq defines the properties of a update client request's body.
type UpdateClientReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Header exported.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"`
}
29 changes: 3 additions & 26 deletions x/ibc/02-client/client/rest/swagger.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package rest

import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/evidence"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
tendermint "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
)

Expand All @@ -17,7 +15,7 @@ type (

QueryHeader struct {
Height int64 `json:"height"`
Result tendermint.Header `json:"result"`
Result ibctmtypes.Header `json:"result"`
}

QueryClientState struct {
Expand All @@ -27,32 +25,11 @@ type (

QueryNodeConsensusState struct {
Height int64 `json:"height"`
Result tendermint.ConsensusState `json:"result"`
Result ibctmtypes.ConsensusState `json:"result"`
}

QueryPath struct {
Height int64 `json:"height"`
Result commitment.Prefix `json:"result"`
}

PostCreateClient struct {
Msgs []types.MsgCreateClient `json:"msg" yaml:"msg"`
Fee authtypes.StdFee `json:"fee" yaml:"fee"`
Signatures []authtypes.StdSignature `json:"signatures" yaml:"signatures"`
Memo string `json:"memo" yaml:"memo"`
}

PostUpdateClient struct {
Msgs []types.MsgUpdateClient `json:"msg" yaml:"msg"`
Fee authtypes.StdFee `json:"fee" yaml:"fee"`
Signatures []authtypes.StdSignature `json:"signatures" yaml:"signatures"`
Memo string `json:"memo" yaml:"memo"`
}

PostSubmitMisbehaviour struct {
Msgs []evidence.MsgSubmitEvidence `json:"msg" yaml:"msg"`
Fee authtypes.StdFee `json:"fee" yaml:"fee"`
Signatures []authtypes.StdSignature `json:"signatures" yaml:"signatures"`
Memo string `json:"memo" yaml:"memo"`
}
)
31 changes: 16 additions & 15 deletions x/ibc/02-client/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
tendermint "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types"
)
Expand Down Expand Up @@ -91,30 +91,30 @@ func QueryConsensusState(

// QueryTendermintHeader takes a client context and returns the appropriate
// tendermint header
func QueryTendermintHeader(cliCtx context.CLIContext) (tendermint.Header, int64, error) {
func QueryTendermintHeader(cliCtx context.CLIContext) (ibctmtypes.Header, int64, error) {
node, err := cliCtx.GetNode()
if err != nil {
return tendermint.Header{}, 0, err
return ibctmtypes.Header{}, 0, err
}

info, err := node.ABCIInfo()
if err != nil {
return tendermint.Header{}, 0, err
return ibctmtypes.Header{}, 0, err
}

height := info.Response.LastBlockHeight

commit, err := node.Commit(&height)
if err != nil {
return tendermint.Header{}, 0, err
return ibctmtypes.Header{}, 0, err
}

validators, err := node.Validators(&height, 0, 10000)
if err != nil {
return tendermint.Header{}, 0, err
return ibctmtypes.Header{}, 0, err
}

header := tendermint.Header{
header := ibctmtypes.Header{
SignedHeader: commit.SignedHeader,
ValidatorSet: tmtypes.NewValidatorSet(validators.Validators),
}
Expand All @@ -124,32 +124,33 @@ func QueryTendermintHeader(cliCtx context.CLIContext) (tendermint.Header, int64,

// QueryNodeConsensusState takes a client context and returns the appropriate
// tendermint consensus state
func QueryNodeConsensusState(cliCtx context.CLIContext) (tendermint.ConsensusState, int64, error) {
func QueryNodeConsensusState(cliCtx context.CLIContext) (ibctmtypes.ConsensusState, int64, error) {
node, err := cliCtx.GetNode()
if err != nil {
return tendermint.ConsensusState{}, 0, err
return ibctmtypes.ConsensusState{}, 0, err
}

info, err := node.ABCIInfo()
if err != nil {
return tendermint.ConsensusState{}, 0, err
return ibctmtypes.ConsensusState{}, 0, err
}

height := info.Response.LastBlockHeight

commit, err := node.Commit(&height)
if err != nil {
return tendermint.ConsensusState{}, 0, err
return ibctmtypes.ConsensusState{}, 0, err
}

validators, err := node.Validators(&height, 0, 10000)
if err != nil {
return tendermint.ConsensusState{}, 0, err
return ibctmtypes.ConsensusState{}, 0, err
}

state := tendermint.ConsensusState{
Root: commitment.NewRoot(commit.AppHash),
ValidatorSetHash: tmtypes.NewValidatorSet(validators.Validators).Hash(),
state := ibctmtypes.ConsensusState{
Timestamp: commit.Time,
Root: commitment.NewRoot(commit.AppHash),
ValidatorSet: tmtypes.NewValidatorSet(validators.Validators),
}

return state, height, nil
Expand Down
22 changes: 22 additions & 0 deletions x/ibc/02-client/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/codec"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
connectionexported "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/exported"
Expand Down Expand Up @@ -90,6 +92,9 @@ type ClientState interface {
type ConsensusState interface {
ClientType() ClientType // Consensus kind

// GetHeight returns the height of the consensus state
GetHeight() uint64

// GetRoot returns the commitment root of the consensus state,
// which is used for key-value pair verification.
GetRoot() commitment.RootI
Expand All @@ -110,6 +115,23 @@ type Header interface {
GetHeight() uint64
}

// MsgCreateClient defines the msg interface that the
// CreateClient Handler expects
type MsgCreateClient interface {
sdk.Msg
GetClientID() string
GetClientType() string
GetConsensusState() ConsensusState
}

// MsgUpdateClient defines the msg interface that the
// UpdateClient Handler expects
type MsgUpdateClient interface {
sdk.Msg
GetClientID() string
GetHeader() Header
}

// ClientType defines the type of the consensus algorithm
type ClientType byte

Expand Down
58 changes: 42 additions & 16 deletions x/ibc/02-client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,52 @@ import (
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
)

// HandleMsgCreateClient defines the sdk.Handler for MsgCreateClient
func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg MsgCreateClient) (*sdk.Result, error) {
clientType := exported.ClientTypeFromString(msg.ClientType)
if clientType == 0 {
return nil, sdkerrors.Wrap(ErrInvalidClientType, msg.ClientType)
func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg exported.MsgCreateClient) (*sdk.Result, error) {
clientType := exported.ClientTypeFromString(msg.GetClientType())
var clientState exported.ClientState
switch clientType {
case 0:
return nil, sdkerrors.Wrap(ErrInvalidClientType, msg.GetClientType())
case exported.Tendermint:
tmMsg, ok := msg.(ibctmtypes.MsgCreateClient)
if !ok {
return nil, sdkerrors.Wrap(ErrInvalidClientType, "Msg is not a Tendermint CreateClient msg")
}
var err error
clientState, err = ibctmtypes.InitializeFromMsg(tmMsg)
if err != nil {
return nil, err
}
default:
return nil, sdkerrors.Wrap(ErrInvalidClientType, msg.GetClientType())
}

_, err := k.CreateClient(ctx, msg.ClientID, clientType, msg.ConsensusState)
_, err := k.CreateClient(
ctx, clientState, msg.GetConsensusState(),
)
if err != nil {
return nil, err
}

attributes := make([]sdk.Attribute, len(msg.GetSigners())+1)
attributes[0] = sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory)
for i, signer := range msg.GetSigners() {
attributes[i+1] = sdk.NewAttribute(sdk.AttributeKeySender, signer.String())
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
EventTypeCreateClient,
sdk.NewAttribute(AttributeKeyClientID, msg.ClientID),
sdk.NewAttribute(AttrbuteKeyClientType, msg.ClientType),
sdk.NewAttribute(AttributeKeyClientID, msg.GetClientID()),
sdk.NewAttribute(AttrbuteKeyClientType, msg.GetClientType()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Signer.String()),
attributes...,
),
})

Expand All @@ -40,22 +62,26 @@ func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg MsgCreateClient) (*sdk
}

// HandleMsgUpdateClient defines the sdk.Handler for MsgUpdateClient
func HandleMsgUpdateClient(ctx sdk.Context, k Keeper, msg MsgUpdateClient) (*sdk.Result, error) {
err := k.UpdateClient(ctx, msg.ClientID, msg.Header)
if err != nil {
func HandleMsgUpdateClient(ctx sdk.Context, k Keeper, msg exported.MsgUpdateClient) (*sdk.Result, error) {
if err := k.UpdateClient(ctx, msg.GetClientID(), msg.GetHeader()); err != nil {
return nil, err
}

attributes := make([]sdk.Attribute, len(msg.GetSigners())+1)
attributes[0] = sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory)
for i, signer := range msg.GetSigners() {
attributes[i+1] = sdk.NewAttribute(sdk.AttributeKeySender, signer.String())
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
EventTypeUpdateClient,
sdk.NewAttribute(AttributeKeyClientID, msg.ClientID),
sdk.NewAttribute(AttrbuteKeyClientType, msg.Header.ClientType().String()),
sdk.NewAttribute(AttributeKeyClientID, msg.GetClientID()),
sdk.NewAttribute(AttrbuteKeyClientType, msg.GetHeader().ClientType().String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Signer.String()),
attributes...,
),
})

Expand Down
Loading

0 comments on commit efb28d7

Please sign in to comment.