Skip to content

Commit

Permalink
refactor custom staking module, remove unnecessary keeper's functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RustNinja committed Dec 22, 2023
1 parent b5658e5 commit 447c600
Show file tree
Hide file tree
Showing 23 changed files with 217 additions and 1,373 deletions.
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.StakingMiddlewareKeeper = stakingmiddleware.NewKeeper(appCodec, appKeepers.keys[stakingmiddlewaretypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())

appKeepers.CustomStakingKeeper = customstaking.NewKeeper(
appCodec /*appKeepers.keys[stakingtypes.StoreKey],*/, *appKeepers.StakingKeeper, appKeepers.AccountKeeper, &appKeepers.MintKeeper, &appKeepers.StakingMiddlewareKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
appCodec /*appKeepers.keys[stakingtypes.StoreKey],*/, *appKeepers.StakingKeeper, appKeepers.AccountKeeper, &appKeepers.StakingMiddlewareKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

appKeepers.DistrKeeper = distrkeeper.NewKeeper(
Expand Down
31 changes: 0 additions & 31 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,17 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"

sdk "github.com/cosmos/cosmos-sdk/types"
mintkeeper "github.com/notional-labs/composable/v6/x/mint/keeper"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper"
)

type Keeper struct {
stakingkeeper.Keeper
cdc codec.BinaryCodec
acck accountkeeper.AccountKeeper
mintkeeper *mintkeeper.Keeper
Stakingmiddleware *stakingmiddleware.Keeper
authority string
}

// func NewBaseKeeper(
// cdc codec.BinaryCodec,
// key storetypes.StoreKey,
// ak types.AccountKeeper,
// acck accountkeeper.AccountKeeper,
// bk bankkeeper.Keeper,
// authority string,
// ) Keeper {
// keeper := Keeper{
// Keeper: *stakingkeeper.NewKeeper(cdc, key, ak, bk, authority),
// acck: acck,
// }
// return keeper
// }

func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, hight int64) []abcicometbft.ValidatorUpdate {
// Calculate validator set changes.
//
Expand Down Expand Up @@ -128,29 +111,15 @@ func NewKeeper(
cdc codec.BinaryCodec,
staking stakingkeeper.Keeper,
acck accountkeeper.AccountKeeper,
mintkeeper *mintkeeper.Keeper,
stakingmiddleware *stakingmiddleware.Keeper,
authority string,
) Keeper {
keeper := Keeper{
Keeper: staking,
acck: acck,
authority: authority,
mintkeeper: mintkeeper,
Stakingmiddleware: stakingmiddleware,
cdc: cdc,
}
return keeper
}

// func (k *Keeper) RegisterKeepers(akk banktypes.StakingKeeper) {
// k.acck = sk
// }

// func (k Keeper) StoreDelegation(ctx sdk.Context, delegation types.Delegation) {
// delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

// store := ctx.KVStore(k.storeKey)
// b := types.MustMarshalDelegation(k.cdc, delegation)
// store.Set(customstakingtypes.GetDelegationKey(delegatorAddress, delegation.GetValidatorAddr()), b)
// }
36 changes: 0 additions & 36 deletions custom/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,18 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida
}

func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {
// //todo add validation. check that this account really has some coins
// ctx := sdk.UnwrapSDKContext(goCtx)

// bondDenom := k.BondDenom(ctx)
// if msg.Amount.Denom != bondDenom {
// return nil, sdkerrors.Wrapf(
// sdkerrors.ErrInvalidRequest, "invalid coin denomination: got %s, expected %s", msg.Amount.Denom, bondDenom,
// )
// }

// // k.mintkeeper.SetLastTotalPower(ctx, math.Int{})
// // k.stakingmiddleware.SetLastTotalPower(ctx, math.Int{})

// // delegations := k.Stakingmiddleware.DequeueAllDelegation(ctx)
// // if len(delegations) > 2 {
// // return nil, sdkerrors.Wrapf(
// // sdkerrors.ErrInvalidRequest, "should always be less then X : got %s, expected %s", len(delegations), 1,
// // )
// // }

// k.Stakingmiddleware.SetDelegation(ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Denom, msg.Amount.Amount)

// return &types.MsgDelegateResponse{}, nil
// return nil, fmt.Errorf("My custom error: Nikita")
return k.msgServer.Delegate(goCtx, msg)
}

func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRedelegate) (*types.MsgBeginRedelegateResponse, error) {
//todo add validation. check that this account really has some coins staked with this validator
// ctx := sdk.UnwrapSDKContext(goCtx)
// k.Stakingmiddleware.SetBeginRedelegation(ctx, msg.DelegatorAddress, msg.ValidatorSrcAddress, msg.ValidatorDstAddress, msg.Amount.Denom, msg.Amount.Amount)
// return &types.MsgBeginRedelegateResponse{}, nil
return k.msgServer.BeginRedelegate(goCtx, msg)
}

func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (*types.MsgUndelegateResponse, error) {
// //todo add validation. check that this account really has some coins staked with this validator
// ctx := sdk.UnwrapSDKContext(goCtx)
// k.Stakingmiddleware.SetUndelegation(ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Denom, msg.Amount.Amount)
// return &types.MsgUndelegateResponse{}, nil
return k.msgServer.Undelegate(goCtx, msg)
}

func (k msgServer) CancelUnbondingDelegation(goCtx context.Context, msg *types.MsgCancelUnbondingDelegation) (*types.MsgCancelUnbondingDelegationResponse, error) {
// //todo add validation. check that this account really has some coins staked with this validator
// ctx := sdk.UnwrapSDKContext(goCtx)
// k.Stakingmiddleware.SetCancelUndelegation(ctx, msg.DelegatorAddress, msg.ValidatorAddress, msg.Amount.Denom, msg.Amount.Amount, msg.CreationHeight)
// return &types.MsgCancelUnbondingDelegationResponse{}, nil
return k.msgServer.CancelUnbondingDelegation(goCtx, msg)
}

Expand Down
70 changes: 0 additions & 70 deletions custom/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,75 +59,5 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}

func (am AppModule) EndBlock(ctx sdk.Context, _abc abcitype.RequestEndBlock) []abcitype.ValidatorUpdate {

return EndBlocker(ctx, &am.keeper)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call

println("EndBlock Custom Staking Module")
params := am.keeper.Stakingmiddleware.GetParams(ctx)
println("BlocksPerEpoch: ", params.BlocksPerEpoch)
println("Height: ", _abc.Height)

should_execute_batch := (_abc.Height % int64(params.BlocksPerEpoch)) == 0
if should_execute_batch {
println("Should batch delegation to be executed at block: ", _abc.Height)

delegations := am.keeper.Stakingmiddleware.DequeueAllDelegation(ctx)
println("Delegations: ", delegations)
println("Delegations len: ", len(delegations))
for _, delegation := range delegations {
println("Delegator Address: ", delegation.DelegatorAddress)
println("Validator Address: ", delegation.ValidatorAddress)
fmt.Println("Amount", delegation.Amount.Amount)

msgDelegate := stakingtypes.MsgDelegate{DelegatorAddress: delegation.DelegatorAddress, ValidatorAddress: delegation.ValidatorAddress, Amount: delegation.Amount}
_, err := am.msgServer.Delegate(ctx, &msgDelegate)
if err != nil {
println("Error for Delegator Address: ", delegation.DelegatorAddress)
}
}

beginredelegations := am.keeper.Stakingmiddleware.DequeueAllRedelegation(ctx)
println("BeginRedelegations: ", beginredelegations)
println("BeginRedelegations len: ", len(beginredelegations))
for _, redelegation := range beginredelegations {
println("Delegator Address: ", redelegation.DelegatorAddress)
println("Validator Address: ", redelegation.ValidatorSrcAddress)

msg_redelegation := stakingtypes.MsgBeginRedelegate{DelegatorAddress: redelegation.DelegatorAddress, ValidatorSrcAddress: redelegation.ValidatorSrcAddress, ValidatorDstAddress: redelegation.ValidatorDstAddress, Amount: redelegation.Amount}
_, err := am.msgServer.BeginRedelegate(ctx, &msg_redelegation)
if err != nil {
println("Error for Delegator Address: ", msg_redelegation.DelegatorAddress)
}
}

undelegations := am.keeper.Stakingmiddleware.DequeueAllUndelegation(ctx)
println("Undelegation: ", beginredelegations)
println("Undelegation len: ", len(beginredelegations))
for _, undelegation := range undelegations {
println("Undelegation Delegator Address: ", undelegation.DelegatorAddress)
println("Undelegation Validator Address: ", undelegation.ValidatorAddress)

msg_undelegate := stakingtypes.MsgUndelegate{DelegatorAddress: undelegation.DelegatorAddress, ValidatorAddress: undelegation.ValidatorAddress, Amount: undelegation.Amount}
_, err := am.msgServer.Undelegate(ctx, &msg_undelegate)
if err != nil {
println("Error for Delegator Address: ", msg_undelegate.DelegatorAddress)
}
}

cancel_unbonding_delegations := am.keeper.Stakingmiddleware.DequeueAllCancelUnbondingDelegation(ctx)
println("Cancel Unbonding Delegations: ", cancel_unbonding_delegations)
println("Cancel Ubonding Delegations len: ", len(cancel_unbonding_delegations))
for _, cancel_unbonding_delegation := range cancel_unbonding_delegations {
println("Cancel Unbonding Delegation Delegator Address: ", cancel_unbonding_delegation.DelegatorAddress)
println("Cancel Unbonding Delegations Validator Address: ", cancel_unbonding_delegation.ValidatorAddress)

msg_cancle_unbonding_delegation := stakingtypes.MsgCancelUnbondingDelegation{DelegatorAddress: cancel_unbonding_delegation.DelegatorAddress, ValidatorAddress: cancel_unbonding_delegation.ValidatorAddress, Amount: cancel_unbonding_delegation.Amount, CreationHeight: cancel_unbonding_delegation.CreationHeight}
_, err := am.msgServer.CancelUnbondingDelegation(ctx, &msg_cancle_unbonding_delegation)
if err != nil {
println("Error for Delegator Address: ", msg_cancle_unbonding_delegation.DelegatorAddress)
}
}
}

return am.AppModule.EndBlock(ctx, _abc)
}
10 changes: 0 additions & 10 deletions custom/staking/types/keeper_interfaces.go

This file was deleted.

7 changes: 0 additions & 7 deletions proto/centauri/mint/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,4 @@ message GenesisState {
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false
];

// last_total_power tracks the total amounts of bonded tokens recorded during
// the previous end block.
bytes last_total_power = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
15 changes: 1 addition & 14 deletions proto/centauri/stakingmiddleware/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,5 @@ option go_package = "x/stakingmiddleware/types";

// GenesisState defines the stakingmiddleware module's genesis state.
message GenesisState {
// last_total_power tracks the total amounts of bonded tokens recorded during
// the previous end block.
bytes last_total_power = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];

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

// delegations defines the delegations active at genesis.
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];
Params params = 1 [ (gogoproto.nullable) = false ];
}
11 changes: 7 additions & 4 deletions proto/centauri/stakingmiddleware/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package centauri.stakingmiddleware.v1beta1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "centauri/stakingmiddleware/v1beta1/stakingmiddleware.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";
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/params";
}
}

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

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryPowerResponse {
message QueryParamsResponse {
// params defines the parameters of the module.
Params params = 1 [ (gogoproto.nullable) = false ];
}
15 changes: 0 additions & 15 deletions proto/centauri/stakingmiddleware/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,8 @@ service Msg {
option (cosmos.msg.v1.service) = true;

rpc UpdateEpochParams(MsgUpdateEpochParams) returns (MsgUpdateParamsEpochResponse);

rpc SetPower(MsgSetPower) returns (MsgSetPowerResponse);
}

// MsgSetPower is the Msg/SetPower request type.
//
// Since: cosmos-sdk 0.47
message MsgSetPower {
string from_address = 1;
}

// MsgSetPowerResponse defines the response structure for executing a
// MsgSetPower message.
//
// Since: cosmos-sdk 0.47
message MsgSetPowerResponse {}

// MsgUpdateParams is the Msg/UpdateParams request type.
//
// Since: cosmos-sdk 0.47
Expand Down
Loading

0 comments on commit 447c600

Please sign in to comment.