Skip to content

Commit

Permalink
add msg epoch params
Browse files Browse the repository at this point in the history
  • Loading branch information
RustNinja committed Dec 21, 2023
1 parent 3ba9ca9 commit 6dafbcf
Show file tree
Hide file tree
Showing 13 changed files with 823 additions and 86 deletions.
10 changes: 6 additions & 4 deletions proto/centauri/stakingmiddleware/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ message GenesisState {
(gogoproto.nullable) = false
];

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

// delegations defines the delegations active at genesis.
repeated Delegation delegations = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated BeginRedelegate begindelegations = 3 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated Undelegate undelegates = 4 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated CancelUnbondingDelegation cancelunbondingdelegations = 5 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated Delegation delegations = 3 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated BeginRedelegate begindelegations = 4 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated Undelegate undelegates = 5 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
repeated CancelUnbondingDelegation cancelunbondingdelegations = 6 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ message CancelUnbondingDelegation {
int64 creation_height = 4;
}



// Params holds parameters for the stakingmiddleware module.
message Params {
// expected blocks per year
uint64 blocks_per_epoch = 1;
}

27 changes: 27 additions & 0 deletions proto/centauri/stakingmiddleware/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import "cosmos/msg/v1/msg.proto";
import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "centauri/stakingmiddleware/v1beta1/stakingmiddleware.proto";

option go_package = "x/stakingmiddleware/types";

// Msg defines the x/stakingmiddleware Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;

rpc UpdateEpochParams(MsgUpdateEpochParams) returns (MsgUpdateParamsEpochResponse);

rpc SetPower(MsgSetPower) returns (MsgSetPowerResponse);
}

Expand All @@ -27,3 +30,27 @@ message MsgSetPower {
//
// Since: cosmos-sdk 0.47
message MsgSetPowerResponse {}

// MsgUpdateParams is the Msg/UpdateParams request type.
//
// Since: cosmos-sdk 0.47
message MsgUpdateEpochParams {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "centauri/x/stakingmiddleware/MsgUpdateParams";

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

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

// MsgUpdateParamsResponse defines the response structure for executing a
// MsgUpdateParams message.
//
// Since: cosmos-sdk 0.47
message MsgUpdateParamsEpochResponse {}
9 changes: 7 additions & 2 deletions x/stakingmiddleware/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
// InitGenesis new mint genesis
func (keeper Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
keeper.SetLastTotalPower(ctx, data.LastTotalPower)

if err := keeper.SetParams(ctx, data.Params); err != nil {
panic(err)
}
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
minter := keeper.GetLastTotalPower(ctx)
return types.NewGenesisState(minter)
power := keeper.GetLastTotalPower(ctx)
params := keeper.GetParams(ctx)
return types.NewGenesisState(power, params)
}
31 changes: 31 additions & 0 deletions x/stakingmiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

"github.com/cometbft/cometbft/libs/log"
"github.com/notional-labs/composable/v6/x/stakingmiddleware/types"

Expand Down Expand Up @@ -79,6 +81,35 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
// store.Set(kkk, b)
// }

// SetParams sets the x/mint module parameters.
func (k Keeper) SetParams(ctx sdk.Context, p types.Params) error {
if p.BlocksPerEpoch < 5 {
//return error
return fmt.Errorf(
"BlocksPerEpoch must be greater than or equal to 5",
)

}

store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&p)
store.Set(types.ParamsKey, bz)

return nil
}

// GetParams returns the current x/mint module parameters.
func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return p
}

k.cdc.MustUnmarshal(bz, &p)
return p
}

// SetLastTotalPower Set the last total validator power.
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdkmath.Int) {
store := ctx.KVStore(k.storeKey)
Expand Down
17 changes: 17 additions & 0 deletions x/stakingmiddleware/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ 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/notional-labs/composable/v6/x/stakingmiddleware/types"
)

Expand All @@ -20,6 +23,20 @@ func NewMsgServerImpl(k Keeper) types.MsgServer {
}
}

// UpdateParams updates the params.
func (ms msgServer) UpdateEpochParams(goCtx context.Context, req *types.MsgUpdateEpochParams) (*types.MsgUpdateParamsEpochResponse, error) {
if ms.authority != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority, req.Authority)
}

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

return &types.MsgUpdateParamsEpochResponse{}, nil
}

// UpdateParams updates the params.
func (ms msgServer) SetPower(goCtx context.Context, req *types.MsgSetPower) (*types.MsgSetPowerResponse, error) {
return &types.MsgSetPowerResponse{}, nil
Expand Down
2 changes: 2 additions & 0 deletions x/stakingmiddleware/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgSetPower{}, "composable/MsgSetPower")
legacy.RegisterAminoMsg(cdc, &MsgUpdateEpochParams{}, "composable/MsgUpdateEpochParams")
}

func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgSetPower{},
&MsgUpdateEpochParams{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
Expand Down
4 changes: 3 additions & 1 deletion x/stakingmiddleware/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
)

// NewGenesisState creates a new GenesisState object
func NewGenesisState(m math.Int) *GenesisState {
func NewGenesisState(m math.Int, params Params) *GenesisState {
return &GenesisState{
LastTotalPower: m,
Params: params,
}
}

// DefaultGenesisState creates a default GenesisState object
func DefaultGenesisState() *GenesisState {
return &GenesisState{
LastTotalPower: sdk.ZeroInt(),
Params: Params{BlocksPerEpoch: 10},
}
}

Expand Down
Loading

0 comments on commit 6dafbcf

Please sign in to comment.