Skip to content

Commit

Permalink
fix: properly wire up updateParams for wasm-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Jan 3, 2024
1 parent 6b54ac5 commit 53695e1
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 205 deletions.
4 changes: 1 addition & 3 deletions proto/sedachain/randomness/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ import "gogoproto/gogo.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/randomness/types";

message GenesisState {
string seed = 1;
}
message GenesisState { string seed = 1; }
9 changes: 3 additions & 6 deletions proto/sedachain/randomness/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import "cosmos/msg/v1/msg.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/randomness/types";

service Msg {
rpc NewSeed(MsgNewSeed)
returns (MsgNewSeedResponse);
}
service Msg { rpc NewSeed(MsgNewSeed) returns (MsgNewSeedResponse); }

message MsgNewSeed {
option (cosmos.msg.v1.signer) = "proposer";

string proposer = 1;
string pi = 2; // VRF proof
string pi = 2; // VRF proof
string beta = 3; // VRF hash
}

Expand Down
14 changes: 10 additions & 4 deletions proto/sedachain/wasm_storage/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ service Msg {
rpc InstantiateAndRegisterProxyContract(
MsgInstantiateAndRegisterProxyContract)
returns (MsgInstantiateAndRegisterProxyContractResponse);
rpc SetMaxWasmSize(MsgSetMaxWasmSize) returns (MsgSetMaxWasmSizeResponse);
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

message MsgStoreDataRequestWasm {
Expand Down Expand Up @@ -63,9 +63,15 @@ message MsgInstantiateAndRegisterProxyContractResponse {
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message MsgSetMaxWasmSize {
uint64 max_wasm_size = 1;
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";

// authority is the address that controls the module (defaults to x/gov unless
// overwritten).
string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

Params params = 2 [ (gogoproto.nullable) = false ];
}

// no data needs to be returned
message MsgSetMaxWasmSizeResponse {}
message MsgUpdateParamsResponse {}
6 changes: 4 additions & 2 deletions proto/sedachain/wasm_storage/v1/wasm_storage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum WasmType {
[ (gogoproto.enumvalue_customname) = "WasmTypeRelayer" ];
}

message Params {
uint64 max_wasm_size = 1;
message Params {
option (gogoproto.equal) = true;

uint64 max_wasm_size = 1;
}
24 changes: 10 additions & 14 deletions x/wasm-storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"encoding/hex"
"errors"
"fmt"

"cosmossdk.io/log"
Expand All @@ -29,6 +28,11 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, authority st
}
}

// GetAuthority returns the module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}

// SetDataRequestWasm stores Data Request Wasm using its hash as the key.
func (k Keeper) SetDataRequestWasm(ctx sdk.Context, wasm *types.Wasm) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -156,21 +160,13 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
}

// SetParams sets the parameters in the store.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set([]byte(types.KeyParams), bz)
}

// SetMaxWasmSize updates the MaxWasmSize parameter.
func (k Keeper) SetMaxWasmSize(ctx sdk.Context, maxWasmSize uint64) error {
if maxWasmSize == 0 {
return errors.New("MaxWasmSize cannot be zero")
bz, err := k.cdc.Marshal(&params)
if err != nil {
return err
}

params := k.GetParams(ctx)
params.MaxWasmSize = maxWasmSize
k.SetParams(ctx, params)
store.Set([]byte(types.KeyParams), bz)

return nil
}
13 changes: 7 additions & 6 deletions x/wasm-storage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ func unzipWasm(wasm []byte) ([]byte, error) {
return unzipped, nil
}

// SetMaxWasmSize sets the MaxWasmSize parameter in the store.
func (k msgServer) SetMaxWasmSize(goCtx context.Context, msg *types.MsgSetMaxWasmSize) (*types.MsgSetMaxWasmSizeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.GetAuthority() != req.Authority {
return nil, fmt.Errorf("invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
}

err := k.Keeper.SetMaxWasmSize(ctx, msg.MaxWasmSize)
if err != nil {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetParams(ctx, req.Params); err != nil {
return nil, err
}

return &types.MsgSetMaxWasmSizeResponse{}, nil
return &types.MsgUpdateParamsResponse{}, nil
}
22 changes: 7 additions & 15 deletions x/wasm-storage/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,48 +262,40 @@ func (s *KeeperTestSuite) TestMarshalJSON() {
}
}

func (s *KeeperTestSuite) TestSetMaxWasmSize() {
func (s *KeeperTestSuite) TestSetParams() {
cases := []struct {
name string
preRun func()
input types.MsgSetMaxWasmSize
input types.Params
expErr bool
expErrMsg string
}{
{
name: "happy path",
input: types.MsgSetMaxWasmSize{
input: types.Params{
MaxWasmSize: 1000000, // 1 MB
},
preRun: func() {},
expErr: false,
expErrMsg: "",
},
{
name: "zero size",
input: types.MsgSetMaxWasmSize{
MaxWasmSize: 0,
},
preRun: func() {},
expErr: true,
expErrMsg: "MaxWasmSize cannot be zero",
},
// negative cases would be caught in ValidateBasic before reaching keeper
}

for _, tc := range cases {
s.Run(tc.name, func() {
s.SetupTest()
tc.preRun()
_, err := s.msgSrvr.SetMaxWasmSize(s.ctx, &tc.input)
err := s.wasmStorageKeeper.SetParams(s.ctx, tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Equal(tc.expErrMsg, err.Error())
} else {
s.Require().NoError(err)

// Check that the MaxWasmSize parameter was correctly set
// Check that the Params were correctly set
params := s.wasmStorageKeeper.GetParams(s.ctx)
s.Require().Equal(tc.input.MaxWasmSize, params.MaxWasmSize)
s.Require().Equal(tc.input, params)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions x/wasm-storage/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package types
import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

func RegisterCodec(cdc *codec.LegacyAmino) {
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgUpdateParams{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

Expand Down
3 changes: 0 additions & 3 deletions x/wasm-storage/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const (

// KeyParams is the store key for the parameters.
KeyParams = "params"

// KeyMaxWasmSize defines the key for the MaxWasmSize parameter
KeyMaxWasmSize = "max-wasm-size"
)

var (
Expand Down
12 changes: 12 additions & 0 deletions x/wasm-storage/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ func (msg MsgInstantiateAndRegisterProxyContract) ValidateBasic() error {
}
return nil
}

func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return fmt.Errorf("invalid authority address: %s", err)
}

if err := m.Params.ValidateBasic(); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 53695e1

Please sign in to comment.