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

Add update functionality for WASM code id #238

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions modules/core/02-client/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
EventTypeUpgradeChain = "upgrade_chain"
EventTypeUpgradeClientProposal = "upgrade_client_proposal"
EventTypePushWasmCode = "push_wasm_code"
EventTypeUpdateWasmCodeId = "update_wasm_code_id"

AttributeValueCategory = fmt.Sprintf("%s_%s", ibcexported.ModuleName, SubModuleName)
)
1 change: 1 addition & 0 deletions modules/light-clients/08-wasm/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewTxCmd() *cobra.Command {

txCmd.AddCommand(
newPushNewWasmCodeCmd(),
newUpdateWasmCodeId(),
)

return txCmd
Expand Down
42 changes: 40 additions & 2 deletions modules/light-clients/08-wasm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"
"github.com/spf13/cobra"

types "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"
)

// newPushNewWasmCodeCmd returns the command to create a PushNewWasmCode transaction
Expand Down Expand Up @@ -47,3 +47,41 @@ func newPushNewWasmCodeCmd() *cobra.Command {

return cmd
}

// newUpdateWasmCodeId returns the command to create a UpdateWasmCodeId transaction
func newUpdateWasmCodeId() *cobra.Command {
cmd := &cobra.Command{
Use: "update-wasm-code-id [client-id] [code-id]",
Short: "Updates wasm code id for a client",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

clientId := args[0]
codeId, err := transfertypes.ParseHexHash(args[1])

if err != nil {
return err
}

msg := &types.MsgUpdateWasmCodeId{
ClientId: clientId,
CodeId: codeId,
Signer: clientCtx.GetFromAddress().String(),
}

if err := msg.ValidateBasic(); err != nil {
return err
}
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
21 changes: 12 additions & 9 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
clientkeeper "github.com/cosmos/ibc-go/v7/modules/core/02-client/keeper"
"math"
"path/filepath"
"strings"
Expand All @@ -25,13 +26,14 @@ import (
)

type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
wasmVM *cosmwasm.VM
authority string
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
wasmVM *cosmwasm.VM
authority string
clientKeeper *clientkeeper.Keeper
}

func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, authority string, homeDir string) Keeper {
func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, authority string, homeDir string, clientKeeper *clientkeeper.Keeper) Keeper {
// Wasm VM
wasmDataDir := filepath.Join(homeDir, "wasm_client_data")
wasmSupportedFeatures := strings.Join([]string{"storage", "iterator"}, ",")
Expand All @@ -48,10 +50,11 @@ func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, authority string,
// governance authority

return Keeper{
cdc: cdc,
storeKey: key,
wasmVM: vm,
authority: authority,
cdc: cdc,
storeKey: key,
wasmVM: vm,
authority: authority,
clientKeeper: clientKeeper,
}
}

Expand Down
56 changes: 55 additions & 1 deletion modules/light-clients/08-wasm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package keeper
import (
"context"
"encoding/hex"

"fmt"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
clienttypes "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/light-clients/08-wasm/types"
)

Expand Down Expand Up @@ -41,3 +43,55 @@ func (k Keeper) PushNewWasmCode(goCtx context.Context, msg *types.MsgPushNewWasm
CodeId: codeID,
}, nil
}

func (k Keeper) ClientStore(ctx sdk.Context, clientID string) sdk.KVStore {
clientPrefix := []byte(fmt.Sprintf("%s/%s/", host.KeyClientStorePrefix, clientID))
return prefix.NewStore(ctx.KVStore(k.storeKey), clientPrefix)
}
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved

// UpdateWasmCodeId defines a rpc handler method for MsgUpdateWasmCodeId
func (k Keeper) UpdateWasmCodeId(goCtx context.Context, msg *types.MsgUpdateWasmCodeId) (*types.MsgUpdateWasmCodeIdResponse, error) {
if k.authority != msg.Signer {
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority: expected %s, got %s", k.authority, msg.Signer)
}

ctx := sdk.UnwrapSDKContext(goCtx)
store := ctx.KVStore(k.storeKey)

codeId := msg.CodeId
if !store.Has(types.CodeID(codeId)) {
return nil, sdkerrors.Wrapf(types.ErrInvalidCodeId, "code id %s does not exist", hex.EncodeToString(codeId))
}

clientId := msg.ClientId
unknownClientState, found := k.clientKeeper.GetClientState(ctx, clientId)
if !found {
return nil, sdkerrors.Wrapf(clienttypes.ErrClientNotFound, "cannot update client with ID %s", clientId)
}

clientState, ok := unknownClientState.(*types.ClientState)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrInvalid, "client state type %T, expected %T", unknownClientState, (*types.ClientState)(nil))
}

clientState.CodeId = codeId

k.clientKeeper.SetClientState(ctx, clientId, clientState)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
clienttypes.EventTypeUpdateWasmCodeId,
sdk.NewAttribute(clienttypes.AttributeKeyClientID, clientId),
sdk.NewAttribute(clienttypes.AttributeKeyWasmCodeID, hex.EncodeToString(codeId)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, clienttypes.AttributeValueCategory),
),
})

return &types.MsgUpdateWasmCodeIdResponse{
ClientId: clientId,
CodeId: codeId,
}, nil
}
46 changes: 46 additions & 0 deletions modules/light-clients/08-wasm/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
var TypeMsgPushNewWasmCode = "push_wasm_code"
var _ sdk.Msg = &MsgPushNewWasmCode{}

var TypeMsgUpdateWasmCodeId = "update_wasm_code_id"
var _ sdk.Msg = &MsgUpdateWasmCodeId{}

// NewMsgPushNewWasmCode creates a new MsgPushNewWasmCode instance
//
//nolint:interfacer
Expand Down Expand Up @@ -47,3 +50,46 @@ func (m MsgPushNewWasmCode) GetSigners() []sdk.AccAddress {
func (msg MsgPushNewWasmCode) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// NewMsgUpdateWasmCodeId creates a new MsgUpdateWasmCodeId instance
//
//nolint:interfacer

// Route Implements Msg.
func (msg MsgUpdateWasmCodeId) Route() string { return ModuleName }

// Type Implements Msg.
func (msg MsgUpdateWasmCodeId) Type() string { return TypeMsgUpdateWasmCodeId }

// TODO: add tests for MsgUpdateWasmCodeId
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved

func NewMsgUpdateWasmCodeId(signer string, codeId []byte, clientId string) *MsgUpdateWasmCodeId {
return &MsgUpdateWasmCodeId{
Signer: signer,
CodeId: codeId,
ClientId: clientId,
}
}

func (m MsgUpdateWasmCodeId) ValidateBasic() error {
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved
if len(m.CodeId) != 32 {
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved
return sdkerrors.Wrapf(ErrWasmEmptyCode,
"invalid code id length (expected 32, got %d)", len(m.CodeId),
)
}

return nil
}

func (m MsgUpdateWasmCodeId) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}

// GetSignBytes implements the LegacyMsg interface.
func (msg MsgUpdateWasmCodeId) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
Loading