Skip to content

Commit

Permalink
fix: TotalLiquidShares nil handling logic in slashing
Browse files Browse the repository at this point in the history
Also, both TotalLiquidShares and TotalValidatorBondShares shouldn't be nil in state, this will be handled by a migration later,
for now to avoid crashes it's done in the EndBlocker.
  • Loading branch information
Max committed Apr 25, 2023
1 parent 35f5aac commit ad71635
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 21 additions & 0 deletions x/lsnative/staking/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,26 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

// FIXME(max): remove this block after state migration is final
if ctx.BlockHeight() > 11060956 {
validators := k.GetLastValidators(ctx)
for _, val := range validators {
var valNeedsUpdate bool

if val.TotalLiquidShares.IsNil() {
val.TotalLiquidShares = sdk.ZeroDec()
valNeedsUpdate = true
}
if val.TotalValidatorBondShares.IsNil() {
val.TotalValidatorBondShares = sdk.ZeroDec()
valNeedsUpdate = true
}

if valNeedsUpdate {
k.SetValidator(ctx, val)
}
}
}

return k.BlockValidatorUpdates(ctx)
}
7 changes: 6 additions & 1 deletion x/lsnative/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh
validator = k.RemoveValidatorTokens(ctx, validator, tokensToBurn)

// Proportionally deduct any liquid tokens from the global total
validatorLiquidRatio := validator.TotalLiquidShares.Quo(validator.DelegatorShares)
var validatorLiquidRatio sdk.Dec
if validator.TotalLiquidShares.IsNil() {
validatorLiquidRatio = sdk.ZeroDec()
} else {
validatorLiquidRatio = validator.TotalLiquidShares.Quo(validator.DelegatorShares)
}
slashedLiquidTokens := validatorLiquidRatio.Mul(sdk.NewDecFromInt(slashAmount)).TruncateInt()
k.DecreaseTotalLiquidStakedTokens(ctx, slashedLiquidTokens)

Expand Down

0 comments on commit ad71635

Please sign in to comment.