Skip to content

Commit

Permalink
Merge pull request cosmos#229 from ComposableFi/vmarkushin/wasm-grand…
Browse files Browse the repository at this point in the history
…pa-decode

Add automatic `Data` field deserialization for 08-wasm structs in CLI
  • Loading branch information
vuong177 authored Jul 24, 2023
2 parents 933036d + 24ffe5a commit 8b6a662
Show file tree
Hide file tree
Showing 11 changed files with 3,509 additions and 28 deletions.
1 change: 0 additions & 1 deletion modules/core/02-client/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
Expand Down
40 changes: 40 additions & 0 deletions modules/core/02-client/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"context"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
wasmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"

tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -61,7 +63,10 @@ func QueryClientStateABCI(
return nil, err
}

MaybeDecodeWasmData(clientCtx.Codec, anyClientState)

clientStateRes := types.NewQueryClientStateResponse(anyClientState, proofBz, proofHeight)

return clientStateRes, nil
}

Expand Down Expand Up @@ -114,6 +119,8 @@ func QueryConsensusStateABCI(
return nil, err
}

MaybeDecodeWasmData(clientCtx.Codec, anyConsensusState)

return types.NewQueryConsensusStateResponse(anyConsensusState, proofBz, proofHeight), nil
}

Expand Down Expand Up @@ -206,3 +213,36 @@ func QuerySelfConsensusState(clientCtx client.Context) (*ibctm.ConsensusState, i

return state, height, nil
}

func MaybeDecodeWasmData(codec codec.BinaryCodec, any *codectypes.Any) {
switch any.TypeUrl {
case "/ibc.lightclients.wasm.v1.ClientState":
var state wasmtypes.ClientState
err := codec.Unmarshal(any.Value, &state)
if err == nil {
var innerAny codectypes.Any
err = codec.Unmarshal(state.Data, &innerAny)
if err == nil {
state.XInner = &wasmtypes.ClientState_Inner{Inner: &innerAny}
bts, err := state.Marshal()
if err == nil {
any.Value = bts
}
}
}
case "/ibc.lightclients.wasm.v1.ConsensusState":
var state wasmtypes.ConsensusState
err := codec.Unmarshal(any.Value, &state)
if err == nil {
var innerAny codectypes.Any
err = codec.Unmarshal(state.Data, &innerAny)
if err == nil {
state.XInner = &wasmtypes.ConsensusState_Inner{Inner: &innerAny}
bts, err := state.Marshal()
if err == nil {
any.Value = bts
}
}
}
}
}
14 changes: 13 additions & 1 deletion modules/core/02-client/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

utils "github.com/cosmos/ibc-go/v7/modules/core/02-client/client/utils"
"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
Expand Down Expand Up @@ -45,6 +46,8 @@ func (q Keeper) ClientState(c context.Context, req *types.QueryClientStateReques
return nil, status.Error(codes.Internal, err.Error())
}

utils.MaybeDecodeWasmData(q.cdc, any)

proofHeight := types.GetSelfHeight(ctx)
return &types.QueryClientStateResponse{
ClientState: any,
Expand Down Expand Up @@ -81,6 +84,9 @@ func (q Keeper) ClientStates(c context.Context, req *types.QueryClientStatesRequ
}

identifiedClient := types.NewIdentifiedClientState(clientID, clientState)

utils.MaybeDecodeWasmData(q.cdc, identifiedClient.ClientState)

clientStates = append(clientStates, identifiedClient)
return true, nil
})
Expand Down Expand Up @@ -136,6 +142,8 @@ func (q Keeper) ConsensusState(c context.Context, req *types.QueryConsensusState
return nil, status.Error(codes.Internal, err.Error())
}

utils.MaybeDecodeWasmData(q.cdc, any)

proofHeight := types.GetSelfHeight(ctx)
return &types.QueryConsensusStateResponse{
ConsensusState: any,
Expand Down Expand Up @@ -174,7 +182,11 @@ func (q Keeper) ConsensusStates(c context.Context, req *types.QueryConsensusStat
return false, err
}

consensusStates = append(consensusStates, types.NewConsensusStateWithHeight(height, consensusState))
consensusStateWithHeight := types.NewConsensusStateWithHeight(height, consensusState)

utils.MaybeDecodeWasmData(q.cdc, consensusStateWithHeight.ConsensusState)

consensusStates = append(consensusStates, consensusStateWithHeight)
return true, nil
})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions modules/core/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
localhost "github.com/cosmos/ibc-go/v7/modules/light-clients/09-localhost"
grandpa "github.com/cosmos/ibc-go/v7/modules/light-clients/10-grandpa"
)

// RegisterInterfaces registers ibc types against interfaces using the global InterfaceRegistry.
Expand All @@ -18,4 +19,5 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
channeltypes.RegisterInterfaces(registry)
commitmenttypes.RegisterInterfaces(registry)
localhost.RegisterInterfaces(registry)
grandpa.RegisterInterfaces(registry)
}
Loading

0 comments on commit 8b6a662

Please sign in to comment.