Skip to content

Commit

Permalink
feat: add ability to change max-wasm-size via gov
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Jan 31, 2024
1 parent 802b78c commit 72ec438
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 47 deletions.
8 changes: 8 additions & 0 deletions proto/sedachain/wasm_storage/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ service Msg {
rpc InstantiateAndRegisterProxyContract(
MsgInstantiateAndRegisterProxyContract)
returns (MsgInstantiateAndRegisterProxyContractResponse);
rpc SetMaxWasmSize(MsgSetMaxWasmSize) returns (MsgSetMaxWasmSizeResponse);
}

message MsgStoreDataRequestWasm {
Expand Down Expand Up @@ -61,3 +62,10 @@ message MsgInstantiateAndRegisterProxyContractResponse {
string contract_address = 1
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message MsgSetMaxWasmSize {
uint64 max_wasm_size = 1;
}

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

message Params { uint64 max_wasm_size = 1; }
message Params {
uint64 max_wasm_size = 1;
}
32 changes: 31 additions & 1 deletion x/wasm-storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

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

"cosmossdk.io/log"
Expand All @@ -10,7 +11,6 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/x/wasm-storage/types"
)

Expand Down Expand Up @@ -145,3 +145,33 @@ func (k Keeper) GetAllWasms(ctx sdk.Context) []types.Wasm {
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

// GetParams returns all the parameters for the module.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.KeyParams))

var params types.Params
k.cdc.MustUnmarshal(bz, &params)
return params
}

// SetParams sets the parameters in the store.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
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")
}

params := k.GetParams(ctx)
params.MaxWasmSize = maxWasmSize
k.SetParams(ctx, params)

return nil
}
12 changes: 12 additions & 0 deletions x/wasm-storage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,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)

err := k.Keeper.SetMaxWasmSize(ctx, msg.MaxWasmSize)
if err != nil {
return nil, err
}

return &types.MsgSetMaxWasmSizeResponse{}, nil
}
47 changes: 47 additions & 0 deletions x/wasm-storage/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,50 @@ func (s *KeeperTestSuite) TestMarshalJSON() {
})
}
}

func (s *KeeperTestSuite) TestSetMaxWasmSize() {
cases := []struct {
name string
preRun func()
input types.MsgSetMaxWasmSize
expErr bool
expErrMsg string
}{
{
name: "happy path",
input: types.MsgSetMaxWasmSize{
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",
},
}

for _, tc := range cases {
s.Run(tc.name, func() {
s.SetupTest()
tc.preRun()
_, err := s.msgSrvr.SetMaxWasmSize(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
params := s.wasmStorageKeeper.GetParams(s.ctx)
s.Require().Equal(tc.input.MaxWasmSize, params.MaxWasmSize)
}
})
}
}
6 changes: 6 additions & 0 deletions x/wasm-storage/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const (

// RouterKey defines the module's message routing key
RouterKey = ModuleName

// 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
Loading

0 comments on commit 72ec438

Please sign in to comment.