Skip to content

Commit

Permalink
ICS 02 Implementation (#4516)
Browse files Browse the repository at this point in the history
* add mapping

* rm unused mapping/*, rm interfaces

* rm unused code

* mv mapping -> state, rm x/ibc

* rm GetIfExists

* add key

* rm custom encoding/decoding in enum/bool

* fix lint

* rm tests

* add commitment

* newtyped remote values

* newtyped context

* revert context newtype

* add README, keypath

* reflect downstream ics

* add merkle

* add test for proving

* soft coded root keypath

* add update

* remove RootUpdate

* separate keypath and keuprefix

* add codec

* separate root/path

* add path to codec

* add client

* add counterpartymanager

* fix manager

* add Is() to counterobject

* add readme, reflect ICS02 revision

* reflect downstream ics

* test in progress

* add test

* in progres

* fin rebase

* in progress

* fin rebase

* add CLIObject in progress

* cli in progress

* add CLIObject

* separate testing from tendermint

* add key to node

* add root and storekey to tests/node, add codec

* rm cli/query.go

* fix test

* fix lint

* fix lint

* add handler/msgs/client

* rm relay

* finalize rebase on 23 root/path sep

* fix lint, fix syntax

* rm freebase, reformat query

* add docs in progre

* add comments

* add comments

* Apply suggestions from code review

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

* add comments in progress

* add comments

* fix comment

* add comments in progress

* recover IntEncoding scheme for integer

* add uint tests, don't use codec in custom types

* finalize merge

* add godoc

* add godoc in progress

* reformat test

* rm XXX

* add godoc

* modify store

* add query

* update query.go

* update query.go

* cli refactor in progress

* cli refactor in progress

* add Query to boolean.go

* fix key

* fix cli / merkle test

* godoc cleanup

* godoc cleanup

* godoc cleanup

* godoc cleanup

* godoc cleanup

* merge from ics04 branch

* merge from ics04 branch

* merge from ics04 branch

* fix lint

* Apply suggestions from code review

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

* applying review in progress

* apply review - make querier interface

* fix dependency

* fix dependency

* revise querier interface to work both on cli & store

* revise querier interface to work both on cli & store

* reflect downstream change

* fix cli

* rm commented lines

* address review in progress

* rename Path -> Prefix

* Store accessor upstream changes (#5119)

* Store accessor upstream changes (#5119)

* add comments, reformat merkle querier

* rm merkle/utils

* ICS 23 upstream changes (#5120)

* ICS 23 upstream changes (#5120)

* update Value

* update test

* fix

* ICS 02 upstream changes (#5122)

* ICS 02 upstream changes (#5122)

* cleanup types and submodule

* more cleanup and godocs

* remove counterPartyManager/State and cleanup

* implement SubmitMisbehaviour and refactor

* errors

* events

* fix test

* refactors

* remove Mapping

* remove store accessors

* proposed refactor

* remove store accessors from ICS02

* refactor queriers, handler and clean keeper

* logger and tx long description

* ineffassign

* Apply suggestions from code review

Co-Authored-By: Jack Zampolin <jack.zampolin@gmail.com>

* Apply suggestions from code review

Co-Authored-By: Jack Zampolin <jack.zampolin@gmail.com>

* add verification functions

* ICS02 module.go

* top level x/ibc structure

* Update x/ibc/02-client/client/cli/query.go

Co-Authored-By: Jack Zampolin <jack.zampolin@gmail.com>

* Update x/ibc/02-client/types/tendermint/consensus_state.go

Co-Authored-By: Jack Zampolin <jack.zampolin@gmail.com>

* address some of the review comments

* minor UX improvements

* rename pkg

* fixes

* refactor ICS23

* cleanup types

* implement batch verification

* gosimple suggestion

* various fixes; remove legacy tests; remove commitment path query

* alias

* minor updates from ICS23

* renaming

* update verification and rename root funcs

* move querier to x/ibc

* update query.go to use 'custom/...' query path

* add tests

* ICS 24 Implementation (#5229)

* add validation functions

* validate path in ics-23

* address @fede comments

* move errors into host package

* flatten ICS23 structure

* fix ApplyPrefix

* updates from ICS23 and ICS24

* msg.ValidateBasic and ADR09 evidence interface

* complete types testing

* delete empty test file

* remove ibc errors from core error package

* custom JSON marshaling

* start batch-verify tests

* minor changes on commitment types

* R4R - Store consensus state correctly (#5242)

* store consensus state correctly

* fix client example

* update alias

* use testsuite

* Integrate Evidence Implementation into ICS-02 (#5258)

* implement evidence in ics-02

* fix build errors and import cycles

* address fede comments

* remove unnecessary pubkey and fix init

* add tests

* finish tendermint tests

* complete merge

* Add tests for msgs

* upstream changes

* fix

* upstream changes

* fix cons state

* context changes
  • Loading branch information
mossid authored and jackzampolin committed Jan 15, 2020
1 parent 220a482 commit 35e0428
Show file tree
Hide file tree
Showing 37 changed files with 2,607 additions and 1 deletion.
85 changes: 85 additions & 0 deletions x/ibc/02-client/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package client

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

import (
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/keeper"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types/errors"
)

const (
DefaultCodespace = errors.DefaultCodespace
CodeClientExists = errors.CodeClientExists
CodeClientNotFound = errors.CodeClientNotFound
CodeClientFrozen = errors.CodeClientFrozen
CodeConsensusStateNotFound = errors.CodeConsensusStateNotFound
CodeInvalidConsensusState = errors.CodeInvalidConsensusState
CodeClientTypeNotFound = errors.CodeClientTypeNotFound
CodeInvalidClientType = errors.CodeInvalidClientType
CodeRootNotFound = errors.CodeRootNotFound
CodeInvalidHeader = errors.CodeInvalidHeader
CodeInvalidEvidence = errors.CodeInvalidEvidence
AttributeKeyClientID = types.AttributeKeyClientID
SubModuleName = types.SubModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
QueryClientState = types.QueryClientState
QueryConsensusState = types.QueryConsensusState
QueryVerifiedRoot = types.QueryVerifiedRoot
)

var (
// functions aliases
NewKeeper = keeper.NewKeeper
QuerierClientState = keeper.QuerierClientState
QuerierConsensusState = keeper.QuerierConsensusState
QuerierVerifiedRoot = keeper.QuerierVerifiedRoot
RegisterCodec = types.RegisterCodec
ErrClientExists = errors.ErrClientExists
ErrClientNotFound = errors.ErrClientNotFound
ErrClientFrozen = errors.ErrClientFrozen
ErrConsensusStateNotFound = errors.ErrConsensusStateNotFound
ErrInvalidConsensus = errors.ErrInvalidConsensus
ErrClientTypeNotFound = errors.ErrClientTypeNotFound
ErrInvalidClientType = errors.ErrInvalidClientType
ErrRootNotFound = errors.ErrRootNotFound
ErrInvalidHeader = errors.ErrInvalidHeader
ErrInvalidEvidence = errors.ErrInvalidEvidence
ClientStatePath = types.ClientStatePath
ClientTypePath = types.ClientTypePath
ConsensusStatePath = types.ConsensusStatePath
RootPath = types.RootPath
KeyClientState = types.KeyClientState
KeyClientType = types.KeyClientType
KeyConsensusState = types.KeyConsensusState
KeyRoot = types.KeyRoot
NewMsgCreateClient = types.NewMsgCreateClient
NewMsgUpdateClient = types.NewMsgUpdateClient
NewMsgSubmitMisbehaviour = types.NewMsgSubmitMisbehaviour
NewQueryClientStateParams = types.NewQueryClientStateParams
NewQueryCommitmentRootParams = types.NewQueryCommitmentRootParams
NewClientState = types.NewClientState

// variable aliases
SubModuleCdc = types.SubModuleCdc
EventTypeCreateClient = types.EventTypeCreateClient
EventTypeUpdateClient = types.EventTypeUpdateClient
EventTypeSubmitMisbehaviour = types.EventTypeSubmitMisbehaviour
AttributeValueCategory = types.AttributeValueCategory
)

type (
Keeper = keeper.Keeper
MsgCreateClient = types.MsgCreateClient
MsgUpdateClient = types.MsgUpdateClient
MsgSubmitMisbehaviour = types.MsgSubmitMisbehaviour
QueryClientStateParams = types.QueryClientStateParams
QueryCommitmentRootParams = types.QueryCommitmentRootParams
State = types.State
)
258 changes: 258 additions & 0 deletions x/ibc/02-client/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
package cli

import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"

tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/client/utils"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types/tendermint"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
)

// GetQueryCmd returns the query commands for IBC clients
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
ics02ClientQueryCmd := &cobra.Command{
Use: "client",
Short: "IBC client query subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
}

ics02ClientQueryCmd.AddCommand(client.GetCommands(
GetCmdQueryConsensusState(queryRoute, cdc),
GetCmdQueryHeader(cdc),
GetCmdQueryClientState(queryRoute, cdc),
GetCmdQueryRoot(queryRoute, cdc),
GetCmdNodeConsensusState(queryRoute, cdc),
GetCmdQueryPath(queryRoute, cdc),
)...)
return ics02ClientQueryCmd
}

// GetCmdQueryClientState defines the command to query the state of a client with
// a given id as defined in https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#query
func GetCmdQueryClientState(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "state [client-id]",
Short: "Query a client state",
Long: strings.TrimSpace(
fmt.Sprintf(`Query stored client state
Example:
$ %s query ibc client state [client-id]
`, version.ClientName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientID := args[0]
if strings.TrimSpace(clientID) == "" {
return errors.New("client ID can't be blank")
}

bz, err := cdc.MarshalJSON(types.NewQueryClientStateParams(clientID))
if err != nil {
return err
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryClientState), bz)
if err != nil {
return err
}

var clientState types.State
if err := cdc.UnmarshalJSON(res, &clientState); err != nil {
return err
}

return cliCtx.PrintOutput(clientState)
},
}
}

// GetCmdQueryRoot defines the command to query
func GetCmdQueryRoot(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "root [client-id] [height]",
Short: "Query a verified commitment root",
Long: strings.TrimSpace(
fmt.Sprintf(`Query an already verified commitment root at a specific height for a particular client
Example:
$ %s query ibc client root [client-id] [height]
`, version.ClientName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientID := args[0]
if strings.TrimSpace(clientID) == "" {
return errors.New("client ID can't be blank")
}

height, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("expected integer height, got: %v", args[1])
}

bz, err := cdc.MarshalJSON(types.NewQueryCommitmentRootParams(clientID, height))
if err != nil {
return err
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryVerifiedRoot), bz)
if err != nil {
return err
}

var root commitment.RootI
if err := cdc.UnmarshalJSON(res, &root); err != nil {
return err
}

return cliCtx.PrintOutput(root)
},
}
}

// GetCmdQueryConsensusState defines the command to query the consensus state of
// the chain as defined in https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#query
func GetCmdQueryConsensusState(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "consensus-state [client-id]",
Short: "Query the latest consensus state of the client",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the consensus state for a particular client
Example:
$ %s query ibc client consensus-state [client-id]
`, version.ClientName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientID := args[0]
if strings.TrimSpace(clientID) == "" {
return errors.New("client ID can't be blank")
}

bz, err := cdc.MarshalJSON(types.NewQueryClientStateParams(clientID))
if err != nil {
return err
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryConsensusState), bz)
if err != nil {
return err
}

var consensusState exported.ConsensusState
if err := cdc.UnmarshalJSON(res, &consensusState); err != nil {
return err
}

return cliCtx.PrintOutput(consensusState)
},
}
}

// GetCmdQueryHeader defines the command to query the latest header on the chain
func GetCmdQueryHeader(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "header",
Short: "Query the latest header of the running chain",
Long: strings.TrimSpace(fmt.Sprintf(`Query the latest Tendermint header
Example:
$ %s query ibc client header
`, version.ClientName),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

header, err := utils.GetTendermintHeader(cliCtx)
if err != nil {
return err
}

return cliCtx.PrintOutput(header)
},
}
}

// GetCmdNodeConsensusState defines the command to query the latest consensus state of a node
// The result is feed to client creation
func GetCmdNodeConsensusState(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "node-state",
Short: "Query a node consensus state",
Long: strings.TrimSpace(
fmt.Sprintf(`Query a node consensus state. This result is feed to the client creation transaction.
Example:
$ %s query ibc client node-state
`, version.ClientName),
),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

node, err := cliCtx.GetNode()
if err != nil {
return err
}

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

height := info.Response.LastBlockHeight
prevHeight := height - 1

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

validators, err := node.Validators(&prevHeight)
if err != nil {
return err
}

state := tendermint.ConsensusState{
ChainID: commit.ChainID,
Height: uint64(commit.Height),
Root: commitment.NewRoot(commit.AppHash),
NextValidatorSet: tmtypes.NewValidatorSet(validators.Validators),
}

return cliCtx.PrintOutput(state)
},
}
}

// GetCmdQueryPath defines the command to query the commitment path.
func GetCmdQueryPath(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "path",
Short: "Query the commitment path of the running chain",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
path := commitment.NewPrefix([]byte("ibc"))
return ctx.PrintOutput(path)
},
}
}
Loading

0 comments on commit 35e0428

Please sign in to comment.