Skip to content

Commit

Permalink
Patch panic from validator not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominator008 committed Mar 21, 2022
1 parent b497862 commit 6d9c89a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions x/slash/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (k Keeper) HandleDoubleSign(ctx sdk.Context, addr crypto.Address, power int
consAddr := sdk.ConsAddress(addr)
validator, found := k.validatorKeeper.GetValidatorByConsAddr(ctx, consAddr)
if !found {
log.Errorf("Cannot find validator %s", consAddr)
log.Warnf("Cannot find validator %s", consAddr)
return
}

Expand All @@ -90,7 +90,7 @@ func (k Keeper) HandleValidatorSignature(ctx sdk.Context, addr crypto.Address, p
consAddr := sdk.ConsAddress(addr)
validator, found := k.validatorKeeper.GetValidatorByConsAddr(ctx, consAddr)
if !found {
log.Errorf("Cannot find validator %s", consAddr)
log.Warnf("Cannot find validator %s", consAddr)
return
}

Expand Down
17 changes: 13 additions & 4 deletions x/validator/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package validator

import (
"bytes"
"fmt"
"sort"

"github.com/celer-network/goutils/log"
Expand Down Expand Up @@ -59,14 +58,17 @@ func applyAndReturnValidatorSetUpdates(ctx sdk.Context, keeper Keeper) (updates
// Iterate over validators, highest power to lowest.
iterator := stakingKeeper.ValidatorsPowerStoreIterator(ctx)
defer iterator.Close()
var valByPowerIndexKeysToRemove [][]byte
for count := 0; iterator.Valid() && count < int(maxValidators); iterator.Next() {
// everything that is iterated in this loop is becoming or already a
// part of the bonded validator set

valAddr := sdk.ValAddress(iterator.Value())
validator, found := stakingKeeper.GetValidator(ctx, valAddr)
if !found {
panic(fmt.Sprintf("validator record not found for address: %X\n", valAddr))
log.Errorf("validator record not found for address: %X, skipping", valAddr)
valByPowerIndexKeysToRemove = append(valByPowerIndexKeysToRemove, iterator.Key())
continue
}

if validator.Jailed {
Expand Down Expand Up @@ -99,12 +101,19 @@ func applyAndReturnValidatorSetUpdates(ctx sdk.Context, keeper Keeper) (updates
count++
totalPower = totalPower.Add(sdk.NewInt(newPower))
}
store := ctx.KVStore(keeper.storeKey)
for _, key := range valByPowerIndexKeysToRemove {
store.Delete(key)
}

noLongerBonded := sortNoLongerBonded(last)
for _, valAddrBytes := range noLongerBonded {
validator, found := stakingKeeper.GetValidator(ctx, sdk.ValAddress(valAddrBytes))
valAddr := sdk.ValAddress(valAddrBytes)
validator, found := stakingKeeper.GetValidator(ctx, valAddr)
if !found {
panic(fmt.Sprintf("validator record not found for address: %X\n", sdk.ValAddress(valAddrBytes)))
log.Errorf("validator record not found for address: %X\n", valAddr)
stakingKeeper.DeleteLastValidatorPower(ctx, valAddr)
continue
}

stakingKeeper.DeleteLastValidatorPower(ctx, validator.GetOperator())
Expand Down

0 comments on commit 6d9c89a

Please sign in to comment.