Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[staking] feat: Add more fields to staking proto messages #471

Open
wants to merge 4 commits into
base: evm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ message UnbondingDelegationEntry {
];
// balance defines the tokens to receive at completion.
string balance = 4 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];

// Incrementing id that uniquely identifies this entry
uint64 unbonding_id = 5;

// Strictly positive if this entry's unbonding has been stopped by external modules
int64 unbonding_on_hold_ref_count = 6;
}

// RedelegationEntry defines a redelegation object with relevant metadata.
Expand Down Expand Up @@ -295,13 +301,14 @@ message Params {
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "max_voting_power_ratio"
];
// max_voting_power_enforcement_threshold defines the minimal bonded voting power of the max voting power ratio enforcement
// max_voting_power_enforcement_threshold defines the minimal bonded voting power of the max voting power ratio
// enforcement
string max_voting_power_enforcement_threshold = 8 [
(gogoproto.moretags) = "yaml:\"max_voting_power_enforcement_threshold\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "max_voting_power_enforcement_threshold"
];
];
}

// DelegationResponse is equivalent to Delegation except that it contains a
Expand Down
8 changes: 7 additions & 1 deletion proto/cosmos/staking/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ message MsgDelegate {
}

// MsgDelegateResponse defines the Msg/Delegate response type.
message MsgDelegateResponse {}
message MsgDelegateResponse {
// shares define the delegation shares received.
string shares = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// MsgBeginRedelegate defines a SDK message for performing a redelegation
// of coins from a delegator and source validator to a destination validator.
Expand Down Expand Up @@ -120,4 +123,7 @@ message MsgUndelegate {
// MsgUndelegateResponse defines the Msg/Undelegate response type.
message MsgUndelegateResponse {
google.protobuf.Timestamp completion_time = 1 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
// Incrementing id that uniquely identifies this entry
uint64 unbonding_id = 2;

}
26 changes: 20 additions & 6 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"bytes"
"fmt"
"slices"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -149,6 +150,19 @@ func (k Keeper) GetUnbondingDelegation(
return ubd, true
}

func (k Keeper) GetUnbondingDelegationByUnbondingID(
ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, unbondingID uint64,
) (ubdEntry types.UnbondingDelegationEntry, found bool) {
ubd, found := k.GetUnbondingDelegation(ctx, delAddr, valAddr)
if !found {
return ubdEntry, false
}
index := slices.IndexFunc(ubd.Entries, func(e types.UnbondingDelegationEntry) bool {
return e.UnbondingId == unbondingID
})
return ubd.Entries[index], index != -1
}

// GetUnbondingDelegationsFromValidator returns all unbonding delegations from a
// particular validator.
func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.ValAddress) (ubds []types.UnbondingDelegation) {
Expand Down Expand Up @@ -829,31 +843,31 @@ func (k Keeper) getBeginInfo(
// processed during the staking EndBlocker.
func (k Keeper) Undelegate(
ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, sharesAmount sdk.Dec,
) (time.Time, error) {
) (unbondingID uint64, completionTime time.Time, err error) {
validator, found := k.GetValidator(ctx, valAddr)
if !found {
return time.Time{}, types.ErrNoDelegatorForAddress
return 0, time.Time{}, types.ErrNoDelegatorForAddress
}

if k.HasMaxUnbondingDelegationEntries(ctx, delAddr, valAddr) {
return time.Time{}, types.ErrMaxUnbondingDelegationEntries
return 0, time.Time{}, types.ErrMaxUnbondingDelegationEntries
}

returnAmount, err := k.Unbond(ctx, delAddr, valAddr, sharesAmount)
if err != nil {
return time.Time{}, err
return 0, time.Time{}, err
}

// transfer the validator tokens to the not bonded pool
if validator.IsBonded() {
k.bondedTokensToNotBonded(ctx, returnAmount)
}

completionTime := ctx.BlockHeader().Time.Add(k.UnbondingTime(ctx))
completionTime = ctx.BlockHeader().Time.Add(k.UnbondingTime(ctx))
ubd := k.SetUnbondingDelegationEntry(ctx, delAddr, valAddr, ctx.BlockHeight(), completionTime, returnAmount)
k.InsertUBDQueue(ctx, ubd, completionTime)

return completionTime, nil
return ubd.Entries[len(ubd.Entries)-1].UnbondingId, completionTime, nil
}

// CompleteUnbonding completes the unbonding of all mature entries in the
Expand Down
32 changes: 15 additions & 17 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func TestSetDelegation(t *testing.T) {
require.Equal(t, 0, len(resBonds))
}


func TestDelegationGentx(t *testing.T) {
_, app, ctx := createTestInput()

Expand Down Expand Up @@ -167,7 +166,6 @@ func TestDelegationGentx(t *testing.T) {
require.Equal(t, 1, len(allBonds))
}


func TestDelegation(t *testing.T) {
_, app, ctx := createTestInput()

Expand Down Expand Up @@ -353,7 +351,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
var completionTime time.Time
for i := uint32(0); i < maxEntries; i++ {
var err error
completionTime, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1))
_, completionTime, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1))
require.NoError(t, err)
}

Expand All @@ -366,7 +364,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
oldNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount

// an additional unbond should fail due to max entries
_, err := app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1))
_, _, err := app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1))
require.Error(t, err)

newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
Expand All @@ -388,7 +386,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
oldNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount

// unbonding should work again
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1))
_, _, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], sdk.NewDec(1))
require.NoError(t, err)

newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
Expand All @@ -397,8 +395,8 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.AddRaw(1)))
}

//// test undelegating self delegation from a validator pushing it below MinSelfDelegation
//// shift it from the bonded to unbonding state and jailed
// // test undelegating self delegation from a validator pushing it below MinSelfDelegation
// // shift it from the bonded to unbonding state and jailed
func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) {
_, app, ctx := createTestInput()

Expand Down Expand Up @@ -446,7 +444,7 @@ func TestUndelegateSelfDelegationBelowMinSelfDelegation(t *testing.T) {
app.StakingKeeper.SetDelegation(ctx, delegation)

val0AccAddr := sdk.AccAddress(addrVals[0].Bytes())
_, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], app.StakingKeeper.TokensFromConsensusPower(ctx, 6).ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], app.StakingKeeper.TokensFromConsensusPower(ctx, 6).ToDec())
require.NoError(t, err)

// end block
Expand Down Expand Up @@ -515,7 +513,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) {

// unbond the all self-delegation to put validator in unbonding state
val0AccAddr := sdk.AccAddress(addrVals[0])
_, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], delTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], delTokens.ToDec())
require.NoError(t, err)

// end block
Expand All @@ -533,7 +531,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) {
ctx = ctx.WithBlockTime(blockTime2)

// unbond some of the other delegation's shares
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], sdk.NewDec(6))
_, _, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], sdk.NewDec(6))
require.NoError(t, err)

// retrieve the unbonding delegation
Expand Down Expand Up @@ -590,7 +588,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {
ctx = ctx.WithBlockTime(time.Unix(333, 0))

// unbond the all self-delegation to put validator in unbonding state
_, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec())
require.NoError(t, err)

// end block
Expand All @@ -613,12 +611,12 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {

// unbond some of the other delegation's shares
unbondTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 6)
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], unbondTokens.ToDec())
_, _, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], unbondTokens.ToDec())
require.NoError(t, err)

// unbond rest of the other delegation's shares
remainingTokens := delTokens.Sub(unbondTokens)
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], remainingTokens.ToDec())
_, _, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], remainingTokens.ToDec())
require.NoError(t, err)

// now validator should be deleted from state
Expand Down Expand Up @@ -674,14 +672,14 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) {
ctx = ctx.WithBlockTime(time.Unix(333, 0))

// unbond the all self-delegation to put validator in unbonding state
_, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec())
require.NoError(t, err)

// end block
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, 1)

// unbond all the remaining delegation
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], delTokens.ToDec())
_, _, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], delTokens.ToDec())
require.NoError(t, err)

// validator should still be in state and still be in unbonding state
Expand Down Expand Up @@ -979,7 +977,7 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) {
ctx = ctx.WithBlockHeader(header)

// unbond the all self-delegation to put validator in unbonding state
_, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], delTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], delTokens.ToDec())
require.NoError(t, err)

// end block
Expand Down Expand Up @@ -1058,7 +1056,7 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) {
ctx = ctx.WithBlockTime(time.Unix(333, 0))

// unbond the all self-delegation to put validator in unbonding state
_, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], delTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], delTokens.ToDec())
require.NoError(t, err)

// end block
Expand Down
8 changes: 4 additions & 4 deletions x/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryUnbondingDelegation() {
unbondingTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 2)
valAddr, err1 := sdk.ValAddressFromBech32(addrVal2)
suite.NoError(err1)
_, err := app.StakingKeeper.Undelegate(ctx, addrAcc2, valAddr, unbondingTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, addrAcc2, valAddr, unbondingTokens.ToDec())
suite.NoError(err)

unbond, found := app.StakingKeeper.GetUnbondingDelegation(ctx, addrAcc2, valAddr)
Expand Down Expand Up @@ -480,11 +480,11 @@ func (suite *KeeperTestSuite) TestGRPCQueryDelegatorUnbondingDelegations() {
unbondingTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 2)
valAddr1, err1 := sdk.ValAddressFromBech32(addrVal)
suite.NoError(err1)
_, err := app.StakingKeeper.Undelegate(ctx, addrAcc, valAddr1, unbondingTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, addrAcc, valAddr1, unbondingTokens.ToDec())
suite.NoError(err)
valAddr2, err1 := sdk.ValAddressFromBech32(addrVal2)
suite.NoError(err1)
_, err = app.StakingKeeper.Undelegate(ctx, addrAcc, valAddr2, unbondingTokens.ToDec())
_, _, err = app.StakingKeeper.Undelegate(ctx, addrAcc, valAddr2, unbondingTokens.ToDec())
suite.NoError(err)

unbond, found := app.StakingKeeper.GetUnbondingDelegation(ctx, addrAcc, valAddr1)
Expand Down Expand Up @@ -724,7 +724,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidatorUnbondingDelegations() {

// undelegate
undelAmount := app.StakingKeeper.TokensFromConsensusPower(ctx, 2)
_, err := app.StakingKeeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec())
suite.NoError(err)
applyValidatorSetUpdates(suite.T(), ctx, app.StakingKeeper, -1)

Expand Down
7 changes: 5 additions & 2 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ
),
})

return &types.MsgDelegateResponse{}, nil
return &types.MsgDelegateResponse{
Shares: newShares,
}, nil
}

// BeginRedelegate defines a method for performing a redelegation of coins from a delegator and source validator to a destination validator
Expand Down Expand Up @@ -342,7 +344,7 @@ func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (
)
}

completionTime, err := k.Keeper.Undelegate(ctx, delegatorAddress, addr, shares)
ubdID, completionTime, err := k.Keeper.Undelegate(ctx, delegatorAddress, addr, shares)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -373,6 +375,7 @@ func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (
})

return &types.MsgUndelegateResponse{
UnbondingId: ubdID,
CompletionTime: completionTime,
}, nil
}
6 changes: 3 additions & 3 deletions x/staking/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func TestQueryDelegation(t *testing.T) {

// Query unbonding delegation
unbondingTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 10)
_, err = app.StakingKeeper.Undelegate(ctx, addrAcc2, val1.GetOperator(), unbondingTokens.ToDec())
_, _, err = app.StakingKeeper.Undelegate(ctx, addrAcc2, val1.GetOperator(), unbondingTokens.ToDec())
require.NoError(t, err)

queryBondParams = types.QueryDelegatorValidatorRequest{DelegatorAddr: addrAcc2.String(), ValidatorAddr: addrVal1.String()}
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestQueryValidatorDelegations_Pagination(t *testing.T) {
// Undelegate
for _, addr := range addrs {
delTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
_, err := app.StakingKeeper.Undelegate(ctx, addr, val1.GetOperator(), delTokens.ToDec())
_, _, err := app.StakingKeeper.Undelegate(ctx, addr, val1.GetOperator(), delTokens.ToDec())
require.NoError(t, err)
}

Expand Down Expand Up @@ -629,7 +629,7 @@ func TestQueryUnbondingDelegation(t *testing.T) {

// undelegate
undelAmount := app.StakingKeeper.TokensFromConsensusPower(ctx, 20)
_, err = app.StakingKeeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec())
_, _, err = app.StakingKeeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), undelAmount.ToDec())
require.NoError(t, err)
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)

Expand Down
Loading