Skip to content

Commit

Permalink
introduce staking middleware prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
RustNinja committed Dec 16, 2023
1 parent 1a79bc5 commit 3708b33
Show file tree
Hide file tree
Showing 15 changed files with 1,600 additions and 1 deletion.
1 change: 1 addition & 0 deletions proto/centauri/stakingmiddleware/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package centauri.stakingmiddleware.v1beta1;

import "gogoproto/gogo.proto";


option go_package = "x/stakingmiddleware/types";

// GenesisState defines the stakingmiddleware module's genesis state.
Expand Down
22 changes: 22 additions & 0 deletions proto/centauri/stakingmiddleware/v1beta1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";
package centauri.stakingmiddleware.v1beta1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";

option go_package = "x/stakingmiddleware/types";

// Query provides defines the gRPC querier service.
service Query {
// Params returns the total set of minting parameters.
rpc Power(QueryPowerRequest) returns (QueryPowerResponse) {
option (google.api.http).get = "/cosmos/stakingmiddleware/v1beta1/params";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
message QueryPowerRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryPowerResponse {
}
28 changes: 28 additions & 0 deletions proto/centauri/stakingmiddleware/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";
package centauri.stakingmiddleware.v1beta1;

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

option go_package = "x/stakingmiddleware/types";

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

rpc SetPower(MsgSetPower) returns (MsgSetPowerResponse);
}

// MsgSetPower is the Msg/SetPower request type.
//
// Since: cosmos-sdk 0.47
message MsgSetPower {
}

// MsgSetPowerResponse defines the response structure for executing a
// MsgSetPower message.
//
// Since: cosmos-sdk 0.47
message MsgSetPowerResponse {}
14 changes: 14 additions & 0 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,17 @@ func (k Keeper) SetLastTotalPower(ctx sdk.Context, power math.Int) {
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: power})
store.Set(types.DelegationKey, bz)
}

func (k Keeper) GetLastTotalPower(ctx sdk.Context) math.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DelegationKey)

if bz == nil {
return math.ZeroInt()
}

ip := sdk.IntProto{}
k.cdc.MustUnmarshal(bz, &ip)

return ip.Int
}
60 changes: 60 additions & 0 deletions x/stakingmiddleware/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

// GetQueryCmd returns the cli query commands for the staking middleware module.
func GetQueryCmd() *cobra.Command {
mintingQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the minting module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

mintingQueryCmd.AddCommand(
GetCmdQueryParams(),
)

return mintingQueryCmd
}

// GetCmdQueryParams implements a command to return the current minting
// parameters.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current minting parameters",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryPowerRequest{}
res, err := queryClient.Power(cmd.Context(), params)
if err != nil {
return err
}
_ = res

// return nil
return clientCtx.PrintString(fmt.Sprintf("%s\n", "hello world"))
// return clientCtx.PrintProto(&res.Power)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
23 changes: 23 additions & 0 deletions x/stakingmiddleware/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

"github.com/notional-labs/composable/v6/x/mint/types"
)

// GetTxCmd returns the tx commands for mint
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Exp transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand()

return txCmd
}
17 changes: 17 additions & 0 deletions x/stakingmiddleware/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

Check failure on line 1 in x/stakingmiddleware/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

// InitGenesis new mint genesis
func (keeper Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
keeper.SetLastTotalPower(ctx, data.LastTotalPower)
}

// 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)
}
101 changes: 101 additions & 0 deletions x/stakingmiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package keeper

import (
"cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
"github.com/notional-labs/composable/v6/x/mint/types"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
// "github.com/notional-labs/composable/v6/x/mint/types"
)

// Keeper of the staking middleware store
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
}

// NewKeeper creates a new middleware Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
authority string,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: key,
authority: authority,
}
}

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

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

// // SetParams sets the x/mint module parameters.
// func (k Keeper) SetParams(ctx sdk.Context, p types.Params) error {
// if err := p.Validate(); err != nil {
// return err
// }

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

// func (k Keeper) StoreDelegation(ctx sdk.Context, delegation stakingtypes.Delegation) {
// delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)
// log := k.Logger(ctx)
// log.Info("StoreDelegation", "delegatorAddress", delegatorAddress, "validatorAddress", delegation.GetValidatorAddr())
// store := ctx.KVStore(k.storeKey)
// b := stakingtypes.MustMarshalDelegation(k.cdc, delegation)
// kkk := types.GetDelegationKey(delegatorAddress, delegation.GetValidatorAddr())
// // log.Info()
// store.Set(kkk, b)
// }

// SetLastTotalPower Set the last total validator power.
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power math.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: power})
store.Set(types.DelegationKey, bz)
}

func (k Keeper) GetLastTotalPower(ctx sdk.Context) math.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DelegationKey)

if bz == nil {
return math.ZeroInt()
}

ip := sdk.IntProto{}
k.cdc.MustUnmarshal(bz, &ip)

return ip.Int
}
27 changes: 27 additions & 0 deletions x/stakingmiddleware/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package keeper

import (
"context"

"github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

var _ types.MsgServer = msgServer{}

// msgServer is a wrapper of Keeper.
type msgServer struct {
Keeper
}

// NewMsgServerImpl returns an implementation of the x/mint MsgServer interface.
func NewMsgServerImpl(k Keeper) types.MsgServer {
return &msgServer{
Keeper: k,
}
}

// UpdateParams updates the params.
func (ms msgServer) SetPower(goCtx context.Context, req *types.MsgSetPower) (*types.MsgUpdateParamsResponse, error) {

Check failure on line 24 in x/stakingmiddleware/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / build

undefined: types.MsgUpdateParamsResponse

Check failure on line 24 in x/stakingmiddleware/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

undefined: types.MsgUpdateParamsResponse (typecheck)

Check failure on line 24 in x/stakingmiddleware/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

undefined: types.MsgUpdateParamsResponse) (typecheck)

Check failure on line 24 in x/stakingmiddleware/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / test

undefined: types.MsgUpdateParamsResponse

Check failure on line 24 in x/stakingmiddleware/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / build

undefined: types.MsgUpdateParamsResponse

return &types.MsgSetPowerResponse{}, nil
}
Loading

0 comments on commit 3708b33

Please sign in to comment.