Skip to content

Commit

Permalink
Merge pull request #318 from ComposableFi/msg-epoch-params
Browse files Browse the repository at this point in the history
Msg epoch params
  • Loading branch information
RustNinja authored Dec 21, 2023
2 parents 3ba9ca9 + 1af77d8 commit 0f073ca
Show file tree
Hide file tree
Showing 14 changed files with 883 additions and 152 deletions.
126 changes: 60 additions & 66 deletions custom/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,77 +61,71 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
func (am AppModule) EndBlock(ctx sdk.Context, _abc abcitype.RequestEndBlock) []abcitype.ValidatorUpdate {

Check failure on line 62 in custom/staking/module.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
println("EndBlock Custom Staking Module")

delegations := am.keeper.Stakingmiddleware.DequeueAllDelegation(ctx)
println("Delegations: ", delegations)
println("Delegations len: ", len(delegations))
//for delegations print the delegator address and the validator address
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)
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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
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 _, beginredelegation := range beginredelegations {
println("Delegator Address: ", beginredelegation.DelegatorAddress)
println("Validator Address: ", beginredelegation.ValidatorSrcAddress)
// 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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
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)
// 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)
// }
}
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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
if err != nil {
println("Error for Delegator Address: ", msg_undelegate.DelegatorAddress)
}
}

//DequeueAllCancelUnbondingDelegation
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 _, undelegation := range cancel_unbonding_delegations {
println("Cancel Unbonding Delegation Delegator Address: ", undelegation.DelegatorAddress)
println("Cancel Unbonding Delegations Validator Address: ", undelegation.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)
// }
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)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
if err != nil {
println("Error for Delegator Address: ", msg_cancle_unbonding_delegation.DelegatorAddress)
}
}
}

// ctx.EventManager().EmitEvent(
// sdk.NewEvent(
// "DequeueAllDelegation",
// // sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
// // sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress),
// // sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress),
// ),
// )

return am.AppModule.EndBlock(ctx, _abc)
}
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

Check failure on line 87 in x/stakingmiddleware/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
return fmt.Errorf(
"BlocksPerEpoch must be greater than or equal to 5",
)

Check failure on line 91 in x/stakingmiddleware/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
}

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 0f073ca

Please sign in to comment.