Skip to content

Commit

Permalink
remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Dec 5, 2024
1 parent 66c593f commit 3786cbb
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 951 deletions.
48 changes: 6 additions & 42 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,12 @@ func (k Keeper) SetUnbondingDelegationEntry(
ctx context.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress,
creationHeight int64, minTime time.Time, balance math.Int,
) (types.UnbondingDelegation, error) {
id, err := k.IncrementUnbondingID(ctx)
if err != nil {
return types.UnbondingDelegation{}, err
}

isNewUbdEntry := true
ubd, err := k.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr)
if err == nil {
isNewUbdEntry = ubd.AddEntry(creationHeight, minTime, balance, id)
ubd.AddEntry(creationHeight, minTime, balance)
} else if errors.Is(err, types.ErrNoUnbondingDelegation) {
ubd = types.NewUnbondingDelegation(delegatorAddr, validatorAddr, creationHeight, minTime, balance, id, k.validatorAddressCodec, k.authKeeper.AddressCodec())
ubd = types.NewUnbondingDelegation(delegatorAddr, validatorAddr, creationHeight, minTime, balance, k.validatorAddressCodec, k.authKeeper.AddressCodec())
} else {
return ubd, err
}
Expand All @@ -329,18 +324,6 @@ func (k Keeper) SetUnbondingDelegationEntry(
return ubd, err
}

// only call the hook for new entries since
// calls to AfterUnbondingInitiated are not idempotent
if isNewUbdEntry {
// Add to the UBDByUnbondingOp index to look up the UBD by the UBDE ID
if err = k.SetUnbondingDelegationByUnbondingID(ctx, ubd, id); err != nil {
return ubd, err
}

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
return ubd, fmt.Errorf("failed to call after unbonding initiated hook: %w", err)
}
}
return ubd, nil
}

Expand Down Expand Up @@ -528,17 +511,13 @@ func (k Keeper) SetRedelegationEntry(ctx context.Context,
minTime time.Time, balance math.Int,
sharesSrc, sharesDst math.LegacyDec,
) (types.Redelegation, error) {
id, err := k.IncrementUnbondingID(ctx)
if err != nil {
return types.Redelegation{}, err
}

red, err := k.Redelegations.Get(ctx, collections.Join3(delegatorAddr.Bytes(), validatorSrcAddr.Bytes(), validatorDstAddr.Bytes()))
if err == nil {
red.AddEntry(creationHeight, minTime, balance, sharesDst, id)
red.AddEntry(creationHeight, minTime, balance, sharesDst)
} else if errors.Is(err, collections.ErrNotFound) {
red = types.NewRedelegation(delegatorAddr, validatorSrcAddr,
validatorDstAddr, creationHeight, minTime, balance, sharesDst, id, k.validatorAddressCodec, k.authKeeper.AddressCodec())
validatorDstAddr, creationHeight, minTime, balance, sharesDst, k.validatorAddressCodec, k.authKeeper.AddressCodec())
} else {
return types.Redelegation{}, err
}
Expand All @@ -547,15 +526,6 @@ func (k Keeper) SetRedelegationEntry(ctx context.Context,
return types.Redelegation{}, err
}

// Add to the UBDByEntry index to look up the UBD by the UBDE ID
if err = k.SetRedelegationByUnbondingID(ctx, red, id); err != nil {
return types.Redelegation{}, err
}

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
return types.Redelegation{}, fmt.Errorf("failed to call after unbonding initiated hook: %w", err)
}

return red, nil
}

Expand Down Expand Up @@ -996,12 +966,9 @@ func (k Keeper) CompleteUnbonding(ctx context.Context, delAddr sdk.AccAddress, v
// loop through all the entries and complete unbonding mature entries
for i := 0; i < len(ubd.Entries); i++ {
entry := ubd.Entries[i]
if entry.IsMature(ctxTime) && !entry.OnHold() {
if entry.IsMature(ctxTime) {
ubd.RemoveEntry(int64(i))
i--
if err = k.DeleteUnbondingIndex(ctx, entry.UnbondingId); err != nil {
return nil, err
}

// track undelegation only when remaining or truncated shares are non-zero
if !entry.Balance.IsZero() {
Expand Down Expand Up @@ -1136,12 +1103,9 @@ func (k Keeper) CompleteRedelegation(
// loop through all the entries and complete mature redelegation entries
for i := 0; i < len(red.Entries); i++ {
entry := red.Entries[i]
if entry.IsMature(ctxTime) && !entry.OnHold() {
if entry.IsMature(ctxTime) {
red.RemoveEntry(int64(i))
i--
if err = k.DeleteUnbondingIndex(ctx, entry.UnbondingId); err != nil {
return nil, err
}

if !entry.InitialBalance.IsZero() {
balances = balances.Add(sdk.NewCoin(bondDenom, entry.InitialBalance))
Expand Down
21 changes: 10 additions & 11 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func (s *KeeperTestSuite) TestUnbondingDelegation() {
0,
time.Unix(0, 0).UTC(),
math.NewInt(5),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
)

Expand Down Expand Up @@ -343,8 +342,8 @@ func (s *KeeperTestSuite) TestUnbondingDelegationsFromValidator() {
0,
time.Unix(0, 0).UTC(),
math.NewInt(5),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
address.NewBech32Codec("cosmosvaloper"),
address.NewBech32Codec("cosmos"),
)

// set and retrieve a record
Expand Down Expand Up @@ -716,7 +715,7 @@ func (s *KeeperTestSuite) TestGetRedelegationsFromSrcValidator() {

rd := stakingtypes.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0), math.NewInt(5),
math.LegacyNewDec(5), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
math.LegacyNewDec(5), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))

// set and retrieve a record
err := keeper.SetRedelegation(ctx, rd)
Expand Down Expand Up @@ -746,7 +745,7 @@ func (s *KeeperTestSuite) TestRedelegation() {

rd := stakingtypes.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0).UTC(), math.NewInt(5),
math.LegacyNewDec(5), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
math.LegacyNewDec(5), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))

// test shouldn't have and redelegations
has, err := keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1])
Expand Down Expand Up @@ -1107,14 +1106,14 @@ func (s *KeeperTestSuite) TestUnbondingDelegationAddEntry() {
creationHeight,
time.Unix(0, 0).UTC(),
math.NewInt(10),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
address.NewBech32Codec("cosmosvaloper"),
address.NewBech32Codec("cosmos"),
)
var initialEntries []stakingtypes.UnbondingDelegationEntry
initialEntries = append(initialEntries, ubd.Entries...)
require.Len(initialEntries, 1)

isNew := ubd.AddEntry(creationHeight, time.Unix(0, 0).UTC(), math.NewInt(5), 1)
isNew := ubd.AddEntry(creationHeight, time.Unix(0, 0).UTC(), math.NewInt(5))
require.False(isNew)
require.Len(ubd.Entries, 1) // entry was merged
require.NotEqual(initialEntries, ubd.Entries)
Expand All @@ -1123,7 +1122,7 @@ func (s *KeeperTestSuite) TestUnbondingDelegationAddEntry() {
require.Equal(ubd.Entries[0].Balance, math.NewInt(15)) // 10 from previous + 5 from merged

newCreationHeight := int64(11)
isNew = ubd.AddEntry(newCreationHeight, time.Unix(1, 0).UTC(), math.NewInt(5), 2)
isNew = ubd.AddEntry(newCreationHeight, time.Unix(1, 0).UTC(), math.NewInt(5))
require.True(isNew)
require.Len(ubd.Entries, 2) // entry was appended
require.NotEqual(initialEntries, ubd.Entries)
Expand All @@ -1149,8 +1148,8 @@ func (s *KeeperTestSuite) TestSetUnbondingDelegationEntry() {
creationHeight,
time.Unix(0, 0).UTC(),
math.NewInt(5),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
address.NewBech32Codec("cosmosvaloper"),
address.NewBech32Codec("cosmos"),
)

// set and retrieve a record
Expand Down
1 change: 0 additions & 1 deletion x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels
entry.SharesDst,
entry.InitialBalance,
val.TokensFromShares(entry.SharesDst).TruncateInt(),
entry.UnbondingId,
)
}

Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() {
require.NoError(err)
require.Equal(del, resDel)

ubd := types.NewUnbondingDelegation(Addr, ValAddr, 10, ctx.HeaderInfo().Time.Add(time.Minute*10), shares.RoundInt(), 0, keeper.ValidatorAddressCodec(), ak.AddressCodec())
ubd := types.NewUnbondingDelegation(Addr, ValAddr, 10, ctx.HeaderInfo().Time.Add(time.Minute*10), shares.RoundInt(), keeper.ValidatorAddressCodec(), ak.AddressCodec())
require.NoError(keeper.SetUnbondingDelegation(ctx, ubd))
resUnbond, err := keeper.GetUnbondingDelegation(ctx, Addr, ValAddr)
require.NoError(err)
Expand Down
4 changes: 2 additions & 2 deletions x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (k Keeper) SlashUnbondingDelegation(ctx context.Context, unbondingDelegatio
continue
}

if entry.IsMature(now) && !entry.OnHold() {
if entry.IsMature(now) {
// Unbonding delegation no longer eligible for slashing, skip it
continue
}
Expand Down Expand Up @@ -346,7 +346,7 @@ func (k Keeper) SlashRedelegation(ctx context.Context, srcValidator types.Valida
case entry.CreationHeight < infractionHeight:
continue
// Unbonding delegation no longer eligible for slashing, skip it
case entry.IsMature(now) && !entry.OnHold():
case entry.IsMature(now):
continue
// Slash the unbonding delegation
default:
Expand Down
Loading

0 comments on commit 3786cbb

Please sign in to comment.