Skip to content

Commit

Permalink
chore: add missing staking messages
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Jan 24, 2025
1 parent 57eed47 commit 0bda564
Show file tree
Hide file tree
Showing 5 changed files with 835 additions and 50 deletions.
25 changes: 25 additions & 0 deletions proto/babylon/epoching/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,28 @@ message EventWrappedCancelUnbondingDelegation {
int64 creation_height = 4;
uint64 epoch_boundary = 5;
}

// EventWrappedEditValidator is the event emitted when a
// MsgWrappedEditValidator has been queued
message EventWrappedEditValidator {
string validator_address = 1;
uint64 epoch_boundary = 2;
}

// EventWrappedStakingUpdateParams is the event emitted when a
// MsgWrappedStakingUpdateParams has been queued
message EventWrappedStakingUpdateParams {
// unbonding_time is the time duration of unbonding.
string unbonding_time = 1;
// max_validators is the maximum number of validators.
uint32 max_validators = 2;
// max_entries is the max entries for either unbonding delegation or redelegation (per pair/trio).
uint32 max_entries = 3;
// historical_entries is the number of historical entries to persist.
uint32 historical_entries = 4;
// bond_denom defines the bondable coin denomination.
string bond_denom = 5;
// min_commission_rate is the chain-wide minimum commission rate that a validator can charge their delegators
string min_commission_rate = 6;
uint64 epoch_boundary = 7;
}
4 changes: 2 additions & 2 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ deps:
- remote: buf.build
owner: cosmos
repository: ibc
commit: f3cb7e9a10ab4cd5a402b9913f65e38d
digest: shake256:aaa8a2a46a3ab5cb4c54f9f14920bc472e5e5f93453fa250de49f15c76dce7f82faa5615afd341967797ead8052306db6b1e6327cb1d8d94345bf07a22f8aebd
commit: baf134b1c13a45e9903beeba3b2a92ec
digest: shake256:0069c2b35e359966bfe3f30ec98bc76180f277eb6fe722cdc6d20e68257d9b888db3b801c9fdc8ae1014439d786420f21ba2147d8fee7b1dc1dc748ff84e1fe4
- remote: buf.build
owner: cosmos
repository: ics23
Expand Down
5 changes: 2 additions & 3 deletions x/epoching/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package keeper
import (
"fmt"

corestoretypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
sdk "github.com/cosmos/cosmos-sdk/types"
stktypes "github.com/cosmos/cosmos-sdk/x/staking/types"

corestoretypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
"github.com/babylonlabs-io/babylon/x/epoching/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
stktypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type (
Expand Down
126 changes: 125 additions & 1 deletion x/epoching/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/cometbft/cometbft/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stktypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/babylonlabs-io/babylon/x/epoching/types"
)
Expand All @@ -16,13 +18,135 @@ type msgServer struct {
Keeper
}

var _ types.MsgServer = msgServer{}

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

var _ types.MsgServer = msgServer{}
// WrappedEditValidator handles the MsgWrappedEditValidator request
func (ms msgServer) WrappedEditValidator(goCtx context.Context, msgWrapped *types.MsgWrappedEditValidator) (*types.MsgWrappedEditValidatorResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if msgWrapped.Msg == nil {
return nil, types.ErrNoWrappedMsg
}
msg := msgWrapped.Msg

// verification rules ported from staking module
valAddr, valErr := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if valErr != nil {
return nil, valErr
}

if msg.Description == (stktypes.Description{}) {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty description")
}

if msg.MinSelfDelegation != nil && !msg.MinSelfDelegation.IsPositive() {
return nil, errorsmod.Wrap(
sdkerrors.ErrInvalidRequest,
"minimum self delegation must be a positive integer",
)
}

if msg.CommissionRate != nil {
if msg.CommissionRate.GT(math.LegacyOneDec()) || msg.CommissionRate.IsNegative() {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "commission rate must be between 0 and 1 (inclusive)")
}

stkParams, err := ms.stk.GetParams(ctx)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, err.Error())
}

minCommissionRate := stkParams.MinCommissionRate

if msg.CommissionRate.LT(minCommissionRate) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "commission rate cannot be less than the min commission rate %s", minCommissionRate.String())
}
}

// validator must already be registered
_, err := ms.stk.GetValidator(ctx, valAddr)
if err != nil {
return nil, err
}

blockHeight := uint64(ctx.HeaderInfo().Height)
if blockHeight == 0 {
return nil, types.ErrZeroEpochMsg
}
blockTime := ctx.HeaderInfo().Time

txid := tmhash.Sum(ctx.TxBytes())
queuedMsg, err := types.NewQueuedMessage(blockHeight, blockTime, txid, msg)
if err != nil {
return nil, err
}

ms.EnqueueMsg(ctx, queuedMsg)

err = ctx.EventManager().EmitTypedEvents(
&types.EventWrappedEditValidator{
ValidatorAddress: msg.ValidatorAddress,
EpochBoundary: ms.GetEpoch(ctx).GetLastBlockHeight(),
},
)
if err != nil {
return nil, err
}

return &types.MsgWrappedEditValidatorResponse{}, nil
}

// WrappedStakingUpdateParams handles the MsgWrappedStakingUpdateParams request
func (ms msgServer) WrappedStakingUpdateParams(goCtx context.Context, msgWrapped *types.MsgWrappedStakingUpdateParams) (*types.MsgWrappedStakingUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if msgWrapped.Msg == nil {
return nil, types.ErrNoWrappedMsg
}
msg := msgWrapped.Msg
if ms.authority != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority, msg.Authority)
}

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

blockHeight := uint64(ctx.HeaderInfo().Height)
if blockHeight == 0 {
return nil, types.ErrZeroEpochMsg
}
blockTime := ctx.HeaderInfo().Time

txid := tmhash.Sum(ctx.TxBytes())
queuedMsg, err := types.NewQueuedMessage(blockHeight, blockTime, txid, msg)
if err != nil {
return nil, err
}

ms.EnqueueMsg(ctx, queuedMsg)

err = ctx.EventManager().EmitTypedEvents(
&types.EventWrappedStakingUpdateParams{
UnbondingTime: msg.Params.UnbondingTime.String(),
MaxValidators: msg.Params.MaxValidators,
MaxEntries: msg.Params.MaxEntries,
HistoricalEntries: msg.Params.HistoricalEntries,
BondDenom: msg.Params.BondDenom,
MinCommissionRate: msg.Params.MinCommissionRate.String(),
EpochBoundary: ms.GetEpoch(ctx).GetLastBlockHeight(),
},
)
if err != nil {
return nil, err
}

return &types.MsgWrappedStakingUpdateParamsResponse{}, nil
}

// WrappedDelegate handles the MsgWrappedDelegate request
func (ms msgServer) WrappedDelegate(goCtx context.Context, msg *types.MsgWrappedDelegate) (*types.MsgWrappedDelegateResponse, error) {
Expand Down
Loading

0 comments on commit 0bda564

Please sign in to comment.