Skip to content

Commit

Permalink
refactor(mint): add tx to update params
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 29, 2023
1 parent fe7c618 commit d35928b
Show file tree
Hide file tree
Showing 7 changed files with 736 additions and 4 deletions.
32 changes: 32 additions & 0 deletions proto/mint/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
syntax = "proto3";

package mint.v1beta1;

import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "mint/v1beta1/mint.proto";

option go_package = "github.com/okp4/okp4d/x/mint/types";

// MsgService defines the service for the logic module.
// Do nothing for now as the service is without any side effects.
service MsgService {
// UpdateParams defined a governance operation for updating the x/mint module parameters.
// The authority is hard-coded to the Cosmos SDK x/gov module account
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// MsgUpdateParams defines a Msg for updating the x/mint module parameters.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
// authority is the address of the governance account.
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// params defines the x/mint parameters to update.
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false];
}

// MsgUpdateParamsResponse defines the response structure for executing a
// MsgUpdateParams message.
message MsgUpdateParamsResponse {}
4 changes: 2 additions & 2 deletions x/logic/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/okp4/okp4d/x/logic/types"
)
Expand All @@ -27,7 +27,7 @@ var _ types.MsgServiceServer = msgServer{}
// account.
func (ms msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.authority.String() != req.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority.String(), req.Authority)
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority.String(), req.Authority)
}

ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
39 changes: 39 additions & 0 deletions x/mint/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package keeper

import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/okp4/okp4d/x/mint/types"
)

type msgServer struct {
Keeper
}

// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServiceServer {
return &msgServer{Keeper: keeper}
}

var _ types.MsgServiceServer = msgServer{}

// UpdateParams implements the gRPC MsgServer interface. When an UpdateParams
// proposal passes, it updates the module parameters. The update can only be
// performed if the requested authority is the Cosmos SDK governance module
// account.
func (ms msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.authority.String() != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority.String(), req.Authority)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := ms.SetParams(ctx, req.Params); err != nil {
return nil, err
}

return &types.MsgUpdateParamsResponse{}, nil
}
5 changes: 4 additions & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func (AppModuleBasic) Name() string {
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}

// RegisterInterfaces registers the module's interface types.
func (b AppModuleBasic) RegisterInterfaces(_ cdctypes.InterfaceRegistry) {}
func (b AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {
types.RegisterInterfaces(reg)
}

// DefaultGenesis returns default genesis state as raw bytes for the mint
// module.
Expand Down Expand Up @@ -118,6 +120,7 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// RegisterServices registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServiceServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

Expand Down
34 changes: 33 additions & 1 deletion x/mint/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,44 @@ package types

import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

var amino = codec.NewLegacyAmino()
var (
amino = codec.NewLegacyAmino()
// ModuleCdc references the global evm module codec. Note, the codec should
// ONLY be used in certain instances of tests and for JSON encoding.
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())

// AminoCdc is a amino codec created to support amino JSON compatible msgs.
AminoCdc = codec.NewAminoCodec(amino)
)

func init() {
cryptocodec.RegisterCrypto(amino)
RegisterLegacyAminoCodec(amino)
amino.Seal()
}

const (
// Amino names.
updateParamsName = "okp4/mint/MsgUpdateParams"
)

// RegisterInterfaces register implementations
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgUpdateParams{},
)

msgservice.RegisterMsgServiceDesc(registry, &_MsgService_serviceDesc)
}

// RegisterLegacyAminoCodec required for EIP-712
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgUpdateParams{}, updateParamsName, nil)
}
32 changes: 32 additions & 0 deletions x/mint/types/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ sdk.Msg = &MsgUpdateParams{}

// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr := sdk.MustAccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}

// ValidateBasic does a sanity check of the provided data.
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}

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

return nil
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&m))
}
Loading

0 comments on commit d35928b

Please sign in to comment.