Skip to content

Commit

Permalink
chore: bring optimization from SDK team on 47 (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe authored and julienrbrt committed Mar 23, 2023
1 parent 3f2e310 commit 2f53d3e
Show file tree
Hide file tree
Showing 19 changed files with 1,334 additions and 941 deletions.
3 changes: 2 additions & 1 deletion x/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress)
func (h Hooks) AfterValidatorBonded(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}
func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}
func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) {
func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) error {
return nil
}
3 changes: 2 additions & 1 deletion x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}
func (h Hooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}
func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ sdk.Dec) {}
func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) {
func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) error {
return nil
}
1 change: 1 addition & 0 deletions x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
require.True(t, found)
require.Equal(t, int64(0), signInfo.MissedBlocksCounter)
require.Equal(t, int64(0), signInfo.IndexOffset)

// array should be cleared
for offset := int64(0); offset < app.SlashingKeeper.SignedBlocksWindow(ctx); offset++ {
missed := app.SlashingKeeper.GetValidatorMissedBlockBitArray(ctx, consAddr, offset)
Expand Down
18 changes: 10 additions & 8 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (k Keeper) SetUnbondingDelegationEntry(
creationHeight int64, minTime time.Time, balance sdk.Int,
) types.UnbondingDelegation {
ubd, found := k.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr)
id := k.IncrementUnbondingId(ctx)
id := k.IncrementUnbondingID(ctx)
if found {
ubd.AddEntry(creationHeight, minTime, balance, id)
} else {
Expand All @@ -318,10 +318,11 @@ func (k Keeper) SetUnbondingDelegationEntry(
k.SetUnbondingDelegation(ctx, ubd)

// Add to the UBDByUnbondingOp index to look up the UBD by the UBDE ID
k.SetUnbondingDelegationByUnbondingId(ctx, ubd, id)
k.SetUnbondingDelegationByUnbondingID(ctx, ubd, id)

// Call hook
k.AfterUnbondingInitiated(ctx, id)
if err := k.AfterUnbondingInitiated(ctx, id); err != nil {
k.Logger(ctx).Error("failed to call after unbonding initiated hook", "error", err)
}

return ubd
}
Expand Down Expand Up @@ -509,7 +510,7 @@ func (k Keeper) SetRedelegationEntry(ctx sdk.Context,
sharesSrc, sharesDst sdk.Dec,
) types.Redelegation {
red, found := k.GetRedelegation(ctx, delegatorAddr, validatorSrcAddr, validatorDstAddr)
id := k.IncrementUnbondingId(ctx)
id := k.IncrementUnbondingID(ctx)
if found {
red.AddEntry(creationHeight, minTime, balance, sharesDst, id)
} else {
Expand All @@ -520,10 +521,11 @@ func (k Keeper) SetRedelegationEntry(ctx sdk.Context,
k.SetRedelegation(ctx, red)

// Add to the UBDByEntry index to look up the UBD by the UBDE ID
k.SetRedelegationByUnbondingId(ctx, red, id)
k.SetRedelegationByUnbondingID(ctx, red, id)

// Call hook
k.AfterUnbondingInitiated(ctx, id)
if err := k.AfterUnbondingInitiated(ctx, id); err != nil {
k.Logger(ctx).Error("failed to call after unbonding initiated hook", "error", err)
}

return red
}
Expand Down
10 changes: 5 additions & 5 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestUnbondingDelegation(t *testing.T) {
0,
time.Unix(0, 0).UTC(),
sdk.NewInt(5),
app.StakingKeeper.IncrementUnbondingId(ctx),
0,
)

// set and retrieve a record
Expand Down Expand Up @@ -306,8 +306,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 @@ -616,7 +616,7 @@ func TestGetRedelegationsFromSrcValidator(t *testing.T) {

rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0), sdk.NewInt(5),
sdk.NewDec(5), 1)
sdk.NewDec(5), 0)

// set and retrieve a record
app.StakingKeeper.SetRedelegation(ctx, rd)
Expand All @@ -643,7 +643,7 @@ func TestRedelegation(t *testing.T) {

rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0).UTC(), sdk.NewInt(5),
sdk.NewDec(5), 1)
sdk.NewDec(5), 0)

// test shouldn't have and redelegations
has := app.StakingKeeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1])
Expand Down
5 changes: 3 additions & 2 deletions x/staking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress,
}

// This is called when an UnbondingDelegationEntry is first created
func (k Keeper) AfterUnbondingInitiated(ctx sdk.Context, id uint64) {
func (k Keeper) AfterUnbondingInitiated(ctx sdk.Context, id uint64) error {
if k.hooks != nil {
k.hooks.AfterUnbondingInitiated(ctx, id)
return k.hooks.AfterUnbondingInitiated(ctx, id)
}
return nil
}
7 changes: 5 additions & 2 deletions x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import (
//
// CONTRACT:
//
// Infraction was committed at the current height or at a past height,
// not at a height in the future
// Infraction was committed at the current height or at a past height,
// not at a height in the future
// ---
//
// Slash implementation doesn't require the infraction (types.Infraction) to work but the IS one does. It is here to have IS satisfy the Slash signature.
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec, _ types.InfractionType) {
logger := k.Logger(ctx)

Expand Down
12 changes: 6 additions & 6 deletions x/staking/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestSlashUnbondingDelegation(t *testing.T) {
// set an unbonding delegation with expiration timestamp (beyond which the
// unbonding delegation shouldn't be slashed)
ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 0,
time.Unix(5, 0), sdk.NewInt(10), app.StakingKeeper.IncrementUnbondingId(ctx))
time.Unix(5, 0), sdk.NewInt(10), 0)

app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)

Expand Down Expand Up @@ -131,7 +131,7 @@ func TestSlashRedelegation(t *testing.T) {
// set a redelegation with an expiration timestamp beyond which the
// redelegation shouldn't be slashed
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(5, 0), sdk.NewInt(10), sdk.NewDec(10), 1)
time.Unix(5, 0), sdk.NewInt(10), sdk.NewDec(10), 0)

app.StakingKeeper.SetRedelegation(ctx, rd)

Expand Down Expand Up @@ -264,7 +264,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
// set an unbonding delegation with expiration timestamp beyond which the
// unbonding delegation shouldn't be slashed
ubdTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 4)
ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11, time.Unix(0, 0), ubdTokens, app.StakingKeeper.IncrementUnbondingId(ctx))
ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11, time.Unix(0, 0), ubdTokens, 0)
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)

// slash validator for the first time
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestSlashWithRedelegation(t *testing.T) {
// set a redelegation
rdTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 6)
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11,
time.Unix(0, 0), rdTokens, rdTokens.ToDec(), 1)
time.Unix(0, 0), rdTokens, rdTokens.ToDec(), 0)
app.StakingKeeper.SetRedelegation(ctx, rd)

// set the associated delegation
Expand Down Expand Up @@ -544,7 +544,7 @@ func TestSlashBoth(t *testing.T) {
rdATokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 6)
rdA := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11,
time.Unix(0, 0), rdATokens,
rdATokens.ToDec(), 1)
rdATokens.ToDec(), 0)
app.StakingKeeper.SetRedelegation(ctx, rdA)

// set the associated delegation
Expand All @@ -555,7 +555,7 @@ func TestSlashBoth(t *testing.T) {
// unbonding delegation shouldn't be slashed)
ubdATokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 4)
ubdA := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11,
time.Unix(0, 0), ubdATokens, app.StakingKeeper.IncrementUnbondingId(ctx))
time.Unix(0, 0), ubdATokens, 0)
app.StakingKeeper.SetUnbondingDelegation(ctx, ubdA)

bondedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, rdATokens.MulRaw(2)))
Expand Down
Loading

0 comments on commit 2f53d3e

Please sign in to comment.