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 all commits
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 @@
EventTypeUpgradeChain = "upgrade_chain"
EventTypeUpgradeClientProposal = "upgrade_client_proposal"
EventTypePushWasmCode = "push_wasm_code"
EventTypeUpdateWasmCodeId = "update_wasm_code_id"

Check failure on line 33 in modules/core/02-client/types/events.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var EventTypeUpdateWasmCodeId should be EventTypeUpdateWasmCodeID (revive)

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 @@
"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 @@
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 {

Check warning on line 48 in modules/light-clients/08-wasm/client/cli/tx.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func newUpdateWasmCodeId should be newUpdateWasmCodeID (revive)
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]

Check warning on line 59 in modules/light-clients/08-wasm/client/cli/tx.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var clientId should be clientID (revive)
codeId, err := transfertypes.ParseHexHash(args[1])

Check warning on line 60 in modules/light-clients/08-wasm/client/cli/tx.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var codeId should be codeID (revive)

Check failure on line 61 in modules/light-clients/08-wasm/client/cli/tx.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
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,9 +6,10 @@
"crypto/sha256"
"encoding/hex"
"fmt"
clientkeeper "github.com/cosmos/ibc-go/v7/modules/core/02-client/keeper"

Check failure on line 9 in modules/light-clients/08-wasm/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"math"
"path/filepath"
"strings"

Check failure on line 12 in modules/light-clients/08-wasm/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -25,13 +26,14 @@
)

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 @@
// 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 @@ -2,8 +2,7 @@

import (
"context"
"encoding/hex"

Check failure on line 5 in modules/light-clients/08-wasm/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

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 @@
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"

Check failure on line 10 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
var _ sdk.Msg = &MsgPushNewWasmCode{}

var TypeMsgUpdateWasmCodeId = "update_wasm_code_id"

Check failure on line 13 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
var _ sdk.Msg = &MsgUpdateWasmCodeId{}

// NewMsgPushNewWasmCode creates a new MsgPushNewWasmCode instance
//
//nolint:interfacer
Expand All @@ -25,7 +30,7 @@
}
}

func (m MsgPushNewWasmCode) ValidateBasic() error {

Check warning on line 33 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name m should be consistent with previous receiver name msg for MsgPushNewWasmCode (revive)
if len(m.Code) == 0 {
return sdkerrors.Wrapf(ErrWasmEmptyCode,
"empty wasm code",
Expand All @@ -35,7 +40,7 @@
return nil
}

func (m MsgPushNewWasmCode) GetSigners() []sdk.AccAddress {

Check warning on line 43 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name m should be consistent with previous receiver name msg for MsgPushNewWasmCode (revive)
signer, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
Expand All @@ -47,3 +52,49 @@
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 {

Check warning on line 66 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func NewMsgUpdateWasmCodeId should be NewMsgUpdateWasmCodeID (revive)
return &MsgUpdateWasmCodeId{
Signer: signer,
CodeId: codeId,
ClientId: clientId,
}
}

func (m MsgUpdateWasmCodeId) ValidateBasic() error {

Check warning on line 74 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name m should be consistent with previous receiver name msg for MsgUpdateWasmCodeId (revive)
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved
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 {

Check warning on line 89 in modules/light-clients/08-wasm/types/msgs.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name m should be consistent with previous receiver name msg for MsgUpdateWasmCodeId (revive)
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
Loading