Skip to content

Commit

Permalink
ICS 02 upstream changes (#5122)
Browse files Browse the repository at this point in the history
* ICS 02 upstream changes (#5122)
  • Loading branch information
fedekunze committed Oct 1, 2019
1 parent 8a3e5ea commit 9975a1d
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 46 deletions.
2 changes: 1 addition & 1 deletion x/ibc/02-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ each corresponds to `spec: Header.{height, proof, state, root}`.

### manager.go

`spec: interface ClientState` is implemented by `type Object`. // TODO
`spec: interface ClientState` is implemented by `type State`. // TODO
14 changes: 11 additions & 3 deletions x/ibc/02-client/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ import (

"github.com/cosmos/cosmos-sdk/store/state"

commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
)

func (obj Object) prefix() []byte {
func (obj State) prefix() []byte {
return bytes.Split(obj.ConsensusState.KeyBytes(), LocalRoot())[0]
}

func (obj Object) ConsensusStateCLI(q state.ABCIQuerier) (res ConsensusState, proof merkle.Proof, err error) {
func (obj State) RootCLI(q state.ABCIQuerier, height uint64) (res commitment.Root, proof merkle.Proof, err error) {
root := obj.Roots.Value(height)
tmproof, err := root.Query(q, &res)
proof = merkle.NewProofFromValue(tmproof, obj.prefix(), root)
return
}

func (obj State) ConsensusStateCLI(q state.ABCIQuerier) (res ConsensusState, proof merkle.Proof, err error) {
tmproof, err := obj.ConsensusState.Query(q, &res)
proof = merkle.NewProofFromValue(tmproof, obj.prefix(), obj.ConsensusState)
return
}

func (obj Object) FrozenCLI(q state.ABCIQuerier) (res bool, proof merkle.Proof, err error) {
func (obj State) FrozenCLI(q state.ABCIQuerier) (res bool, proof merkle.Proof, err error) {
res, tmproof, err := obj.Frozen.Query(q)
proof = merkle.NewProofFromValue(tmproof, obj.prefix(), obj.Frozen)
return
Expand Down
52 changes: 47 additions & 5 deletions x/ibc/02-client/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/cosmos/cosmos-sdk/store/state"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/x/ibc"
client "github.com/cosmos/cosmos-sdk/x/ibc/02-client"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/tendermint"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
"github.com/cosmos/cosmos-sdk/x/ibc/version"
)

func mapping(cdc *codec.Codec, storeKey string, version int64) state.Mapping {
prefix := []byte(strconv.FormatInt(version, 10) + "/")
func mapping(cdc *codec.Codec, storeKey string, v int64) state.Mapping {
prefix := version.Prefix(v)
return state.NewMapping(sdk.NewKVStoreKey(storeKey), cdc, prefix)
}

Expand All @@ -35,8 +35,10 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {

ibcQueryCmd.AddCommand(cli.GetCommands(
GetCmdQueryConsensusState(storeKey, cdc),
GetCmdQueryPath(storeKey, cdc),
GetCmdQueryHeader(cdc),
GetCmdQueryClient(storeKey, cdc),
GetCmdQueryRoot(storeKey, cdc),
)...)
return ibcQueryCmd
}
Expand All @@ -49,11 +51,11 @@ func GetCmdQueryClient(storeKey string, cdc *codec.Codec) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
q := state.NewCLIQuerier(ctx)
mapp := mapping(cdc, storeKey, ibc.Version)
mapp := mapping(cdc, storeKey, version.Version)
man := client.NewManager(mapp)
id := args[0]

state, _, err := man.Object(id).ConsensusStateCLI(q)
state, _, err := man.State(id).ConsensusStateCLI(q)
if err != nil {
return err
}
Expand All @@ -65,6 +67,33 @@ func GetCmdQueryClient(storeKey string, cdc *codec.Codec) *cobra.Command {
}
}

func GetCmdQueryRoot(storeKey string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "root",
Short: "Query stored root",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
q := state.NewCLIQuerier(ctx)
mapp := mapping(cdc, storeKey, version.Version)
man := client.NewManager(mapp)
id := args[0]
height, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

root, _, err := man.State(id).RootCLI(q, height)
if err != nil {
return err
}

fmt.Printf("%s\n", codec.MustMarshalJSONIndent(cdc, root))

return nil
},
}
}
func GetCmdQueryConsensusState(storeKey string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "consensus-state",
Expand Down Expand Up @@ -109,6 +138,19 @@ func GetCmdQueryConsensusState(storeKey string, cdc *codec.Codec) *cobra.Command
}
}

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 {
mapp := mapping(cdc, storeName, version.Version)
path := merkle.NewPrefix([][]byte{[]byte(storeName)}, mapp.PrefixBytes())
fmt.Printf("%s\n", codec.MustMarshalJSONIndent(cdc, path))
return nil
},
}
}

func GetCmdQueryHeader(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "header",
Expand Down
8 changes: 4 additions & 4 deletions x/ibc/02-client/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const (

func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
ibcTxCmd := &cobra.Command{
Use: "ibc",
Short: "IBC transaction subcommands",
Use: "client",
Short: "Client transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
}
Expand All @@ -48,7 +48,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {

func GetCmdCreateClient(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-client",
Use: "create",
Short: "create new client with a consensus state",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -81,7 +81,7 @@ func GetCmdCreateClient(cdc *codec.Codec) *cobra.Command {

func GetCmdUpdateClient(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "update-client",
Use: "update",
Short: "update existing client with a header",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
10 changes: 5 additions & 5 deletions x/ibc/02-client/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
)

var MsgCdc = codec.New()

func init() {
RegisterCodec(MsgCdc)
}
var MsgCdc *codec.Codec

func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*ConsensusState)(nil), nil)
Expand All @@ -17,3 +13,7 @@ func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgCreateClient{}, "ibc/client/MsgCreateClient", nil)
cdc.RegisterConcrete(MsgUpdateClient{}, "ibc/client/MsgUpdateClient", nil)
}

func SetMsgCodec(cdc *codec.Codec) {
MsgCdc = cdc
}
78 changes: 58 additions & 20 deletions x/ibc/02-client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,72 +40,80 @@ func (man Manager) RegisterKind(kind Kind, pred ValidityPredicate) Manager {
return man
}
*/
func (man Manager) Object(id string) Object {
return Object{
func (man Manager) State(id string) State {
return State{
id: id,
Roots: man.protocol.Prefix([]byte(id + "/roots/")).Indexer(state.Dec),
ConsensusState: man.protocol.Value([]byte(id)),
Frozen: man.protocol.Value([]byte(id + "/freeze")).Boolean(),
}
}

func (man Manager) Create(ctx sdk.Context, id string, cs ConsensusState) (Object, error) {
obj := man.Object(id)
func (man Manager) Create(ctx sdk.Context, id string, cs ConsensusState) (State, error) {
obj := man.State(id)
if obj.exists(ctx) {
return Object{}, errors.New("Create client on already existing id")
return State{}, errors.New("Create client on already existing id")
}
obj.Roots.Set(ctx, cs.GetHeight(), cs.GetRoot())
obj.ConsensusState.Set(ctx, cs)
return obj, nil
}

func (man Manager) Query(ctx sdk.Context, id string) (Object, error) {
res := man.Object(id)
func (man Manager) Query(ctx sdk.Context, id string) (State, error) {
res := man.State(id)
if !res.exists(ctx) {
return Object{}, errors.New("client not exists")
return State{}, errors.New("client not exists")
}
return res, nil
}

func (man CounterpartyManager) Object(id string) CounterObject {
return CounterObject{
func (man CounterpartyManager) State(id string) CounterState {
return CounterState{
id: id,
ConsensusState: man.protocol.Value([]byte(id)),
}
}

func (man CounterpartyManager) Query(id string) CounterObject {
return man.Object(id)
func (man CounterpartyManager) Query(id string) CounterState {
return man.State(id)
}

// Any actor holding the Object can access on and modify that client information
type Object struct {
// Any actor holding the Stage can access on and modify that client information
type State struct {
id string
Roots state.Indexer
ConsensusState state.Value // ConsensusState
Frozen state.Boolean
}

type CounterObject struct {
type CounterState struct {
id string
ConsensusState commitment.Value
}

func (obj Object) ID() string {
func (obj State) ID() string {
return obj.id
}

func (obj Object) GetConsensusState(ctx sdk.Context) (res ConsensusState) {
func (obj State) GetConsensusState(ctx sdk.Context) (res ConsensusState) {
obj.ConsensusState.Get(ctx, &res)
return
}

func (obj CounterObject) Is(ctx sdk.Context, client ConsensusState) bool {
func (obj State) GetRoot(ctx sdk.Context, height uint64) (res commitment.Root, err error) {
err = obj.Roots.GetSafe(ctx, height, &res)
return
}

func (obj CounterState) Is(ctx sdk.Context, client ConsensusState) bool {
return obj.ConsensusState.Is(ctx, client)
}

func (obj Object) exists(ctx sdk.Context) bool {
func (obj State) exists(ctx sdk.Context) bool {
return obj.ConsensusState.Exists(ctx)
}

func (obj Object) Update(ctx sdk.Context, header Header) error {
func (obj State) Update(ctx sdk.Context, header Header) error {
if !obj.exists(ctx) {
panic("should not update nonexisting client")
}
Expand All @@ -121,6 +129,36 @@ func (obj Object) Update(ctx sdk.Context, header Header) error {
}

obj.ConsensusState.Set(ctx, updated)
obj.Roots.Set(ctx, updated.GetHeight(), updated.GetRoot())

return nil
}

func (obj State) Freeze(ctx sdk.Context) error {
if !obj.exists(ctx) {
panic("should not freeze nonexisting client")
}

if obj.Frozen.Get(ctx) {
return errors.New("client is already Frozen")
}

obj.Frozen.Set(ctx, true)

return nil
}

func (obj State) Delete(ctx sdk.Context) error {
if !obj.exists(ctx) {
panic("should not delete nonexisting client")
}

if !obj.Frozen.Get(ctx) {
return errors.New("client is not Frozen")
}

obj.ConsensusState.Delete(ctx)
obj.Frozen.Delete(ctx)

return nil
}
5 changes: 0 additions & 5 deletions x/ibc/02-client/tendermint/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ package tendermint

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client"
)

func init() {
RegisterCodec(client.MsgCdc)
}

func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(ConsensusState{}, "ibc/client/tendermint/ConsensusState", nil)
cdc.RegisterConcrete(Header{}, "ibc/client/tendermint/Header", nil)
Expand Down
3 changes: 0 additions & 3 deletions x/ibc/version.go

This file was deleted.

13 changes: 13 additions & 0 deletions x/ibc/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package version

import "strconv"

const Version int64 = 1

func DefaultPrefix() []byte {
return Prefix(Version)
}

func Prefix(version int64) []byte {
return []byte("v" + strconv.FormatInt(version, 10) + "/")
}

0 comments on commit 9975a1d

Please sign in to comment.