Skip to content

Commit

Permalink
Add MsgUpdateParams (#300)
Browse files Browse the repository at this point in the history
MsgUpdateParams defines a Msg for updating the x/rollup module
parameters.
  • Loading branch information
natebeauregard authored Nov 26, 2024
1 parent 12630bf commit 81caf73
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 56 deletions.
71 changes: 62 additions & 9 deletions gen/rollup/module/v1/module.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions proto/rollup/module/v1/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "github.com/polymerdao/monomer/x/rollup"
};

// authority defines the custom module authority. If not set, defaults to the governance module.
string authority = 1;
}
19 changes: 19 additions & 0 deletions proto/rollup/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "amino/amino.proto";
import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "rollup/v1/rollup.proto";

option go_package = "github.com/polymerdao/monomer/x/rollup/types";

Expand All @@ -20,6 +21,9 @@ service Msg {

// InitiateFeeWithdrawal defines a method for initiating a withdrawal of fees from L2 to the L1 fee recipient address.
rpc InitiateFeeWithdrawal(MsgInitiateFeeWithdrawal) returns (MsgInitiateFeeWithdrawalResponse);

// UpdateParams defines a method for updating the x/rollup module parameters.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// MsgApplyL1Txs defines the message for applying all L1 system and user deposit txs.
Expand Down Expand Up @@ -66,3 +70,18 @@ message MsgInitiateFeeWithdrawal {

// MsgInitiateFeeWithdrawalResponse defines the Msg/InitiateFeeWithdrawal response type.
message MsgInitiateFeeWithdrawalResponse {}

// MsgUpdateParams defines a Msg for updating the x/rollup module parameters.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";

// authority is the address of the admin account.
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// params defines the x/rollup parameters to update.
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// MsgUpdateParamsResponse defines the Msg/MsgUpdateParams response type.
message MsgUpdateParamsResponse {}
20 changes: 17 additions & 3 deletions x/rollup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ import (
"google.golang.org/grpc/status"
)

var _ types.QueryServer = (*Keeper)(nil)
type Querier struct {
Keeper
}

var _ types.QueryServer = &Keeper{}

func NewQuerier(keeper *Keeper) Querier {
return Querier{Keeper: *keeper}
}

// L1BlockInfo implements the Query/L1BlockInfo gRPC method
func (k *Keeper) L1BlockInfo(ctx context.Context, req *types.QueryL1BlockInfoRequest) (*types.QueryL1BlockInfoResponse, error) {
func (k Keeper) L1BlockInfo( //nolint:gocritic // hugeParam
ctx context.Context,
req *types.QueryL1BlockInfoRequest,
) (*types.QueryL1BlockInfoResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand All @@ -25,7 +36,10 @@ func (k *Keeper) L1BlockInfo(ctx context.Context, req *types.QueryL1BlockInfoReq
}

// Params implements the Query/Params gRPC method
func (k *Keeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (k Keeper) Params( //nolint:gocritic // hugeParam
ctx context.Context,
req *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand Down
3 changes: 3 additions & 0 deletions x/rollup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type Keeper struct {
cdc codec.BinaryCodec
storeService store.KVStoreService
authority sdk.AccAddress
rollupCfg *rollup.Config
bankkeeper types.BankKeeper
accountkeeper types.AccountKeeper
Expand All @@ -22,6 +23,7 @@ type Keeper struct {
func NewKeeper(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
authority sdk.AccAddress,
// dependencies
bankKeeper types.BankKeeper,
accountKeeper types.AccountKeeper,
Expand All @@ -32,6 +34,7 @@ func NewKeeper(
bankkeeper: bankKeeper,
accountkeeper: accountKeeper,
rollupCfg: &rollup.Config{},
authority: authority,
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/rollup/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/polymerdao/monomer/x/rollup/keeper"
rolluptestutil "github.com/polymerdao/monomer/x/rollup/testutil"
"github.com/polymerdao/monomer/x/rollup/types"
Expand Down Expand Up @@ -51,6 +52,7 @@ func (s *KeeperTestSuite) setup() {
s.rollupKeeper = keeper.NewKeeper(
moduletestutil.MakeTestEncodingConfig().Codec,
runtime.NewKVStoreService(storeKey),
authtypes.NewModuleAddress(govtypes.ModuleName),
s.bankKeeper,
s.accountKeeper,
)
Expand Down
21 changes: 21 additions & 0 deletions x/rollup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,24 @@ func (k *Keeper) InitiateFeeWithdrawal(

return &types.MsgInitiateFeeWithdrawalResponse{}, nil
}

func (k *Keeper) UpdateParams(
goCtx context.Context,
msg *types.MsgUpdateParams,
) (*types.MsgUpdateParamsResponse, error) {
if k.authority.String() != msg.Authority {
return nil, types.WrapError(types.ErrUpdateParams, "invalid authority: expected %s, got %s", k.authority.String(), msg.Authority)
}

params := &msg.Params
if err := params.Validate(); err != nil {
return nil, types.WrapError(types.ErrUpdateParams, "validate params: %w", err)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetParams(ctx, &msg.Params); err != nil {
return nil, types.WrapError(types.ErrUpdateParams, "set params: %w", err)
}

return &types.MsgUpdateParamsResponse{}, nil
}
50 changes: 50 additions & 0 deletions x/rollup/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/ethereum-optimism/optimism/op-service/eth"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -279,3 +280,52 @@ func (s *KeeperTestSuite) TestInitiateFeeWithdrawal() {
})
}
}

func (s *KeeperTestSuite) TestUpdateParams() {
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
validParams := types.DefaultParams()
invalidParams := types.Params{}

tests := map[string]struct {
authority sdk.AccAddress
params types.Params
shouldError bool
}{
"valid authority with valid params": {
authority: authority,
params: validParams,
shouldError: false,
},
"invalid authority": {
authority: sdk.AccAddress("invalid_authority"),
params: validParams,
shouldError: true,
},
"valid authority with invalid params": {
authority: authority,
params: invalidParams,
shouldError: true,
},
}

for name, test := range tests {
s.Run(name, func() {
resp, err := s.rollupKeeper.UpdateParams(s.ctx, &types.MsgUpdateParams{
Authority: test.authority.String(),
Params: test.params,
})

if test.shouldError {
s.Require().Error(err)
s.Require().Nil(resp)
} else {
s.Require().NoError(err)
s.Require().NotNil(resp)

params, err := s.rollupKeeper.GetParams(sdk.UnwrapSDKContext(s.ctx))
s.Require().NoError(err)
s.Require().Equal(test.params, *params)
}
})
}
}
11 changes: 10 additions & 1 deletion x/rollup/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
protov1 "github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand All @@ -29,6 +31,7 @@ import (
type ModuleInputs struct {
depinject.In

Config *modulev1.Module
Codec codec.Codec
StoreService store.KVStoreService
BankKeeper bankkeeper.Keeper
Expand Down Expand Up @@ -57,7 +60,13 @@ func ProvideCustomGetSigner() signing.CustomGetSigner {
}

func ProvideModule(in ModuleInputs) ModuleOutputs { //nolint:gocritic // hugeParam
k := keeper.NewKeeper(in.Codec, in.StoreService, in.BankKeeper, in.AccountKeeper)
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}

k := keeper.NewKeeper(in.Codec, in.StoreService, authority, in.BankKeeper, in.AccountKeeper)
return ModuleOutputs{
Keeper: k,
Module: NewAppModule(in.Codec, k),
Expand Down
Loading

0 comments on commit 81caf73

Please sign in to comment.