Skip to content

Commit

Permalink
Merge pull request #238 from ComposableFi/vmarkushin/update-wasm-code-id
Browse files Browse the repository at this point in the history
Add update functionality for WASM code id
  • Loading branch information
vuong177 authored Sep 25, 2023
2 parents bdea9fe + ce6f5ef commit e25dbd0
Show file tree
Hide file tree
Showing 10 changed files with 783 additions and 34 deletions.
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
36 changes: 33 additions & 3 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 All @@ -35,10 +35,40 @@ func newPushNewWasmCodeCmd() *cobra.Command {
Signer: clientCtx.GetFromAddress().String(),
}

if err := msg.ValidateBasic(); err != nil {
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

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(),
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand Down
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
48 changes: 47 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,7 +3,6 @@ package keeper
import (
"context"
"encoding/hex"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -41,3 +40,50 @@ func (k Keeper) PushNewWasmCode(goCtx context.Context, msg *types.MsgPushNewWasm
CodeId: codeID,
}, nil
}

// 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
}
51 changes: 51 additions & 0 deletions modules/light-clients/08-wasm/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package types

import (
"github.com/cometbft/cometbft/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
)

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 +52,49 @@ 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 }

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

func (m MsgUpdateWasmCodeId) ValidateBasic() error {
if len(m.CodeId) != tmhash.Size {
return sdkerrors.Wrapf(ErrWasmEmptyCode,
"invalid code id length (expected 32, got %d)", len(m.CodeId),
)
}

err := host.ClientIdentifierValidator(m.ClientId)
if err != nil {
return err
}

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

0 comments on commit e25dbd0

Please sign in to comment.